public ServletContext getContext(String uri) {
// Validate the format of the specified argument
if (uri==null || !uri.startsWith(“/”)) {
return null;
}
Context child=null;
try {
// Look for an exact match
Container host=context.getParent();
child=(Context) host.findChild(uri);
// Non-running contexts should be ignored.
if (child !=null && !child.getState().isAvailable()) {
child=null;
}
// Remove any version information and use the mapper
if (child==null) {
int i=uri.indexOf(“##”);
if (i > -1) {
uri=uri.substring(0, i);
}
// Note: This could be more efficient with a dedicated Mapper
// method but such an implementation would require some
// refactoring of the Mapper to avoid copy/paste of
// existing code.
MessageBytes hostMB=MessageBytes.newInstance();
hostMB.setString(host.getName());
MessageBytes pathMB=MessageBytes.newInstance();
pathMB.setString(uri);
MappingData mappingData=new MappingData();
((Engine) host.getParent()).getService().findConnectors()[0].getMapper().map(
hostMB, pathMB, null, mappingData);
child=(Context) mappingData.context;
}
} catch (Throwable t) {
ExceptionUtils.handleThrowable(t);
return null;
}
if (child==null) {
return null;
}
if (context.getCrossContext()) {
// If crossContext is enabled, can always return the context
return child.getServletContext();
} else if (child==context) {
// Can still return the current context
return context.getServletContext();
} else {
// Nothing to return
return null;
}
}