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

📄 main.java

📁 《精通RMI:Java与EJB企业级应用开发》书籍的源代码. 本书是讲述RMI技术的经典著作
💻 JAVA
字号:
/*
 * Copyright 1999 by dreamBean Software,
 * All rights reserved.
 */
package masteringrmi.helloapplet.server;

import java.io.IOException;
import java.net.MalformedURLException;
import java.rmi.RemoteException;
import java.rmi.ServerException;
import java.rmi.AlreadyBoundException;
import java.util.Properties;

import java.rmi.registry.Registry;

import javax.naming.InitialContext;
import javax.naming.NamingException;

import com.dreambean.dynaserver.DynaServer;

/**
 *   This is the main server class. It creates a webserver that can be used for 
 *   dynamic class downloading, creates an internal RMI-registry for naming, 
 *   creates a remote object, and finally registers this remote object in the
 *   created registry by using JNDI.
 *
 *   Any clients of this server may download any required classes using 
 *   dynamic class downloading.
 *
 *   An applet which connects to this server can be loaded by pointing
 *   the appletviewer to <a href="http://localhost:8080/index.html">http://localhost:8080/index.html</a>.
 *   
 *   @see HelloWorldImpl
 *   @author Rickard 謆erg
 *   @version $Revision:$
 */
public class Main
{
   // Attributes ----------------------------------------------------


   /**
    * @link aggregation
    * @label manages 
    */
   HelloWorldImpl server;
   
   // Static --------------------------------------------------------
   public static void main(String[] args)
      throws Exception
   {
      // Load system properties from resource file
      // This file is located in /lib/resources, and is included in classpath
      // through the manifest in server.jar
      System.getProperties().load(
         Thread.currentThread().
         getContextClassLoader().
         getResourceAsStream("system.properties"));
      
      // Create server
      new Main();
      
      System.out.println("Server has been started");
   }
   
   // Constructors --------------------------------------------------
   public Main()
      throws IOException
   {
      startWebServer();
      startNaming();
      startService();
 
      // Done. 
      // The remote object, HelloWorldImpl, can now be invoked through the applet!
   }
   
   // Protected -----------------------------------------------------
   protected void startWebServer()
      throws IOException
   {
      // Create webserver for dynamic class downloading
      DynaServer srv = new DynaServer();
      srv.setDebug(true);
      
      // Add the classloader for this application
      // This will allow any client to download classes/resources that
      // are in the classpath for this application
      srv.addClassLoader(Thread.currentThread().getContextClassLoader());
      
      // Start the webserver
      srv.start();
   }

   protected void startNaming()
      throws RemoteException
   {
      // Create registry if not already running
      try 
      {
         java.rmi.registry.LocateRegistry.createRegistry(Registry.REGISTRY_PORT);
      } catch (java.rmi.server.ExportException ee) 
      {
         // Registry already exists
      } catch (RemoteException e) 
      {
         throw new ServerException("Could not create registry", e);
      }
   }

   protected void startService()
      throws RemoteException
   {
      // Create remote object
      server = new HelloWorldImpl();
   
      // Register server with naming service
      // Use JNDI to hide away the fact that we're using the registry
      // This allows us to easily change the naming service later on
      try
      {
         new InitialContext().rebind(HelloWorldImpl.NAME,server);
      } catch (NamingException e)
      {
         throw new ServerException("The server could not be bound into the naming service", e);
      } 
   }
}

⌨️ 快捷键说明

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