View Javadoc

    1   //========================================================================
    2   //$Id: Jetty6PluginServer.java 2094 2007-09-10 06:11:26Z janb $
    3   //Copyright 2000-2004 Mort Bay Consulting Pty. Ltd.
    4   //------------------------------------------------------------------------
    5   //Licensed under the Apache License, Version 2.0 (the "License");
    6   //you may not use this file except in compliance with the License.
    7   //You may obtain a copy of the License at
    8   //http://www.apache.org/licenses/LICENSE-2.0
    9   //Unless required by applicable law or agreed to in writing, software
    10  //distributed under the License is distributed on an "AS IS" BASIS,
    11  //WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  //See the License for the specific language governing permissions and
    13  //limitations under the License.
    14  //========================================================================
    15
    16  package org.mortbay.jetty.plugin;
    17
    18
    19  import org.mortbay.jetty.Connector;
    20  import org.mortbay.jetty.Handler;
    21  import org.mortbay.jetty.RequestLog;
    22  import org.mortbay.jetty.Server;
    23  import org.mortbay.jetty.handler.ContextHandlerCollection;
    24  import org.mortbay.jetty.handler.DefaultHandler;
    25  import org.mortbay.jetty.handler.HandlerCollection;
    26  import org.mortbay.jetty.handler.RequestLogHandler;
    27  import org.mortbay.jetty.nio.SelectChannelConnector;
    28  import org.mortbay.jetty.plugin.util.JettyPluginServer;
    29  import org.mortbay.jetty.plugin.util.PluginLog;
    30  import org.mortbay.jetty.security.UserRealm;
    31  import org.mortbay.jetty.webapp.WebAppContext;
    32  import org.mortbay.resource.Resource;
    33
    34  /**
    35   * Jetty6PluginServer
    36   *
    37   * Jetty6 version of a wrapper for the Server class.
    38   *
    39   */
    40  public class Jetty6PluginServer implements JettyPluginServer
    41  {
    42      public static int DEFAULT_PORT = 8080;
    43      public static int DEFAULT_MAX_IDLE_TIME = 30000;
    44      private Server server;
    45      private ContextHandlerCollection contexts; //the list of ContextHandlers
    46      HandlerCollection handlers; //the list of lists of Handlers
    47      private RequestLogHandler requestLogHandler; //the request log handler
    48      private DefaultHandler defaultHandler; //default handler
    49
    50      private RequestLog requestLog; //the particular request log implementation
    51
    52
    53      /**
    54       * @see org.mortbay.jetty.plugin.util.JettyPluginServer#create()
    55       */
    56      public Jetty6PluginServer()
    57      {
    58          this.server = new Server();
    59          this.server.setStopAtShutdown(true);
    60          //make sure Jetty does not use URLConnection caches with the plugin
    61          Resource.setDefaultUseCaches(false);
    62      }
    63
    64      /**
    65       * @see org.mortbay.jetty.plugin.util.JettyPluginServer#setConnectorNames(org.mortbay.jetty.plugin.util.JettyPluginConnector[])
    66       */
    67      public void setConnectors(Object[] connectors)
    68      {
    69          if (connectors==null || connectors.length==0)
    70              return;
    71
    72          for (int i=0; i<connectors.length;i++)
    73          {
    74              Connector connector = (Connector)connectors[i];
    75              PluginLog.getLog().debug("Setting Connector: "+connector.getClass().getName()+" on port "+connector.getPort());
    76              this.server.addConnector(connector);
    77          }
    78      }
    79
    80
    81
    82      /**
    83       *
    84       *
    85       * @see org.mortbay.jetty.plugin.util.JettyPluginServer#getConnectors()
    86       */
    87      public Object[] getConnectors()
    88      {
    89          return this.server.getConnectors();
    90      }
    91
    92      /**
    93       *
    94       *
    95       * @see org.mortbay.jetty.plugin.JettyPluginServer#setUserRealms(org.mortbay.jetty.plugin.JettyPluginUserRealm[])
    96       */
    97      public void setUserRealms(Object[] realms) throws Exception
    98      {
    99          if (realms == null)
    100             return;
    101
    102          for (int i=0; i<realms.length;i++)
    103              this.server.addUserRealm((UserRealm)realms[i]);
    104     }
    105
    106     /**
    107      *
    108      * @see org.mortbay.jetty.plugin.util.JettyPluginServer#getUserRealms()
    109      */
    110     public Object[] getUserRealms()
    111     {
    112         return this.server.getUserRealms();
    113     }
    114
    115
    116     public void setRequestLog (Object requestLog)
    117     {
    118         this.requestLog = (RequestLog)requestLog;
    119     }
    120
    121     public Object getRequestLog ()
    122     {
    123         return this.requestLog;
    124     }
    125
    126     /**
    127      * @see org.mortbay.jetty.plugin.util.JettyPluginServer#start()
    128      */
    129     public void start() throws Exception
    130     {
    131         PluginLog.getLog().info("Starting jetty "+this.server.getClass().getPackage().getImplementationVersion()+" ...");
    132         this.server.start();
    133     }
    134
    135     /**
    136      * @see org.mortbay.jetty.plugin.util.Proxy#getProxiedObject()
    137      */
    138     public Object getProxiedObject()
    139     {
    140         return this.server;
    141     }
    142
    143     /**
    144      * @see org.mortbay.jetty.plugin.util.JettyPluginServer#addWebApplication(java.lang.Object)
    145      */
    146     public void addWebApplication(WebAppContext webapp) throws Exception
    147     {
    148         contexts.addHandler (webapp);
    149     }
    150
    151
    152     /**
    153      * Set up the handler structure to receive a webapp.
    154      * Also put in a DefaultHandler so we get a nice page
    155      * than a 404 if we hit the root and the webapp's
    156      * context isn't at root.
    157      * @throws Exception
    158      */
    159     public void configureHandlers () throws Exception
    160     {
    161         this.defaultHandler = new DefaultHandler();
    162         this.requestLogHandler = new RequestLogHandler();
    163         if (this.requestLog != null)
    164             this.requestLogHandler.setRequestLog(this.requestLog);
    165
    166         this.contexts = (ContextHandlerCollection)server.getChildHandlerByClass(ContextHandlerCollection.class);
    167         if (this.contexts==null)
    168         {
    169             this.contexts = new ContextHandlerCollection();
    170             this.handlers = (HandlerCollection)server.getChildHandlerByClass(HandlerCollection.class);
    171             if (this.handlers==null)
    172             {
    173                 this.handlers = new HandlerCollection();
    174                 this.server.setHandler(handlers);
    175                 this.handlers.setHandlers(new Handler[]{this.contexts, this.defaultHandler, this.requestLogHandler});
    176             }
    177             else
    178             {
    179                 this.handlers.addHandler(this.contexts);
    180             }
    181         }
    182     }
    183
    184
    185
    186
    187     /**
    188      * @see org.mortbay.jetty.plugin.JettyPluginServer#createDefaultConnector()
    189      */
    190     public Object createDefaultConnector(String portnum) throws Exception
    191     {
    192         SelectChannelConnector connector = new SelectChannelConnector();
    193         connector = new SelectChannelConnector();
    194         int port = ((portnum==null||portnum.equals(""))?DEFAULT_PORT:Integer.parseInt(portnum.trim()));
    195         connector.setPort(port);
    196         connector.setMaxIdleTime(DEFAULT_MAX_IDLE_TIME);
    197
    198         return connector;
    199     }
    200
    201
    202
    203
    204     public void join () throws Exception
    205     {
    206         this.server.getThreadPool().join();
    207     }
    208 }