KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > springframework > webflow > engine > FlowExecutionExceptionHandlerSet


1 /*
2  * Copyright 2002-2006 the original author or authors.
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.springframework.webflow.engine;
17
18 import java.util.Iterator JavaDoc;
19 import java.util.LinkedList JavaDoc;
20 import java.util.List JavaDoc;
21
22 import org.springframework.core.style.StylerUtils;
23 import org.springframework.webflow.core.collection.CollectionUtils;
24 import org.springframework.webflow.execution.FlowExecutionException;
25 import org.springframework.webflow.execution.ViewSelection;
26
27 /**
28  * A typed set of state exception handlers, mainly for use internally by
29  * artifacts that can apply state exception handling logic.
30  *
31  * @see FlowExecutionExceptionHandler
32  * @see Flow#getExceptionHandlerSet()
33  * @see State#getExceptionHandlerSet()
34  *
35  * @author Keith Donald
36  */

37 public class FlowExecutionExceptionHandlerSet {
38
39     /**
40      * The set of exception handlers.
41      */

42     private List JavaDoc exceptionHandlers = new LinkedList JavaDoc();
43
44     /**
45      * Add a state exception handler to this set.
46      * @param exceptionHandler the exception handler to add
47      * @return true if this set's contents changed as a result of the add
48      * operation
49      */

50     public boolean add(FlowExecutionExceptionHandler exceptionHandler) {
51         if (contains(exceptionHandler)) {
52             return false;
53         }
54         return exceptionHandlers.add(exceptionHandler);
55     }
56
57     /**
58      * Add a collection of state exception handler instances to this set.
59      * @param exceptionHandlers the exception handlers to add
60      * @return true if this set's contents changed as a result of the add
61      * operation
62      */

63     public boolean addAll(FlowExecutionExceptionHandler[] exceptionHandlers) {
64         return CollectionUtils.addAllNoDuplicates(this.exceptionHandlers, exceptionHandlers);
65     }
66
67     /**
68      * Tests if this state exception handler is in this set.
69      * @param exceptionHandler the exception handler
70      * @return true if the state exception handler is contained in this set,
71      * false otherwise
72      */

73     public boolean contains(FlowExecutionExceptionHandler exceptionHandler) {
74         return exceptionHandlers.contains(exceptionHandler);
75     }
76
77     /**
78      * Remove the exception handler instance from this set.
79      * @param exceptionHandler the exception handler to add
80      * @return true if this set's contents changed as a result of the remove
81      * operation
82      */

83     public boolean remove(FlowExecutionExceptionHandler exceptionHandler) {
84         return exceptionHandlers.remove(exceptionHandler);
85     }
86
87     /**
88      * Returns the size of this state exception handler set.
89      * @return the exception handler set size
90      */

91     public int size() {
92         return exceptionHandlers.size();
93     }
94
95     /**
96      * Convert this list to a typed state exception handler array.
97      * @return the exception handler list, as a typed array
98      */

99     public FlowExecutionExceptionHandler[] toArray() {
100         return (FlowExecutionExceptionHandler[])exceptionHandlers.toArray(new FlowExecutionExceptionHandler[exceptionHandlers.size()]);
101     }
102
103     /**
104      * Handle an exception that occured during the context of the current flow
105      * execution request.
106      * <p>
107      * This implementation iterates over the ordered set of exception handler
108      * objects, delegating to each handler in the set until one handles the
109      * exception that occured and selects a non-null error view.
110      * @param exception the exception that occured
111      * @param context the flow execution control context
112      * @return the selected error view, or <code>null</code> if no handler
113      * matched or returned a non-null view selection
114      */

115     public ViewSelection handleException(FlowExecutionException exception, RequestControlContext context) {
116         Iterator JavaDoc it = exceptionHandlers.iterator();
117         while (it.hasNext()) {
118             FlowExecutionExceptionHandler handler = (FlowExecutionExceptionHandler)it.next();
119             if (handler.handles(exception)) {
120                 ViewSelection result = handler.handle(exception, context);
121                 if (result != null) {
122                     return result;
123                 }
124                 // else continue with next handler
125
}
126         }
127         return null;
128     }
129
130     public String JavaDoc toString() {
131         return StylerUtils.style(exceptionHandlers);
132     }
133 }
Popular Tags