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.SslSelectChannelConnector;
    30  import org.mortbay.jetty.security.SslSocketConnector;
    31  import org.mortbay.jetty.security.UserRealm;
    32  import org.mortbay.jetty.webapp.WebAppContext;
    33  import org.mortbay.thread.BoundedThreadPool;
    34  import org.mortbay.thread.QueuedThreadPool;
    35
    36  public class LikeJettyXml
    37  {
    38      public static void main(String[] args)
    39          throws Exception
    40      {
    41          String jetty_default=new java.io.File("./start.jar").exists()?".":"../..";;
    42          String jetty_home = System.getProperty("jetty.home",jetty_default);
    43
    44          Server server = new Server();
    45
    46          QueuedThreadPool threadPool = new QueuedThreadPool();
    47          threadPool.setMaxThreads(100);
    48          server.setThreadPool(threadPool);
    49
    50          SelectChannelConnector connector=new SelectChannelConnector();
    51          connector.setPort(8080);
    52          connector.setMaxIdleTime(30000);
    53          connector.setConfidentialPort(8443);
    54          server.setConnectors(new Connector[]{connector});
    55
    56          SslSelectChannelConnector ssl_connector=new SslSelectChannelConnector();
    57          ssl_connector.setPort(8443);
    58          ssl_connector.setKeystore(jetty_home+"/etc/keystore");
    59          ssl_connector.setPassword("OBF:1vny1zlo1x8e1vnw1vn61x8g1zlu1vn4");
    60          ssl_connector.setKeyPassword("OBF:1u2u1wml1z7s1z7a1wnl1u2g");
    61          ssl_connector.setTruststore(jetty_home+"/etc/keystore");
    62          ssl_connector.setTrustPassword("OBF:1vny1zlo1x8e1vnw1vn61x8g1zlu1vn4");
    63          server.addConnector(ssl_connector);
    64
    65          SslSocketConnector ssl_b_connector=new SslSocketConnector();
    66          ssl_b_connector.setPort(8444);
    67          ssl_b_connector.setKeystore(jetty_home+"/etc/keystore");
    68          ssl_b_connector.setPassword("OBF:1vny1zlo1x8e1vnw1vn61x8g1zlu1vn4");
    69          ssl_b_connector.setKeyPassword("OBF:1u2u1wml1z7s1z7a1wnl1u2g");
    70          ssl_b_connector.setTruststore(jetty_home+"/etc/keystore");
    71          ssl_b_connector.setTrustPassword("OBF:1vny1zlo1x8e1vnw1vn61x8g1zlu1vn4");
    72          server.addConnector(ssl_b_connector);
    73
    74          HandlerCollection handlers = new HandlerCollection();
    75          ContextHandlerCollection contexts = new ContextHandlerCollection();
    76          RequestLogHandler requestLogHandler = new RequestLogHandler();
    77          handlers.setHandlers(new Handler[]{contexts,new DefaultHandler(),requestLogHandler});
    78          server.setHandler(handlers);
    79
    80          ContextDeployer deployer0 = new ContextDeployer();
    81          deployer0.setContexts(contexts);
    82          deployer0.setConfigurationDir(jetty_home+"/contexts");
    83          deployer0.setScanInterval(1);
    84          server.addLifeCycle(deployer0);
    85
    86          WebAppDeployer deployer1 = new WebAppDeployer();
    87          deployer1.setContexts(contexts);
    88          deployer1.setWebAppDir(jetty_home+"/webapps");
    89          deployer1.setParentLoaderPriority(false);
    90          deployer1.setExtract(true);
    91          deployer1.setAllowDuplicates(false);
    92          deployer1.setDefaultsDescriptor(jetty_home+"/etc/webdefault.xml");
    93          server.addLifeCycle(deployer1);
    94
    95          HashUserRealm userRealm = new HashUserRealm();
    96          userRealm.setName("Test Realm");
    97          userRealm.setConfig(jetty_home+"/etc/realm.properties");
    98          server.setUserRealms(new UserRealm[]{userRealm});
    99
    100         NCSARequestLog requestLog = new NCSARequestLog(jetty_home+"/logs/jetty-yyyy_mm_dd.log");
    101         requestLog.setExtended(false);
    102         requestLogHandler.setRequestLog(requestLog);
    103
    104         server.setStopAtShutdown(true);
    105         server.setSendServerVersion(true);
    106
    107         server.start();
    108         server.join();
    109     }
    110
    111 }