View Javadoc

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