Skip to content

Instantly share code, notes, and snippets.

@shyiko
Created June 7, 2011 11:50
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save shyiko/1012081 to your computer and use it in GitHub Desktop.
Save shyiko/1012081 to your computer and use it in GitHub Desktop.
Tomcat Embedded Server
import org.apache.catalina.Context;
import org.apache.catalina.Engine;
import org.apache.catalina.Host;
import org.apache.catalina.LifecycleException;
import org.apache.catalina.connector.Connector;
import org.apache.catalina.startup.Embedded;
import java.io.File;
/**
* Adapter to the {@link org.apache.catalina.startup.Embedded}
*
* @author <a href="mailto:stanley.shyiko@gmail.com">shyiko</a>
* @since 07.05.2011
*/
public class TomcatEmbeddedServer {
private String homeDirectory;
private Embedded embeddedServer;
private Host host;
/**
* Construct new instance using custom Tomcat home directory
* @param homeDirectory catalina root directory
*/
public TomcatEmbeddedServer(String homeDirectory) {
if (homeDirectory == null) {
throw new IllegalArgumentException("homeDirectory should not be null");
}
this.homeDirectory = homeDirectory.trim();
}
/**
* Start Tomcat embedded server
*/
public void start() {
embeddedServer = new Embedded();
embeddedServer.setCatalinaHome(homeDirectory);
Engine engine = this.embeddedServer.createEngine();
engine.setDefaultHost("localhost");
File webappsDirectory = new File(homeDirectory, "webapps");
webappsDirectory.mkdirs();
host = this.embeddedServer.createHost(engine.getDefaultHost(), webappsDirectory.getAbsolutePath());
engine.addChild(host);
embeddedServer.addEngine(engine);
try {
Connector connector = new Connector();
connector.setPort(8080);
embeddedServer.addConnector(connector);
embeddedServer.start();
} catch (Exception e) {
throw new RuntimeException(e);
}
}
/**
* Deploy web application
* @param contextPath context path
* @param artifactPath artifact location (e.g. war archive)
*/
public void deploy(String contextPath, String artifactPath) {
if (embeddedServer == null) {
throw new IllegalStateException("Server is not being started");
}
Context context = embeddedServer.createContext(getContextPath(contextPath), artifactPath);
context.setReloadable(false);
host.addChild(context);
}
/**
* Undeploy web application
* @param contextPath context path
*/
public void undeploy(String contextPath) {
if (embeddedServer == null) {
throw new IllegalStateException("Server is not being started");
}
Context context = host.map(getContextPath(contextPath));
host.removeChild(context);
}
private String getContextPath(String contextPath) {
if (contextPath == null) {
throw new IllegalArgumentException("contextPath should not be null");
}
contextPath = contextPath.trim();
return contextPath.startsWith("/") ? contextPath : "/" + contextPath;
}
/**
* Stop Tomcat embedded server
*/
public void stop() {
try {
if (embeddedServer != null) {
embeddedServer.stop();
}
} catch (LifecycleException e) {
throw new RuntimeException(e);
} finally {
embeddedServer = null;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment