com.art4ul.jcoonsample.server.ServerLauncher.java Source code

Java tutorial

Introduction

Here is the source code for com.art4ul.jcoonsample.server.ServerLauncher.java

Source

/*
 * Copyright (C) 2014 Artsem Semianenka (http://art4ul.com/)
 *
 * 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.art4ul.jcoonsample.server;

import org.eclipse.jetty.server.Server;
import org.eclipse.jetty.servlet.ServletContextHandler;
import org.eclipse.jetty.servlet.ServletHolder;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.context.ContextLoaderListener;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.AnnotationConfigWebApplicationContext;
import org.springframework.web.servlet.DispatcherServlet;

import java.io.IOException;

/**
 * Created by Artsem_Semianenka on 11/14/2014.
 */
public class ServerLauncher {

    private static final Logger LOG = LoggerFactory.getLogger(ServerLauncher.class);

    private static final int DEFAULT_PORT = 8080;
    private static final String CONTEXT_PATH = "/";
    private static final String CONFIG_LOCATION = "com.art4ul.jcoonsample.server.config";
    private static final String MAPPING_URL = "/*";
    private Server server;

    public void startServer() throws Exception {
        LOG.info("Starting HTTP Server...");
        startServer(getContext());
    }

    public void startServer(WebApplicationContext applicationContext) throws Exception {
        server = new Server(DEFAULT_PORT);
        server.setHandler(getServletContextHandler(applicationContext));
        server.start();
        LOG.info("HTTP Server has started.");
        LOG.info("............................................");
    }

    public void stopServer() throws Exception {
        if (server != null) {
            server.stop();
            LOG.info("............................................");
            LOG.info("HTTP Server has stopped.");
        }
    }

    private static ServletContextHandler getServletContextHandler(WebApplicationContext context)
            throws IOException {
        ServletContextHandler contextHandler = new ServletContextHandler();
        contextHandler.setErrorHandler(null);
        contextHandler.setContextPath(CONTEXT_PATH);
        contextHandler.addServlet(new ServletHolder(new DispatcherServlet(context)), MAPPING_URL);
        contextHandler.addEventListener(new ContextLoaderListener(context));
        return contextHandler;
    }

    public static WebApplicationContext getContext() {
        AnnotationConfigWebApplicationContext context = new AnnotationConfigWebApplicationContext();
        context.setConfigLocation(CONFIG_LOCATION);
        return context;
    }
}