⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 chatserveradministrator.java

📁 java2高级教程大学教程的配套光盘源码
💻 JAVA
字号:
// ChatServerAdministrator.java
// ChatServerAdministrator is a utility for starting and stopping
// the RMI-IIOP ChatServer implementation.
package com.deitel.messenger.rmi_iiop.server;

// Java core packages
import java.rmi.*;
import java.rmi.activation.*;
import java.util.*;

// Java extension packages
import javax.rmi.*;
import javax.naming.*;

// Deitel packages
import com.deitel.messenger.rmi.server.StoppableChatServer;

public class ChatServerAdministrator {
   
   // set up ChatServer object
   private static void startServer()
   {
      // register ChatServer in Naming Service
      try {
         
         // create ChatServerImpl object
         ChatServerImpl chatServerImpl = new ChatServerImpl();
      
         // create InitialContext for naming service
         Context namingContext = new InitialContext();
         
         System.err.println( "Binding server to naming service..." );
         
         // bind ChatServerImpl object to naming service
         namingContext.rebind( "ChatServer", chatServerImpl );
         
         System.out.println( "Server bound to naming service" );
         
      } // end try
      
      // handle exception registering ChatServer
      catch ( NamingException namingException ) {
         namingException.printStackTrace();
         System.exit( 1 );
      }
      
      // handle exception creating ChatServer
      catch ( RemoteException remoteException ) {
         remoteException.printStackTrace();
         System.exit( 1 );
      }
      
   } // end method startServer
   
   // terminate server
   private static void terminateServer()
   {
      // look up and terminate to ChatServer
      try {
         
         // create naming Context for looking up server
         Context namingContext = new InitialContext();
             
         // find ChatServer remote object
         Object remoteObject = 
            namingContext.lookup( "ChatServer" );
         
         // narrow remoteObject to StoppableChatServer
         StoppableChatServer chatServer = 
            ( StoppableChatServer ) PortableRemoteObject.narrow(
               remoteObject, StoppableChatServer.class );

         // stop ChatServer
         chatServer.stopServer();   
         
         // remove ChatServer from Naming Service
         namingContext.unbind( "ChatServer" );
         
      } // end try
      
      // handle exception looking up ChatServer
      catch ( NamingException namingException ) {
         namingException.printStackTrace();
      } 
      
      // handle exception communicating with ChatServer
      catch ( RemoteException remoteException ) {
         remoteException.printStackTrace();
      }
      
   } // end method terminateServer
   
   // launch ChatServerAdministrator application
   public static void main( String args[] )
   {
      // check command-line arguments and start or stop server
      if ( args[ 0 ].equals( "start" ) )
         startServer();
      
      else if ( args[ 0 ].equals( "stop" ) )
         terminateServer();
      
      // print usage information
      else
         System.err.println( 
            "Usage: java ChatServerAdministrator start | stop" );
      
   } // end method main
}

/***************************************************************
 * (C) Copyright 2002 by Deitel & Associates, Inc. and         *
 * Prentice Hall. All Rights Reserved.                         *
 *                                                             *
 * DISCLAIMER: The authors and publisher of this book have     *
 * used their best efforts in preparing the book. These        *
 * efforts include the development, research, and testing of   *
 * the theories and programs to determine their effectiveness. *
 * The authors and publisher make no warranty of any kind,     *
 * expressed or implied, with regard to these programs or to   *
 * the documentation contained in these books. The authors     *
 * and publisher shall not be liable in any event for          *
 * incidental or consequential damages in connection with, or  *
 * arising out of, the furnishing, performance, or use of      *
 * these programs.                                             *
 ***************************************************************/

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -