View Javadoc

1   //
2   //  ========================================================================
3   //  Copyright (c) 1995-2014 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  package org.eclipse.jetty.maven.plugin;
20  
21  
22  import java.io.File;
23  import java.util.Collections;
24  import java.util.Enumeration;
25  import java.util.List;
26  import java.util.Map;
27  
28  import org.eclipse.jetty.server.Handler;
29  import org.eclipse.jetty.server.RequestLog;
30  import org.eclipse.jetty.server.handler.ContextHandlerCollection;
31  import org.eclipse.jetty.server.handler.DefaultHandler;
32  import org.eclipse.jetty.server.handler.HandlerCollection;
33  import org.eclipse.jetty.server.handler.RequestLogHandler;
34  import org.eclipse.jetty.util.resource.Resource;
35  import org.eclipse.jetty.webapp.WebAppContext;
36  import org.eclipse.jetty.xml.XmlConfiguration;
37  
38  
39  /**
40   * JettyServer
41   * 
42   * Maven jetty plugin version of a wrapper for the Server class.
43   * 
44   */
45  public class JettyServer extends org.eclipse.jetty.server.Server
46  { 
47      private RequestLog requestLog;
48      private ContextHandlerCollection contexts;
49      
50      
51      
52      /**
53       * 
54       */
55      public JettyServer()
56      {
57          super();
58          //make sure Jetty does not use URLConnection caches with the plugin
59          Resource.setDefaultUseCaches(false);
60      }
61  
62     
63      public void setRequestLog (RequestLog requestLog)
64      {
65          this.requestLog = requestLog;
66      }
67  
68      /**
69       * @see org.eclipse.jetty.server.Server#doStart()
70       */
71      public void doStart() throws Exception
72      {
73          super.doStart();
74      }
75  
76   
77      /**
78       * @see org.eclipse.jetty.server.handler.HandlerCollection#addHandler(org.eclipse.jetty.server.Handler)
79       */
80      public void addWebApplication(WebAppContext webapp) throws Exception
81      {  
82          contexts.addHandler (webapp);
83      }
84  
85      
86      /**
87       * Set up the handler structure to receive a webapp.
88       * Also put in a DefaultHandler so we get a nice page
89       * than a 404 if we hit the root and the webapp's
90       * context isn't at root.
91       * @throws Exception
92       */
93      public void configureHandlers () throws Exception 
94      {
95          DefaultHandler defaultHandler = new DefaultHandler();
96          RequestLogHandler requestLogHandler = new RequestLogHandler();
97          if (this.requestLog != null)
98              requestLogHandler.setRequestLog(this.requestLog);
99          
100         contexts = (ContextHandlerCollection)super.getChildHandlerByClass(ContextHandlerCollection.class);
101         if (contexts==null)
102         {   
103             contexts = new ContextHandlerCollection();
104             HandlerCollection handlers = (HandlerCollection)super.getChildHandlerByClass(HandlerCollection.class);
105             if (handlers==null)
106             {
107                 handlers = new HandlerCollection();               
108                 super.setHandler(handlers);                            
109                 handlers.setHandlers(new Handler[]{contexts, defaultHandler, requestLogHandler});
110             }
111             else
112             {
113                 handlers.addHandler(contexts);
114             }
115         }  
116     }
117 
118     /**
119      * Apply xml files to server startup, passing in ourselves as the 
120      * "Server" instance.
121      * 
122      * @param files
123      * @throws Exception
124      */
125     public  void applyXmlConfigurations (List<File> files) 
126     throws Exception
127     {
128         if (files == null || files.isEmpty())
129             return;
130 
131        Map<String,Object> lastMap = Collections.singletonMap("Server", (Object)this);
132 
133         for ( File xmlFile : files )
134         {
135             if (PluginLog.getLog() != null)
136                 PluginLog.getLog().info( "Configuring Jetty from xml configuration file = " + xmlFile.getCanonicalPath() );   
137 
138 
139             XmlConfiguration xmlConfiguration = new XmlConfiguration(Resource.toURL(xmlFile));
140 
141             //chain ids from one config file to another
142             if (lastMap != null)
143                 xmlConfiguration.getIdMap().putAll(lastMap); 
144 
145             //Set the system properties each time in case the config file set a new one
146             Enumeration<?> ensysprop = System.getProperties().propertyNames();
147             while (ensysprop.hasMoreElements())
148             {
149                 String name = (String)ensysprop.nextElement();
150                 xmlConfiguration.getProperties().put(name,System.getProperty(name));
151             }
152             xmlConfiguration.configure(); 
153             lastMap = xmlConfiguration.getIdMap();
154         }
155     }
156 }