1
package org.jboss.remoting.samples.chat.server;
6
* <p>Copyright: Copyright (c) 2003</p>
8
* @author not attributable
12
import java.io.Serializable;
13
import java.util.ArrayList;
14
import java.util.Collection;
15
import java.util.Hashtable;
16
import java.util.Iterator;
18
import org.jboss.logging.Logger;
19
import org.jboss.remoting.samples.chat.client.ChatInfo;
20
import org.jboss.remoting.samples.chat.exceptions.ShuttingDownException;
21
import org.jboss.remoting.samples.chat.utility.ShutDownGate;
23
public class ChatStore implements Serializable
25
private static final long serialVersionUID = 1;
26
protected static final Logger log = Logger.getLogger(ChatStore.class);
28
private Hashtable extendedChatInfoMap;
29
private ShutDownGate shutDownGate;
33
extendedChatInfoMap = new Hashtable();
34
shutDownGate = new ShutDownGate();
38
// databaseManager = new DatabaseManager();
40
// System.out.println("ChatStore(): created DatabaseManager");
42
// if (databaseManager.chatDBExists())
44
// restoreExtendedChatInfoMap();
45
// System.out.println("ChatStore(): restored chat store");
49
// databaseManager.createChatDB();
50
// System.out.println("ChatStore(): created new data base");
53
// catch (DatabaseException e)
55
// e.printStackTrace();
60
public ShutDownGate getShutDownGate()
61
{ return shutDownGate; }
64
public void addChat(ExtendedChatInfo eci) throws ShuttingDownException
67
String key = eci.getChatInfo().get_key();
68
extendedChatInfoMap.put(key, eci);
69
log.info("adding chat " + key);
74
/* fail-fast iterator*/
75
ArrayList listChats() throws ShuttingDownException
78
Collection c = extendedChatInfoMap.values();
79
ArrayList chatArrayList = new ArrayList();
81
Iterator it = c.iterator();
84
ExtendedChatInfo eci = (ExtendedChatInfo) it.next();
85
ChatInfo ci = eci.getChatInfo();
86
ci.set_currentMembers(eci.getMembers().size());
87
ci.set_size(eci.getMessages().size());
88
chatArrayList.add(ci);
89
log.debug("eci.getMembers(): " + eci.getMembers());
96
public ExtendedChatInfo getChat(String key) throws ShuttingDownException
99
ExtendedChatInfo eci = (ExtendedChatInfo) extendedChatInfoMap.get(key);
104
public Collection getChatKeySet()
105
{ return extendedChatInfoMap.keySet(); }
108
// private void restoreExtendedChatInfoMap()
110
// ArrayList chats = null;
114
// chats = databaseManager.getChats();
116
// catch (DatabaseException e)
118
// e.printStackTrace();
120
// Iterator it = chats.iterator();
122
// while (it.hasNext())
124
// ChatInfo chatInfo = (ChatInfo) it.next();
125
// ExtendedChatInfo eci = new ExtendedChatInfo(chatInfo);
127
//// ArrayList members = databaseManager.getChatMembers(chatInfo.get_key());
128
//// Iterator mit = members.iterator();
130
//// while (mit.hasNext())
132
// // eci.addMember((ChatMember) it.next(), null);
135
// ArrayList messages;
138
// messages = databaseManager.getChatMessages(chatInfo.get_key());
139
// eci.addMessages(messages);
141
// catch (DatabaseException e1)
143
// e1.printStackTrace();
150
// } catch (ShuttingDownException sde) {} // ignore: we're just starting up