KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > scriptella > core > OnErrorHandler


1 /*
2  * Copyright 2006-2007 The Scriptella Project Team.
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 scriptella.core;
17
18 import scriptella.configuration.OnErrorEl;
19 import scriptella.configuration.ScriptEl;
20 import scriptella.spi.ProviderException;
21 import scriptella.util.ExceptionUtils;
22
23 import java.util.ArrayList JavaDoc;
24 import java.util.Collections JavaDoc;
25 import java.util.List JavaDoc;
26 import java.util.Set JavaDoc;
27 import java.util.regex.Pattern JavaDoc;
28
29 /**
30  * Error handler for scripting elements.
31  * <p>This class maintains an internal copy of {@link #onerrorElements} for
32  * {@link ScriptEl} to allow calling {@link #onError(Throwable)} several times
33  * with different return result.
34  *
35  * @author Fyodor Kupolov
36  * @version 1.0
37  */

38 public class OnErrorHandler {
39     private List JavaDoc<OnErrorEl> onerrorElements;
40     //Stores error identifiers
41

42     /**
43      * Initialize error handler with the list of {@link OnErrorEl}.
44      *
45      * @param scriptEl scripting element configuration to get onerror info.
46      */

47     public OnErrorHandler(ScriptEl scriptEl) {
48         List JavaDoc<OnErrorEl> onerrorElements = scriptEl.getOnerrorElements();
49         if (onerrorElements != null && onerrorElements.size() > 0) {
50             this.onerrorElements = new ArrayList JavaDoc<OnErrorEl>(onerrorElements);
51         }
52     }
53
54     /**
55      * Called on error to get a {@link OnErrorEl} with fallback script.
56      * <p><em>Note:</em> The returned OnErrorEl element is removed from internal list of elements to avoid
57      * handling the same error condition twice.
58      *
59      * @param throwable
60      * @return OnErrorEl matching error condition.
61      */

62     public OnErrorEl onError(Throwable JavaDoc throwable) {
63         if (onerrorElements == null) {
64             return null;
65         }
66         //walks down the throwables chain
67
for (Throwable JavaDoc t = throwable; t != null; t = ExceptionUtils.getCause(t)) {
68             //iterates through elements
69
for (OnErrorEl onErrorEl : onerrorElements) {
70                 Pattern JavaDoc type = onErrorEl.getType();
71                 if (type != null && !type.matcher(t.getClass().getName()).matches()) {
72                     continue;
73                 }
74                 Pattern JavaDoc msg = onErrorEl.getMessage();
75                 //if onerror has message, but exception hasn't or different message - skip this case
76
if (msg != null && (t.getMessage() == null || !msg.matcher(t.getMessage()).matches())) {
77                     continue;
78                 }
79                 Set JavaDoc<String JavaDoc> codes = onErrorEl.getCodes();
80                 if (codes != null && !codes.isEmpty()) {
81                     Set JavaDoc<String JavaDoc> errorCodes = getErrorCodes(t);
82                     boolean match = false;
83                     //check if onerror matches any error code for the throwable
84
for (String JavaDoc ec : errorCodes) {
85                         if (codes.contains(ec)) {
86                             match = true;
87                             break;
88                         }
89                     }
90                     if (!match) {
91                         continue;
92                     }
93                 }
94                 //Next time we should not visit this element
95
onerrorElements.remove(onErrorEl);
96                 return onErrorEl;
97             }
98         }
99         return null;
100
101     }
102
103     /**
104      * @param t throwables chain.
105      * @return not null set of found error codes in throwable chain.
106      */

107     protected Set JavaDoc<String JavaDoc> getErrorCodes(Throwable JavaDoc t) {
108         if (t instanceof ProviderException) {
109             return ((ProviderException) t).getErrorCodes();
110         }
111         return Collections.emptySet();
112     }
113
114
115 }
116
Popular Tags