1 16 package org.springframework.webflow.engine; 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.definition.FlowDefinition; 23 import org.springframework.webflow.definition.StateDefinition; 24 import org.springframework.webflow.execution.FlowExecutionException; 25 import org.springframework.webflow.execution.ViewSelection; 26 27 53 public abstract class State extends AnnotatedObject implements StateDefinition { 54 55 58 protected final Log logger = LogFactory.getLog(getClass()); 59 60 63 private Flow flow; 64 65 68 private String id; 69 70 73 private ActionList entryActionList = new ActionList(); 74 75 78 private FlowExecutionExceptionHandlerSet exceptionHandlerSet = new FlowExecutionExceptionHandlerSet(); 79 80 91 protected State(Flow flow, String id) throws IllegalArgumentException { 92 setId(id); 93 setFlow(flow); 94 } 95 96 98 public FlowDefinition getOwner() { 99 return flow; 100 } 101 102 public String getId() { 103 return id; 104 } 105 106 108 111 public Flow getFlow() { 112 return flow; 113 } 114 115 120 private void setFlow(Flow flow) throws IllegalArgumentException { 121 Assert.hasText(getId(), "The id of the state should be set before adding the state to a flow"); 122 Assert.notNull(flow, "The owning flow is required"); 123 this.flow = flow; 124 flow.add(this); 125 } 126 127 131 private void setId(String id) { 132 Assert.hasText(id, "This state must have a valid identifier"); 133 this.id = id; 134 } 135 136 141 public ActionList getEntryActionList() { 142 return entryActionList; 143 } 144 145 154 public FlowExecutionExceptionHandlerSet getExceptionHandlerSet() { 155 return exceptionHandlerSet; 156 } 157 158 163 public boolean isStartState() { 164 return flow.getStartState() == this; 165 } 166 167 169 public boolean equals(Object o) { 170 if (!(o instanceof State)) { 171 return false; 172 } 173 State other = (State)o; 174 return id.equals(other.id) && flow.equals(other.flow); 175 } 176 177 public int hashCode() { 178 return id.hashCode() + flow.hashCode(); 179 } 180 181 183 194 public final ViewSelection enter(RequestControlContext context) throws FlowExecutionException { 195 if (logger.isDebugEnabled()) { 196 logger.debug("Entering state '" + getId() + "' of flow '" + getFlow().getId() + "'"); 197 } 198 context.setCurrentState(this); 199 entryActionList.execute(context); 200 return doEnter(context); 201 } 202 203 213 protected abstract ViewSelection doEnter(RequestControlContext context) throws FlowExecutionException; 214 215 223 public ViewSelection handleException(FlowExecutionException exception, RequestControlContext context) { 224 return getExceptionHandlerSet().handleException(exception, context); 225 } 226 227 public String toString() { 228 String flowName = (flow == null ? "<not set>" : flow.getId()); 229 ToStringCreator creator = new ToStringCreator(this).append("id", getId()).append("flow", flowName).append( 230 "entryActionList", entryActionList).append("exceptionHandlerSet", exceptionHandlerSet); 231 appendToString(creator); 232 return creator.toString(); 233 } 234 235 240 protected void appendToString(ToStringCreator creator) { 241 } 242 } | Popular Tags |