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