001: /*
002: * Copyright 2004-2007 the original author or authors.
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016: package org.springframework.webflow.engine;
017:
018: import java.util.Iterator;
019: import java.util.LinkedList;
020: import java.util.List;
021:
022: import org.springframework.core.style.StylerUtils;
023: import org.springframework.webflow.core.collection.CollectionUtils;
024: import org.springframework.webflow.execution.FlowExecutionException;
025: import org.springframework.webflow.execution.ViewSelection;
026:
027: /**
028: * A typed set of state exception handlers, mainly for use internally by
029: * artifacts that can apply state exception handling logic.
030: *
031: * @see FlowExecutionExceptionHandler
032: * @see Flow#getExceptionHandlerSet()
033: * @see State#getExceptionHandlerSet()
034: *
035: * @author Keith Donald
036: */
037: public class FlowExecutionExceptionHandlerSet {
038:
039: /**
040: * The set of exception handlers.
041: */
042: private List exceptionHandlers = new LinkedList();
043:
044: /**
045: * Add a state exception handler to this set.
046: * @param exceptionHandler the exception handler to add
047: * @return true if this set's contents changed as a result of the add
048: * operation
049: */
050: public boolean add(FlowExecutionExceptionHandler exceptionHandler) {
051: if (contains(exceptionHandler)) {
052: return false;
053: }
054: return exceptionHandlers.add(exceptionHandler);
055: }
056:
057: /**
058: * Add a collection of state exception handler instances to this set.
059: * @param exceptionHandlers the exception handlers to add
060: * @return true if this set's contents changed as a result of the add
061: * operation
062: */
063: public boolean addAll(
064: FlowExecutionExceptionHandler[] exceptionHandlers) {
065: return CollectionUtils.addAllNoDuplicates(
066: this .exceptionHandlers, exceptionHandlers);
067: }
068:
069: /**
070: * Tests if this state exception handler is in this set.
071: * @param exceptionHandler the exception handler
072: * @return true if the state exception handler is contained in this set,
073: * false otherwise
074: */
075: public boolean contains(
076: FlowExecutionExceptionHandler exceptionHandler) {
077: return exceptionHandlers.contains(exceptionHandler);
078: }
079:
080: /**
081: * Remove the exception handler instance from this set.
082: * @param exceptionHandler the exception handler to add
083: * @return true if this set's contents changed as a result of the remove
084: * operation
085: */
086: public boolean remove(FlowExecutionExceptionHandler exceptionHandler) {
087: return exceptionHandlers.remove(exceptionHandler);
088: }
089:
090: /**
091: * Returns the size of this state exception handler set.
092: * @return the exception handler set size
093: */
094: public int size() {
095: return exceptionHandlers.size();
096: }
097:
098: /**
099: * Convert this list to a typed state exception handler array.
100: * @return the exception handler list, as a typed array
101: */
102: public FlowExecutionExceptionHandler[] toArray() {
103: return (FlowExecutionExceptionHandler[]) exceptionHandlers
104: .toArray(new FlowExecutionExceptionHandler[exceptionHandlers
105: .size()]);
106: }
107:
108: /**
109: * Handle an exception that occured during the context of the current flow
110: * execution request.
111: * <p>
112: * This implementation iterates over the ordered set of exception handler
113: * objects, delegating to each handler in the set until one handles the
114: * exception that occured and selects a non-null error view.
115: * @param exception the exception that occured
116: * @param context the flow execution control context
117: * @return the selected error view, or <code>null</code> if no handler
118: * matched or returned a non-null view selection
119: */
120: public ViewSelection handleException(
121: FlowExecutionException exception,
122: RequestControlContext context) {
123: Iterator it = exceptionHandlers.iterator();
124: while (it.hasNext()) {
125: FlowExecutionExceptionHandler handler = (FlowExecutionExceptionHandler) it
126: .next();
127: if (handler.handles(exception)) {
128: ViewSelection result = handler.handle(exception,
129: context);
130: if (result != null) {
131: return result;
132: }
133: // else continue with next handler
134: }
135: }
136: return null;
137: }
138:
139: public String toString() {
140: return StylerUtils.style(exceptionHandlers);
141: }
142: }
|