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

📄 main.java

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

import java.io.File;
import java.io.IOException;
import java.net.URL;
import java.net.MalformedURLException;
import java.rmi.server.UnicastRemoteObject;
import java.rmi.AlreadyBoundException;
import java.rmi.RemoteException;
import java.rmi.ServerException;
import java.rmi.registry.Registry;
import java.util.Properties;

import javax.naming.InitialContext;

import com.dreambean.dynaserver.DynaServer;

/**
 *   This is the manager of the agent host.
 *
 *   It starts the basic services required by the host,
 *   and also manage an instance of the host itself.
 *
 *   @author Rickard 謆erg
 *   @version $Revision:$
 */
public class Main
{
   // Constants -----------------------------------------------------
    
   // Attributes ----------------------------------------------------

   // Static --------------------------------------------------------
   public static void main(String[] args)
      throws Exception
   {
      // Load system properties
      System.getProperties().load(
         Thread.currentThread().
         getContextClassLoader().
         getResourceAsStream("system.properties"));
         
      // Set policy to use
      System.setProperty("java.security.policy", Main.class.getResource("/server.policy").toString());
      
      // Set a security manager
      System.setSecurityManager(new SecurityManager());
      
      new Main();
   }
   
   // Constructors --------------------------------------------------
   public Main()
   {
	   // Start server
	   try
	   {
	      startWebServer();
	      
	      startNamingServer();
	      
	      startAgentServer();
	      
	      System.out.println("Server has been started and registered");
	   
	   } catch (Exception e)
	   {
	      System.out.println("The server could not be started");
	      e.printStackTrace(System.err);
	   }
   }

   // Public --------------------------------------------------------
   public void startWebServer()
      throws IOException
   {
      // Start webserver for dynamic class loading
      // The webserver can access any class available from the default classloader
      // Start webserver for dynamic class loading
      DynaServer srv = new DynaServer();
      srv.setDebug(true);
      srv.addClassLoader(Thread.currentThread().getContextClassLoader());
      srv.start();
   }
   
   protected void startNamingServer()
      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);
      }
   }
   
   public void startAgentServer()
      throws Exception
   {
      // Create remote object
      AgentHostImpl server = new AgentHostImpl();
      UnicastRemoteObject.exportObject(server);
   
      // Register server with naming service
      new InitialContext().bind("agenthost",server);
   }
   
   // Y overrides ---------------------------------------------------

   // Package protected ---------------------------------------------
    
   // Protected -----------------------------------------------------
    
   // Private -------------------------------------------------------

   // Inner classes -------------------------------------------------
}

⌨️ 快捷键说明

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