1
package org.jboss.remoting.samples.chat.server;
4
import java.io.FileInputStream;
5
import java.io.FileOutputStream;
6
import java.io.ObjectInputStream;
7
import java.io.ObjectOutputStream;
8
import java.util.ArrayList;
11
import javax.management.MBeanServer;
13
import org.jboss.logging.Logger;
14
import org.jboss.remoting.InvocationRequest;
15
import org.jboss.remoting.InvokerLocator;
16
import org.jboss.remoting.ServerInvocationHandler;
17
import org.jboss.remoting.ServerInvoker;
18
import org.jboss.remoting.callback.InvokerCallbackHandler;
19
import org.jboss.remoting.invocation.RemoteInvocation;
20
import org.jboss.remoting.samples.chat.client.ChatInfo;
21
import org.jboss.remoting.samples.chat.client.ChatMember;
22
import org.jboss.remoting.samples.chat.exceptions.InitializeException;
23
import org.jboss.remoting.samples.chat.exceptions.NameInUseException;
24
import org.jboss.remoting.samples.chat.exceptions.RemoteConnectionException;
25
import org.jboss.remoting.samples.chat.exceptions.ShuttingDownException;
26
import org.jboss.remoting.transport.Connector;
31
public class ChatManager implements ServerInvocationHandler
33
protected static final Logger log = Logger.getLogger(ChatManager.class);
34
private static final String chatServerLocator = "socket://localhost";
36
private ChatStore chatStore;
37
private final File chatStoreFile = new File("chatStore.ser");
38
private boolean chatStoreSaved = false;
39
private boolean shuttingDown = false;
48
catch (InitializeException ie)
50
log.error("ChatManager_Impl: cannot initialize: " + ie);
56
protected void initialize() throws InitializeException
59
if (chatStoreFile.exists())
63
log.info("ChatManager_Impl: reading existing ChatStore");
64
FileInputStream file = new FileInputStream(chatStoreFile);
65
ObjectInputStream input = new ObjectInputStream(file);
66
chatStore = (ChatStore) input.readObject();
67
chatStore.getShutDownGate().reset();
68
log.info("ChatManager_Impl: read existing ChatStore");
70
catch (java.io.IOException ioe) {
71
log.error("ChatManager_Impl: i/o error reading chatStore: " + ioe);
74
catch (java.lang.ClassNotFoundException cnfe) {
75
log.error("ChatManager_Impl: ChatStore class not found: " + cnfe);
81
chatStore = new ChatStore();
82
log.info("ChatManager_Impl: created new ChatStore");
85
// Runtime.getRuntime().addShutdownHook(
87
// public void run() {
88
// System.out.println("ShutDownHook: shutting down");
89
//// new ShutDownDialog().show();
96
public void shutdown()
98
log.info("shutdown(): shutting down");
102
log.info("shutdown(): chatStore already saved");
106
chatStore.getShutDownGate().shutDown();
108
ObjectOutputStream out = null;
111
out = new ObjectOutputStream(new FileOutputStream(chatStoreFile));
112
out.writeObject(chatStore);
115
catch (java.io.IOException ioe)
117
log.error("ChatManager_Impl: i/o error writing chatStore" + ioe);
125
catch (java.io.IOException ioe)
127
log.error("ChatManager_Impl: i/o error closing chatStore" + ioe);
131
chatStoreSaved = true;
132
log.info("shutdown(): shut down");
136
public Object invoke(InvocationRequest invocation) throws Throwable
138
if (!(invocation.getParameter() instanceof RemoteInvocation))
139
throw new Exception("invalid request format: expecting RemoteInvocation");
141
RemoteInvocation request = (RemoteInvocation) invocation.getParameter();
142
String methodName = request.getMethodName();
143
Object[] args = request.getParameters();
145
if (methodName.equals("createChat"))
146
return createChat(args);
148
else if (methodName.equals("join"))
154
else if (methodName.equals("leave"))
160
else if (methodName.equals("list"))
164
throw new Exception("unrecognized method name: " + methodName);
168
protected ArrayList list(Object[] args) throws RemoteConnectionException, ShuttingDownException
170
return chatStore.listChats();
174
protected InvokerLocator createChat(Object[] args) throws Exception
176
String description = (String) args[0];
177
ChatMember owner = (ChatMember) args[1];
179
ChatInfo chatInfo = new ChatInfo();
180
ExtendedChatInfo extendedChatInfo = new ExtendedChatInfo(chatInfo);
182
Connector connector = new Connector();
183
connector.setInvokerLocator(chatServerLocator);
185
connector.addInvocationHandler("chatServer", new ChatServer_Impl(extendedChatInfo, chatStore.getShutDownGate()));
188
InvokerLocator chatLocator = connector.getLocator();
189
String key = chatLocator.getLocatorURI();
190
chatInfo.set_key(key);
191
chatInfo.set_description(description);
192
chatInfo.set_owner(owner);
193
chatInfo.set_origin(new Date());
194
extendedChatInfo.addMember(owner);
195
chatStore.addChat(extendedChatInfo);
200
protected void join(Object[] args)
201
throws NameInUseException, ShuttingDownException
203
String key = (String) args[0];
204
ChatMember newMember = (ChatMember) args[1];
205
ExtendedChatInfo eci = chatStore.getChat(key);
206
eci.addMember(newMember);
210
protected void leave(Object[] args) throws ShuttingDownException
212
String key = (String) args[0];
213
ChatMember member = (ChatMember) args[1];
214
ExtendedChatInfo eci = chatStore.getChat(key);
215
eci.getMembers().remove(member);
219
public void setMBeanServer(MBeanServer server)
223
public void setInvoker(ServerInvoker invoker)
227
public void addListener(InvokerCallbackHandler callbackHandler)
231
public void removeListener(InvokerCallbackHandler callbackHandler)
b'\\ No newline at end of file'