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      private RequestLog requestLog;
42      private ContextHandlerCollection contexts;
43      
44      
45      
46      /**
47       * 
48       */
49      public JettyServer()
50      {
51          super();
52          setStopAtShutdown(true);
53          //make sure Jetty does not use URLConnection caches with the plugin
54          Resource.setDefaultUseCaches(false);
55      }
56  
57     
58      public void setRequestLog (RequestLog requestLog)
59      {
60          this.requestLog = requestLog;
61      }
62  
63      /**
64       * @see org.eclipse.jetty.server.Server#doStart()
65       */
66      public void doStart() throws Exception
67      {
68          super.doStart();
69      }
70  
71   
72      /**
73       * @see org.eclipse.jetty.server.handler.HandlerCollection#addHandler(org.eclipse.jetty.server.Handler)
74       */
75      public void addWebApplication(WebAppContext webapp) throws Exception
76      {  
77          contexts.addHandler (webapp);
78      }
79  
80      
81      /**
82       * Set up the handler structure to receive a webapp.
83       * Also put in a DefaultHandler so we get a nice page
84       * than a 404 if we hit the root and the webapp's
85       * context isn't at root.
86       * @throws Exception
87       */
88      public void configureHandlers () throws Exception 
89      {
90          DefaultHandler defaultHandler = new DefaultHandler();
91          RequestLogHandler requestLogHandler = new RequestLogHandler();
92          if (this.requestLog != null)
93              requestLogHandler.setRequestLog(this.requestLog);
94          
95          contexts = (ContextHandlerCollection)super.getChildHandlerByClass(ContextHandlerCollection.class);
96          if (contexts==null)
97          {   
98              contexts = new ContextHandlerCollection();
99              HandlerCollection handlers = (HandlerCollection)super.getChildHandlerByClass(HandlerCollection.class);
100             if (handlers==null)
101             {
102                 handlers = new HandlerCollection();               
103                 super.setHandler(handlers);                            
104                 handlers.setHandlers(new Handler[]{contexts, defaultHandler, requestLogHandler});
105             }
106             else
107             {
108                 handlers.addHandler(contexts);
109             }
110         }  
111     }
112 }