KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > cocoon > selection > ExceptionSelector


1 /*
2  * Copyright 1999-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 package org.apache.cocoon.selection;
17
18 import java.util.Map JavaDoc;
19
20 import org.apache.avalon.framework.configuration.Configurable;
21 import org.apache.avalon.framework.configuration.Configuration;
22 import org.apache.avalon.framework.configuration.ConfigurationException;
23 import org.apache.avalon.framework.parameters.Parameters;
24 import org.apache.cocoon.environment.ObjectModelHelper;
25 import org.apache.cocoon.util.ClassUtils;
26 import org.apache.commons.lang.exception.ExceptionUtils;
27
28 /**
29  * In a <map:handle-errors>, selects depending on the exception that caused the error.
30  * The configuration of this selector allows to map exception class names to symbolic names
31  * that are used in the <map:when> alternatives.
32  * <p>
33  * Example configuration :
34  * <pre>
35  * &lt;map:selector type="error" SRC="....ExceptionSelector">
36  * &lt;exception class="org.xml.sax.SAXException" name="sax" unroll="true"/>
37  * &lt;exception name="not-found" class="org.apache.cocoon.ResourceNotFoundException"/>
38  * &lt;exception class="org.apache.cocoon.ProcessingException" unroll="true"/>
39  * &lt;exception name="denied" class="java.security.SecurityException"/>
40  * &lt;exception name="denied" class="my.comp.auth.AuthenticationFailure"/>
41  * &lt;/map:selector>
42  * </pre>
43  * This example shows several features :
44  * <li>the "class" is the class name of the exception (which can be any <code>Throwable</code>),</li>
45  * <li>an exception can be given a name, which is used in the &lt;map:when> tests,</li>
46  * <li>an exception can be unrolled, meaning we try to get its cause and then consider this cause for
47  * the exception name</li>
48  * Note that both "name" and "unroll" can be specified. In that case, we first try to unroll the exception,
49  * and if none of the causes has a name, then the "name" attribute is considered.
50  *
51  * @author <a HREF="mailto:juergen.seitz@basf-it-services.com">J&uuml;rgen Seitz</a>
52  * @author <a HREF="mailto:bluetkemeier@s-und-n.de">Bj&ouml;rn L&uuml;tkemeier</a>
53  * @author <a HREF="mailto:sylvain@apache.org">Sylvain Wallez</a>
54  * @since 2.1
55  * @version CVS $Id: ExceptionSelector.java 280628 2005-09-13 19:14:35Z sylvain $
56  */

57
58 public class ExceptionSelector extends AbstractSwitchSelector implements Configurable {
59
60     /** Exception classes */
61     private Class JavaDoc[] clazz;
62     
63     /** Associated symbolic names (can be null) */
64     private String JavaDoc[] name;
65     
66     /** Do we want to unroll them ? */
67     private boolean[] unroll;
68
69     public void configure(Configuration conf) throws ConfigurationException {
70
71         Configuration[] children = conf.getChildren("exception");
72
73         this.clazz = new Class JavaDoc[children.length];
74         this.name = new String JavaDoc[children.length];
75         this.unroll = new boolean[children.length];
76
77         for (int i = 0; i < children.length; i++) {
78             Configuration child = children[i];
79
80             String JavaDoc childClassName = child.getAttribute("class");
81             Class JavaDoc childClass = null;
82             try {
83                 childClass = ClassUtils.loadClass(childClassName);
84             }
85             catch (Exception JavaDoc e) {
86                 throw new ConfigurationException("Cannot load class '" + childClassName + "' at " + child.getLocation());
87             }
88             
89             // Check that this class is not hidden by a more general class already declared
90
for (int j = 0; j < i; j++) {
91                 if (this.clazz[j].isAssignableFrom(childClass)) {
92                     throw new ConfigurationException("Class '" + this.clazz[j].getName() + "' hides its subclass '" +
93                         childClassName + "' at " + child.getLocation());
94                 }
95             }
96
97             this.clazz[i] = childClass;
98             this.name[i] = child.getAttribute("name", null);
99             this.unroll[i] = child.getAttributeAsBoolean("unroll", false);
100
101             if (this.name[i] == null && !this.unroll[i]) {
102                 throw new ConfigurationException("Must specify one of 'name' or 'unroll' at " + child.getLocation());
103             }
104         }
105     }
106
107     /**
108      * Compute the exception type, given the configuration and the exception stored in the object model.
109      *
110      * @see ObjectModelHelper#getThrowable(java.util.Map)
111      */

112     public Object JavaDoc getSelectorContext(Map JavaDoc objectModel, Parameters parameters) {
113         // Get the name of the exception
114
Throwable JavaDoc thr = ObjectModelHelper.getThrowable(objectModel);
115         if (thr == null) {
116             throw new IllegalStateException JavaDoc("No exception in object model. ExceptionSelector can only be used in <map:handle-errors>");
117         }
118
119         return find(thr);
120     }
121
122     private FindResult find(Throwable JavaDoc thr) {
123         // Now find the proper name
124
for (int i = 0; i < this.clazz.length; i++) {
125             if (this.clazz[i].isInstance(thr)) {
126
127                 // If exception needs to be unrolled, and it has a cause,
128
// return the cause name, if not null (recursively)
129
if (this.unroll[i]) {
130                     Throwable JavaDoc cause = ExceptionUtils.getCause(thr);
131                     if (cause != null) {
132                         FindResult result = find(cause);
133                         if (result != null) {
134                             return result;
135                         }
136                     }
137                 }
138
139                 // Not unrolled
140
return new FindResult(this.name[i], thr);
141             }
142         }
143
144         // Not found
145
return null;
146     }
147
148     public boolean select(String JavaDoc expression, Object JavaDoc selectorContext) {
149         if ( selectorContext == null ) {
150             return false;
151         }
152         // Just compare the expression with the previously found name
153
boolean result = expression.equals(((FindResult)selectorContext).getName());
154         
155         if (result) {
156             if (getLogger().isDebugEnabled())
157                 getLogger().debug("select succesfull for condition " + selectorContext.toString());
158         }
159         
160         return result;
161     }
162     
163     static class FindResult {
164         private String JavaDoc name;
165         private Throwable JavaDoc throwable;
166         
167         public FindResult(String JavaDoc name, Throwable JavaDoc throwable) {
168             this.name = name;
169             this.throwable = throwable;
170         }
171         
172         public String JavaDoc getName() {
173             return this.name;
174         }
175
176         public void setName(String JavaDoc name) {
177             this.name = name;
178         }
179
180         public Throwable JavaDoc getThrowable() {
181             return this.throwable;
182         }
183
184         public void setThrowable(Throwable JavaDoc throwable) {
185             this.throwable = throwable;
186         }
187         
188         public String JavaDoc toString() {
189             return this.name;
190         }
191     }
192
193 }
194
Popular Tags