[25] | 1 | package geniusweb.clienttest;
|
---|
| 2 |
|
---|
| 3 | import java.io.File;
|
---|
| 4 |
|
---|
| 5 | import org.apache.catalina.LifecycleException;
|
---|
| 6 | import org.apache.catalina.startup.Tomcat;
|
---|
| 7 |
|
---|
| 8 | class EmbeddedTomcat {
|
---|
| 9 | private Tomcat tomcat = new Tomcat();
|
---|
| 10 |
|
---|
| 11 | public void start() {
|
---|
| 12 | try {
|
---|
| 13 | // If I don't want to copy files around then the base directory must be '.'
|
---|
| 14 | String baseDir = ".";
|
---|
| 15 | tomcat.setPort(8080);
|
---|
| 16 | tomcat.setBaseDir(baseDir);
|
---|
| 17 | tomcat.getHost().setAppBase(baseDir);
|
---|
| 18 | tomcat.getHost().setDeployOnStartup(true);
|
---|
| 19 | tomcat.getHost().setAutoDeploy(true);
|
---|
| 20 | tomcat.start();
|
---|
| 21 | } catch (LifecycleException e) {
|
---|
| 22 | throw new RuntimeException(e);
|
---|
| 23 | }
|
---|
| 24 | }
|
---|
| 25 |
|
---|
| 26 | public void stop() {
|
---|
| 27 | try {
|
---|
| 28 | tomcat.stop();
|
---|
| 29 | tomcat.destroy();
|
---|
| 30 | // Tomcat creates a work folder where the temporary files are stored
|
---|
| 31 | // FileUtils.deleteDirectory(new File("work"));
|
---|
| 32 | } catch (LifecycleException e) {
|
---|
| 33 | throw new RuntimeException(e);
|
---|
| 34 | // } catch (IOException e) {
|
---|
| 35 | // throw new RuntimeException(e);
|
---|
| 36 | }
|
---|
| 37 | }
|
---|
| 38 |
|
---|
| 39 | public void deploy(String appName) {
|
---|
| 40 | // tomcat.addWebapp(tomcat.getHost(), "/" + appName, new
|
---|
| 41 | // File("src/main/webapp").getAbsolutePath());
|
---|
| 42 | tomcat.addWebapp(tomcat.getHost(), "/" + appName, new File("src/main/webapp").getAbsolutePath());
|
---|
| 43 | }
|
---|
| 44 |
|
---|
| 45 | public String getApplicationUrl(String appName) {
|
---|
| 46 | return String.format("http://%s:%d/%s", tomcat.getHost().getName(), tomcat.getConnector().getLocalPort(),
|
---|
| 47 | appName);
|
---|
| 48 | }
|
---|
| 49 | }
|
---|