Newer
Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
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");
}
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();
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()));
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
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");
if (webXml.exists()) {
webXml.delete();
}
FileUtils.copyURLToFile(getClass().getResource("/web.xml"), webXml);
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());
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
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;
}
}