1 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 55 class RequestControlContextImpl implements RequestControlContext { 56 57 private static final Log logger = LogFactory.getLog(RequestControlContextImpl.class); 58 59 62 private FlowExecutionImpl flowExecution; 63 64 67 private LocalAttributeMap requestScope = new LocalAttributeMap(); 68 69 72 private ExternalContext externalContext; 73 74 77 private Event lastEvent; 78 79 82 private Transition lastTransition; 83 84 87 private AttributeMap attributes; 88 89 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 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 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 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 { 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 229 233 protected FlowExecutionListeners getExecutionListeners() { 234 return flowExecution.getListeners(); 235 } 236 237 240 protected Flow getActiveFlowInternal() { 241 return (Flow)getActiveSession().getDefinition(); 242 } 243 244 247 protected State getCurrentStateInternal() { 248 return (State)getActiveSession().getState(); 249 } 250 251 255 protected FlowSessionImpl getActiveSession() { 256 return flowExecution.getActiveSessionInternal(); 257 } 258 259 public String 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 |