KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > springframework > webflow > execution > repository > support > CompositeFlowExecutionKey


1 /*
2  * Copyright 2002-2006 the original author or authors.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16 package org.springframework.webflow.execution.repository.support;
17
18 import java.io.Serializable JavaDoc;
19
20 import org.springframework.util.Assert;
21 import org.springframework.webflow.conversation.ConversationId;
22 import org.springframework.webflow.conversation.ConversationManager;
23 import org.springframework.webflow.execution.repository.BadlyFormattedFlowExecutionKeyException;
24 import org.springframework.webflow.execution.repository.FlowExecutionKey;
25 import org.springframework.webflow.execution.repository.continuation.FlowExecutionContinuation;
26
27 /**
28  * A flow execution key consisting of two parts:
29  * <ol>
30  * <li>A <i>conversationId</i>, identifying an active conversation managed by a
31  * {@link ConversationManager}.
32  * <li>A <i>continuationId</i>, identifying a restorable
33  * {@link FlowExecutionContinuation} within a continuation group governed by
34  * that conversation.
35  * </ol>
36  * <p>
37  * This key is used to restore a FlowExecution from a conversation-service
38  * backed store.
39  *
40  * @see ConversationManager
41  * @see FlowExecutionContinuation
42  *
43  * @author Keith Donald
44  */

45 class CompositeFlowExecutionKey extends FlowExecutionKey {
46     
47     /**
48      * The default conversation id prefix delimiter ("_c").
49      */

50     private static final String JavaDoc CONVERSATION_ID_PREFIX = "_c";
51
52     /**
53      * The default continuation id prefix delimiter ("_k").
54      */

55     private static final String JavaDoc CONTINUATION_ID_PREFIX = "_k";
56
57     /**
58      * The format of the default string-encoded form, as returned
59      * by toString().
60      */

61     private static final String JavaDoc FORMAT =
62         CONVERSATION_ID_PREFIX + "<conversationId>" + CONTINUATION_ID_PREFIX + "<continuationId>";
63
64     /**
65      * The conversation id.
66      */

67     private ConversationId conversationId;
68
69     /**
70      * The continuation id.
71      */

72     private Serializable JavaDoc continuationId;
73
74     /**
75      * Create a new composite flow execution key given the composing parts.
76      * @param conversationId the conversation id
77      * @param continuationId the continuation id
78      */

79     public CompositeFlowExecutionKey(ConversationId conversationId, Serializable JavaDoc continuationId) {
80         Assert.notNull(conversationId, "The conversation id is required");
81         Assert.notNull(continuationId, "The continuation id is required");
82         this.conversationId = conversationId;
83         this.continuationId = continuationId;
84     }
85
86     /**
87      * Returns the conversation id.
88      */

89     public ConversationId getConversationId() {
90         return conversationId;
91     }
92
93     /**
94      * Returns the continuation id.
95      */

96     public Serializable JavaDoc getContinuationId() {
97         return continuationId;
98     }
99
100     public boolean equals(Object JavaDoc obj) {
101         if (!(obj instanceof CompositeFlowExecutionKey)) {
102             return false;
103         }
104         CompositeFlowExecutionKey other = (CompositeFlowExecutionKey)obj;
105         return conversationId.equals(other.conversationId) && continuationId.equals(other.continuationId);
106     }
107
108     public int hashCode() {
109         return conversationId.hashCode() + continuationId.hashCode();
110     }
111
112     public String JavaDoc toString() {
113         return new StringBuffer JavaDoc().append(CONVERSATION_ID_PREFIX).append(getConversationId())
114             .append(CONTINUATION_ID_PREFIX).append(getContinuationId()).toString();
115     }
116     
117     // static helpers
118

119     /**
120      * Helper that splits the string-form of an instance of this class into its
121      * "parts" so the parts can be easily parsed.
122      * @param encodedKey the string-encoded composite flow execution key
123      * @return the composite key parts as a String array (conversationId = 0,
124      * continuationId = 1)
125      */

126     public static String JavaDoc[] keyParts(String JavaDoc encodedKey) throws BadlyFormattedFlowExecutionKeyException {
127         if (!encodedKey.startsWith(CONVERSATION_ID_PREFIX)) {
128             throw new BadlyFormattedFlowExecutionKeyException(encodedKey, FORMAT);
129         }
130         int continuationStart = encodedKey.indexOf(CONTINUATION_ID_PREFIX, CONVERSATION_ID_PREFIX.length());
131         if (continuationStart == -1) {
132             throw new BadlyFormattedFlowExecutionKeyException(encodedKey, FORMAT);
133         }
134         String JavaDoc conversationId = encodedKey.substring(CONVERSATION_ID_PREFIX.length(), continuationStart);
135         String JavaDoc continuationId = encodedKey.substring(continuationStart + CONTINUATION_ID_PREFIX.length());
136         return new String JavaDoc[] { conversationId, continuationId };
137     }
138 }
Popular Tags