Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  package org.jboss.remoting.samples.chat.server;
  
  import java.io.File;
  import java.util.Date;
 
 
 import  org.jboss.logging.Logger;
 
 
 public class ChatManager implements ServerInvocationHandler
 {
    protected static final Logger log = Logger.getLogger(ChatManager.class);
    private static final String chatServerLocator = "socket://localhost";
    
    private ChatStore chatStore;
    private final File chatStoreFile = new File("chatStore.ser");
    private boolean chatStoreSaved = false;
    private boolean shuttingDown = false;
    
    
    public ChatManager()
    {
       try
       {
          initialize();
       }
       catch (InitializeException ie)
       {
          .error("ChatManager_Impl: cannot initialize: " + ie);
          System.exit(1);
       }
    }
 
 
    protected void initialize() throws InitializeException
    {
       
       if (.exists())
       {
          try
          {
             .info("ChatManager_Impl: reading existing ChatStore");
             FileInputStream file = new FileInputStream();
             ObjectInputStream input = new ObjectInputStream(file);
              = (ChatStoreinput.readObject();
             .getShutDownGate().reset();
             .info("ChatManager_Impl: read existing ChatStore");
          }
          catch (java.io.IOException ioe) {
             .error("ChatManager_Impl: i/o error reading chatStore: " + ioe);
             System.exit(1);
          }
          catch (java.lang.ClassNotFoundException cnfe) {
             .error("ChatManager_Impl: ChatStore class not found: " + cnfe);
             System.exit(2);
          }
       }
       else
       {
           = new ChatStore();
          .info("ChatManager_Impl: created new ChatStore");
       }
 
       //       Runtime.getRuntime().addShutdownHook(
       //        new Thread() {
       //         public void run() {
       //           System.out.println("ShutDownHook: shutting down");
       ////           new ShutDownDialog().show();
       //           shutdown();
       //         }
       //       });
 
    }
 
    public void shutdown()
    {
       .info("shutdown(): shutting down");
 
      if ()
      {
         .info("shutdown(): chatStore already saved");
         return;
      }
      ObjectOutputStream out = null;
      try
      {
         out = new ObjectOutputStream(new FileOutputStream());
         out.writeObject();
         out.flush();
      }
      catch (java.io.IOException ioe)
      {
         .error("ChatManager_Impl: i/o error writing chatStore" + ioe);
      }
      finally
      {
         try
         {
            out.close();
         }
         catch (java.io.IOException ioe)
         {
            .error("ChatManager_Impl: i/o error closing chatStore" + ioe);
         }
      }
       = true;
      .info("shutdown(): shut down");
   }
   
   public Object invoke(InvocationRequest invocationthrows Throwable
   {
      if (!(invocation.getParameter() instanceof RemoteInvocation))
         throw new Exception("invalid request format: expecting RemoteInvocation");
         
      RemoteInvocation request = (RemoteInvocationinvocation.getParameter();
      String methodName = request.getMethodName();
      Object[] args = request.getParameters();
      if (methodName.equals("createChat"))
         return createChat(args);
      
      else if (methodName.equals("join"))
      {
         join(args);
         return null;
      }
      
      else if (methodName.equals("leave"))
      {
         leave(args);
         return null;
      }
      
      else if (methodName.equals("list"))
         return list(args);
      
      else
         throw new Exception("unrecognized method name: " + methodName);
   }
   
   
   {
      return .listChats();
   }
   
   protected InvokerLocator createChat(Object[] argsthrows Exception
   {
      String description = (Stringargs[0];
      ChatMember owner = (ChatMemberargs[1];
      
      ChatInfo chatInfo = new ChatInfo();
      ExtendedChatInfo extendedChatInfo = new ExtendedChatInfo(chatInfo);
      
      Connector connector = new Connector();
      connector.setInvokerLocator();
      connector.create();
      connector.addInvocationHandler("chatServer"new ChatServer_Impl(extendedChatInfo.getShutDownGate()));
      connector.start();
      
      InvokerLocator chatLocator = connector.getLocator();
      String key = chatLocator.getLocatorURI();
      chatInfo.set_key(key);
      chatInfo.set_description(description);
      chatInfo.set_owner(owner);
      chatInfo.set_origin(new Date());
      extendedChatInfo.addMember(owner);
      .addChat(extendedChatInfo);
      return chatLocator;
   }
   
   protected void join(Object[] args)
   {
      String key = (Stringargs[0];
      ChatMember newMember = (ChatMemberargs[1];
      ExtendedChatInfo eci = .getChat(key);
      eci.addMember(newMember);
   }
   
   protected void leave(Object[] argsthrows ShuttingDownException
   {
      String key = (Stringargs[0];
      ChatMember member = (ChatMemberargs[1];
      ExtendedChatInfo eci = .getChat(key);
      eci.getMembers().remove(member);
   }
   public void setMBeanServer(MBeanServer server)
   {
   }
   public void setInvoker(ServerInvoker invoker)
   {
   }
   public void addListener(InvokerCallbackHandler callbackHandler)
   {
   }
   public void removeListener(InvokerCallbackHandler callbackHandler)
   {
   }
New to GrepCode? Check out our FAQ X