1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21 package org.apache.struts.chain.commands;
22
23 import org.apache.commons.logging.Log;
24 import org.apache.commons.logging.LogFactory;
25 import org.apache.struts.chain.contexts.ActionContext;
26 import org.apache.struts.config.ActionConfig;
27 import org.apache.struts.config.ExceptionConfig;
28 import org.apache.struts.config.ForwardConfig;
29 import org.apache.struts.config.ModuleConfig;
30
31 /**
32 * <p>Invoke the local or global exception handler configured for the
33 * exception class that occurred.</p>
34 *
35 * @version $Rev: 471754 $ $Date: 2005-11-12 13:01:44 -0500 (Sat, 12 Nov 2005)
36 * $
37 */
38 public abstract class AbstractExceptionHandler extends ActionCommandBase {
39
40
41 /**
42 * Provide a Commons logging instance for this class.
43 */
44 private static final Log LOG =
45 LogFactory.getLog(AbstractExceptionHandler.class);
46
47
48
49 /**
50 * <p>Invoke the appropriate <code>Action</code> for this request, and
51 * cache the returned <code>ActionForward</code>.</p>
52 *
53 * @param actionCtx The <code>Context</code> for the current request
54 * @return <code>false</code> if a <code>ForwardConfig</code> is returned,
55 * else <code>true</code> to complete processing
56 * @throws Exception if thrown by the Action class and not declared by an
57 * Exception Handler
58 */
59 public boolean execute(ActionContext actionCtx)
60 throws Exception {
61
62 Exception exception = actionCtx.getException();
63
64 if (exception == null) {
65 LOG.warn("No Exception found in ActionContext");
66
67 return (true);
68 }
69
70
71 ExceptionConfig exceptionConfig = null;
72 ActionConfig actionConfig = actionCtx.getActionConfig();
73 ModuleConfig moduleConfig = actionCtx.getModuleConfig();
74
75 if (actionConfig != null) {
76 if (LOG.isDebugEnabled()) {
77 LOG.debug("See if actionConfig " + actionConfig
78 + " has an exceptionConfig for "
79 + exception.getClass().getName());
80 }
81
82 exceptionConfig = actionConfig.findException(exception.getClass());
83 } else if (moduleConfig != null) {
84 if (LOG.isDebugEnabled()) {
85 LOG.debug("No action yet, see if moduleConfig " + moduleConfig
86 + " has an exceptionConfig "
87 + exception.getClass().getName());
88 }
89
90 exceptionConfig = moduleConfig.findException(exception.getClass());
91 }
92
93
94 if (exceptionConfig == null) {
95 LOG.warn("Unhandled exception", exception);
96 throw exception;
97 }
98
99 ForwardConfig forwardConfig =
100 handle(actionCtx, exception, exceptionConfig, actionConfig,
101 moduleConfig);
102
103 if (forwardConfig != null) {
104 actionCtx.setForwardConfig(forwardConfig);
105
106 return (false);
107 } else {
108 return (true);
109 }
110 }
111
112
113
114 /**
115 * <p>Perform the required handling of the specified exception.</p>
116 *
117 * @param context The <code>Context</code> for this request
118 * @param exception The exception being handled
119 * @param exceptionConfig The corresponding {@link ExceptionConfig}
120 * @param actionConfig The {@link ActionConfig} for this request
121 * @param moduleConfig The {@link ModuleConfig} for this request
122 * @return the <code>ForwardConfig</code> to be processed next (if any),
123 * or <code>null</code> if processing has been completed
124 * @throws Exception if there are any problems handling the exception
125 */
126 protected abstract ForwardConfig handle(ActionContext context,
127 Exception exception, ExceptionConfig exceptionConfig,
128 ActionConfig actionConfig, ModuleConfig moduleConfig)
129 throws Exception;
130 }