~ubuntu-branches/ubuntu/raring/libjboss-remoting-java/raring

« back to all changes in this revision

Viewing changes to src/main/org/jboss/remoting/samples/chat/server/ChatManager.java

  • Committer: Package Import Robot
  • Author(s): Torsten Werner
  • Date: 2011-09-09 14:01:03 UTC
  • mto: This revision was merged to the branch mainline in revision 9.
  • Revision ID: package-import@ubuntu.com-20110909140103-o8ucrolqt5g25k57
Tags: upstream-2.5.3.SP1
ImportĀ upstreamĀ versionĀ 2.5.3.SP1

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
package org.jboss.remoting.samples.chat.server;
 
2
 
 
3
import java.io.File;
 
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;
 
9
import java.util.Date;
 
10
 
 
11
import javax.management.MBeanServer;
 
12
 
 
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;
 
27
 
 
28
/**
 
29
 */
 
30
 
 
31
public class ChatManager implements ServerInvocationHandler
 
32
{
 
33
   protected static final Logger log = Logger.getLogger(ChatManager.class);
 
34
   private static final String chatServerLocator = "socket://localhost";
 
35
   
 
36
   private ChatStore chatStore;
 
37
   private final File chatStoreFile = new File("chatStore.ser");
 
38
   private boolean chatStoreSaved = false;
 
39
   private boolean shuttingDown = false;
 
40
   
 
41
   
 
42
   public ChatManager()
 
43
   {
 
44
      try
 
45
      {
 
46
         initialize();
 
47
      }
 
48
      catch (InitializeException ie)
 
49
      {
 
50
         log.error("ChatManager_Impl: cannot initialize: " + ie);
 
51
         System.exit(1);
 
52
      }
 
53
   }
 
54
 
 
55
 
 
56
   protected void initialize() throws InitializeException
 
57
   {
 
58
      
 
59
      if (chatStoreFile.exists())
 
60
      {
 
61
         try
 
62
         {
 
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");
 
69
         }
 
70
         catch (java.io.IOException ioe) {
 
71
            log.error("ChatManager_Impl: i/o error reading chatStore: " + ioe);
 
72
            System.exit(1);
 
73
         }
 
74
         catch (java.lang.ClassNotFoundException cnfe) {
 
75
            log.error("ChatManager_Impl: ChatStore class not found: " + cnfe);
 
76
            System.exit(2);
 
77
         }
 
78
      }
 
79
      else
 
80
      {
 
81
         chatStore = new ChatStore();
 
82
         log.info("ChatManager_Impl: created new ChatStore");
 
83
      }
 
84
 
 
85
      //       Runtime.getRuntime().addShutdownHook(
 
86
      //        new Thread() {
 
87
      //         public void run() {
 
88
      //           System.out.println("ShutDownHook: shutting down");
 
89
      ////           new ShutDownDialog().show();
 
90
      //           shutdown();
 
91
      //         }
 
92
      //       });
 
93
 
 
94
   }
 
95
 
 
96
   public void shutdown()
 
97
   {
 
98
      log.info("shutdown(): shutting down");
 
99
 
 
100
      if (chatStoreSaved)
 
101
      {
 
102
         log.info("shutdown(): chatStore already saved");
 
103
         return;
 
104
      }
 
105
 
 
106
      chatStore.getShutDownGate().shutDown();
 
107
 
 
108
      ObjectOutputStream out = null;
 
109
      try
 
110
      {
 
111
         out = new ObjectOutputStream(new FileOutputStream(chatStoreFile));
 
112
         out.writeObject(chatStore);
 
113
         out.flush();
 
114
      }
 
115
      catch (java.io.IOException ioe)
 
116
      {
 
117
         log.error("ChatManager_Impl: i/o error writing chatStore" + ioe);
 
118
      }
 
119
      finally
 
120
      {
 
121
         try
 
122
         {
 
123
            out.close();
 
124
         }
 
125
         catch (java.io.IOException ioe)
 
126
         {
 
127
            log.error("ChatManager_Impl: i/o error closing chatStore" + ioe);
 
128
         }
 
129
      }
 
130
 
 
131
      chatStoreSaved = true;
 
132
      log.info("shutdown(): shut down");
 
133
   }
 
134
 
 
135
   
 
136
   public Object invoke(InvocationRequest invocation) throws Throwable
 
137
   {
 
138
      if (!(invocation.getParameter() instanceof RemoteInvocation))
 
139
         throw new Exception("invalid request format: expecting RemoteInvocation");
 
140
         
 
141
      RemoteInvocation request = (RemoteInvocation) invocation.getParameter();
 
142
      String methodName = request.getMethodName();
 
143
      Object[] args = request.getParameters();
 
144
 
 
145
      if (methodName.equals("createChat"))
 
146
         return createChat(args);
 
147
      
 
148
      else if (methodName.equals("join"))
 
149
      {
 
150
         join(args);
 
151
         return null;
 
152
      }
 
153
      
 
154
      else if (methodName.equals("leave"))
 
155
      {
 
156
         leave(args);
 
157
         return null;
 
158
      }
 
159
      
 
160
      else if (methodName.equals("list"))
 
161
         return list(args);
 
162
      
 
163
      else
 
164
         throw new Exception("unrecognized method name: " + methodName);
 
165
   }
 
166
   
 
167
   
 
168
   protected ArrayList list(Object[] args) throws RemoteConnectionException, ShuttingDownException
 
169
   {
 
170
      return chatStore.listChats();
 
171
   }
 
172
 
 
173
   
 
174
   protected InvokerLocator createChat(Object[] args) throws Exception
 
175
   {
 
176
      String description = (String) args[0];
 
177
      ChatMember owner = (ChatMember) args[1];
 
178
      
 
179
      ChatInfo chatInfo = new ChatInfo();
 
180
      ExtendedChatInfo extendedChatInfo = new ExtendedChatInfo(chatInfo);
 
181
      
 
182
      Connector connector = new Connector();
 
183
      connector.setInvokerLocator(chatServerLocator);
 
184
      connector.create();
 
185
      connector.addInvocationHandler("chatServer", new ChatServer_Impl(extendedChatInfo, chatStore.getShutDownGate()));
 
186
      connector.start();
 
187
      
 
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);
 
196
      return chatLocator;
 
197
   }
 
198
 
 
199
   
 
200
   protected void join(Object[] args)
 
201
   throws NameInUseException, ShuttingDownException
 
202
   {
 
203
      String key = (String) args[0];
 
204
      ChatMember newMember = (ChatMember) args[1];
 
205
      ExtendedChatInfo eci = chatStore.getChat(key);
 
206
      eci.addMember(newMember);
 
207
   }
 
208
 
 
209
   
 
210
   protected void leave(Object[] args) throws ShuttingDownException
 
211
   {
 
212
      String key = (String) args[0];
 
213
      ChatMember member = (ChatMember) args[1];
 
214
      ExtendedChatInfo eci = chatStore.getChat(key);
 
215
      eci.getMembers().remove(member);
 
216
   }
 
217
 
 
218
 
 
219
   public void setMBeanServer(MBeanServer server)
 
220
   {
 
221
   }
 
222
 
 
223
   public void setInvoker(ServerInvoker invoker)
 
224
   {
 
225
   }
 
226
 
 
227
   public void addListener(InvokerCallbackHandler callbackHandler)
 
228
   {
 
229
   }
 
230
 
 
231
   public void removeListener(InvokerCallbackHandler callbackHandler)
 
232
   {
 
233
   }
 
234
}
 
 
b'\\ No newline at end of file'