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     public File getTempDirectory()
140     {
141         return tempDirectory;
142     }
143 
144     /**
145      * @param tempDirectory
146      */
147     public void setTempDirectory(File tempDirectory)
148     {
149         this.tempDirectory = tempDirectory;
150     }
151 
152     public File getJettyXml()
153     {
154         return jettyXml;
155     }
156 
157     /**
158      * @param jettyXml
159      */
160     public void setJettyXml(File jettyXml)
161     {
162         this.jettyXml = jettyXml;
163     }
164 
165     /**
166      * @param className
167      */
168     public void setRequestLog(String className)
169     {
170         try
171         {
172             this.requestLog = (RequestLog) Class.forName(className).newInstance();
173         }
174         catch (InstantiationException e)
175         {
176             throw new BuildException("Request logger instantiation exception: " + e);
177         }
178         catch (IllegalAccessException e)
179         {
180             throw new BuildException("Request logger instantiation exception: " + e);
181         }
182         catch (ClassNotFoundException e)
183         {
184             throw new BuildException("Unknown request logger class: " + className);
185         }
186     }
187 
188     public String getRequestLog()
189     {
190         if (requestLog != null)
191         {
192             return requestLog.getClass().getName();
193         }
194 
195         return "";
196     }
197 
198     /**
199      * Sets the port Jetty uses for the default connector.
200      * 
201      * @param jettyPort The port Jetty will use for the default connector
202      */
203     public void setJettyPort(final int jettyPort)
204     {
205         this.jettyPort = jettyPort;
206     }
207 
208     /**
209      * Executes this Ant task. The build flow is being stopped until Jetty
210      * server stops.
211      *
212      * @throws BuildException
213      */
214     public void execute() throws BuildException
215     {
216 
217         TaskLog.log("Configuring Jetty for project: " + getProject().getName());
218         
219         setSystemProperties();
220 
221         List<Connector> connectorsList = null;
222 
223         if (connectors != null)
224             connectorsList = connectors.getConnectors();
225         else
226             connectorsList = new Connectors(jettyPort,30000).getDefaultConnectors();
227 
228         List<LoginService> loginServicesList = (loginServices != null?loginServices.getLoginServices():new ArrayList<LoginService>());
229         ServerProxyImpl server = new ServerProxyImpl();
230         server.setConnectors(connectorsList);
231         server.setLoginServices(loginServicesList);
232         server.setRequestLog(requestLog);
233         server.setJettyXml(jettyXml);
234         server.setDaemon(daemon);
235         server.setStopPort(stopPort);
236         server.setStopKey(stopKey);
237         server.setContextHandlers(contextHandlers);
238         server.setTempDirectory(tempDirectory);
239         server.setScanIntervalSecs(scanIntervalSeconds);
240 
241         try
242         {
243             for (WebAppContext webapp: webapps)
244             {
245                 server.addWebApplication((AntWebAppContext)webapp);
246             }
247         }
248         catch (Exception e)
249         {
250             throw new BuildException(e);
251         }
252 
253         server.start();
254     }
255 
256     public int getStopPort()
257     {
258         return stopPort;
259     }
260 
261     public void setStopPort(int stopPort)
262     {
263         this.stopPort = stopPort;
264         TaskLog.log("stopPort="+stopPort);
265     }
266 
267     public String getStopKey()
268     {
269         return stopKey;
270     }
271 
272     public void setStopKey(String stopKey)
273     {
274         this.stopKey = stopKey;
275         TaskLog.log("stopKey="+stopKey);
276     }
277 
278     /**
279      * @return the daemon
280      */
281     public boolean isDaemon()
282     {
283         return daemon;
284     }
285 
286     /**
287      * @param daemon the daemon to set
288      */
289     public void setDaemon(boolean daemon)
290     {
291         this.daemon = daemon;
292         TaskLog.log("Daemon="+daemon);
293     }
294 
295     public int getScanIntervalSeconds()
296     {
297         return scanIntervalSeconds;
298     }
299 
300     /**
301      * @param secs
302      */
303     public void setScanIntervalSeconds(int secs)
304     {
305         scanIntervalSeconds = secs;
306         TaskLog.log("scanIntervalSecs="+secs);
307     }
308     
309 
310     
311     /**
312      * Sets the system properties.
313      */
314     private void setSystemProperties()
315     {
316         if (systemProperties != null)
317         {
318             Iterator propertiesIterator = systemProperties.getSystemProperties().iterator();
319             while (propertiesIterator.hasNext())
320             {
321                 Property property = ((Property) propertiesIterator.next());
322                 SystemProperties.setIfNotSetAlready(property);
323             }
324         }
325     }
326 
327 }