KickJava   Java API By Example, From Geeks To Geeks.

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


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 java.io.Serializable JavaDoc;
19 import java.util.ArrayList JavaDoc;
20 import java.util.Iterator JavaDoc;
21 import java.util.List JavaDoc;
22
23 import org.apache.commons.logging.Log;
24 import org.apache.commons.logging.LogFactory;
25 import org.springframework.webflow.conversation.Conversation;
26 import org.springframework.webflow.conversation.ConversationId;
27 import org.springframework.webflow.conversation.ConversationParameters;
28 import org.springframework.webflow.conversation.NoSuchConversationException;
29
30 /**
31  * Container for conversations that is stored in the session. When the
32  * session expires this container will go with it, implicitly expiring all
33  * contained conversations.
34  * <p>
35  * This is an internal helper class of the {@link SessionBindingConversationManager}.
36  *
37  * @author Erwin Vervaet
38  */

39 class ConversationContainer implements Serializable JavaDoc {
40     
41     private static final Log logger = LogFactory.getLog(ConversationContainer.class);
42
43     /**
44      * Maximum number of conversations in this container. -1 for
45      * unlimited.
46      */

47     private int maxConversations;
48
49     /**
50      * The contained conversations. A list of {@link ContainedConversation} objects.
51      */

52     private List JavaDoc conversations;
53
54     /**
55      * Create a new conversation container.
56      * @param maxConversations the maximum number of allowed concurrent
57      * conversations, -1 for unlimited
58      */

59     public ConversationContainer(int maxConversations) {
60         this.maxConversations = maxConversations;
61         this.conversations = new ArrayList JavaDoc();
62     }
63
64     /**
65      * Create a new conversation based on given parameters and add it to the
66      * container.
67      * @param id the unique id of the conversation
68      * @param parameters descriptive parameters
69      * @return the created conversation
70      */

71     public synchronized Conversation createAndAddConversation(ConversationId id, ConversationParameters parameters) {
72         if (logger.isInfoEnabled()) {
73             logger.info("Creating new conversation '" + parameters + "' with id '" + id + "' " +
74                     "and adding it to conversation container");
75         }
76         ContainedConversation conversation = new ContainedConversation(this, id);
77         conversations.add(conversation);
78         if (maxExceeded()) {
79             // end oldest conversation
80
((Conversation)conversations.get(0)).end();
81         }
82         return conversation;
83     }
84
85     /**
86      * Return the identified conversation.
87      * @param id the id to lookup
88      * @return the conversation
89      * @throws NoSuchConversationException if the conversation cannot be
90      * found
91      */

92     public synchronized Conversation getConversation(ConversationId id) throws NoSuchConversationException {
93         if (logger.isDebugEnabled()) {
94             logger.debug("Getting conversation with id '" + id +"'");
95         }
96         for (Iterator JavaDoc it = conversations.iterator(); it.hasNext();) {
97             ContainedConversation conversation = (ContainedConversation)it.next();
98             if (conversation.getId().equals(id)) {
99                 return conversation;
100             }
101         }
102         throw new NoSuchConversationException(id);
103     }
104
105     /**
106      * Remove identified conversation from this container.
107      */

108     public synchronized void removeConversation(ConversationId id) {
109         if (logger.isInfoEnabled()) {
110             logger.info("Removing conversation with id '" + id + "'");
111         }
112         for (Iterator JavaDoc it = conversations.iterator(); it.hasNext();) {
113             ContainedConversation conversation = (ContainedConversation)it.next();
114             if (conversation.getId().equals(id)) {
115                 it.remove();
116                 break;
117             }
118         }
119     }
120
121     /**
122      * Has the maximum number of allowed concurrent conversations in the
123      * session been exceeded?
124      */

125     private boolean maxExceeded() {
126         return maxConversations > 0 && conversations.size() > maxConversations;
127     }
128 }
Popular Tags