1 16 package org.springframework.webflow.conversation.impl; 17 18 import java.io.Serializable ; 19 import java.util.ArrayList ; 20 import java.util.Iterator ; 21 import java.util.List ; 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 39 class ConversationContainer implements Serializable { 40 41 private static final Log logger = LogFactory.getLog(ConversationContainer.class); 42 43 47 private int maxConversations; 48 49 52 private List conversations; 53 54 59 public ConversationContainer(int maxConversations) { 60 this.maxConversations = maxConversations; 61 this.conversations = new ArrayList (); 62 } 63 64 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 ((Conversation)conversations.get(0)).end(); 81 } 82 return conversation; 83 } 84 85 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 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 108 public synchronized void removeConversation(ConversationId id) { 109 if (logger.isInfoEnabled()) { 110 logger.info("Removing conversation with id '" + id + "'"); 111 } 112 for (Iterator 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 125 private boolean maxExceeded() { 126 return maxConversations > 0 && conversations.size() > maxConversations; 127 } 128 } | Popular Tags |