View Javadoc

1   //
2   //  ========================================================================
3   //  Copyright (c) 1995-2015 Mort Bay Consulting Pty. Ltd.
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  
20  package org.eclipse.jetty.maven.plugin;
21  
22  import java.io.File;
23  import java.util.Enumeration;
24  import java.util.HashMap;
25  import java.util.List;
26  import java.util.Map;
27  
28  import org.eclipse.jetty.security.LoginService;
29  import org.eclipse.jetty.server.Connector;
30  import org.eclipse.jetty.server.Handler;
31  import org.eclipse.jetty.server.RequestLog;
32  import org.eclipse.jetty.server.Server;
33  import org.eclipse.jetty.server.handler.ContextHandlerCollection;
34  import org.eclipse.jetty.server.handler.DefaultHandler;
35  import org.eclipse.jetty.server.handler.HandlerCollection;
36  import org.eclipse.jetty.server.handler.RequestLogHandler;
37  import org.eclipse.jetty.util.resource.Resource;
38  import org.eclipse.jetty.webapp.WebAppContext;
39  import org.eclipse.jetty.xml.XmlConfiguration;
40  
41  /**
42   * ServerSupport
43   *
44   * Helps configure the Server instance.
45   * 
46   */
47  public class ServerSupport
48  {
49      /**
50       * Set up the handler structure to receive a webapp.
51       * Also put in a DefaultHandler so we get a nice page
52       * than a 404 if we hit the root and the webapp's
53       * context isn't at root.
54       * @param server the server
55       * @param requestLog the request log
56       * @throws Exception if unable to configure the handlers
57       */
58      public static void configureHandlers (Server server, RequestLog requestLog) throws Exception 
59      {
60          if (server == null)
61              throw new IllegalArgumentException ("Server is null");
62  
63          DefaultHandler defaultHandler = new DefaultHandler();
64          RequestLogHandler requestLogHandler = new RequestLogHandler();
65          if (requestLog != null)
66              requestLogHandler.setRequestLog(requestLog);
67  
68          ContextHandlerCollection contexts = findContextHandlerCollection(server);
69          if (contexts == null)
70          {   
71              contexts = new ContextHandlerCollection();
72              HandlerCollection handlers = (HandlerCollection)server.getChildHandlerByClass(HandlerCollection.class);
73              if (handlers == null)
74              {
75                  handlers = new HandlerCollection();               
76                  server.setHandler(handlers);                            
77                  handlers.setHandlers(new Handler[]{contexts, defaultHandler, requestLogHandler});
78              }
79              else
80              {
81                  handlers.addHandler(contexts);
82              }
83          }  
84      }
85      
86  
87      /**
88       * Configure at least one connector for the server
89       * 
90       * @param server the server
91       * @param connector the connector
92       */
93      public static void configureConnectors (Server server, Connector connector)
94      {
95          if (server == null)
96              throw new IllegalArgumentException("Server is null");
97          
98          //if a connector is provided, use it
99          if (connector != null)
100         {
101             server.addConnector(connector);
102             return;
103         }
104         
105         
106 
107         // if the user hasn't configured the connectors in a jetty.xml file so use a default one
108         Connector[] connectors = server.getConnectors();
109         if (connectors == null || connectors.length == 0)
110         {
111             //Make a new default connector
112             MavenServerConnector tmp = new MavenServerConnector();               
113             //use any jetty.http.port settings provided
114             String port = System.getProperty(MavenServerConnector.PORT_SYSPROPERTY, System.getProperty("jetty.port", MavenServerConnector.DEFAULT_PORT_STR));
115             tmp.setPort(Integer.parseInt(port.trim()));
116             tmp.setServer(server);
117             server.setConnectors(new Connector[] {tmp});
118         }
119     }
120     
121     
122     /**
123      * Set up any security LoginServices provided.
124      * 
125      * @param server the server
126      * @param loginServices the login services
127      */
128     public static void configureLoginServices (Server server, LoginService[] loginServices)
129     {
130         if (server == null)
131             throw new IllegalArgumentException ("Server is null");
132 
133         if (loginServices != null)
134         {
135             for (LoginService loginService:loginServices)
136             {
137                 PluginLog.getLog().debug(loginService.getClass().getName() + ": "+ loginService.toString());
138                 server.addBean(loginService);
139             }
140         }
141     }
142     
143     public static void addWebApplication(Server server, WebAppContext webapp) throws Exception
144     {  
145         if (server == null)
146             throw new IllegalArgumentException ("Server is null");
147        ContextHandlerCollection contexts = findContextHandlerCollection(server);
148        if (contexts == null)
149            throw new IllegalStateException("ContextHandlerCollection is null");
150        contexts.addHandler (webapp);
151     }
152     
153 
154     public static ContextHandlerCollection findContextHandlerCollection (Server server)
155     {
156         if (server == null)
157             return null;
158 
159         return (ContextHandlerCollection)server.getChildHandlerByClass(ContextHandlerCollection.class);
160     }
161 
162 
163     /**
164      * Apply xml files to server startup, passing in ourselves as the 
165      * "Server" instance.
166      * 
167      * @param server the server to apply the xml to
168      * @param files the list of xml files
169      * @return the Server implementation, after the xml is applied
170      * @throws Exception if unable to apply the xml configuration
171      */
172     public static Server applyXmlConfigurations (Server server, List<File> files) 
173     throws Exception
174     {
175         if (files == null || files.isEmpty())
176             return server;
177 
178         Map<String,Object> lastMap = new HashMap<String,Object>();
179         
180         if (server != null)
181             lastMap.put("Server", server);
182      
183 
184         for ( File xmlFile : files )
185         {
186             if (PluginLog.getLog() != null)
187                 PluginLog.getLog().info( "Configuring Jetty from xml configuration file = " + xmlFile.getCanonicalPath() );   
188 
189 
190             XmlConfiguration xmlConfiguration = new XmlConfiguration(Resource.toURL(xmlFile));
191 
192             //chain ids from one config file to another
193             if (lastMap != null)
194                 xmlConfiguration.getIdMap().putAll(lastMap); 
195 
196             //Set the system properties each time in case the config file set a new one
197             Enumeration<?> ensysprop = System.getProperties().propertyNames();
198             while (ensysprop.hasMoreElements())
199             {
200                 String name = (String)ensysprop.nextElement();
201                 xmlConfiguration.getProperties().put(name,System.getProperty(name));
202             }
203             xmlConfiguration.configure(); 
204             lastMap = xmlConfiguration.getIdMap();
205         }
206         
207         return (Server)lastMap.get("Server");
208     }
209 
210 }