1 16 package org.springframework.webflow.execution.repository.support; 17 18 import java.io.Serializable ; 19 20 import org.springframework.util.Assert; 21 import org.springframework.webflow.conversation.ConversationManager; 22 import org.springframework.webflow.execution.FlowExecution; 23 import org.springframework.webflow.execution.repository.FlowExecutionKey; 24 import org.springframework.webflow.execution.repository.PermissionDeniedFlowExecutionAccessException; 25 import org.springframework.webflow.util.RandomGuidUidGenerator; 26 import org.springframework.webflow.util.UidGenerator; 27 28 51 public class SimpleFlowExecutionRepository extends AbstractConversationFlowExecutionRepository { 52 53 56 private static final String FLOW_EXECUTION_ENTRY_ATTRIBUTE = "flowExecutionEntry"; 57 58 62 private boolean alwaysGenerateNewNextKey = true; 63 64 67 private UidGenerator continuationIdGenerator = new RandomGuidUidGenerator(); 68 69 74 public SimpleFlowExecutionRepository(FlowExecutionStateRestorer executionStateRestorer, 75 ConversationManager conversationManager) { 76 super(executionStateRestorer, conversationManager); 77 } 78 79 83 public boolean isAlwaysGenerateNewNextKey() { 84 return alwaysGenerateNewNextKey; 85 } 86 87 92 public void setAlwaysGenerateNewNextKey(boolean alwaysGenerateNewNextKey) { 93 this.alwaysGenerateNewNextKey = alwaysGenerateNewNextKey; 94 } 95 96 100 public UidGenerator getContinuationIdGenerator() { 101 return continuationIdGenerator; 102 } 103 104 108 public void setContinuationIdGenerator(UidGenerator continuationIdGenerator) { 109 Assert.notNull(continuationIdGenerator, "The continuation id generator is required"); 110 this.continuationIdGenerator = continuationIdGenerator; 111 } 112 113 public FlowExecutionKey getNextKey(FlowExecution flowExecution, FlowExecutionKey previousKey) { 114 if (isAlwaysGenerateNewNextKey()) { 115 return super.getNextKey(flowExecution, previousKey); 116 } 117 else { 118 return previousKey; 119 } 120 } 121 122 public FlowExecution getFlowExecution(FlowExecutionKey key) { 123 try { 124 FlowExecution execution = getEntry(key).access(getContinuationId(key)); 125 return getExecutionStateRestorer().restoreState(execution, getConversationScope(key)); 128 } 129 catch (InvalidContinuationIdException e) { 130 throw new PermissionDeniedFlowExecutionAccessException(key, e); 131 } 132 } 133 134 public void putFlowExecution(FlowExecutionKey key, FlowExecution flowExecution) { 135 FlowExecutionEntry entry = new FlowExecutionEntry(getContinuationId(key), flowExecution); 136 putEntry(key, entry); 137 putConversationScope(key, flowExecution.getConversationScope()); 138 } 139 140 protected Serializable generateContinuationId(FlowExecution flowExecution) { 141 return continuationIdGenerator.generateUid(); 142 } 143 144 protected Serializable parseContinuationId(String encodedId) { 145 return continuationIdGenerator.parseUid(encodedId); 146 } 147 148 150 153 private FlowExecutionEntry getEntry(FlowExecutionKey key) { 154 FlowExecutionEntry entry = 155 (FlowExecutionEntry)getConversation(key).getAttribute(FLOW_EXECUTION_ENTRY_ATTRIBUTE); 156 if (entry == null) { 157 throw new IllegalStateException ("No '" + FLOW_EXECUTION_ENTRY_ATTRIBUTE 158 + "' attribute present in the governing conversation: " 159 + "possible programmer error -- do not call get before calling put"); 160 } 161 return entry; 162 } 163 164 169 private void putEntry(FlowExecutionKey key, FlowExecutionEntry entry) { 170 getConversation(key).putAttribute(FLOW_EXECUTION_ENTRY_ATTRIBUTE, entry); 171 } 172 173 179 private static class FlowExecutionEntry implements Serializable { 180 181 184 private Serializable continuationId; 185 186 189 private FlowExecution flowExecution; 190 191 196 public FlowExecutionEntry(Serializable continuationId, FlowExecution flowExecution) { 197 this.continuationId = continuationId; 198 this.flowExecution = flowExecution; 199 } 200 201 208 public FlowExecution access(Serializable continuationId) throws InvalidContinuationIdException { 209 if (!this.continuationId.equals(continuationId)) { 210 throw new InvalidContinuationIdException(continuationId); 211 } 212 return flowExecution; 213 } 214 } 215 } | Popular Tags |