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      private int scanIntervalSeconds; 
45      
46      /** Temporary files directory. */
47      private File tempDirectory;
48  
49      /** List of web applications to be deployed. */
50      private List<AntWebAppContext> webapps = new ArrayList<AntWebAppContext>();
51  
52      /** Location of jetty.xml file. */
53      private File jettyXml;
54  
55      /** List of server connectors. */
56      private Connectors connectors = null;
57  
58      /** Server request logger object. */
59      private RequestLog requestLog;
60  
61      /** List of login services. */
62      private LoginServices loginServices;
63  
64      /** List of system properties to be set. */
65      private SystemProperties systemProperties;
66      
67      /** List of other contexts to deploy */
68      private ContextHandlers contextHandlers;
69  
70   
71      /** Port Jetty will use for the default connector */
72      private int jettyPort = 8080;
73      
74      private int stopPort;
75      
76      private String stopKey;
77  
78      private boolean daemon;
79      
80      public JettyRunTask()
81      {
82          TaskLog.setTask(this);
83      }
84  
85      /**
86       * Creates a new <code>WebApp</code> Ant object.
87       * @param webapp the webapp context 
88       */
89      public void addWebApp(AntWebAppContext webapp)
90      {
91         webapps.add(webapp);
92      }
93  
94      /**
95       * Adds a new Ant's connector tag object if it have not been created yet.
96       * @param connectors the connectors 
97       */
98      public void addConnectors(Connectors connectors)
99      {
100         if (this.connectors != null)
101             throw new BuildException("Only one <connectors> tag is allowed!");
102         this.connectors = connectors;
103     }
104 
105     public void addLoginServices(LoginServices services)
106     {        
107         if (this.loginServices != null )
108             throw new BuildException("Only one <loginServices> tag is allowed!");       
109         this.loginServices = services;  
110     }
111 
112     public void addSystemProperties(SystemProperties systemProperties)
113     {
114         if (this.systemProperties != null)
115             throw new BuildException("Only one <systemProperties> tag is allowed!");
116         this.systemProperties = systemProperties;
117     }
118     
119     public void addContextHandlers (ContextHandlers handlers)
120     {
121         if (this.contextHandlers != null)
122             throw new BuildException("Only one <contextHandlers> tag is allowed!");
123         this.contextHandlers = handlers;
124     }
125 
126     public File getTempDirectory()
127     {
128         return tempDirectory;
129     }
130 
131     public void setTempDirectory(File tempDirectory)
132     {
133         this.tempDirectory = tempDirectory;
134     }
135 
136     public File getJettyXml()
137     {
138         return jettyXml;
139     }
140 
141     public void setJettyXml(File jettyXml)
142     {
143         this.jettyXml = jettyXml;
144     }
145 
146     public void setRequestLog(String className)
147     {
148         try
149         {
150             this.requestLog = (RequestLog) Class.forName(className).newInstance();
151         }
152         catch (InstantiationException e)
153         {
154             throw new BuildException("Request logger instantiation exception: " + e);
155         }
156         catch (IllegalAccessException e)
157         {
158             throw new BuildException("Request logger instantiation exception: " + e);
159         }
160         catch (ClassNotFoundException e)
161         {
162             throw new BuildException("Unknown request logger class: " + className);
163         }
164     }
165 
166     public String getRequestLog()
167     {
168         if (requestLog != null)
169         {
170             return requestLog.getClass().getName();
171         }
172 
173         return "";
174     }
175 
176     /**
177      * Sets the port Jetty uses for the default connector.
178      * 
179      * @param jettyPort The port Jetty will use for the default connector
180      */
181     public void setJettyPort(final int jettyPort)
182     {
183         this.jettyPort = jettyPort;
184     }
185 
186     /**
187      * Executes this Ant task. The build flow is being stopped until Jetty
188      * server stops.
189      *
190      * @throws BuildException if unable to build
191      */
192     public void execute() throws BuildException
193     {
194 
195         TaskLog.log("Configuring Jetty for project: " + getProject().getName());
196         
197         setSystemProperties();
198 
199         List<Connector> connectorsList = null;
200 
201         if (connectors != null)
202             connectorsList = connectors.getConnectors();
203         else
204             connectorsList = new Connectors(jettyPort,30000).getDefaultConnectors();
205 
206         List<LoginService> loginServicesList = (loginServices != null?loginServices.getLoginServices():new ArrayList<LoginService>());
207         ServerProxyImpl server = new ServerProxyImpl();
208         server.setConnectors(connectorsList);
209         server.setLoginServices(loginServicesList);
210         server.setRequestLog(requestLog);
211         server.setJettyXml(jettyXml);
212         server.setDaemon(daemon);
213         server.setStopPort(stopPort);
214         server.setStopKey(stopKey);
215         server.setContextHandlers(contextHandlers);
216         server.setTempDirectory(tempDirectory);
217         server.setScanIntervalSecs(scanIntervalSeconds);
218 
219         try
220         {
221             for (WebAppContext webapp: webapps)
222             {
223                 server.addWebApplication((AntWebAppContext)webapp);
224             }
225         }
226         catch (Exception e)
227         {
228             throw new BuildException(e);
229         }
230 
231         server.start();
232     }
233 
234     public int getStopPort()
235     {
236         return stopPort;
237     }
238 
239     public void setStopPort(int stopPort)
240     {
241         this.stopPort = stopPort;
242         TaskLog.log("stopPort="+stopPort);
243     }
244 
245     public String getStopKey()
246     {
247         return stopKey;
248     }
249 
250     public void setStopKey(String stopKey)
251     {
252         this.stopKey = stopKey;
253         TaskLog.log("stopKey="+stopKey);
254     }
255 
256     /**
257      * @return the daemon
258      */
259     public boolean isDaemon()
260     {
261         return daemon;
262     }
263 
264     /**
265      * @param daemon the daemon to set
266      */
267     public void setDaemon(boolean daemon)
268     {
269         this.daemon = daemon;
270         TaskLog.log("Daemon="+daemon);
271     }
272 
273     public int getScanIntervalSeconds()
274     {
275         return scanIntervalSeconds;
276     }
277 
278     public void setScanIntervalSeconds(int secs)
279     {
280         scanIntervalSeconds = secs;
281         TaskLog.log("scanIntervalSecs="+secs);
282     }
283     
284     /**
285      * Sets the system properties.
286      */
287     private void setSystemProperties()
288     {
289         if (systemProperties != null)
290         {
291             Iterator propertiesIterator = systemProperties.getSystemProperties().iterator();
292             while (propertiesIterator.hasNext())
293             {
294                 Property property = ((Property) propertiesIterator.next());
295                 SystemProperties.setIfNotSetAlready(property);
296             }
297         }
298     }
299 
300 }