View Javadoc

    1   //========================================================================
    2   //Copyright 2006 Mort Bay Consulting Pty. Ltd.
    3   //------------------------------------------------------------------------
    4   //Licensed under the Apache License, Version 2.0 (the "License");
    5   //you may not use this file except in compliance with the License.
    6   //You may obtain a copy of the License at
    7   //http://www.apache.org/licenses/LICENSE-2.0
    8   //Unless required by applicable law or agreed to in writing, software
    9   //distributed under the License is distributed on an "AS IS" BASIS,
    10  //WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    11  //See the License for the specific language governing permissions and
    12  //limitations under the License.
    13  //========================================================================
    14
    15  package org.mortbay.jetty.example;
    16
    17  import org.mortbay.jetty.Connector;
    18  import org.mortbay.jetty.Handler;
    19  import org.mortbay.jetty.NCSARequestLog;
    20  import org.mortbay.jetty.Server;
    21  import org.mortbay.jetty.deployer.ContextDeployer;
    22  import org.mortbay.jetty.deployer.WebAppDeployer;
    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.security.HashUserRealm;
    29  import org.mortbay.jetty.security.UserRealm;
    30  import org.mortbay.jetty.webapp.WebAppContext;
    31  import org.mortbay.thread.BoundedThreadPool;
    32  import org.mortbay.thread.QueuedThreadPool;
    33
    34  public class LikeJettyXml
    35  {
    36      public static void main(String[] args)
    37          throws Exception
    38      {
    39          String jetty_default=new java.io.File("./start.jar").exists()?".":"../..";;
    40          String jetty_home = System.getProperty("jetty.home",jetty_default);
    41
    42          Server server = new Server();
    43
    44          QueuedThreadPool threadPool = new QueuedThreadPool();
    45          threadPool.setMaxThreads(100);
    46          server.setThreadPool(threadPool);
    47
    48          Connector connector=new SelectChannelConnector();
    49          connector.setPort(8080);
    50          connector.setMaxIdleTime(30000);
    51          server.setConnectors(new Connector[]{connector});
    52
    53          HandlerCollection handlers = new HandlerCollection();
    54          ContextHandlerCollection contexts = new ContextHandlerCollection();
    55          RequestLogHandler requestLogHandler = new RequestLogHandler();
    56          handlers.setHandlers(new Handler[]{contexts,new DefaultHandler(),requestLogHandler});
    57          server.setHandler(handlers);
    58
    59          ContextDeployer deployer0 = new ContextDeployer();
    60          deployer0.setContexts(contexts);
    61          deployer0.setConfigurationDir(jetty_home+"/contexts");
    62          deployer0.setScanInterval(1);
    63          server.addLifeCycle(deployer0);
    64
    65          WebAppDeployer deployer1 = new WebAppDeployer();
    66          deployer1.setContexts(contexts);
    67          deployer1.setWebAppDir(jetty_home+"/webapps");
    68          deployer1.setParentLoaderPriority(false);
    69          deployer1.setExtract(true);
    70          deployer1.setAllowDuplicates(false);
    71          deployer1.setDefaultsDescriptor(jetty_home+"/etc/webdefault.xml");
    72          server.addLifeCycle(deployer1);
    73
    74          HashUserRealm userRealm = new HashUserRealm();
    75          userRealm.setName("Test Realm");
    76          userRealm.setConfig(jetty_home+"/etc/realm.properties");
    77          server.setUserRealms(new UserRealm[]{userRealm});
    78
    79          NCSARequestLog requestLog = new NCSARequestLog(jetty_home+"/logs/jetty-yyyy_mm_dd.log");
    80          requestLog.setExtended(false);
    81          requestLogHandler.setRequestLog(requestLog);
    82
    83          server.setStopAtShutdown(true);
    84          server.setSendServerVersion(true);
    85
    86          server.start();
    87          server.join();
    88      }
    89
    90  }