1 16 package org.springframework.webflow.executor; 17 18 import org.springframework.binding.mapping.AttributeMapper; 19 import org.springframework.util.Assert; 20 import org.springframework.webflow.context.ExternalContext; 21 import org.springframework.webflow.context.ExternalContextHolder; 22 import org.springframework.webflow.core.FlowException; 23 import org.springframework.webflow.core.collection.LocalAttributeMap; 24 import org.springframework.webflow.core.collection.MutableAttributeMap; 25 import org.springframework.webflow.definition.FlowDefinition; 26 import org.springframework.webflow.definition.registry.FlowDefinitionLocator; 27 import org.springframework.webflow.execution.FlowExecution; 28 import org.springframework.webflow.execution.FlowExecutionFactory; 29 import org.springframework.webflow.execution.ViewSelection; 30 import org.springframework.webflow.execution.repository.FlowExecutionKey; 31 import org.springframework.webflow.execution.repository.FlowExecutionLock; 32 import org.springframework.webflow.execution.repository.FlowExecutionRepository; 33 34 93 public class FlowExecutorImpl implements FlowExecutor { 94 95 98 private FlowDefinitionLocator definitionLocator; 99 100 103 private FlowExecutionFactory executionFactory; 104 105 109 private FlowExecutionRepository executionRepository; 110 111 124 private AttributeMapper inputMapper = new RequestParameterInputMapper(); 125 126 135 public FlowExecutorImpl(FlowDefinitionLocator definitionLocator, FlowExecutionFactory executionFactory, 136 FlowExecutionRepository executionRepository) { 137 Assert.notNull(definitionLocator, "The locator for accessing flow definitions is required"); 138 Assert.notNull(executionFactory, "The execution factory for creating new flow executions is required"); 139 Assert.notNull(executionRepository, "The repository for persisting flow executions is required"); 140 this.definitionLocator = definitionLocator; 141 this.executionFactory = executionFactory; 142 this.executionRepository = executionRepository; 143 } 144 145 150 public AttributeMapper getInputMapper() { 151 return inputMapper; 152 } 153 154 163 public void setInputMapper(AttributeMapper inputMapper) { 164 this.inputMapper = inputMapper; 165 } 166 167 172 public FlowDefinitionLocator getDefinitionLocator() { 173 return definitionLocator; 174 } 175 176 181 public FlowExecutionFactory getExecutionFactory() { 182 return executionFactory; 183 } 184 185 189 public FlowExecutionRepository getExecutionRepository() { 190 return executionRepository; 191 } 192 193 public ResponseInstruction launch(String flowDefinitionId, ExternalContext context) throws FlowException { 194 ExternalContextHolder.setExternalContext(context); 196 try { 197 FlowDefinition flowDefinition = definitionLocator.getFlowDefinition(flowDefinitionId); 198 FlowExecution flowExecution = executionFactory.createFlowExecution(flowDefinition); 199 ViewSelection selectedView = flowExecution.start(createInput(context), context); 200 if (flowExecution.isActive()) { 201 FlowExecutionKey key = executionRepository.generateKey(flowExecution); 203 executionRepository.putFlowExecution(key, flowExecution); 204 return new ResponseInstruction(key.toString(), flowExecution, selectedView); 205 } 206 else { 207 return new ResponseInstruction(flowExecution, selectedView); 209 } 210 } 211 finally { 212 ExternalContextHolder.setExternalContext(null); 213 } 214 } 215 216 public ResponseInstruction resume(String flowExecutionKey, String eventId, ExternalContext context) 217 throws FlowException { 218 ExternalContextHolder.setExternalContext(context); 220 try { 221 FlowExecutionKey key = executionRepository.parseFlowExecutionKey(flowExecutionKey); 222 FlowExecutionLock lock = executionRepository.getLock(key); 223 lock.lock(); 225 try { 226 FlowExecution flowExecution = executionRepository.getFlowExecution(key); 227 ViewSelection selectedView = flowExecution.signalEvent(eventId, context); 228 if (flowExecution.isActive()) { 229 key = executionRepository.getNextKey(flowExecution, key); 231 executionRepository.putFlowExecution(key, flowExecution); 232 return new ResponseInstruction(key.toString(), flowExecution, selectedView); 233 } 234 else { 235 executionRepository.removeFlowExecution(key); 237 return new ResponseInstruction(flowExecution, selectedView); 238 } 239 } 240 finally { 241 lock.unlock(); 242 } 243 } 244 finally { 245 ExternalContextHolder.setExternalContext(null); 246 } 247 } 248 249 public ResponseInstruction refresh(String flowExecutionKey, ExternalContext context) throws FlowException { 250 ExternalContextHolder.setExternalContext(context); 252 try { 253 FlowExecutionKey key = executionRepository.parseFlowExecutionKey(flowExecutionKey); 254 FlowExecutionLock lock = executionRepository.getLock(key); 255 lock.lock(); 257 try { 258 FlowExecution flowExecution = executionRepository.getFlowExecution(key); 259 ViewSelection selectedView = flowExecution.refresh(context); 260 executionRepository.putFlowExecution(key, flowExecution); 263 return new ResponseInstruction(key.toString(), flowExecution, selectedView); 264 } 265 finally { 266 lock.unlock(); 267 } 268 } 269 finally { 270 ExternalContextHolder.setExternalContext(null); 271 } 272 } 273 274 276 283 protected MutableAttributeMap createInput(ExternalContext context) { 284 if (inputMapper != null) { 285 MutableAttributeMap inputMap = new LocalAttributeMap(); 286 inputMapper.map(context, inputMap, null); 287 return inputMap; 288 } 289 else { 290 return null; 291 } 292 } 293 } | Popular Tags |