KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > springframework > webflow > engine > impl > RequestControlContextImpl


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.impl;
17
18 import org.apache.commons.logging.Log;
19 import org.apache.commons.logging.LogFactory;
20 import org.springframework.core.style.ToStringCreator;
21 import org.springframework.util.Assert;
22 import org.springframework.webflow.context.ExternalContext;
23 import org.springframework.webflow.core.collection.AttributeMap;
24 import org.springframework.webflow.core.collection.CollectionUtils;
25 import org.springframework.webflow.core.collection.LocalAttributeMap;
26 import org.springframework.webflow.core.collection.MutableAttributeMap;
27 import org.springframework.webflow.core.collection.ParameterMap;
28 import org.springframework.webflow.definition.FlowDefinition;
29 import org.springframework.webflow.definition.StateDefinition;
30 import org.springframework.webflow.definition.TransitionDefinition;
31 import org.springframework.webflow.engine.Flow;
32 import org.springframework.webflow.engine.RequestControlContext;
33 import org.springframework.webflow.engine.State;
34 import org.springframework.webflow.engine.Transition;
35 import org.springframework.webflow.execution.Event;
36 import org.springframework.webflow.execution.FlowExecutionContext;
37 import org.springframework.webflow.execution.FlowExecutionException;
38 import org.springframework.webflow.execution.FlowSession;
39 import org.springframework.webflow.execution.FlowSessionStatus;
40 import org.springframework.webflow.execution.ViewSelection;
41
42 /**
43  * Default request control context implementation used internally by the web
44  * flow system. This class is closely coupled with
45  * <code>FlowExecutionImpl</code> and <code>FlowSessionImpl</code>. The
46  * three classes work together to form a complete flow execution implementation
47  * based on a finite state machine.
48  *
49  * @see org.springframework.webflow.engine.machine.FlowExecutionImpl
50  * @see org.springframework.webflow.engine.machine.FlowSessionImpl
51  *
52  * @author Keith Donald
53  * @author Erwin Vervaet
54  */

