package geniusweb.clienttest; import java.io.File; import org.apache.catalina.LifecycleException; import org.apache.catalina.startup.Tomcat; class EmbeddedTomcat { private Tomcat tomcat = new Tomcat(); public void start() { try { // If I don't want to copy files around then the base directory must // be '.' String baseDir = "."; tomcat.setPort(8080); tomcat.setBaseDir(baseDir); tomcat.getHost().setAppBase(baseDir); tomcat.getHost().setDeployOnStartup(true); tomcat.getHost().setAutoDeploy(true); tomcat.start(); } catch (LifecycleException e) { throw new RuntimeException(e); } } public void stop() { try { tomcat.stop(); tomcat.destroy(); // Tomcat creates a work folder where the temporary files are stored } catch (LifecycleException e) { throw new RuntimeException(e); } } /** * Deploys the webapp contained in this project * * @param appName the name to use on the server */ public void deploy(String appName) { deployWar(appName, new File("src/main/webapp")); } /** * Deploys the webapp in the given war * * @param appName the name to use on the server * @param warfile the warfile to deploy. Should include ".war" extension. */ public void deployWar(String appName, File warfile) { tomcat.addWebapp(tomcat.getHost(), "/" + appName, warfile.getAbsolutePath()); } public String getApplicationUrl(String appName) { return String.format("http://%s:%d/%s", tomcat.getHost().getName(), tomcat.getConnector().getLocalPort(), appName); } }