001: package com.mockrunner.struts;
002:
003: import javax.servlet.ServletException;
004: import javax.servlet.http.HttpServletRequest;
005: import javax.servlet.http.HttpServletResponse;
006:
007: import org.apache.struts.action.ActionForm;
008: import org.apache.struts.action.ActionMapping;
009: import org.apache.struts.action.ExceptionHandler;
010: import org.apache.struts.config.ExceptionConfig;
011:
012: import com.mockrunner.base.NestedApplicationException;
013:
014: /**
015: * The default implementation of {@link ExceptionHandlerConfig}. It uses the Struts
016: * exception handling mechanism. Use the various constructors to provide your subclass
017: * of <code>ExceptionHandler</code> or to configure exception handling using an
018: * instance of <code>ExceptionConfig</code>. The <code>ExceptionConfig</code> class
019: * allows you to set the handler class an exception type.
020: * Use {@link ActionTestModule#addExceptionHandler} to register an exception handler.
021: */
022: public class DefaultExceptionHandlerConfig implements
023: ExceptionHandlerConfig {
024: private ExceptionConfig exceptionConfig;
025: private ExceptionHandler exceptionHandler;
026: private Class exceptionClass;
027:
028: public DefaultExceptionHandlerConfig(ExceptionConfig exceptionConfig) {
029: this .exceptionConfig = exceptionConfig;
030: try {
031: Class handlerClass = exceptionConfig.getClass()
032: .getClassLoader().loadClass(
033: exceptionConfig.getHandler());
034: exceptionHandler = (ExceptionHandler) handlerClass
035: .newInstance();
036: exceptionClass = exceptionConfig.getClass()
037: .getClassLoader().loadClass(
038: exceptionConfig.getType());
039: } catch (Exception exc) {
040: throw new NestedApplicationException(exc);
041: }
042: }
043:
044: public DefaultExceptionHandlerConfig(
045: ExceptionHandler exceptionHandler,
046: ExceptionConfig exceptionConfig) {
047: this .exceptionHandler = exceptionHandler;
048: this .exceptionConfig = exceptionConfig;
049: this .exceptionConfig.setHandler(exceptionHandler.getClass()
050: .getName());
051: try {
052: exceptionClass = exceptionConfig.getClass()
053: .getClassLoader().loadClass(
054: exceptionConfig.getType());
055: } catch (Exception exc) {
056: throw new NestedApplicationException(exc);
057: }
058: }
059:
060: public DefaultExceptionHandlerConfig(
061: ExceptionHandler exceptionHandler, Class exceptionClass) {
062: this .exceptionHandler = exceptionHandler;
063: this .exceptionClass = exceptionClass;
064: this .exceptionConfig = new ExceptionConfig();
065: this .exceptionConfig.setHandler(exceptionHandler.getClass()
066: .getName());
067: this .exceptionConfig.setType(exceptionClass.getName());
068: }
069:
070: public DefaultExceptionHandlerConfig(Class exceptionClass) {
071: this .exceptionHandler = new ExceptionHandler();
072: this .exceptionClass = exceptionClass;
073: this .exceptionConfig = new ExceptionConfig();
074: this .exceptionConfig.setHandler(ExceptionHandler.class
075: .getName());
076: this .exceptionConfig.setType(exceptionClass.getName());
077: }
078:
079: public boolean canHandle(Exception exception) {
080: return exceptionClass.isInstance(exception);
081: }
082:
083: public Object handle(Exception exception, ActionMapping mapping,
084: ActionForm form, HttpServletRequest request,
085: HttpServletResponse response) throws ServletException {
086: if (!canHandle(exception))
087: return null;
088: if (null == exceptionHandler)
089: return null;
090: return exceptionHandler.execute((Exception) exception,
091: exceptionConfig, mapping, form, request, response);
092: }
093:
094: /**
095: * Get the underlying <code>ExceptionConfig</code>. If you did not provide
096: * an instance of <code>ExceptionConfig</code>, this class will create one
097: * internally.
098: * @return the <code>ExceptionConfig</code>
099: */
100: public ExceptionConfig getExceptionConfig() {
101: return exceptionConfig;
102: }
103:
104: /**
105: * Get the underlying <code>ExceptionHandler</code>.
106: * @return the <code>ExceptionHandler</code>
107: */
108: public ExceptionHandler getExceptionHandler() {
109: return exceptionHandler;
110: }
111: }
|