Skip to content
Bootstrap.java 6.29 KiB
Newer Older
qiaokun's avatar
qiaokun committed
package com.cesgroup.tomcat.plugin.bootstrap;


import com.cesgroup.tomcat.plugin.bootstrap.listener.FileListener;
import com.cesgroup.tomcat.plugin.bootstrap.module.FolderModule;
import com.cesgroup.tomcat.plugin.bootstrap.module.Module;
import com.cesgroup.tomcat.plugin.bootstrap.module.WarModule;
import org.apache.catalina.Context;
import org.apache.catalina.Host;
import org.apache.catalina.core.StandardContext;
import org.apache.catalina.core.StandardHost;
import org.apache.catalina.startup.ContextConfig;
import org.apache.catalina.startup.Tomcat;
import org.apache.commons.io.FileUtils;
import org.apache.commons.io.monitor.FileAlterationMonitor;
import org.apache.commons.io.monitor.FileAlterationObserver;
import org.apache.naming.resources.VirtualDirContext;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;


public class Bootstrap implements Container {
    private static final String webapp = "src" + File.separator + "main" + File.separator + "webapp";
    protected Logger logger = LoggerFactory.getLogger(getClass());

    private List<Module> modules = new ArrayList<Module>();

    private String projectPath;

    private File originWebappFile = null;

    private File destDirectoryFile = null;

    private int port = 8080;

    private String context;

    public Bootstrap() {
        this(System.getProperty("user.dir"), webapp, webapp, "destWork");
    }
qiaokun's avatar
qiaokun committed

    public Bootstrap(String originRoot) {
        this(originRoot, webapp, webapp, "destWork");
    }

    public Bootstrap(String originRoot, String destRoot) {
        this(originRoot, webapp, destRoot, "destWork");
    }


    public Bootstrap(String originRoot, String webapp, String destRoot, String destDirectory) {
        //设置默认context为当前项目名称
        String projectPath = System.getProperty("user.dir");
        File temp = new File(projectPath);
        this.context = temp.getName();

qiaokun's avatar
qiaokun committed
        this.projectPath = originRoot;
        this.originWebappFile = new File(projectPath, webapp);

        if (webapp != destRoot && !webapp.equals(destRoot)) {
            this.destDirectoryFile = new File(destRoot, destDirectory);
            this.destDirectoryFile.mkdirs();
            clearAndCompile(originWebappFile, destDirectoryFile);
            this.monitor(originWebappFile.getAbsolutePath(),
                    new FileListener(originWebappFile.getAbsolutePath(), this.destDirectoryFile.getAbsolutePath()));
qiaokun's avatar
qiaokun committed
            this.destDirectoryFile = this.originWebappFile;
        }
    }


    public void clearAndCompile(File oriWebappFile, File destDirectory) {
        try {
            FileUtils.cleanDirectory(destDirectory);
            FileUtils.copyDirectory(oriWebappFile, destDirectory);
        } catch (IOException e) {
            logger.debug(e.getMessage(), e);
        }
    }

    public void addWarMoudules(WarModule warModule) {

        modules.add(warModule);
    }

    public void addWarMoudules(String path) {

        modules.add(new WarModule(path, this));
    }

    public void addWarMoudules(String group, String artifactId, String version) {

        modules.add(new WarModule(group, artifactId, version, this));
    }

    public void addFolderMoudules(String path) {

        modules.add(new FolderModule(path, this));
        this.monitor(path, new FileListener(path, this.destDirectoryFile.getAbsolutePath()));

    }


    public void run() throws Exception {


        final String work = new File(projectPath, "work").getAbsolutePath();

        final String classes = new File(projectPath, "target/classes").getAbsolutePath();

        final String testclasses = new File(projectPath, "target/test-classes").getAbsolutePath();

        final File webXml = new File(testclasses, "web.xml");
qiaokun's avatar
qiaokun committed

        if (webXml.exists()) {
           webXml.delete();
        }
qiaokun's avatar
qiaokun committed

        FileUtils.copyURLToFile(getClass().getResource("/web.xml"), webXml);
qiaokun's avatar
qiaokun committed

        Tomcat tomcat = new Tomcat() {

qiaokun's avatar
qiaokun committed
            public Context addWebapp(Host host, String url, String name, String path) {


                Context ctx = new StandardContext();

                ctx.setName(name);
                ctx.setPath(url);
                ctx.setDocBase(path);

                ContextConfig ctxCfg = new ContextConfig();
                ctx.addLifecycleListener(ctxCfg);

                ctxCfg.setDefaultWebXml(new File(testclasses, "web.xml").getPath());
qiaokun's avatar
qiaokun committed
                if (host == null) {
                    getHost().addChild(ctx);
                } else {
                    host.addChild(ctx);
                }

                return ctx;
            }
        };

        StandardHost host = (StandardHost) tomcat.getHost();
        host.setAppBase("webapps");

        StandardContext context = (StandardContext) tomcat.addWebapp(null == this.context ? "/" : "/" + this.context, destDirectoryFile.getAbsolutePath());

        VirtualDirContext dirContext = new VirtualDirContext();

        dirContext.setExtraResourcePaths("/WEB-INF/classes=" + classes);

        context.setResources(dirContext);

        context.setWorkDir(work);

        tomcat.setPort(port);

        tomcat.start();

        tomcat.getServer().await();


    }

    public List<Module> getModules() {
        return modules;
    }

    public void setModules(List<Module> modules) {
        this.modules = modules;
    }

    public String getProjectPath() {
        return projectPath;
    }

    public void setProjectPath(String projectPath) {
        this.projectPath = projectPath;
    }


    public String getWebapp() {
        return destDirectoryFile.getAbsolutePath();
    }


    public void monitor(String directory, FileListener fileListener) {

        FileAlterationObserver observer = new FileAlterationObserver(directory);
        observer.addListener(fileListener);
        FileAlterationMonitor monitor = new FileAlterationMonitor(1L, observer);
        try {
            monitor.start();
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
        }

    }

    public int getPort() {
        return port;
    }

    public void setPort(int port) {
        this.port = port;
    }

    public String getContext() {
        return context;
    }

    public void setContext(String context) {
        this.context = context;
    }


}