host.setAppBase("");
host.setName("localhost");
engine.addChild(host);
// Create the root context
StandardContext context = new StandardContext();
context.setParentClassLoader(Thread.currentThread().getContextClassLoader());
context.setDocBase("");
context.setPath("");
ContextConfig config = new ContextConfig();
((Lifecycle)context).addLifecycleListener(config);
host.addChild(context);
// Install an HTTP connector
Connector connector;
try {
engine.start();
connector = new CustomConnector();
connector.setPort(portNumber);
connector.setContainer(engine);
connector.initialize();
connector.start();
} catch (Exception e) {
throw new ServletMappingException(e);
}
// Keep track of the running server
port = new Port(engine, host, connector);
ports.put(portNumber, port);
}
// Register the servlet mapping
String path = uri.getPath();
if (!path.startsWith("/")) {
path = '/' + path;
}
if (!path.startsWith(contextPath)) {
path = contextPath + path;
}
ServletWrapper wrapper;
if (servlet instanceof DefaultResourceServlet) {
String defaultServletPath = path;
// Optimize the handling of resource requests, use the Tomcat default servlet
// instead of our default resource servlet
if (defaultServletPath.endsWith("*")) {
defaultServletPath = defaultServletPath.substring(0, defaultServletPath.length()-1);
}
if (defaultServletPath.endsWith("/")) {
defaultServletPath = defaultServletPath.substring(0, defaultServletPath.length()-1);
}
DefaultResourceServlet resourceServlet = (DefaultResourceServlet)servlet;
TomcatDefaultServlet defaultServlet = new TomcatDefaultServlet(defaultServletPath, resourceServlet.getDocumentRoot());
wrapper = new ServletWrapper(defaultServlet);
} else {
wrapper = new ServletWrapper(servlet);
}
Context context = port.getHost().map(path);
wrapper.setName(path);
wrapper.addMapping(path);
context.addChild(wrapper);
context.addServletMapping(path, path);
port.getConnector().getMapper().addWrapper("localhost", "", path, wrapper);
// Initialize the servlet
try {
wrapper.initServlet();