1 16 package org.springframework.webflow.config; 17 18 import java.util.Map ; 19 20 import org.springframework.beans.factory.FactoryBean; 21 import org.springframework.beans.factory.InitializingBean; 22 import org.springframework.util.Assert; 23 import org.springframework.webflow.conversation.ConversationManager; 24 import org.springframework.webflow.conversation.impl.SessionBindingConversationManager; 25 import org.springframework.webflow.core.collection.AttributeMap; 26 import org.springframework.webflow.core.collection.LocalAttributeMap; 27 import org.springframework.webflow.core.collection.MutableAttributeMap; 28 import org.springframework.webflow.definition.registry.FlowDefinitionLocator; 29 import org.springframework.webflow.definition.registry.FlowDefinitionRegistry; 30 import org.springframework.webflow.engine.impl.FlowExecutionImplFactory; 31 import org.springframework.webflow.engine.impl.FlowExecutionImplStateRestorer; 32 import org.springframework.webflow.execution.FlowExecutionFactory; 33 import org.springframework.webflow.execution.FlowExecutionListener; 34 import org.springframework.webflow.execution.factory.FlowExecutionListenerLoader; 35 import org.springframework.webflow.execution.factory.StaticFlowExecutionListenerLoader; 36 import org.springframework.webflow.execution.repository.FlowExecutionRepository; 37 import org.springframework.webflow.execution.repository.continuation.ClientContinuationFlowExecutionRepository; 38 import org.springframework.webflow.execution.repository.continuation.ContinuationFlowExecutionRepository; 39 import org.springframework.webflow.execution.repository.support.FlowExecutionStateRestorer; 40 import org.springframework.webflow.execution.repository.support.SimpleFlowExecutionRepository; 41 import org.springframework.webflow.executor.FlowExecutor; 42 import org.springframework.webflow.executor.FlowExecutorImpl; 43 44 64 public class FlowExecutorFactoryBean implements FactoryBean, InitializingBean { 65 66 70 private FlowDefinitionLocator definitionLocator; 71 72 75 private MutableAttributeMap executionAttributes; 76 77 80 private FlowExecutionListenerLoader executionListenerLoader; 81 82 86 private ConversationManager conversationManager = new SessionBindingConversationManager(); 87 88 92 private RepositoryType repositoryType; 93 94 97 private FlowExecutor flowExecutor; 98 99 102 private FlowSystemDefaults defaults = new FlowSystemDefaults(); 103 104 109 public void setDefinitionLocator(FlowDefinitionLocator definitionLocator) { 110 this.definitionLocator = definitionLocator; 111 } 112 113 123 public void setExecutionAttributes(Map executionAttributes) { 124 this.executionAttributes = new LocalAttributeMap(executionAttributes); 125 } 126 127 132 public void setExecutionListener(FlowExecutionListener executionListener) { 133 setExecutionListeners(new FlowExecutionListener[] { executionListener }); 134 } 135 136 141 public void setExecutionListeners(FlowExecutionListener[] executionListeners) { 142 setExecutionListenerLoader(new StaticFlowExecutionListenerLoader(executionListeners)); 143 } 144 145 151 public void setExecutionListenerLoader(FlowExecutionListenerLoader executionListenerLoader) { 152 this.executionListenerLoader = executionListenerLoader; 153 } 154 155 162 public void setRepositoryType(RepositoryType repositoryType) { 163 this.repositoryType = repositoryType; 164 } 165 166 173 public void setConversationManager(ConversationManager conversationManager) { 174 this.conversationManager = conversationManager; 175 } 176 177 181 public void setDefaults(FlowSystemDefaults defaults) { 182 this.defaults = defaults; 183 } 184 185 187 public void afterPropertiesSet() throws Exception { 188 Assert.notNull(definitionLocator, "The flow definition locator is required"); 189 190 executionAttributes = defaults.applyExecutionAttributes(executionAttributes); 192 repositoryType = defaults.applyIfNecessary(repositoryType); 193 194 197 FlowExecutionFactory executionFactory = 199 createFlowExecutionFactory(executionAttributes, executionListenerLoader); 200 201 FlowExecutionStateRestorer executionStateRestorer = 203 createFlowExecutionStateRestorer(definitionLocator, executionAttributes, executionListenerLoader); 204 205 FlowExecutionRepository executionRepository = 207 createExecutionRepository(repositoryType, executionStateRestorer, conversationManager); 208 209 flowExecutor = createFlowExecutor(definitionLocator, executionFactory, executionRepository); 211 } 212 213 215 224 protected FlowExecutionFactory createFlowExecutionFactory( 225 AttributeMap executionAttributes, FlowExecutionListenerLoader executionListenerLoader) { 226 FlowExecutionImplFactory executionFactory = new FlowExecutionImplFactory(); 227 executionFactory.setExecutionAttributes(executionAttributes); 228 if (executionListenerLoader != null) { 229 executionFactory.setExecutionListenerLoader(executionListenerLoader); 230 } 231 return executionFactory; 232 } 233 234 244 protected FlowExecutionStateRestorer createFlowExecutionStateRestorer( 245 FlowDefinitionLocator definitionLocator, AttributeMap executionAttributes, 246 FlowExecutionListenerLoader executionListenerLoader) { 247 FlowExecutionImplStateRestorer executionStateRestorer = new FlowExecutionImplStateRestorer(definitionLocator); 248 executionStateRestorer.setExecutionAttributes(executionAttributes); 249 if (executionListenerLoader != null) { 250 executionStateRestorer.setExecutionListenerLoader(executionListenerLoader); 251 } 252 return executionStateRestorer; 253 } 254 255 265 protected FlowExecutionRepository createExecutionRepository( 266 RepositoryType repositoryType, FlowExecutionStateRestorer executionStateRestorer, 267 ConversationManager conversationManager) { 268 if (repositoryType == RepositoryType.SIMPLE) { 269 return new SimpleFlowExecutionRepository(executionStateRestorer, conversationManager); 270 } 271 else if (repositoryType == RepositoryType.CONTINUATION) { 272 return new ContinuationFlowExecutionRepository(executionStateRestorer, conversationManager); 273 } 274 else if (repositoryType == RepositoryType.CLIENT) { 275 return new ClientContinuationFlowExecutionRepository(executionStateRestorer, conversationManager); 276 } 277 else if (repositoryType == RepositoryType.SINGLEKEY) { 278 SimpleFlowExecutionRepository repository = new SimpleFlowExecutionRepository(executionStateRestorer, 279 conversationManager); 280 repository.setAlwaysGenerateNewNextKey(false); 281 return repository; 282 } 283 else { 284 throw new IllegalStateException ("Cannot create execution repository - unsupported repository type " 285 + repositoryType); 286 } 287 } 288 289 298 protected FlowExecutor createFlowExecutor( 299 FlowDefinitionLocator definitionLocator, FlowExecutionFactory executionFactory, 300 FlowExecutionRepository executionRepository) { 301 return new FlowExecutorImpl(definitionLocator, executionFactory, executionRepository); 302 } 303 304 306 public Class getObjectType() { 307 return FlowExecutor.class; 308 } 309 310 public boolean isSingleton() { 311 return true; 312 } 313 314 public Object getObject() throws Exception { 315 return flowExecutor; 316 } 317 } | Popular Tags |