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;
    16
    17  import org.mortbay.jetty.bio.SocketConnector;
    18  import org.mortbay.jetty.handler.ContextHandler;
    19  import org.mortbay.jetty.handler.ContextHandlerCollection;
    20  import org.mortbay.jetty.servlet.ServletHandler;
    21  import org.mortbay.jetty.webapp.WebAppContext;
    22  import org.mortbay.log.Log;
    23  import org.mortbay.util.URIUtil;
    24
    25  public class Main
    26  {
    27
    28      /* ------------------------------------------------------------ */
    29      /* ------------------------------------------------------------ */
    30      /** Construct server from command line arguments.
    31       * @param args
    32       */
    33      public static void main(String[] args)
    34      {
    35          if (args.length<1 || args.length>3)
    36          {
    37              System.err.println("Usage - java org.mortbay.jetty.Main [<addr>:]<port>");
    38              System.err.println("Usage - java org.mortbay.jetty.Main [<addr>:]<port> docroot");
    39              System.err.println("Usage - java org.mortbay.jetty.Main [<addr>:]<port> -webapp myapp.war");
    40              System.err.println("Usage - java org.mortbay.jetty.Main [<addr>:]<port> -webapps webapps");
    41              System.err.println("Usage - java -jar jetty-x.x.x-standalone.jar [<addr>:]<port>");
    42              System.err.println("Usage - java -jar jetty-x.x.x-standalone.jar [<addr>:]<port> docroot");
    43              System.err.println("Usage - java -jar jetty-x.x.x-standalone.jar [<addr>:]<port> -webapp myapp.war");
    44              System.err.println("Usage - java -jar jetty-x.x.x-standalone.jar [<addr>:]<port> -webapps webapps");
    45              System.exit(1);
    46          }
    47
    48          try{
    49
    50              // Create the server
    51              Server server = new Server();
    52              ContextHandlerCollection contexts = new ContextHandlerCollection();
    53              server.setHandler(contexts);
    54
    55              SocketConnector connector = new SocketConnector();
    56              String address = args[0];
    57              int colon = address.lastIndexOf(':');
    58              if (colon<0)
    59                  connector.setPort(Integer.parseInt(address));
    60              else
    61              {
    62                  connector.setHost(address.substring(0,colon));
    63                  connector.setPort(Integer.parseInt(address.substring(colon+1)));
    64              }
    65              server.setConnectors(new Connector[]{connector});
    66
    67              if (args.length<3)
    68              {
    69                  ContextHandler context = new ContextHandler();
    70                  context.setContextPath(URIUtil.SLASH);
    71                  context.setResourceBase(args.length==1?".":args[1]);
    72                  ServletHandler servlet = new ServletHandler();
    73                  servlet.addServletWithMapping("org.mortbay.jetty.servlet.DefaultServlet", URIUtil.SLASH);
    74                  context.setHandler(servlet);
    75                  contexts.addHandler(context);
    76              }
    77              else if ("-webapps".equals(args[1]))
    78              {
    79                  WebAppContext.addWebApplications(server, args[2], WebAppContext.WEB_DEFAULTS_XML, true, true);
    80              }
    81              else if ("-webapp".equals(args[1]))
    82              {
    83                  WebAppContext webapp = new WebAppContext();
    84                  webapp.setResourceBase(args[2]);
    85                  webapp.setContextPath(URIUtil.SLASH);
    86                  contexts.addHandler(webapp);
    87
    88              }
    89
    90              server.start();
    91
    92          }
    93          catch (Exception e)
    94          {
    95              Log.warn(Log.EXCEPTION,e);
    96          }
    97      }
    98  }