55 class RequestControlContextImpl implements RequestControlContext {
56
57     private static final Log logger = LogFactory.getLog(RequestControlContextImpl.class);
58
59     /**
60      * The owning flow execution.
61      */

62     private FlowExecutionImpl flowExecution;
63
64     /**
65      * The request scope data map.
66      */

67     private LocalAttributeMap requestScope = new LocalAttributeMap();
68
69     /**
70      * A source context for the caller who initiated this request.
71      */

72     private ExternalContext externalContext;
73
74     /**
75      * The last event that occured in this request context.
76      */

77     private Event lastEvent;
78
79     /**
80      * The last transition that executed in this request context.
81      */

82     private Transition lastTransition;
83
84     /**
85      * Holder for contextual execution properties.
86      */

87     private AttributeMap attributes;
88
89     /**
90      * Create a new request context.
91      * @param flowExecution the owning flow execution
92      * @param externalContext the external context that originated the flow
93      * execution request
94      */

95     public RequestControlContextImpl(FlowExecutionImpl flowExecution, ExternalContext externalContext) {
96         Assert.notNull(flowExecution, "The owning flow execution is required");
97         this.externalContext = externalContext;
98         this.flowExecution = flowExecution;
99     }
100
101     // implementing RequestContext
102

103     public FlowDefinition getActiveFlow() {
104         return flowExecution.getActiveSession().getDefinition();
105     }
106
107     public StateDefinition getCurrentState() {
108         return flowExecution.getActiveSession().getState();
109     }
110
111     public MutableAttributeMap getRequestScope() {
112         return requestScope;
113     }
114
115     public MutableAttributeMap getFlashScope() {
116         return flowExecution.getActiveSession().getFlashMap();
117     }
118
119     public MutableAttributeMap getFlowScope() {
120         return flowExecution.getActiveSession().getScope();
121     }
122
123     public MutableAttributeMap getConversationScope() {
124         return flowExecution.getConversationScope();
125     }
126
127     public ParameterMap getRequestParameters() {
128         return externalContext.getRequestParameterMap();
129     }
130
131     public ExternalContext getExternalContext() {
132         return externalContext;
133     }
134
135     public FlowExecutionContext getFlowExecutionContext() {
136         return flowExecution;
137     }
138
139     public Event getLastEvent() {
140         return lastEvent;
141     }
142
143     public TransitionDefinition getLastTransition() {
144         return lastTransition;
145     }
146
147     public AttributeMap getAttributes() {
148         return attributes;
149     }
150
151     public void setAttributes(AttributeMap attributes) {
152         if (attributes == null) {
153             this.attributes = CollectionUtils.EMPTY_ATTRIBUTE_MAP;
154         }
155         else {
156             this.attributes = attributes;
157         }
158     }
159
160     public AttributeMap getModel() {
161         return getConversationScope().union(getFlowScope()).union(getFlashScope()).union(getRequestScope());
162     }
163
164     // implementing RequestControlContext
165

166     public void setLastEvent(Event lastEvent) {
167         this.lastEvent = lastEvent;
168     }
169
170     public void setLastTransition(Transition lastTransition) {
171         this.lastTransition = lastTransition;
172     }
173
174     public void setCurrentState(State state) {
175         getExecutionListeners().fireStateEntering(this, state);
176         State previousState = getCurrentStateInternal();
177         flowExecution.setCurrentState(state);
178         if (previousState == null) {
179             getActiveSession().setStatus(FlowSessionStatus.ACTIVE);
180         }
181         getExecutionListeners().fireStateEntered(this, previousState);
182     }
183
184     public ViewSelection start(Flow flow, MutableAttributeMap input) throws FlowExecutionException {
185         if (input == null) {
186             // create a mutable map so entries can be added by listeners!
187
input = new LocalAttributeMap();
188         }
189         if (logger.isDebugEnabled()) {
190             logger.debug("Activating new session for flow '" + flow.getId() + "' in state '"
191                     + flow.getStartState().getId() + "' with input " + input);
192         }
193         getExecutionListeners().fireSessionStarting(this, flow, input);
194         FlowSession session = flowExecution.activateSession(flow);
195         ViewSelection selectedView = flow.start(this, input);
196         getExecutionListeners().fireSessionStarted(this, session);
197         return selectedView;
198     }
199
200     public ViewSelection signalEvent(Event event) throws FlowExecutionException {
201         if (logger.isDebugEnabled()) {
202             logger.debug("Signaling event '" + event.getId() + "' in state '" + getCurrentState().getId()
203                     + "' of flow '" + getActiveFlow().getId() + "'");
204         }
205         setLastEvent(event);
206         getExecutionListeners().fireEventSignaled(this, event);
207         ViewSelection selectedView = getActiveFlowInternal().onEvent(this);
208         return selectedView;
209     }
210
211     public FlowSession endActiveFlowSession(MutableAttributeMap output) throws IllegalStateException JavaDoc {
212         FlowSession session = getFlowExecutionContext().getActiveSession();
213         getExecutionListeners().fireSessionEnding(this, session, output);
214         getActiveFlowInternal().end(this, output);
215         if (logger.isDebugEnabled()) {
216             logger.debug("Ending active session " + session + "; exposed session output is " + output);
217         }
218         session = flowExecution.endActiveFlowSession();
219         getExecutionListeners().fireSessionEnded(this, session, output);
220         return session;
221     }
222
223     public ViewSelection execute(Transition transition) {
224         return transition.execute(getCurrentStateInternal(), this);
225     }
226
227     // internal helpers
228

229     /**
230      * Returns the execution listerns for the flow execution of this request
231      * context.
232      */

233     protected FlowExecutionListeners getExecutionListeners() {
234         return flowExecution.getListeners();
235     }
236
237     /**
238      * Returns the active flow in the flow execution of this request context.
239      */

240     protected Flow getActiveFlowInternal() {
241         return (Flow)getActiveSession().getDefinition();
242     }
243
244     /**
245      * Returns the current state in the flow execution of this request context.
246      */

247     protected State getCurrentStateInternal() {
248         return (State)getActiveSession().getState();
249     }
250
251     /**
252      * Returns the active flow session in the flow execution of this request
253      * context.
254      */

255     protected FlowSessionImpl getActiveSession() {
256         return flowExecution.getActiveSessionInternal();
257     }
258
259     public String JavaDoc toString() {
260         return new ToStringCreator(this).append("externalContext", externalContext)
261                 .append("requestScope", requestScope).append("attributes", attributes).append("flowExecution",
262                         flowExecution).toString();
263     }
264 }
Popular Tags