KickJava   Java API By Example, From Geeks To Geeks.

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


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 java.io.Externalizable JavaDoc;
19 import java.io.IOException JavaDoc;
20 import java.io.ObjectInput JavaDoc;
21 import java.io.ObjectOutput JavaDoc;
22
23 import org.springframework.core.style.ToStringCreator;
24 import org.springframework.util.Assert;
25 import org.springframework.webflow.core.collection.LocalAttributeMap;
26 import org.springframework.webflow.core.collection.MutableAttributeMap;
27 import org.springframework.webflow.definition.FlowDefinition;
28 import org.springframework.webflow.definition.StateDefinition;
29 import org.springframework.webflow.engine.Flow;
30 import org.springframework.webflow.engine.State;
31 import org.springframework.webflow.execution.FlowSession;
32 import org.springframework.webflow.execution.FlowSessionStatus;
33
34 /**
35  * Implementation of the FlowSession interfaced used internally by the
36  * <code>FlowExecutionImpl</code>. This class is closely coupled with
37  * <code>FlowExecutionImpl</code> and <code>RequestControlContextImpl</code>.
38  * The three classes work together to form a complete flow execution
39  * implementation.
40  *
41  * @author Keith Donald
42  * @author Erwin Vervaet
43  * @author Ben Hale
44  */

45 class FlowSessionImpl implements FlowSession, Externalizable JavaDoc {
46
47     /**
48      * The flow definition (a singleton).
49      * <p>
50      * Transient to support restoration by the
51      * {@link FlowExecutionImplStateRestorer}.
52      */

53     private transient Flow flow;
54
55     /**
56      * Set so the transient {@link #flow} field can be restored by the
57      * {@link FlowExecutionImplStateRestorer}.
58      */

59     private String JavaDoc flowId;
60
61     /**
62      * The current state of this flow session.
63      * <p>
64      * Transient to support restoration by the
65      * {@link FlowExecutionImplStateRestorer}.
66      */

67     private transient State state;
68
69     /**
70      * Set so the transient {@link #state} field can be restored by the
71      * {@link FlowExecutionImplStateRestorer}.
72      */

73     private String JavaDoc stateId;
74
75     /**
76      * The session status; may be CREATED, STARTING, ACTIVE, PAUSED, SUSPENDED,
77      * or ENDED.
78      */

79     private FlowSessionStatus status = FlowSessionStatus.CREATED;
80
81     /**
82      * The session data model ("flow scope").
83      */

84     private MutableAttributeMap scope = new LocalAttributeMap();
85
86     /**
87      * The flash map ("flash scope").
88      */

89     private MutableAttributeMap flashMap = new LocalAttributeMap();
90
91     /**
92      * The parent session of this session (may be <code>null</code> if this is
93      * a root session.)
94      */

95     private FlowSessionImpl parent;
96
97     /**
98      * Default constructor required for externalizable serialization. Should NOT
99      * be called programmatically.
100      */

101     public FlowSessionImpl() {
102     }
103
104     /**
105      * Create a new flow session.
106      * @param flow the flow definition associated with this flow session
107      * @param parent this session's parent (may be null)
108      */

109     public FlowSessionImpl(Flow flow, FlowSessionImpl parent) {
110         setFlow(flow);
111         this.parent = parent;
112     }
113
114     // implementing FlowSession
115

116     public FlowDefinition getDefinition() {
117         return flow;
118     }
119
120     public StateDefinition getState() {
121         return state;
122     }
123
124     public FlowSessionStatus getStatus() {
125         return status;
126     }
127
128     public MutableAttributeMap getScope() {
129         return scope;
130     }
131
132     public MutableAttributeMap getFlashMap() {
133         return flashMap;
134     }
135
136     public FlowSession getParent() {
137         return parent;
138     }
139
140     public boolean isRoot() {
141         return parent == null;
142     }
143
144     // custom serialization
145

146     public void readExternal(ObjectInput JavaDoc in) throws IOException JavaDoc, ClassNotFoundException JavaDoc {
147         flowId = (String JavaDoc)in.readObject();
148         stateId = (String JavaDoc)in.readObject();
149         status = (FlowSessionStatus)in.readObject();
150         scope = (MutableAttributeMap)in.readObject();
151         flashMap = (MutableAttributeMap)in.readObject();
152         parent = (FlowSessionImpl)in.readObject();
153     }
154
155     public void writeExternal(ObjectOutput JavaDoc out) throws IOException JavaDoc {
156         out.writeObject(flowId);
157         out.writeObject(stateId);
158         out.writeObject(status);
159         out.writeObject(scope);
160         out.writeObject(flashMap);
161         out.writeObject(parent);
162     }
163
164     // package private setters for setting/updating internal state
165
// used by FlowExecutionImplStateRestorer
166

167     /**
168      * Restores the definition of this flow session.
169      * @param flow the flow sessions definition
170      * @see FlowExecutionImplStateRestorer
171      */

172     void setFlow(Flow flow) {
173         Assert.notNull(flow, "The flow is required");
174         this.flow = flow;
175         this.flowId = flow.getId();
176     }
177
178     /**
179      * Set the current state of this flow session.
180      * @param state the state that is currently active in this flow session
181      * @see FlowExecutionImpl#setCurrentState(State)
182      * @see FlowExecutionImplStateRestorer
183      */

184     void setState(State state) {
185         Assert.notNull(state, "The state is required");
186         Assert.isTrue(flow == state.getOwner(),
187                 "The state does not belong to the flow associated with this flow session");
188         this.state = state;
189         this.stateId = state.getId();
190     }
191
192     /**
193      * Set the status of this flow session.
194      * @param status the new status to set
195      */

196     void setStatus(FlowSessionStatus status) {
197         Assert.notNull(status, "The flow session status is requred");
198         this.status = status;
199     }
200
201     /**
202      * Returns the id of the flow of this session.
203      */

204     String JavaDoc getFlowId() {
205         return flowId;
206     }
207
208     /**
209      * Returns the id of the current state of this session.
210      */

211     String JavaDoc getStateId() {
212         return stateId;
213     }
214     
215     public String JavaDoc toString() {
216         return new ToStringCreator(this).append("flow", flowId).append("state", stateId).append("scope", scope).append(
217                 "flashMap", flashMap).append("status", status).toString();
218     }
219 }
Popular Tags