001: //========================================================================
002: //Copyright 2006 Mort Bay Consulting Pty. Ltd.
003: //------------------------------------------------------------------------
004: //Licensed under the Apache License, Version 2.0 (the "License");
005: //you may not use this file except in compliance with the License.
006: //You may obtain a copy of the License at
007: //http://www.apache.org/licenses/LICENSE-2.0
008: //Unless required by applicable law or agreed to in writing, software
009: //distributed under the License is distributed on an "AS IS" BASIS,
010: //WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
011: //See the License for the specific language governing permissions and
012: //limitations under the License.
013: //========================================================================
014:
015: package org.mortbay.jetty;
016:
017: import org.mortbay.jetty.bio.SocketConnector;
018: import org.mortbay.jetty.handler.ContextHandler;
019: import org.mortbay.jetty.handler.ContextHandlerCollection;
020: import org.mortbay.jetty.servlet.ServletHandler;
021: import org.mortbay.jetty.webapp.WebAppContext;
022: import org.mortbay.log.Log;
023:
024: public class Main {
025:
026: /* ------------------------------------------------------------ */
027: /* ------------------------------------------------------------ */
028: /** Construct server from command line arguments.
029: * @param args
030: */
031: public static void main(String[] args) {
032: if (args.length < 1 || args.length > 3) {
033: System.err
034: .println("Usage - java org.mortbay.jetty.Main [<addr>:]<port>");
035: System.err
036: .println("Usage - java org.mortbay.jetty.Main [<addr>:]<port> docroot");
037: System.err
038: .println("Usage - java org.mortbay.jetty.Main [<addr>:]<port> -webapp myapp.war");
039: System.err
040: .println("Usage - java org.mortbay.jetty.Main [<addr>:]<port> -webapps webapps");
041: System.err
042: .println("Usage - java -jar jetty-x.x.x-standalone.jar [<addr>:]<port>");
043: System.err
044: .println("Usage - java -jar jetty-x.x.x-standalone.jar [<addr>:]<port> docroot");
045: System.err
046: .println("Usage - java -jar jetty-x.x.x-standalone.jar [<addr>:]<port> -webapp myapp.war");
047: System.err
048: .println("Usage - java -jar jetty-x.x.x-standalone.jar [<addr>:]<port> -webapps webapps");
049: System.exit(1);
050: }
051:
052: try {
053:
054: // Create the server
055: Server server = new Server();
056: ContextHandlerCollection contexts = new ContextHandlerCollection();
057: server.setHandler(contexts);
058:
059: SocketConnector connector = new SocketConnector();
060: String address = args[0];
061: int colon = address.lastIndexOf(':');
062: if (colon < 0)
063: connector.setPort(Integer.parseInt(address));
064: else {
065: connector.setHost(address.substring(0, colon));
066: connector.setPort(Integer.parseInt(address
067: .substring(colon + 1)));
068: }
069: server.setConnectors(new Connector[] { connector });
070:
071: if (args.length < 3) {
072: ContextHandler context = new ContextHandler();
073: context.setContextPath("/");
074: context.setResourceBase(args.length == 1 ? "."
075: : args[1]);
076: ServletHandler servlet = new ServletHandler();
077: servlet
078: .addServletWithMapping(
079: "org.mortbay.jetty.servlet.DefaultServlet",
080: "/");
081: context.setHandler(servlet);
082: contexts.addHandler(context);
083: } else if ("-webapps".equals(args[1])) {
084: WebAppContext.addWebApplications(server, args[2],
085: WebAppContext.WEB_DEFAULTS_XML, true, true);
086: } else if ("-webapp".equals(args[1])) {
087: WebAppContext webapp = new WebAppContext();
088: webapp.setResourceBase(args[2]);
089: webapp.setContextPath("/");
090: contexts.addHandler(webapp);
091:
092: }
093:
094: server.start();
095:
096: } catch (Exception e) {
097: Log.warn(Log.EXCEPTION, e);
098: }
099: }
100: }
|