KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > struts > chain > AbstractExceptionHandler


1 /*
2  * Copyright 2003,2004 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16
17 package org.apache.struts.chain;
18
19
20 import org.apache.commons.chain.Command;
21 import org.apache.commons.chain.Context;
22 import org.apache.commons.logging.Log;
23 import org.apache.commons.logging.LogFactory;
24 import org.apache.struts.chain.Constants;
25 import org.apache.struts.config.ActionConfig;
26 import org.apache.struts.config.ExceptionConfig;
27 import org.apache.struts.config.ForwardConfig;
28 import org.apache.struts.config.ModuleConfig;
29
30
31 /**
32  * <p>Invoke the local or global exception handler configured for the
33  * exception class that occurred.</p>
34  *
35  * @author Craig R. McClanahan
36  * @version $Rev: 54933 $ $Date: 2004-10-16 18:04:52 +0100 (Sat, 16 Oct 2004) $
37  */

38
39 public abstract class AbstractExceptionHandler implements Command {
40
41
42     // ------------------------------------------------------ Instance Variables
43

44
45     private String JavaDoc actionConfigKey = Constants.ACTION_CONFIG_KEY;
46     private String JavaDoc exceptionKey = Constants.EXCEPTION_KEY;
47     private String JavaDoc forwardConfigKey = Constants.FORWARD_CONFIG_KEY;
48     private String JavaDoc moduleConfigKey = Constants.MODULE_CONFIG_KEY;
49
50     private static final Log log =
51         LogFactory.getLog(AbstractExceptionHandler.class);
52
53
54     // -------------------------------------------------------------- Properties
55

56
57     /**
58      * <p>Return the context attribute key under which the
59      * <code>ActionConfig</code> for the currently selected application
60      * action is stored.</p>
61      */

62     public String JavaDoc getActionConfigKey() {
63
64         return (this.actionConfigKey);
65
66     }
67
68
69     /**
70      * <p>Set the context attribute key under which the
71      * <code>ActionConfig</code> for the currently selected application
72      * action is stored.</p>
73      *
74      * @param actionConfigKey The new context attribute key
75      */

76     public void setActionConfigKey(String JavaDoc actionConfigKey) {
77
78         this.actionConfigKey = actionConfigKey;
79
80     }
81
82
83     /**
84      * <p>Return the context attribute key under which any
85      * thrown exception will be stored.</p>
86      */

87     public String JavaDoc getExceptionKey() {
88
89         return (this.exceptionKey);
90
91     }
92
93
94     /**
95      * <p>Set the context attribute key under which any
96      * thrown exception will be stored.</p>
97      *
98      * @param exceptionKey The new context attribute key
99      */

100     public void setExceptionKey(String JavaDoc exceptionKey) {
101
102         this.exceptionKey = exceptionKey;
103
104     }
105
106
107     /**
108      * <p>Return the context attribute key under which the
109      * <code>ForwardConfig</code> for the currently selected application
110      * action is stored.</p>
111      */

112     public String JavaDoc getForwardConfigKey() {
113
114         return (this.forwardConfigKey);
115
116     }
117
118
119     /**
120      * <p>Set the context attribute key under which the
121      * <code>ForwardConfig</code> for the currently selected application
122      * action is stored.</p>
123      *
124      * @param forwardConfigKey The new context attribute key
125      */

126     public void setForwardConfigKey(String JavaDoc forwardConfigKey) {
127
128         this.forwardConfigKey = forwardConfigKey;
129
130     }
131
132
133     /**
134      * <p>Return the context attribute key under which the
135      * <code>ModuleConfig</code> for the currently selected application
136      * action is stored.</p>
137      */

138     public String JavaDoc getModuleConfigKey() {
139
140         return (this.moduleConfigKey);
141
142     }
143
144
145     /**
146      * <p>Set the context attribute key under which the
147      * <code>ModuleConfig</code> for the currently selected application
148      * action is stored.</p>
149      *
150      * @param moduleConfigKey The new context attribute key
151      */

152     public void setModuleConfigKey(String JavaDoc moduleConfigKey) {
153
154         this.moduleConfigKey = moduleConfigKey;
155
156     }
157
158
159     // ---------------------------------------------------------- Public Methods
160

161
162     /**
163      * <p>Invoke the appropriate <code>Action</code> for this request, and cache
164      * the returned <code>ActionForward</code>.</p>
165      *
166      * @param context The <code>Context</code> for the current request
167      *
168      * @exception InvalidPathException if no valid
169      * action can be identified for this request
170      *
171      * @return <code>false</code> if a <code>ForwardConfig</code> is returned,
172      * else <code>true</code> to complete processing
173      */

174     public boolean execute(Context context) throws Exception JavaDoc {
175
176         // Look up the exception that was thrown
177
Exception JavaDoc exception = (Exception JavaDoc)
178             context.get(getExceptionKey());
179         if (exception == null) {
180             log.warn("No Exception found under key '" +
181                      getExceptionKey() + "'");
182             return (true);
183         }
184
185         // Look up the local or global exception handler configuration
186
ExceptionConfig exceptionConfig = null;
187         ActionConfig actionConfig = (ActionConfig)
188             context.get(getActionConfigKey());
189         ModuleConfig moduleConfig = (ModuleConfig)
190             context.get(getModuleConfigKey());
191
192
193         if (actionConfig != null) {
194             log.debug("See if actionConfig " + actionConfig + " has an exceptionConfig for " + exception.getClass().getName());
195             exceptionConfig =
196                 actionConfig.findException(exception.getClass());
197         }
198
199         // Handle the exception in the configured manner
200
if (exceptionConfig == null) {
201             log.warn("Unhandled exception", exception);
202             throw exception;
203         }
204         ForwardConfig forwardConfig =
205             handle(context, exception, exceptionConfig,
206                    actionConfig, moduleConfig);
207         if (forwardConfig != null) {
208             context.put(getForwardConfigKey(), forwardConfig);
209             return (false);
210         } else {
211             return (true);
212         }
213
214     }
215
216
217     // ------------------------------------------------------- Protected Methods
218

219
220     /**
221      * <p>Perform the required handling of the specified exception.</p>
222      *
223      * @param context The <code>Context</code> for this request
224      * @param exception The exception being handled
225      * @param exceptionConfig The corresponding {@link ExceptionConfig}
226      * @param actionConfig The {@link ActionConfig} for this request
227      * @param moduleConfig The {@link ModuleConfig} for this request
228      *
229      * @return the <code>ForwardConfig</code> to be processed next (if any),
230      * or <code>null</code> if processing has been completed
231      */

232     protected abstract ForwardConfig handle(Context context,
233                                             Exception JavaDoc exception,
234                                             ExceptionConfig exceptionConfig,
235                                             ActionConfig actionConfig,
236                                             ModuleConfig moduleConfig)
237         throws Exception JavaDoc;
238
239 }
240
Popular Tags