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

com.github.quartzwebui.server.StartWebServer Maven / Gradle / Ivy

The newest version!
/**
 * Licensed under the Apache License, Version 2.0 (the "License");
 */
package com.github.quartzwebui.server;


import com.github.quartzwebui.utils.PropertiesLoaderUtils;
import org.eclipse.jetty.server.Server;
import org.eclipse.jetty.servlet.ServletHolder;
import org.eclipse.jetty.webapp.WebAppContext;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.servlet.Servlet;
import java.io.File;
import java.net.URL;
import java.util.Map;
import java.util.Properties;

/**
 * @author quxiucheng [[email protected]]
 */
public class StartWebServer {

    private static Logger logger = LoggerFactory.getLogger(StartWebServer.class);

    public void startServer(){
        Thread thread = new Thread(new Runnable() {

            public void run() {
            try {
                Properties properties = PropertiesLoaderUtils.loadProperties("quartzuiServer.properties");
                String port = properties.getProperty("port");
                String resourcePath = properties.getProperty("resourcePath");
                String contextPath = properties.getProperty("contextPath");
                String startWebServerServletClass = properties.getProperty("startWebServer.servlet.class");
                logger.info("jetty server start.....,port:" + port +
                        ",contextPath:" + contextPath +
                        ",resourcePath:" + resourcePath);
                Class servletClass = (Class) Class.forName(startWebServerServletClass);
                // 新建web服务器
                Server server = new Server(Integer.parseInt(port));
                // 配置web应用上下文
                WebAppContext webAppContext = new WebAppContext();
                webAppContext.setContextPath(contextPath);
                // 设置配置文件扫描路径
                ClassLoader classLoader = StartWebServer.class.getClassLoader();
                URL resource = classLoader.getResource("");

                assert resource != null;
                String path = resource.getPath();
                File resDir = new File(path);

                webAppContext.setResourceBase(resDir.getCanonicalPath() + resourcePath);

                webAppContext.setConfigurationDiscovered(true);
                webAppContext.setParentLoaderPriority(true);
                ServletHolder servletHolder = new ServletHolder(servletClass);
                for (Map.Entry propEntry : properties.entrySet()) {
                    servletHolder.setInitParameter(propEntry.getKey().toString(), properties.getProperty(propEntry.getKey().toString()));
                }
                webAppContext.addServlet(servletHolder, "/*");


                server.setHandler(webAppContext);
                // 启动web服务器
                server.start();
            } catch (Exception e) {
                e.printStackTrace();
            }
            }
        });
        thread.start();
    }

    public static void main(String[] args) {
        new StartWebServer().startServer();
    }

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy