Commit 0e6905af authored by qiaokun's avatar qiaokun

first push

parents
## .gitignore for Grails 1.2 and 1.3
# .gitignore for maven
target/
*.releaseBackup
# web application files
#/web-app/WEB-INF
# IDE support files
/.classpath
/.launch
/.project
/.settings
/*.launch
/*.tmproj
/ivy*
/eclipse
# default HSQL database files for production mode
/prodDb.*
# general HSQL database files
*Db.properties
*Db.script
# logs
/stacktrace.log
/test/reports
/logs
*.log
*.log.*
# project release file
/*.war
# plugin release file
/*.zip
/*.zip.sha1
# older plugin install locations
/plugins
/web-app/plugins
/web-app/WEB-INF/classes
# "temporary" build files
target/
out/
build/
# other
*.iws
#.gitignore for java
*.class
# Package Files #
*.jar
*.war
*.ear
## .gitignore for eclipse
*.pydevproject
.project
.metadata
bin/**
tmp/**
tmp/**/*
*.tmp
*.bak
*.swp
*~.nib
local.properties
.classpath
.settings/
.loadpath
# External tool builders
.externalToolBuilders/
# Locally stored "Eclipse launch configurations"
*.launch
# CDT-specific
.cproject
# PDT-specific
.buildpath
## .gitignore for intellij
*.iml
*.ipr
*.iws
.idea/
## .gitignore for linux
.*
!.gitignore
!.gitattributes
!.editorconfig
!.eslintrc
!.travis.yml
*~
## .gitignore for windows
# Windows image file caches
Thumbs.db
ehthumbs.db
# Folder config file
Desktop.ini
# Recycle Bin used on file shares
$RECYCLE.BIN/
## .gitignore for mac os x
.DS_Store
.AppleDouble
.LSOverride
Icon
# Thumbnails
._*
# Files that might appear on external disk
.Spotlight-V100
.Trashes
## hack for graddle wrapper
!wrapper/*.jar
!**/wrapper/*.jar
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.cesgroup.plugin</groupId>
<artifactId>container-plugin</artifactId>
<version>1.0.0</version>
<packaging>jar</packaging>
<properties>
<tomcat-plugin.version>7.0.47</tomcat-plugin.version>
</properties>
<dependencies>
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>2.4</version>
</dependency>
<dependency>
<groupId>org.apache.tomcat.maven</groupId>
<artifactId>tomcat7-maven-plugin</artifactId>
<version>2.2</version>
</dependency>
<!--<dependency>-->
<!--<groupId>org.apache.tomcat.embed</groupId>-->
<!--<artifactId>tomcat-embed-logging-log4j</artifactId>-->
<!--<version>${tomcat-plugin.version}</version>-->
<!--</dependency>-->
<!--<dependency>-->
<!--<groupId>org.apache.tomcat.embed</groupId>-->
<!--<artifactId>tomcat-embed-logging-juli</artifactId>-->
<!--<version>${tomcat-plugin.version}</version>-->
<!--</dependency>-->
<!--<dependency>-->
<!--<groupId>org.apache.tomcat</groupId>-->
<!--<artifactId>tomcat-jasper</artifactId>-->
<!--<version>${tomcat-plugin.version}</version>-->
<!--</dependency>-->
<!--<dependency>-->
<!--<groupId>org.apache.tomcat</groupId>-->
<!--<artifactId>tomcat-el-core</artifactId>-->
<!--<version>${tomcat-plugin.version}</version>-->
<!--</dependency>-->
<!--<dependency>-->
<!--<groupId>org.apache.tomcat</groupId>-->
<!--<artifactId>tomcat-embed-dbcp</artifactId>-->
<!--<version>${tomcat-plugin.version}</version>-->
<!--</dependency>-->
<!--<dependency>-->
<!--<groupId>org.apache.tomcat.embed</groupId>-->
<!--<artifactId>tomcat-embed-core</artifactId>-->
<!--<version>${tomcat-plugin.version}</version>-->
<!--</dependency>-->
<!--<dependency>-->
<!--<groupId>org.apache.tomcat.embed</groupId>-->
<!--<artifactId>tomcat-annotation-api</artifactId>-->
<!--<version>${tomcat-plugin.version}</version>-->
<!--</dependency>-->
<!--<dependency>-->
<!--<groupId>org.eclipse.jdt.core.compile</groupId>-->
<!--<artifactId>ecj</artifactId>-->
<!--<version>4.2.2</version>-->
<!--</dependency>-->
<!--<dependency>-->
<!--<groupId>org.slf4j</groupId>-->
<!--<artifactId>jcl-over-slf4j</artifactId>-->
<!--<version>1.7.5</version>-->
<!--</dependency>-->
<!--<dependency>-->
<!--<groupId>org.apache.commons</groupId>-->
<!--<artifactId>commons-compress</artifactId>-->
<!--<version>1.4.1</version>-->
<!--</dependency>-->
</dependencies>
</project>
\ No newline at end of file
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;
import java.util.jar.JarFile;
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(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) {
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()));
} else{
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");
webXml.deleteOnExit();
FileUtils.copyURLToFile(getClass().getResource("/web.xml"),webXml);
Tomcat tomcat = new Tomcat() {
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());
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;
}
}
package com.cesgroup.tomcat.plugin.bootstrap;
/**
* web 容器
* @version 1.0.0 2018/07/24
*/
public interface Container {
/**
* 取得webapp的路径
* @return
*/
String getWebapp();
/**
* 启动一个容器
* @throws Exception
*/
void run() throws Exception;
/**
* 增加目录依赖的WAR模块
* @param path
*/
void addFolderMoudules(String path);
/**
* 增加一个war 模块的依赖
* @param group
* @param artifactId
* @param version
*/
void addWarMoudules(String group ,String artifactId,String version);
/**
* 设定容器的上下文
* @param context
*/
void setContext(String context);
/**
* 设计容器的端口
* @param port
*/
void setPort(int port);
}
package com.cesgroup.tomcat.plugin.bootstrap.listener;
import java.io.File;
import com.cesgroup.tomcat.plugin.util.Operate;
import org.apache.commons.io.FileUtils;
import org.apache.commons.io.monitor.FileAlterationListenerAdaptor;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
* 文件监听类
* @version 1.0.0 2018/07/24
*/
public class FileListener extends FileAlterationListenerAdaptor{
protected Logger logger = LoggerFactory.getLogger(getClass());
// 原目录路径
private String orig_root_path;
// 目标目录路径
private String dest_root_path;
public FileListener(String orig_root_path,String dest_root_path) {
this.orig_root_path = orig_root_path;
this.dest_root_path = dest_root_path;
}
/**
* 目录发生变化时执行
* @param directory
*/
@Override
public void onDirectoryChange(File directory) {
logger.debug("onDirectoryChange"+directory);
}
/**
* 创建目录时执行
* @param directory
*/
@Override
public void onDirectoryCreate(File directory) {
logger.debug("onDirectoryCreate"+directory);
}
/**
* 删除目录时执行
* @param directory
*/
@Override
public void onDirectoryDelete(File directory) {
logger.debug("onFileCreate"+directory);
}
/**
* 目录下文件变化时执行
* @param file
*/
@Override
public void onFileChange(File file) {
logger.debug("onFileChange"+file);
handleFile( file,Operate.CHANGE);
}
/**
* 目录下创建文件时执行
* @param file
*/
@Override
public void onFileCreate(File file) {
logger.debug("onFileCreate"+file);
handleFile( file,Operate.ADD);
}
/**
* 目录下删除文件时执行
* @param file
*/
@Override
public void onFileDelete(File file) {
logger.debug("onFileDelete"+file);
handleFile( file,Operate.DEL);
}
/**
* 文件变化时处理逻辑
* @param file
* @param operateType
*/
public void handleFile(File file,Operate operateType) {
try {
String suffix = file.getAbsolutePath().replace(orig_root_path, "");
File newFile = new File(dest_root_path,suffix);
newFile.getParentFile().mkdirs();
switch(operateType) {
case ADD:
FileUtils.copyFile(file, newFile);
case CHANGE:
FileUtils.copyFile(file, newFile);
break;
case DEL:
newFile.deleteOnExit();break;
}
}catch(Exception e) {
logger.error(e.getMessage(), e);
}
}
}
package com.cesgroup.tomcat.plugin.bootstrap.module;
import com.cesgroup.tomcat.plugin.bootstrap.Container;
/**
* 目录结构的文件模块(非压缩)
* @version 1.0.0 2018/07/23
*/
public class FolderModule extends Module{
/**
* 创建文件模块
* @param path 项目路径
* @param container web 容器
*/
public FolderModule(String path, Container container) {
super(path, container);
perfomCopyDirectory();
}
/**
* 复制项目到启动容器下
*/
@Override
protected void perfomCopyDirectory() {
copyDirectory(path,container.getWebapp());
}
}
package com.cesgroup.tomcat.plugin.bootstrap.module;
import com.cesgroup.tomcat.plugin.bootstrap.Container;
import org.apache.commons.compress.archivers.ArchiveInputStream;
import org.apache.commons.compress.archivers.ArchiveStreamFactory;
import org.apache.commons.compress.archivers.jar.JarArchiveEntry;
import org.apache.commons.io.FileUtils;
import org.apache.commons.io.IOUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.io.*;
import java.util.ArrayList;
import java.util.List;
/**
* 项目启动时关联的模块
* @version 1.0.0 2018/07/23
*/
public abstract class Module {
protected Logger logger = LoggerFactory.getLogger(getClass());
// 项目路径
protected String path;
// web 容器
protected Container container;
// 不包含的文件
protected List<String> excludes = new ArrayList<String>();
// 包含的文件
protected List<String> includes = new ArrayList<String>();
public Module(String path, Container container) {
this.path = path;
this.container = container;
}
protected abstract void perfomCopyDirectory() ;
/**
* 复制目录下文件
* @param srcDir 源目录
* @param destDir 目标目录
*/
public void copyDirectory(String srcDir,String destDir) {
try {
FileUtils.copyDirectory(new File(srcDir),new File(destDir),new FileFilter() {
public boolean accept(File pathname) {
if(!includes.isEmpty()) {
for(String include:includes) {
if(pathname.getAbsolutePath().contains(include)) {
return true;
}
}
return false;
} else {
for(String exclude:excludes) {
if(pathname.getAbsolutePath().contains(exclude)) {
return false;
}
}
return true;
}
}
} );
} catch (Exception e) {
logger.error(e.getMessage(),e);
}
}
/**
* 解压文件
* @param warPath war包的路径
* @param unzipPath 解压位置
*/
public void unzip(String warPath,String unzipPath) {
File warFile = new File(warPath);
BufferedInputStream bufferedInputStream = null;
try {
bufferedInputStream = new BufferedInputStream(new FileInputStream(warFile));
ArchiveInputStream in = new ArchiveStreamFactory().createArchiveInputStream(ArchiveStreamFactory.JAR,bufferedInputStream);
JarArchiveEntry entry = null;
while ((entry = (JarArchiveEntry) in.getNextEntry()) != null) {
if (entry.isDirectory()) {
new File(unzipPath, entry.getName()).mkdir();
} else {
OutputStream out = FileUtils.openOutputStream(new File(unzipPath, entry.getName()));
IOUtils.copy(in, out);
out.close();
}
}
in.close();
} catch(Exception e) {
logger.error(e.getMessage(),e);
}
}
public List<String> getExcludes() {
return excludes;
}
public void setExcludes(List<String> excludes) {
this.excludes = excludes;
}
public List<String> getIncludes() {
return includes;
}
public void setIncludes(List<String> includes) {
this.includes = includes;
}
}
package com.cesgroup.tomcat.plugin.bootstrap.module;
import com.cesgroup.tomcat.plugin.bootstrap.Container;
import org.apache.commons.io.FileUtils;
import org.w3c.dom.Document;
import org.w3c.dom.NodeList;
import javax.xml.parsers.DocumentBuilderFactory;
import java.io.File;
import java.io.IOException;
/**
* war包相关的模块
* @version 1.0.0 2018/07/23
*/
public class WarModule extends Module {
// 组Id
private String group;
// 构件Id
private String artifactId;
// 版本
private String version;
/**
* 按war路径的方式导入模块
* @param path war 相关的路径
* @param container web 容器
*/
public WarModule(String path,Container container) {
super(path,container);
perfomCopyDirectory();
}
/**
* 按maven 包的方式 导入模板
* @param group
* @param artifactId
* @param version
* @param container
*/
public WarModule(String group,String artifactId,String version,Container container) {
this(null,container);
this.group = group;
this.artifactId = artifactId;
this.version = version;
try {
this.path = getWarPath();
perfomCopyDirectory();
} catch (Exception e) {
logger.error(e.getMessage(), e);
}
}
/**
* 复制项目到启动容器中
*/
@Override
protected void perfomCopyDirectory() {
File unzipPath = new File(new File(this.path).getParentFile(),"temp");
// 解压相关的war文件
unzip(path, unzipPath.getAbsolutePath());
// 复制相关项目到启动容器中
copyDirectory(container.getWebapp(), unzipPath.getAbsolutePath());
try {
FileUtils.deleteDirectory(unzipPath);
} catch (IOException e) {
logger.error(e.getMessage(), e);
}
}
/**
* 取得war包所在的路径
* @return 返回相关路径
*/
private String getWarPath() {
// 取得maven home 的路径
String maven_home = System.getenv("MAVEN_HOME");
// 取得默认的路径
String repositoryPath = new File(System.getProperty("user.home"),"/.m2/repository").getAbsolutePath();
// 取得setting 的路径
File setting = new File(maven_home,"conf/settings.xml");
Document doc = null;
try {
doc = DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(setting);
} catch (Exception e) {
logger.warn(e.getMessage(),e);
}
NodeList nodes = doc.getElementsByTagName("localRepository");
if(nodes.getLength() == 1) {
repositoryPath = nodes.item(0).getTextContent();
}
String groupPath = new File(repositoryPath,group.replace(".", "/")).getAbsolutePath();
String artifactIdPath = new File(groupPath,artifactId).getAbsolutePath();
String versionPath = new File(artifactIdPath,version).getAbsolutePath();
// 返回war 的路径
return new File(versionPath,artifactId+"-"+version + ".war").getAbsolutePath();
}
}
package com.cesgroup.tomcat.plugin.util;
public enum Operate {
ADD,CHANGE,DEL
}
This source diff could not be displayed because it is too large. You can view the blob instead.
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment