KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > springframework > webflow > conversation > impl > SessionBindingConversationManager


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.conversation.impl;
17
18 import org.springframework.webflow.context.ExternalContextHolder;
19 import org.springframework.webflow.conversation.Conversation;
20 import org.springframework.webflow.conversation.ConversationException;
21 import org.springframework.webflow.conversation.ConversationId;
22 import org.springframework.webflow.conversation.ConversationManager;
23 import org.springframework.webflow.conversation.ConversationParameters;
24 import org.springframework.webflow.core.collection.SharedAttributeMap;
25 import org.springframework.webflow.util.RandomGuidUidGenerator;
26 import org.springframework.webflow.util.UidGenerator;
27
28 /**
29  * Simple implementation of a conversation manager that stores conversations in
30  * the session attribute map.
31  *
32  * @author Erwin Vervaet
33  */

34 public class SessionBindingConversationManager implements ConversationManager {
35
36     /**
37      * Key of the session attribute holding the conversation container.
38      */

39     private static final String JavaDoc CONVERSATION_CONTAINER_KEY = "webflow.conversation.container";
40
41     /**
42      * The conversation uid generation strategy to use.
43      */

44     private UidGenerator conversationIdGenerator = new RandomGuidUidGenerator();
45
46     /**
47      * The maximum number of active conversations allowed in a session.
48      */

49     private int maxConversations = -1;
50
51     /**
52      * Sets the configured generator for conversation ids.
53      */

54     public void setConversationIdGenerator(UidGenerator uidGenerator) {
55         this.conversationIdGenerator = uidGenerator;
56     }
57
58     /**
59      * Set the maximum number of allowed concurrent conversations. Set to -1 for
60      * no limit.
61      */

62     public void setMaxConversations(int maxConversations) {
63         this.maxConversations = maxConversations;
64     }
65
66     public Conversation beginConversation(ConversationParameters conversationParameters) throws ConversationException {
67         ConversationId conversationId = new SimpleConversationId(conversationIdGenerator.generateUid());
68         return getConversationContainer().createAndAddConversation(conversationId, conversationParameters);
69     }
70
71     public Conversation getConversation(ConversationId id) throws ConversationException {
72         return getConversationContainer().getConversation(id);
73     }
74
75     public ConversationId parseConversationId(String JavaDoc encodedId) throws ConversationException {
76         return new SimpleConversationId(conversationIdGenerator.parseUid(encodedId));
77     }
78
79     // internal helpers
80

81     /**
82      * Obtain the conversation container from the session. Create a new empty
83      * container and add it to the session if no existing container can be
84      * found.
85      */

86     private ConversationContainer getConversationContainer() {
87         SharedAttributeMap sessionMap = ExternalContextHolder.getExternalContext().getSessionMap();
88         synchronized (sessionMap.getMutex()) {
89             ConversationContainer container = (ConversationContainer)sessionMap.get(CONVERSATION_CONTAINER_KEY);
90             if (container == null) {
91                 container = new ConversationContainer(maxConversations);
92                 sessionMap.put(CONVERSATION_CONTAINER_KEY, container);
93             }
94             return container;
95         }
96     }
97 }
Popular Tags