View Javadoc

1   //
2   //  ========================================================================
3   //  Copyright (c) 1995-2013 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 org.eclipse.jetty.server.Handler;
23  import org.eclipse.jetty.server.RequestLog;
24  import org.eclipse.jetty.server.handler.ContextHandlerCollection;
25  import org.eclipse.jetty.server.handler.DefaultHandler;
26  import org.eclipse.jetty.server.handler.HandlerCollection;
27  import org.eclipse.jetty.server.handler.RequestLogHandler;
28  import org.eclipse.jetty.util.resource.Resource;
29  import org.eclipse.jetty.webapp.WebAppContext;
30  
31  /**
32   * JettyServer
33   * 
34   * Maven jetty plugin version of a wrapper for the Server class.
35   * 
36   */
37  public class JettyServer extends org.eclipse.jetty.server.Server
38  {
39      public static final JettyServer __instance = new JettyServer();
40      
41      /**
42       * Singleton instance
43       * @return
44       */
45      public static JettyServer getInstance()
46      {
47          return __instance;
48      }
49  
50      
51      private RequestLog requestLog;
52      private ContextHandlerCollection contexts;
53      
54      
55      
56      /**
57       * 
58       */
59      private JettyServer()
60      {
61          super();
62          setStopAtShutdown(true);
63          //make sure Jetty does not use URLConnection caches with the plugin
64          Resource.setDefaultUseCaches(false);
65      }
66  
67     
68      public void setRequestLog (RequestLog requestLog)
69      {
70          this.requestLog = requestLog;
71      }
72  
73      /**
74       * @see org.eclipse.jetty.server.Server#doStart()
75       */
76      public void doStart() throws Exception
77      {
78          super.doStart();
79      }
80  
81   
82      /**
83       * @see org.eclipse.jetty.server.handler.HandlerCollection#addHandler(org.eclipse.jetty.server.Handler)
84       */
85      public void addWebApplication(WebAppContext webapp) throws Exception
86      {  
87          contexts.addHandler (webapp);
88      }
89  
90      
91      /**
92       * Set up the handler structure to receive a webapp.
93       * Also put in a DefaultHandler so we get a nice page
94       * than a 404 if we hit the root and the webapp's
95       * context isn't at root.
96       * @throws Exception
97       */
98      public void configureHandlers () throws Exception 
99      {
100         DefaultHandler defaultHandler = new DefaultHandler();
101         RequestLogHandler requestLogHandler = new RequestLogHandler();
102         if (this.requestLog != null)
103             requestLogHandler.setRequestLog(this.requestLog);
104         
105         contexts = (ContextHandlerCollection)super.getChildHandlerByClass(ContextHandlerCollection.class);
106         if (contexts==null)
107         {   
108             contexts = new ContextHandlerCollection();
109             HandlerCollection handlers = (HandlerCollection)super.getChildHandlerByClass(HandlerCollection.class);
110             if (handlers==null)
111             {
112                 handlers = new HandlerCollection();               
113                 super.setHandler(handlers);                            
114                 handlers.setHandlers(new Handler[]{contexts, defaultHandler, requestLogHandler});
115             }
116             else
117             {
118                 handlers.addHandler(contexts);
119             }
120         }  
121     }
122 }