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.impl;
017:
018: import org.springframework.webflow.core.collection.AttributeMap;
019: import org.springframework.webflow.core.collection.MutableAttributeMap;
020: import org.springframework.webflow.definition.FlowDefinition;
021: import org.springframework.webflow.definition.StateDefinition;
022: import org.springframework.webflow.execution.Event;
023: import org.springframework.webflow.execution.FlowExecutionException;
024: import org.springframework.webflow.execution.FlowExecutionListener;
025: import org.springframework.webflow.execution.FlowSession;
026: import org.springframework.webflow.execution.RequestContext;
027: import org.springframework.webflow.execution.ViewSelection;
028:
029: /**
030: * A helper that aids in publishing events to an array of
031: * <code>FlowExecutionListener</code> objects.
032: *
033: * @see org.springframework.webflow.execution.FlowExecutionListener
034: *
035: * @author Keith Donald
036: * @author Erwin Vervaet
037: */
038: class FlowExecutionListeners {
039:
040: /**
041: * The list of listeners that should receive event callbacks during managed
042: * flow executions.
043: */
044: private FlowExecutionListener[] listeners;
045:
046: /**
047: * Create a flow execution listener helper that wraps an empty listener
048: * array.
049: */
050: public FlowExecutionListeners() {
051: this (null);
052: }
053:
054: /**
055: * Create a flow execution listener helper that wraps the specified listener
056: * array.
057: * @param listeners the listener array
058: */
059: public FlowExecutionListeners(FlowExecutionListener[] listeners) {
060: if (listeners != null) {
061: this .listeners = listeners;
062: } else {
063: this .listeners = new FlowExecutionListener[0];
064: }
065: }
066:
067: /**
068: * Returns the wrapped listener array.
069: * @return the listener array
070: */
071: public FlowExecutionListener[] getArray() {
072: return listeners;
073: }
074:
075: /**
076: * Returns the size of the listener array.
077: */
078: public int size() {
079: return listeners.length;
080: }
081:
082: // methods to fire events to all listeners
083:
084: /**
085: * Notify all interested listeners that a request was submitted to the flow
086: * execution.
087: */
088: public void fireRequestSubmitted(RequestContext context) {
089: for (int i = 0; i < listeners.length; i++) {
090: listeners[i].requestSubmitted(context);
091: }
092: }
093:
094: /**
095: * Notify all interested listeners that the flow execution finished
096: * processing a request.
097: */
098: public void fireRequestProcessed(RequestContext context) {
099: for (int i = 0; i < listeners.length; i++) {
100: listeners[i].requestProcessed(context);
101: }
102: }
103:
104: /**
105: * Notify all interested listeners that a flow execution session is starting
106: * (about to be created).
107: */
108: public void fireSessionStarting(RequestContext context,
109: FlowDefinition flow, MutableAttributeMap input) {
110: for (int i = 0; i < listeners.length; i++) {
111: listeners[i].sessionStarting(context, flow, input);
112: }
113: }
114:
115: /**
116: * Notify all interested listeners that a flow execution session has been
117: * activated (created, on the stack and about to start).
118: */
119: public void fireSessionCreated(RequestContext context,
120: FlowSession session) {
121: for (int i = 0; i < listeners.length; i++) {
122: listeners[i].sessionCreated(context, session);
123: }
124: }
125:
126: /**
127: * Notify all interested listeners that a flow execution session has
128: * started (has entered its start state).
129: */
130: public void fireSessionStarted(RequestContext context,
131: FlowSession session) {
132: for (int i = 0; i < listeners.length; i++) {
133: listeners[i].sessionStarted(context, session);
134: }
135: }
136:
137: /**
138: * Notify all interested listeners that an event was signaled in the flow
139: * execution.
140: */
141: public void fireEventSignaled(RequestContext context, Event event) {
142: for (int i = 0; i < listeners.length; i++) {
143: listeners[i].eventSignaled(context, event);
144: }
145: }
146:
147: /**
148: * Notify all interested listeners that a state is being entered in the flow
149: * execution.
150: */
151: public void fireStateEntering(RequestContext context,
152: StateDefinition nextState) {
153: for (int i = 0; i < listeners.length; i++) {
154: listeners[i].stateEntering(context, nextState);
155: }
156: }
157:
158: /**
159: * Notify all interested listeners that a state was entered in the flow
160: * execution.
161: */
162: public void fireStateEntered(RequestContext context,
163: StateDefinition previousState) {
164: for (int i = 0; i < listeners.length; i++) {
165: listeners[i].stateEntered(context, previousState, context
166: .getCurrentState());
167: }
168: }
169:
170: /**
171: * Notify all interested listeners that a flow session was paused in the
172: * flow execution.
173: */
174: public void firePaused(RequestContext context,
175: ViewSelection selectedView) {
176: for (int i = 0; i < listeners.length; i++) {
177: listeners[i].paused(context, selectedView);
178: }
179: }
180:
181: /**
182: * Notify all interested listeners that the flow execution was resumed.
183: */
184: public void fireResumed(RequestContext context) {
185: for (int i = 0; i < listeners.length; i++) {
186: listeners[i].resumed(context);
187: }
188: }
189:
190: /**
191: * Notify all interested listeners that the active flow execution session is
192: * ending.
193: */
194: public void fireSessionEnding(RequestContext context,
195: FlowSession session, MutableAttributeMap output) {
196: for (int i = 0; i < listeners.length; i++) {
197: listeners[i].sessionEnding(context, session, output);
198: }
199: }
200:
201: /**
202: * Notify all interested listeners that a flow execution session has ended.
203: */
204: public void fireSessionEnded(RequestContext context,
205: FlowSession session, AttributeMap output) {
206: for (int i = 0; i < listeners.length; i++) {
207: listeners[i].sessionEnded(context, session, output);
208: }
209: }
210:
211: /**
212: * Notify all interested listeners that a flow execution threw an exception.
213: */
214: public void fireExceptionThrown(RequestContext context,
215: FlowExecutionException exception) {
216: for (int i = 0; i < listeners.length; i++) {
217: listeners[i].exceptionThrown(context, exception);
218: }
219: }
220: }
|