com.github.dactiv.fear.commons.test.JettyRunner.java Source code

Java tutorial

Introduction

Here is the source code for com.github.dactiv.fear.commons.test.JettyRunner.java

Source

/*
 * Copyright 2015 dactiv
 *
 * 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 com.github.dactiv.fear.commons.test;

import org.eclipse.jetty.server.Connector;
import org.eclipse.jetty.server.Server;
import org.eclipse.jetty.server.nio.SelectChannelConnector;
import org.eclipse.jetty.webapp.WebAppClassLoader;
import org.eclipse.jetty.webapp.WebAppContext;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.core.env.AbstractEnvironment;

/**
 * jetty ?
 *
 * @author maurice
 */
public class JettyRunner {

    private static final Logger LOGGER = LoggerFactory.getLogger(JettyRunner.class);
    /**
     * ? jetty ?
     */
    private int port = 8083;
    /**
     * ? jetty ? web 
     */
    private String webPath = "src/main/webapp";
    /**
     * ? jetty ??
     */
    private String contextPath;
    /**
     * ?
     */
    private String environment;

    /**
     * jetty ?
     *
     * @param port        ??
     * @param webPath     web?
     * @param contextPath ???
     * @param environment ?
     */
    public JettyRunner(int port, String webPath, String contextPath, String environment) {
        this.port = port;
        this.webPath = webPath;
        this.contextPath = contextPath;
        this.environment = environment;
    }

    /**
     * jetty ?
     *
     * @param port        ??
     * @param contextPath ???
     * @param environment ?
     */
    public JettyRunner(int port, String contextPath, String environment) {
        this.port = port;
        this.contextPath = contextPath;
        this.environment = environment;
    }

    /**
     * jetty ?
     */
    public JettyRunner() {

    }

    /**
     * ??
     */
    public Server startServer() {
        System.setProperty(AbstractEnvironment.ACTIVE_PROFILES_PROPERTY_NAME, this.environment);
        Server server = new Server();
        // JVMJetty
        server.setStopAtShutdown(true);

        SelectChannelConnector connector = new SelectChannelConnector();
        connector.setPort(port);
        //  apache shiro  rememberMe ?cookie jetty  header buffer size ????
        connector.setResponseHeaderSize(8192);
        // ???Jetty??
        connector.setReuseAddress(false);
        server.setConnectors(new Connector[] { connector });

        WebAppContext webContext = new WebAppContext(webPath, contextPath);
        server.setHandler(webContext);

        try {

            server.start();
            LOGGER.info("Browse URL : http://localhost:" + port + contextPath);
            return server;
        } catch (Exception e) {
            LOGGER.error("jetty ?", e);
        }

        return null;
    }

    /**
     * ???
     *
     * @return ??
     */
    public int getPort() {
        return port;
    }

    /**
     * ??
     *
     * @param port ??
     */
    public void setPort(int port) {
        this.port = port;
    }

    /**
     * ?? jetty ? web 
     *
     * @return web 
     */
    public String getWebPath() {
        return webPath;
    }

    /**
     * ? jetty ? web 
     *
     * @param webPath web 
     */
    public void setWebPath(String webPath) {
        this.webPath = webPath;
    }

    /**
     * ????
     *
     * @return ???
     */
    public String getContextPath() {
        return contextPath;
    }

    /**
     * ???
     *
     * @param contextPath ???
     */
    public void setContextPath(String contextPath) {
        this.contextPath = contextPath;
    }

    /**
     * ? spring ?
     *
     * @return ?
     */
    public String getEnvironment() {
        return environment;
    }

    /**
     *  spring ?
     *
     * @param environment ?
     */
    public void setEnvironment(String environment) {
        this.environment = environment;
    }
}