Package net.usersource.jettyembed.jetty7

Source Code of net.usersource.jettyembed.jetty7.Startup

//
// Copyright 2011, Glen Ford
//
// Apache 2.0 License
// Please see README.md, LICENSE and NOTICE
//


package net.usersource.jettyembed.jetty7;

import org.eclipse.jetty.server.Connector;
import org.eclipse.jetty.server.Server;
import org.eclipse.jetty.server.bio.SocketConnector;
import org.eclipse.jetty.server.nio.SelectChannelConnector;
import org.eclipse.jetty.server.ssl.SslConnector;
import org.eclipse.jetty.server.ssl.SslSelectChannelConnector;
import org.eclipse.jetty.server.ssl.SslSocketConnector;
import org.eclipse.jetty.webapp.WebAppClassLoader;
import org.eclipse.jetty.webapp.WebAppContext;
import org.eclipse.jetty.util.URIUtil;

import java.io.File;
import java.io.IOException;
import java.net.URL;
import java.security.ProtectionDomain;




public class Startup {

    private static final String JETTY_PORT_NAME = "jettyPort";
    private static final String JETTY_SSL_PORT_NAME = "jettySslPort";
    private static final String JETTY_SSL_KEY_PASSWORD_NAME = "jettySslKeyPassword";
    private static final String JETTY_SSL_KEY_STOREFILE_NAME = "jettySslKeyStoreFile";
    private static final String JETTY_INTERACTIVE_NAME = "jettyInteractive";
    private static final String JETTY_USE_NIO_NAME = "jettyNio";
    private static final String JETTY_DEBUG_NAME = "jettyDebug";
    private static final String JETTY_MAX_IDLE_NAME = "jettyMaxIdle";
   
    private static boolean debug = false;
    private static boolean isInteractive = false;
    private static boolean usingNIO = true;
    private static boolean usingSSL = false;
    private static String tempDir = null;
    private static int jettyPort = 8080;
    private static int jettySSLPort = 0;
    private static String keyFileName = null;
    private static String keyPassword = null;
    private static int maxIdleTime = 30000;



    private static void processOptions() {

        if( System.getProperty(JETTY_DEBUG_NAME) != null ) debug = Boolean.getBoolean(JETTY_DEBUG_NAME);

        if( System.getProperty(JETTY_USE_NIO_NAME) != null ) usingNIO = Boolean.getBoolean(JETTY_USE_NIO_NAME);

        if( System.getProperty(JETTY_INTERACTIVE_NAME) != null ) isInteractive = Boolean.getBoolean(JETTY_INTERACTIVE_NAME);

        tempDir = System.getProperty("jettyTempDir");

        maxIdleTime = Integer.getInteger(JETTY_MAX_IDLE_NAME,maxIdleTime);

        jettyPort = Integer.getInteger(JETTY_PORT_NAME, jettyPort);
        jettySSLPort = Integer.getInteger(JETTY_SSL_PORT_NAME,jettySSLPort);
        usingSSL = (jettySSLPort != 0);

        if( usingSSL ) {
            keyFileName = System.getProperty(JETTY_SSL_KEY_STOREFILE_NAME,keyFileName);
            keyPassword = System.getProperty(JETTY_SSL_KEY_PASSWORD_NAME,keyPassword);
        }

        if( debug ) {
            System.out.println("=================");
            System.out.println("Jetty Embed Debug");
            System.out.println("=================");
            System.out.println("Interactive : " + isInteractive );
            System.out.println("NIO : " + usingNIO );
            System.out.println("HTTP Port : "  + jettyPort );
            System.out.println("Max Idle Time : " + maxIdleTime );
            System.out.println("SSL Port : " + (usingSSL ? jettySSLPort : "(disabled)") );
            if(usingSSL) {
                System.out.println("SSL Key Password : " + keyPassword );
                System.out.println("SSL Key File Name : " + keyFileName );
            }
            System.out.println("=================");
            System.setProperty("org.eclipse.jetty.util.log.DEBUG", "true");
        }
    }



     private static Connector buildConnector() {
        Connector connector;
        if (usingNIO) {
            connector = new SelectChannelConnector();
        } else {
            connector = new SocketConnector();
        }
        connector.setPort(jettyPort);
        connector.setMaxIdleTime(maxIdleTime);
        return connector;
    }

    private static Connector buildSslConnector() {
        SslConnector sslConnector;
        if( usingNIO ) {
            sslConnector = new SslSelectChannelConnector();
        }
        else {
            sslConnector = new SslSocketConnector();
        }
        sslConnector.setPort(jettySSLPort);
        sslConnector.setMaxIdleTime(maxIdleTime);
        sslConnector.setKeyPassword(keyPassword);
        if (keyFileName != null && keyFileName.length() != 0 ) {
            sslConnector.setKeystore(keyFileName);
        }
        return sslConnector;
    }


    private static void setThreadClassLoader() {
        Thread.currentThread().setContextClassLoader(WebAppClassLoader.class.getClassLoader());
    }

    private static WebAppContext buildContext() throws IOException {
        ProtectionDomain protectionDomain = Startup.class.getProtectionDomain();
        URL location = protectionDomain.getCodeSource().getLocation();

        WebAppContext context = new WebAppContext();
        WebAppClassLoader webAppClassLoader = new WebAppClassLoader(Startup.class.getClassLoader(),context);
        context.setClassLoader(webAppClassLoader);
        context.setContextPath(URIUtil.SLASH);
        context.setWar(location.toExternalForm());

        if( tempDir != null ) {
            File tempDirectory = new File(tempDir);
            context.setTempDirectory(tempDirectory);
        }
        return context;
    }

    public static void main(String[] args) throws Exception {

        setThreadClassLoader();
        processOptions();
        WebAppContext context = buildContext();

        if( tempDir != null ) {
            File tempDirectory = new File(tempDir);
            context.setTempDirectory(tempDirectory);
        }

        Server server = new Server();
        if( usingSSL ) {
            server.setConnectors(new Connector[]{buildConnector(),buildSslConnector()});
        }
        else {
            server.setConnectors(new Connector[]{buildConnector()});
        }
        server.setHandler(context);
        server.setSendServerVersion(false);

        run(server);

    }


    private static void run(Server server) {
        try {
            server.start();
            if( isInteractive ) {
                System.out.println("Press any key to exit.");
                System.in.read();
                server.stop();
            }
            else {
                server.join();
            }
        }
        catch (Exception e) {
            e.printStackTrace();
            System.exit(1);
        }
    }
}
TOP

Related Classes of net.usersource.jettyembed.jetty7.Startup

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.