import javaioFile;
import InetAddress;
import orgapachecatalinaContext;
import orgapachecatalinaEngine;
import orgapachecatalinaHost;
import orgapachecatalinastartupEmbedded;
public class EmbeddedTomcat {
private String contextPath = null;
private String hostName=null;
private String catalinaHomePath=null;
private int port=;
private Embedded embedded = null;
private Host host = null;
public EmbeddedTomcat(String contextPathString catalinaHomePathString hostNameint port)
{
ntextPath=contextPath;
thiscatalinaHomePath=catalinaHomePath;
thishostName=hostName;
thisport=port;
}
/**
* This method Starts the Tomcat server
*/
public void startTomcat() throws Exception {
// Create an embedded server
embedded = new Embedded();
Engine engine = null;
// SystemsetProperty(catalinahome getPath());
embeddedsetCatalinaHome(catalinaHomePath);
// Create an engine
engine = embeddedcreateEngine();
// Create a default virtual host
host = embeddedcreateHost(hostName contextPath+/webapps);
engineaddChild(host);
enginesetDefaultHost(hostgetName());
// Create the ROOT context
Context rootCxt = embeddedcreateContext(contextPath + /webapps/ROOT);
Context manageCxt = embeddedcreateContext(/managercontextPath+/webapps/manager);
//Create your own context
Context scoreCxt = embeddedcreateContext(/vmm contextPath+/webapps/vmm);
rootCxtsetPrivileged(true);
hostaddChild(rootCxt);
hostaddChild(manageCxt);
hostaddChild(scoreCxt);
// Install the assembled container hierarchy
embeddedaddEngine(engine);
// Assemble and install a default HTTP connector
embeddedaddConnector(embeddedcreateConnector(InetAddressgetByName(null) port false));
// Start the embedded server
embeddedstart();
}
/**
* This method Stops the Tomcat server
*/
public void stopTomcat() throws Exception {
// Stop the embedded server
embeddedstop();
}
public static void main(String args[]) {
try {
String contextPath=(new File())getCanonicalPath();
String catalinaHomePath =(new File(/conf/tomcat))getCanonicalPath();
String hostName=localhost;
int port =;
Systemoutprintln(contextPath:+contextPath);
Systemoutprintln(catalinaHomePath:+catalinaHomePath);
EmbeddedTomcat tomcat = new EmbeddedTomcat(contextPathcatalinaHomePathhostNameport);
tomcatstartTomcat();
}
catch( Exception e ) {
eprintStackTrace();
}
}
}