📄 applicationdispatcher.java
字号:
} catch (ServletException e) {
log(sm.getString("applicationDispatcher.allocateException",
wrapper.getName()), e);
servletException = e;
servlet = null;
} catch (Throwable e) {
log(sm.getString("applicationDispatcher.allocateException",
wrapper.getName()), e);
servletException = new ServletException
(sm.getString("applicationDispatcher.allocateException",
wrapper.getName()), e);
servlet = null;
}
// Get the FilterChain Here
ApplicationFilterFactory factory = ApplicationFilterFactory.getInstance();
ApplicationFilterChain filterChain = factory.createFilterChain(request,
wrapper,servlet);
// Call the service() method for the allocated servlet instance
try {
String jspFile = wrapper.getJspFile();
if (jspFile != null)
request.setAttribute(Globals.JSP_FILE_ATTR, jspFile);
else
request.removeAttribute(Globals.JSP_FILE_ATTR);
support.fireInstanceEvent(InstanceEvent.BEFORE_DISPATCH_EVENT,
servlet, request, response);
// for includes/forwards
if ((servlet != null) && (filterChain != null)) {
filterChain.doFilter(request, response);
}
// Servlet Service Method is called by the FilterChain
request.removeAttribute(Globals.JSP_FILE_ATTR);
support.fireInstanceEvent(InstanceEvent.AFTER_DISPATCH_EVENT,
servlet, request, response);
} catch (ClientAbortException e) {
request.removeAttribute(Globals.JSP_FILE_ATTR);
support.fireInstanceEvent(InstanceEvent.AFTER_DISPATCH_EVENT,
servlet, request, response);
ioException = e;
} catch (IOException e) {
request.removeAttribute(Globals.JSP_FILE_ATTR);
support.fireInstanceEvent(InstanceEvent.AFTER_DISPATCH_EVENT,
servlet, request, response);
log(sm.getString("applicationDispatcher.serviceException",
wrapper.getName()), e);
ioException = e;
} catch (UnavailableException e) {
request.removeAttribute(Globals.JSP_FILE_ATTR);
support.fireInstanceEvent(InstanceEvent.AFTER_DISPATCH_EVENT,
servlet, request, response);
log(sm.getString("applicationDispatcher.serviceException",
wrapper.getName()), e);
servletException = e;
wrapper.unavailable(e);
} catch (ServletException e) {
request.removeAttribute(Globals.JSP_FILE_ATTR);
support.fireInstanceEvent(InstanceEvent.AFTER_DISPATCH_EVENT,
servlet, request, response);
Throwable rootCause = e;
Throwable rootCauseCheck = null;
// Extra aggressive rootCause finding
do {
try {
rootCauseCheck = (Throwable)PropertyUtils.getProperty
(rootCause, "rootCause");
if (rootCauseCheck!=null)
rootCause = rootCauseCheck;
} catch (ClassCastException ex) {
rootCauseCheck = null;
} catch (IllegalAccessException ex) {
rootCauseCheck = null;
} catch (NoSuchMethodException ex) {
rootCauseCheck = null;
} catch (java.lang.reflect.InvocationTargetException ex) {
rootCauseCheck = null;
}
} while (rootCauseCheck != null);
log(sm.getString("applicationDispatcher.serviceException",
wrapper.getName()), rootCause);
servletException = e;
} catch (RuntimeException e) {
request.removeAttribute(Globals.JSP_FILE_ATTR);
support.fireInstanceEvent(InstanceEvent.AFTER_DISPATCH_EVENT,
servlet, request, response);
log(sm.getString("applicationDispatcher.serviceException",
wrapper.getName()), e);
runtimeException = e;
}
// Release the filter chain (if any) for this request
try {
if (filterChain != null)
filterChain.release();
} catch (Throwable e) {
log.error(sm.getString("standardWrapper.releaseFilters",
wrapper.getName()), e);
//FIXME Exception handling needs to be simpiler to what is in the StandardWrapperValue
}
// Deallocate the allocated servlet instance
try {
if (servlet != null) {
wrapper.deallocate(servlet);
}
} catch (ServletException e) {
log(sm.getString("applicationDispatcher.deallocateException",
wrapper.getName()), e);
servletException = e;
} catch (Throwable e) {
log(sm.getString("applicationDispatcher.deallocateException",
wrapper.getName()), e);
servletException = new ServletException
(sm.getString("applicationDispatcher.deallocateException",
wrapper.getName()), e);
}
// Reset the old context class loader
if (oldCCL != null)
Thread.currentThread().setContextClassLoader(oldCCL);
// Rethrow an exception if one was thrown by the invoked servlet
if (ioException != null)
throw ioException;
if (servletException != null)
throw servletException;
if (runtimeException != null)
throw runtimeException;
}
/**
* Log a message on the Logger associated with our Context (if any)
*
* @param message Message to be logged
*/
private void log(String message) {
Logger logger = context.getLogger();
if (logger != null)
logger.log("ApplicationDispatcher[" + context.getPath() +
"]: " + message);
else
System.out.println("ApplicationDispatcher[" +
context.getPath() + "]: " + message);
}
/**
* Log a message on the Logger associated with our Container (if any)
*
* @param message Message to be logged
* @param throwable Associated exception
*/
private void log(String message, Throwable throwable) {
Logger logger = context.getLogger();
if (logger != null)
logger.log("ApplicationDispatcher[" + context.getPath() +
"] " + message, throwable);
else {
System.out.println("ApplicationDispatcher[" +
context.getPath() + "]: " + message);
throwable.printStackTrace(System.out);
}
}
/**
* Set up to handle the specified request and response
*
* @param request The servlet request specified by the caller
* @param response The servlet response specified by the caller
* @param including Are we performing an include() as opposed to
* a forward()?
*/
private void setup(ServletRequest request, ServletResponse response,
boolean including) {
this.appRequest = request;
this.appResponse = response;
this.outerRequest = request;
this.outerResponse = response;
this.including = including;
}
/**
* Unwrap the request if we have wrapped it.
*/
private void unwrapRequest() {
if (wrapRequest == null)
return;
ServletRequest previous = null;
ServletRequest current = outerRequest;
while (current != null) {
// If we run into the container request we are done
if ((current instanceof Request)
|| (current instanceof RequestFacade))
break;
// Remove the current request if it is our wrapper
if (current == wrapRequest) {
ServletRequest next =
((ServletRequestWrapper) current).getRequest();
if (previous == null)
outerRequest = next;
else
((ServletRequestWrapper) previous).setRequest(next);
break;
}
// Advance to the next request in the chain
previous = current;
current = ((ServletRequestWrapper) current).getRequest();
}
}
/**
* Unwrap the response if we have wrapped it.
*/
private void unwrapResponse() {
if (wrapResponse == null)
return;
ServletResponse previous = null;
ServletResponse current = outerResponse;
while (current != null) {
// If we run into the container response we are done
if ((current instanceof Response)
|| (current instanceof ResponseFacade))
break;
// Remove the current response if it is our wrapper
if (current == wrapResponse) {
ServletResponse next =
((ServletResponseWrapper) current).getResponse();
if (previous == null)
outerResponse = next;
else
((ServletResponseWrapper) previous).setResponse(next);
break;
}
// Advance to the next response in the chain
previous = current;
current = ((ServletResponseWrapper) current).getResponse();
}
}
/**
* Create and return a request wrapper that has been inserted in the
* appropriate spot in the request chain.
*/
private ServletRequest wrapRequest() {
// Locate the request we should insert in front of
ServletRequest previous = null;
ServletRequest current = outerRequest;
while (current != null) {
if ("org.apache.catalina.servlets.InvokerHttpRequest".
equals(current.getClass().getName()))
break; // KLUDGE - Make nested RD.forward() using invoker work
if (!(current instanceof ServletRequestWrapper))
break;
if (current instanceof ApplicationHttpRequest)
break;
if (current instanceof ApplicationRequest)
break;
if (current instanceof Request)
break;
previous = current;
current = ((ServletRequestWrapper) current).getRequest();
}
// Instantiate a new wrapper at this point and insert it in the chain
ServletRequest wrapper = null;
if ((current instanceof ApplicationHttpRequest) ||
(current instanceof HttpRequest) ||
(current instanceof HttpServletRequest)) {
// Compute a crossContext flag
HttpServletRequest hcurrent = (HttpServletRequest) current;
boolean crossContext =
!(context.getPath().equals(hcurrent.getContextPath()));
wrapper = new ApplicationHttpRequest
(hcurrent, context, crossContext);
} else {
wrapper = new ApplicationRequest(current);
}
if (previous == null)
outerRequest = wrapper;
else
((ServletRequestWrapper) previous).setRequest(wrapper);
wrapRequest = wrapper;
return (wrapper);
}
/**
* Create and return a response wrapper that has been inserted in the
* appropriate spot in the response chain.
*/
private ServletResponse wrapResponse() {
// Locate the response we should insert in front of
ServletResponse previous = null;
ServletResponse current = outerResponse;
while (current != null) {
if (!(current instanceof ServletResponseWrapper))
break;
if (current instanceof ApplicationHttpResponse)
break;
if (current instanceof ApplicationResponse)
break;
if (current instanceof Response)
break;
previous = current;
current = ((ServletResponseWrapper) current).getResponse();
}
// Instantiate a new wrapper at this point and insert it in the chain
ServletResponse wrapper = null;
if ((current instanceof ApplicationHttpResponse) ||
(current instanceof HttpResponse) ||
(current instanceof HttpServletResponse))
wrapper =
new ApplicationHttpResponse((HttpServletResponse) current,
including);
else
wrapper = new ApplicationResponse(current, including);
if (previous == null)
outerResponse = wrapper;
else
((ServletResponseWrapper) previous).setResponse(wrapper);
wrapResponse = wrapper;
return (wrapper);
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -