View Javadoc

1   // ========================================================================
2   // Copyright 2006-2007 Sabre Holdings.
3   // ------------------------------------------------------------------------
4   // Licensed under the Apache License, Version 2.0 (the "License");
5   // you may not use this file except in compliance with the License.
6   // You may obtain a copy of the License at
7   // http://www.apache.org/licenses/LICENSE-2.0
8   // Unless required by applicable law or agreed to in writing, software
9   // distributed under the License is distributed on an "AS IS" BASIS,
10  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11  // See the License for the specific language governing permissions and
12  // limitations under the License.
13  // ========================================================================
14  
15  package org.mortbay.jetty.ant;
16  
17  import java.io.File;
18  import java.io.IOException;
19  import java.net.MalformedURLException;
20  import java.util.HashMap;
21  import java.util.Iterator;
22  import java.util.List;
23  import java.util.Map;
24  
25  import org.mortbay.jetty.Connector;
26  import org.mortbay.jetty.Handler;
27  import org.mortbay.jetty.RequestLog;
28  import org.mortbay.jetty.Server;
29  import org.mortbay.jetty.ant.utils.ServerProxy;
30  import org.mortbay.jetty.ant.utils.TaskLog;
31  import org.mortbay.jetty.ant.utils.WebApplicationProxy;
32  import org.mortbay.jetty.handler.ContextHandlerCollection;
33  import org.mortbay.jetty.handler.DefaultHandler;
34  import org.mortbay.jetty.handler.HandlerCollection;
35  import org.mortbay.jetty.handler.RequestLogHandler;
36  import org.mortbay.jetty.security.UserRealm;
37  import org.mortbay.resource.Resource;
38  import org.mortbay.xml.XmlConfiguration;
39  
40  /**
41   * A proxy class for interaction with Jetty server object. Used to have some
42   * level of abstraction over standard Jetty classes.
43   * 
44   * @author Jakub Pawlowicz
45   */
46  public class ServerProxyImpl implements ServerProxy
47  {
48  
49      /** Proxied Jetty server object. */
50      private Server server;
51  
52      /** Collection of context handlers (web application contexts). */
53      private ContextHandlerCollection contexts;
54  
55      /** Location of jetty.xml file. */
56      private File jettyXml;
57  
58      /** List of connectors. */
59      private List connectors;
60  
61      /** Request logger. */
62      private RequestLog requestLog;
63  
64      /** User realms. */
65      private List userRealms;
66  
67      /** List of added web applications. */
68      private Map webApplications = new HashMap();
69  
70      /**
71       * Default constructor. Creates a new Jetty server with a standard connector
72       * listening on a given port.
73       * 
74       * @param userRealmsList
75       * 
76       * @param port default connector port number.
77       * @param maxIdleTime default connector maximum idle time of for each
78       *            connection.
79       */
80      public ServerProxyImpl(List connectors, List userRealmsList, RequestLog requestLog,
81              File jettyXml)
82      {
83          server = new Server();
84          server.setStopAtShutdown(true);
85  
86          this.connectors = connectors;
87          this.userRealms = userRealmsList;
88          this.requestLog = requestLog;
89          this.jettyXml = jettyXml;
90          configure();
91      }
92  
93      /**
94       * @see org.mortbay.jetty.ant.utils.ServerProxy#addWebApplication(WebApplicationProxy,
95       *      int)
96       */
97      public void addWebApplication(WebApplicationProxy webApp, int scanIntervalSeconds)
98      {
99          webApp.createApplicationContext(contexts);
100 
101         if (scanIntervalSeconds > 0)
102         {
103             webApplications.put(webApp, new Integer(scanIntervalSeconds));
104         }
105     }
106 
107     /**
108      * Configures Jetty server before adding any web applications to it.
109      */
110     private void configure()
111     {
112         // Applies external configuration via jetty.xml
113         applyJettyXml();
114 
115         // Configures connectores for this server instance.
116         Iterator connectorIterator = connectors.iterator();
117         while (connectorIterator.hasNext())
118         {
119             Connector jettyConnector = (Connector) connectorIterator.next();
120             server.addConnector(jettyConnector);
121         }
122 
123         // Configures user realms
124         Iterator realmsIterator = userRealms.iterator();
125         while (realmsIterator.hasNext())
126         {
127             UserRealm realm = (UserRealm) realmsIterator.next();
128             server.addUserRealm(realm);
129         }
130 
131         // Does not cache resources, to prevent Windows from locking files
132         Resource.setDefaultUseCaches(false);
133 
134         // Set default server handlers
135         configureHandlers();
136     }
137 
138     private void configureHandlers()
139     {
140         RequestLogHandler requestLogHandler = new RequestLogHandler();
141         if (requestLog != null)
142         {
143             requestLogHandler.setRequestLog(requestLog);
144         }
145 
146         contexts = (ContextHandlerCollection) server
147                 .getChildHandlerByClass(ContextHandlerCollection.class);
148         if (contexts == null)
149         {
150             contexts = new ContextHandlerCollection();
151             HandlerCollection handlers = (HandlerCollection) server
152                     .getChildHandlerByClass(HandlerCollection.class);
153             if (handlers == null)
154             {
155                 handlers = new HandlerCollection();
156                 server.setHandler(handlers);
157                 handlers.setHandlers(new Handler[] { contexts, new DefaultHandler(),
158                         requestLogHandler });
159             }
160             else
161             {
162                 handlers.addHandler(contexts);
163             }
164         }
165     }
166 
167     /**
168      * Applies jetty.xml configuration to the Jetty server instance.
169      */
170     private void applyJettyXml()
171     {
172         if (jettyXml != null && jettyXml.exists())
173         {
174             TaskLog.log("Configuring jetty from xml configuration file = "
175                     + jettyXml.getAbsolutePath());
176             XmlConfiguration configuration;
177             try
178             {
179                 configuration = new XmlConfiguration(jettyXml.toURL());
180                 configuration.configure(server);
181             }
182             catch (Exception e)
183             {
184                 throw new RuntimeException(e);
185             }
186         }
187     }
188 
189     /**
190      * @see org.mortbay.jetty.ant.utils.ServerProxy#start()
191      */
192     public void start()
193     {
194         try
195         {
196             server.start();
197             startScanners();
198             server.join();
199 
200         }
201         catch (InterruptedException e)
202         {
203             new RuntimeException(e);
204         }
205         catch (Exception e)
206         {
207             new RuntimeException(e);
208         }
209     }
210 
211     /**
212      * Starts web applications' scanners.
213      */
214     private void startScanners()
215     {
216         Iterator i = webApplications.keySet().iterator();
217         while (i.hasNext())
218         {
219             WebApplicationProxyImpl webApp = (WebApplicationProxyImpl) i.next();
220             Integer scanIntervalSeconds = (Integer) webApplications.get(webApp);
221             JettyRunTask.startScanner(webApp, scanIntervalSeconds.intValue());
222         }
223     }
224 
225     /**
226      * @see org.mortbay.jetty.ant.utils.ServerProxy#getProxiedObject()
227      */
228     public Object getProxiedObject()
229     {
230         return server;
231     }
232 }