All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.mortbay.jetty.Main Maven / Gradle / Ivy

There is a newer version: 7.0.0.pre5
Show newest version
//========================================================================
//Copyright 2006 Mort Bay Consulting Pty. Ltd.
//------------------------------------------------------------------------
//Licensed under the Apache License, Version 2.0 (the "License");
//you may not use this file except in compliance with the License.
//You may obtain a copy of the License at 
//http://www.apache.org/licenses/LICENSE-2.0
//Unless required by applicable law or agreed to in writing, software
//distributed under the License is distributed on an "AS IS" BASIS,
//WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
//See the License for the specific language governing permissions and
//limitations under the License.
//========================================================================

package org.mortbay.jetty;

import org.mortbay.jetty.bio.SocketConnector;
import org.mortbay.jetty.handler.ContextHandler;
import org.mortbay.jetty.handler.ContextHandlerCollection;
import org.mortbay.jetty.servlet.ServletHandler;
import org.mortbay.jetty.webapp.WebAppContext;
import org.mortbay.log.Log;
import org.mortbay.util.URIUtil;

public class Main
{

    /* ------------------------------------------------------------ */
    /* ------------------------------------------------------------ */
    /** Construct server from command line arguments.
     * @param args 
     */
    public static void main(String[] args)
    {
        if (args.length<1 || args.length>3)
        {
            System.err.println("Usage - java org.mortbay.jetty.Main [:]");
            System.err.println("Usage - java org.mortbay.jetty.Main [:] docroot");
            System.err.println("Usage - java org.mortbay.jetty.Main [:] -webapp myapp.war");
            System.err.println("Usage - java org.mortbay.jetty.Main [:] -webapps webapps");
            System.err.println("Usage - java -jar jetty-x.x.x-standalone.jar [:]");
            System.err.println("Usage - java -jar jetty-x.x.x-standalone.jar [:] docroot");
            System.err.println("Usage - java -jar jetty-x.x.x-standalone.jar [:] -webapp myapp.war");
            System.err.println("Usage - java -jar jetty-x.x.x-standalone.jar [:] -webapps webapps");
            System.exit(1);
        }
        
        try{
            
            // Create the server
            Server server = new Server();
            ContextHandlerCollection contexts = new ContextHandlerCollection();
            server.setHandler(contexts);
            
            SocketConnector connector = new SocketConnector();
            String address = args[0];
            int colon = address.lastIndexOf(':');
            if (colon<0)
                connector.setPort(Integer.parseInt(address));
            else
            {
                connector.setHost(address.substring(0,colon));
                connector.setPort(Integer.parseInt(address.substring(colon+1)));
            }
            server.setConnectors(new Connector[]{connector});
            
            if (args.length<3)
            {
                ContextHandler context = new ContextHandler();
                context.setContextPath(URIUtil.SLASH);
                context.setResourceBase(args.length==1?".":args[1]);
                ServletHandler servlet = new ServletHandler();
                servlet.addServletWithMapping("org.mortbay.jetty.servlet.DefaultServlet", URIUtil.SLASH);
                context.setHandler(servlet);
                contexts.addHandler(context);
            }
            else if ("-webapps".equals(args[1]))
            {
                WebAppContext.addWebApplications(server, args[2], WebAppContext.WEB_DEFAULTS_XML, true, true);
            }
            else if ("-webapp".equals(args[1]))
            {
                WebAppContext webapp = new WebAppContext();
                webapp.setResourceBase(args[2]);
                webapp.setContextPath(URIUtil.SLASH);
                contexts.addHandler(webapp);
                
            }
                
            server.start();
            
        }
        catch (Exception e)
        {
            Log.warn(Log.EXCEPTION,e);
        }
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy