KickJava   Java API By Example, From Geeks To Geeks.

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


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.util.ListIterator JavaDoc;
19 import java.util.Map JavaDoc;
20
21 import org.springframework.util.Assert;
22 import org.springframework.webflow.core.collection.AttributeMap;
23 import org.springframework.webflow.core.collection.CollectionUtils;
24 import org.springframework.webflow.core.collection.LocalAttributeMap;
25 import org.springframework.webflow.core.collection.MutableAttributeMap;
26 import org.springframework.webflow.definition.registry.FlowDefinitionLocator;
27 import org.springframework.webflow.engine.Flow;
28 import org.springframework.webflow.execution.FlowExecution;
29 import org.springframework.webflow.execution.factory.FlowExecutionListenerLoader;
30 import org.springframework.webflow.execution.factory.StaticFlowExecutionListenerLoader;
31 import org.springframework.webflow.execution.repository.support.FlowExecutionStateRestorer;
32
33 /**
34  * Restores the transient state of deserialized {@link FlowExecutionImpl}
35  * objects.
36  *
37  * @author Keith Donald
38  */

39 public class FlowExecutionImplStateRestorer implements FlowExecutionStateRestorer {
40
41     /**
42      * Used to restore the flow execution's flow definition.
43      */

44     private FlowDefinitionLocator definitionLocator;
45
46     /**
47      * Used to restore the flow execution's listeners.
48      */

49     private FlowExecutionListenerLoader executionListenerLoader = StaticFlowExecutionListenerLoader.EMPTY_INSTANCE;
50
51     /**
52      * Used to restore the flow execution's system attributes.
53      */

54     private AttributeMap executionAttributes = CollectionUtils.EMPTY_ATTRIBUTE_MAP;
55
56     /**
57      * Creates a new execution transient state restorer.
58      * @param definitionLocator the flow definition locator
59      */

60     public FlowExecutionImplStateRestorer(FlowDefinitionLocator definitionLocator) {
61         Assert.notNull(definitionLocator, "The flow definition locator is required");
62         this.definitionLocator = definitionLocator;
63     }
64
65     /**
66      * Sets the attributes to apply to restored flow executions.
67      * Execution attributes may affect flow execution behavior.
68      * @param executionAttributes flow execution system attributes
69      */

70     public void setExecutionAttributes(AttributeMap executionAttributes) {
71         Assert.notNull(executionAttributes, "The execution attributes map is required");
72         this.executionAttributes = executionAttributes;
73     }
74
75     /**
76      * Sets the attributes to apply to restored flow executions.
77      * Execution attributes may affect flow execution behavior.
78      * <p>
79      * Convenience setter that takes a simple <code>java.util.Map</code> to ease
80      * bean style configuration.
81      * @param executionAttributes flow execution system attributes
82      */

83     public void setExecutionAttributesMap(Map JavaDoc executionAttributes) {
84         Assert.notNull(executionAttributes, "The execution attributes map is required");
85         this.executionAttributes = new LocalAttributeMap(executionAttributes);
86     }
87     
88     /**
89      * Sets the strategy for loading listeners that should observe executions of
90      * a flow definition. Allows full control over what listeners should apply.
91      * for executions of a flow definition.
92      */

93     public void setExecutionListenerLoader(FlowExecutionListenerLoader executionListenerLoader) {
94         Assert.notNull(executionListenerLoader, "The listener loader is required");
95         this.executionListenerLoader = executionListenerLoader;
96     }
97
98     public FlowExecution restoreState(FlowExecution flowExecution, MutableAttributeMap conversationScope) {
99         FlowExecutionImpl impl = (FlowExecutionImpl)flowExecution;
100         // the root flow should be a top-level flow visible by the flow def locator
101
Flow flow = (Flow)definitionLocator.getFlowDefinition(impl.getFlowId());
102         impl.setFlow(flow);
103         if (impl.hasSessions()) {
104             FlowSessionImpl root = impl.getRootSession();
105             root.setFlow(flow);
106             root.setState(flow.getStateInstance(root.getStateId()));
107             if (impl.hasSubflowSessions()) {
108                 Flow parent = flow;
109                 for (ListIterator JavaDoc it = impl.getSubflowSessionIterator(); it.hasNext();) {
110                     FlowSessionImpl subflow = (FlowSessionImpl)it.next();
111                     Flow definition;
112                     if (parent.containsInlineFlow(subflow.getFlowId())) {
113                         // subflow is an inline flow of it's parent
114
definition = parent.getInlineFlow(subflow.getFlowId());
115                     } else {
116                         // subflow is a top-level flow
117
definition = (Flow)definitionLocator.getFlowDefinition(subflow.getFlowId());
118                     }
119                     subflow.setFlow(definition);
120                     subflow.setState(definition.getStateInstance(subflow.getStateId()));
121                     parent = definition;
122                 }
123             }
124         }
125         if (conversationScope == null) {
126             conversationScope = new LocalAttributeMap();
127         }
128         impl.setConversationScope(conversationScope);
129         impl.setListeners(new FlowExecutionListeners(executionListenerLoader.getListeners(flow)));
130         impl.setAttributes(executionAttributes);
131         return flowExecution;
132     }
133 }
Popular Tags