View Javadoc

1   //
2   //  ========================================================================
3   //  Copyright (c) 1995-2012 Sabre Holdings.
4   //  ------------------------------------------------------------------------
5   //  All rights reserved. This program and the accompanying materials
6   //  are made available under the terms of the Eclipse Public License v1.0
7   //  and Apache License v2.0 which accompanies this distribution.
8   //
9   //      The Eclipse Public License is available at
10  //      http://www.eclipse.org/legal/epl-v10.html
11  //
12  //      The Apache License v2.0 is available at
13  //      http://www.opensource.org/licenses/apache2.0.php
14  //
15  //  You may elect to redistribute this code under either of these licenses.
16  //  ========================================================================
17  //
18  
19  package org.eclipse.jetty.ant;
20  
21  import java.io.File;
22  import java.util.ArrayList;
23  import java.util.Iterator;
24  import java.util.List;
25  
26  import org.apache.tools.ant.BuildException;
27  import org.apache.tools.ant.Task;
28  import org.apache.tools.ant.taskdefs.Property;
29  import org.eclipse.jetty.ant.types.Connector;
30  import org.eclipse.jetty.ant.types.Connectors;
31  import org.eclipse.jetty.ant.types.ContextHandlers;
32  import org.eclipse.jetty.ant.types.LoginServices;
33  import org.eclipse.jetty.ant.types.SystemProperties;
34  import org.eclipse.jetty.ant.utils.TaskLog;
35  import org.eclipse.jetty.security.LoginService;
36  import org.eclipse.jetty.server.RequestLog;
37  import org.eclipse.jetty.webapp.WebAppContext;
38  
39  /**
40   * Ant task for running a Jetty server.
41   */
42  public class JettyRunTask extends Task
43  {
44  
45      private int scanIntervalSeconds; 
46      
47      
48      /** Temporary files directory. */
49      private File tempDirectory;
50  
51      /** List of web applications to be deployed. */
52      private List<AntWebAppContext> webapps = new ArrayList<AntWebAppContext>();
53  
54      /** Location of jetty.xml file. */
55      private File jettyXml;
56  
57      /** List of server connectors. */
58      private Connectors connectors = null;
59  
60      /** Server request logger object. */
61      private RequestLog requestLog;
62  
63      /** List of login services. */
64      private LoginServices loginServices;
65  
66      /** List of system properties to be set. */
67      private SystemProperties systemProperties;
68      
69      /** List of other contexts to deploy */
70      private ContextHandlers contextHandlers;
71  
72   
73      /** Port Jetty will use for the default connector */
74      private int jettyPort = 8080;
75      
76      private int stopPort;
77      
78      private String stopKey;
79  
80      private boolean daemon;
81      
82    
83  
84  
85      public JettyRunTask()
86      {
87          TaskLog.setTask(this);
88      }
89  
90      /**
91       * Creates a new <code>WebApp</code> Ant object.
92       *
93       */
94      public void addWebApp(AntWebAppContext webapp)
95      {
96         webapps.add(webapp);
97      }
98      
99      
100 
101     /**
102      * Adds a new Ant's connector tag object if it have not been created yet.
103      */
104     public void addConnectors(Connectors connectors)
105     {
106         if (this.connectors != null)
107             throw new BuildException("Only one <connectors> tag is allowed!");
108         this.connectors = connectors;
109     }
110 
111 
112     /**
113      * @param services
114      */
115     public void addLoginServices(LoginServices services)
116     {        
117         if (this.loginServices != null )
118             throw new BuildException("Only one <loginServices> tag is allowed!");       
119         this.loginServices = services;  
120     }
121 
122     public void addSystemProperties(SystemProperties systemProperties)
123     {
124         if (this.systemProperties != null)
125             throw new BuildException("Only one <systemProperties> tag is allowed!");
126         this.systemProperties = systemProperties;
127     }
128     
129     /**
130      * @param handlers
131      */
132     public void addContextHandlers (ContextHandlers handlers)
133     {
134         if (this.contextHandlers != null)
135             throw new BuildException("Only one <contextHandlers> tag is allowed!");
136         this.contextHandlers = handlers;
137     }
138 
139     /**
140      * @return
141      */
142     public File getTempDirectory()
143     {
144         return tempDirectory;
145     }
146 
147     /**
148      * @param tempDirectory
149      */
150     public void setTempDirectory(File tempDirectory)
151     {
152         this.tempDirectory = tempDirectory;
153     }
154 
155     /**
156      * @return
157      */
158     public File getJettyXml()
159     {
160         return jettyXml;
161     }
162 
163     /**
164      * @param jettyXml
165      */
166     public void setJettyXml(File jettyXml)
167     {
168         this.jettyXml = jettyXml;
169     }
170 
171     /**
172      * @param className
173      */
174     public void setRequestLog(String className)
175     {
176         try
177         {
178             this.requestLog = (RequestLog) Class.forName(className).newInstance();
179         }
180         catch (InstantiationException e)
181         {
182             throw new BuildException("Request logger instantiation exception: " + e);
183         }
184         catch (IllegalAccessException e)
185         {
186             throw new BuildException("Request logger instantiation exception: " + e);
187         }
188         catch (ClassNotFoundException e)
189         {
190             throw new BuildException("Unknown request logger class: " + className);
191         }
192     }
193 
194     /**
195      * @return
196      */
197     public String getRequestLog()
198     {
199         if (requestLog != null)
200         {
201             return requestLog.getClass().getName();
202         }
203 
204         return "";
205     }
206 
207     /**
208      * Sets the port Jetty uses for the default connector.
209      * 
210      * @param jettyPort The port Jetty will use for the default connector
211      */
212     public void setJettyPort(final int jettyPort)
213     {
214         this.jettyPort = jettyPort;
215     }
216 
217     /**
218      * Executes this Ant task. The build flow is being stopped until Jetty
219      * server stops.
220      *
221      * @throws BuildException
222      */
223     public void execute() throws BuildException
224     {
225 
226         TaskLog.log("Configuring Jetty for project: " + getProject().getName());
227         
228         setSystemProperties();
229 
230         List<Connector> connectorsList = null;
231 
232         if (connectors != null)
233             connectorsList = connectors.getConnectors();
234         else
235             connectorsList = new Connectors(jettyPort,30000).getDefaultConnectors();
236 
237         List<LoginService> loginServicesList = (loginServices != null?loginServices.getLoginServices():new ArrayList<LoginService>());
238         ServerProxyImpl server = new ServerProxyImpl();
239         server.setConnectors(connectorsList);
240         server.setLoginServices(loginServicesList);
241         server.setRequestLog(requestLog);
242         server.setJettyXml(jettyXml);
243         server.setDaemon(daemon);
244         server.setStopPort(stopPort);
245         server.setStopKey(stopKey);
246         server.setContextHandlers(contextHandlers);
247         server.setTempDirectory(tempDirectory);
248         server.setScanIntervalSecs(scanIntervalSeconds);
249 
250         try
251         {
252             for (WebAppContext webapp: webapps)
253             {
254                 server.addWebApplication((AntWebAppContext)webapp);
255             }
256         }
257         catch (Exception e)
258         {
259             throw new BuildException(e);
260         }
261 
262         server.start();
263     }
264 
265     public int getStopPort()
266     {
267         return stopPort;
268     }
269 
270     public void setStopPort(int stopPort)
271     {
272         this.stopPort = stopPort;
273         TaskLog.log("stopPort="+stopPort);
274     }
275 
276     public String getStopKey()
277     {
278         return stopKey;
279     }
280 
281     public void setStopKey(String stopKey)
282     {
283         this.stopKey = stopKey;
284         TaskLog.log("stopKey="+stopKey);
285     }
286 
287     /**
288      * @return the daemon
289      */
290     public boolean isDaemon()
291     {
292         return daemon;
293     }
294 
295     /**
296      * @param daemon the daemon to set
297      */
298     public void setDaemon(boolean daemon)
299     {
300         this.daemon = daemon;
301         TaskLog.log("Daemon="+daemon);
302     }
303 
304     /**
305      * @return
306      */
307     public int getScanIntervalSeconds()
308     {
309         return scanIntervalSeconds;
310     }
311 
312     /**
313      * @param secs
314      */
315     public void setScanIntervalSeconds(int secs)
316     {
317         scanIntervalSeconds = secs;
318         TaskLog.log("scanIntervalSecs="+secs);
319     }
320     
321 
322     
323     /**
324      * Sets the system properties.
325      */
326     private void setSystemProperties()
327     {
328         if (systemProperties != null)
329         {
330             Iterator propertiesIterator = systemProperties.getSystemProperties().iterator();
331             while (propertiesIterator.hasNext())
332             {
333                 Property property = ((Property) propertiesIterator.next());
334                 SystemProperties.setIfNotSetAlready(property);
335             }
336         }
337     }
338 
339 }