KickJava   Java API By Example, From Geeks To Geeks.

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


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.Catalog;
21 import org.apache.commons.chain.CatalogFactory;
22 import org.apache.commons.chain.Command;
23 import org.apache.commons.chain.Context;
24 import org.apache.commons.chain.Filter;
25 import org.apache.commons.logging.Log;
26 import org.apache.commons.logging.LogFactory;
27 import org.apache.struts.chain.Constants;
28
29
30 /**
31  * <p>Intercept any exception thrown by a subsequent <code>Command</code>
32  * in this processing chain, and fire the configured exception handler chain
33  * after storing the exception that has occurred into the <code>Context</code>.
34  * </p>
35  *
36  * @author Craig R. McClanahan
37  * @version $Rev: 55324 $ $Date: 2004-10-22 19:55:27 +0100 (Fri, 22 Oct 2004) $
38  */

39
40 public class ExceptionCatcher implements Filter {
41
42
43     // ------------------------------------------------------ Instance Variables
44

45
46     private String JavaDoc catalogName = null;
47     private String JavaDoc exceptionCommand = null;
48     private String JavaDoc exceptionKey = Constants.EXCEPTION_KEY;
49
50     private static final Log log = LogFactory.getLog(ExceptionCatcher.class);
51
52
53     // -------------------------------------------------------------- Properties
54

55
56     /**
57      * <p>Return the name of the <code>Catalog</code> in which to perform
58      * lookups, or <code>null</code> for the default <code>Catalog</code>.</p>
59      */

60     public String JavaDoc getCatalogName() {
61
62         return (this.catalogName);
63
64     }
65
66
67     /**
68      * <p>Set the name of the <code>Catalog</code> in which to perform
69      * lookups, or <code>null</code> for the default <code>Catalog</code>.</p>
70      *
71      * @param catalogName The new catalog name or <code>null</code>
72      */

73     public void setCatalogName(String JavaDoc catalogName) {
74
75         this.catalogName = catalogName;
76
77     }
78
79
80     /**
81      * <p>Return the name of the command to be executed
82      * if an exception occurs.</p>
83      */

84     public String JavaDoc getExceptionCommand() {
85
86         return (this.exceptionCommand);
87
88     }
89
90
91     /**
92      * <p>Set the name of the command to be executed
93      * if an exception occurs.</p>
94      *
95      * @param exceptionCommand The name of the chain to be executed
96      */

97     public void setExceptionCommand(String JavaDoc exceptionCommand) {
98
99         this.exceptionCommand = exceptionCommand;
100
101     }
102
103
104     /**
105      * <p>Return the context attribute key under which any
106      * thrown exception will be stored.</p>
107      */

108     public String JavaDoc getExceptionKey() {
109
110         return (this.exceptionKey);
111
112     }
113
114
115     /**
116      * <p>Set the context attribute key under which any
117      * thrown exception will be stored.</p>
118      *
119      * @param exceptionKey The new context attribute key
120      */

121     public void setExceptionKey(String JavaDoc exceptionKey) {
122
123         this.exceptionKey = exceptionKey;
124
125     }
126
127
128     // ---------------------------------------------------------- Public Methods
129

130
131     /**
132      * <p>Clear any existing stored exception and pass the <code>context</code>
133      * on to the remainder of the current chain.</p>
134      *
135      * @param context The <code>Context</code> for the current request
136      *
137      * @return <code>false</code> so that processing continues
138      */

139     public boolean execute(Context context) throws Exception JavaDoc {
140
141         context.remove(getExceptionKey());
142         return (false);
143
144     }
145
146
147     /**
148      * <p>If an exception was thrown by a subsequent <code>Command</code>,
149      * pass it on to the specified exception handling chain. Otherwise,
150      * do nothing.</p>
151      *
152      * @param context The {@link Context} to be processed by this
153      * {@link Filter}
154      * @param exception The <code>Exception</code> (if any) that was thrown
155      * by the last {@link Command} that was executed; otherwise
156      * <code>null</code>
157      */

158     public boolean postprocess(Context context, Exception JavaDoc exception) {
159
160         // Do nothing if there was no exception thrown
161
if (exception == null) {
162             return (false);
163         }
164
165         // Stash the exception in the specified context attribute
166
if (log.isDebugEnabled()) {
167             log.debug("Attempting to handle a thrown exception");
168         }
169         context.put(getExceptionKey(), exception);
170
171         // Execute the specified command
172
try {
173             String JavaDoc catalogName = getCatalogName();
174             Catalog catalog = null;
175             if (catalogName == null) {
176                 catalog = CatalogFactory.getInstance().getCatalog();
177                 if (catalog == null) {
178                     log.error("Cannot find default catalog");
179                     throw new IllegalArgumentException JavaDoc
180                         ("Cannot find default catalog");
181                 }
182             } else {
183                 catalog = CatalogFactory.getInstance().getCatalog(catalogName);
184                 if (catalog == null) {
185                     log.error("Cannot find catalog '" + catalogName + "'");
186                     throw new IllegalArgumentException JavaDoc
187                         ("Cannot find catalog '" + catalogName + "'");
188                 }
189             }
190             String JavaDoc exceptionCommand = getExceptionCommand();
191             if (exceptionCommand == null) {
192                 log.error("No exceptionCommand property specified");
193                 throw new IllegalStateException JavaDoc
194                     ("No exceptionCommand property specfied");
195             }
196             Command command = catalog.getCommand(exceptionCommand);
197             if (command == null) {
198                 log.error("Cannot find exceptionCommand '" +
199                           exceptionCommand + "'");
200                 throw new IllegalStateException JavaDoc
201                     ("Cannot find exceptionCommand '" +
202                      exceptionCommand + "'");
203             }
204             if (log.isTraceEnabled()) {
205                 log.trace("Calling exceptionCommand '" + exceptionCommand
206                           + "'");
207             }
208             command.execute(context);
209         } catch (Exception JavaDoc e) {
210             log.warn("Exception from exceptionCommand '" +
211                      exceptionCommand + "'", e);
212             throw new IllegalStateException JavaDoc("Exception chain threw exception");
213         }
214         return (true);
215
216     }
217
218
219
220 }
221
Popular Tags