| Method from org.apache.tomcat.core.ContextManager Detail: |
public Context addContext(String path,
URL docBase) {
if (path == null) {
String msg = sm.getString("server.defaultContext.path.npe");
throw new NullPointerException(msg);
}
path = path.trim();
if (path.length() > 0 &&
docBase == null) {
String msg = sm.getString("server.defaultContext.docBase.npe");
throw new NullPointerException(msg);
}
if (contexts.get(path) != null) {
String msg = sm.getString("server.createctx.existname",
path);
throw new IllegalStateException(msg);
}
if (contextMaps.get(path) != null) {
String msg = sm.getString("server.createctx.existmap",
path);
throw new IllegalStateException(msg);
}
Context context = new Context(this, path);
if (docBase != null) {
context.setDocumentBase(docBase);
}
// check to see if defaultContext
if (path.length() == 0) {
contexts.put(
org.apache.tomcat.core.Constants.Context.Default.Name,
context);
} else {
contexts.put(path, context);
contextMaps.put(path, context);
}
return context;
}
Adds a new Context to the set managed by this ContextManager.
XXX Why is there no context name argument?
XXX Should this be synchronized? |
public Context getContext(String name) {
return (Context)contexts.get(name);
}
Gets a context by it's name, or null if there is
no such context. |
public Context getContextByPath(String path) {
String realPath = path;
Context ctx = null;
// XXX
// needs help ... this needs to be optimized out.
lookup:
do {
ctx = (Context)contextMaps.get(path);
if (ctx == null) {
int i = path.lastIndexOf('/");
if (i > -1 && path.length() > 1) {
path = path.substring(0, i);
if (path.length() == 0) {
path = "/";
}
} else {
// path too short
break lookup;
}
} else {
}
} while (ctx == null);
if (ctx == null) {
ctx = defaultContext;
}
return ctx;
}
Gets the context that is responsible for requests for a
particular path. If no specifically assigned Context can be
identified, returns the default Context. |
public Enumeration getContextNames() {
return contexts.keys();
}
Get the names of all the contexts in this server. |
public Context getDefaultContext() {
return defaultContext;
}
Gets the default Context for this server. |
public URL getDocumentBase() {
return defaultContext.getDocumentBase();
}
Gets the document base of the default context for this server. |
public String getHostName() {
return hostname;
}
Gets the virtual host name of this server. |
public int getPort() {
return port;
}
Gets the port number on which this server listens. |
public String getServerInfo() {
return serverInfo;
// that is the behavior in the current tomcat
// return defaultContext.getEngineHeader();
}
Gets the server info string for this server |
public void removeContext(String name) {
if (name.equals(
org.apache.tomcat.core.Constants.Context.Default.Name)){
throw new IllegalArgumentException(name);
}
Context context = (Context)contexts.get(name);
if(context != null) {
context.shutdown();
contexts.remove(name);
contextMaps.remove(name);
}
}
Removes a context from service.
XXX Should this be synchronized? |
public void service(Request rrequest,
Response rresponse) {
try {
rrequest.setResponse(rresponse);
rresponse.setRequest(rrequest);
// XXX
// return if an error was detected in processing the
// request line
if (rresponse.getStatus() >= 400) {
rresponse.finish();
rrequest.recycle();
rresponse.recycle();
return;
}
// resolve the server that we are for
String path = rrequest.getRequestURI();
Context ctx= this.getContextByPath(path);
// final fix on response & request
// rresponse.setServerHeader(server.getServerHeader());
String ctxPath = ctx.getPath();
String pathInfo =path.substring(ctxPath.length(),
path.length());
// don't do headers if request protocol is http/0.9
if (rrequest.getProtocol() == null) {
rresponse.setOmitHeaders(true);
}
// do it
// System.out.println( request + " " + rresponse );
ctx.handleRequest(rrequest, rresponse);
// finish and clean up
rresponse.finish();
// protocol notification
rresponse.endResponse();
} catch (Exception e) {
// XXX
// this isn't what we want, we want to log the problem somehow
System.out.println("HANDLER THREAD PROBLEM: " + e);
e.printStackTrace();
}
}
Common for all connectors, needs to be shared in order to avoid
code duplication |
public void setDefaultContext(Context ctx) {
defaultContext=ctx;
}
Sets the default Context for this server. |
public void setDocumentBase(URL docBase) {
defaultContext.setDocumentBase(docBase);
}
Sets the document base of the default context for this server. |
public void setHostName(String host) {
this.hostname=host;
}
Sets the virtual host name of this server. |
public void setPort(int port) {
this.port=port;
}
Sets the port number on which this server listens. |
public void setServerInfo(String serverInfo) {
this.serverInfo = serverInfo;
}
Sets the server info string for this server. This string must
be of the form / [()] |