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

📄 agenthostimpl.java

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

import java.beans.beancontext.BeanContextServicesSupport;

import java.rmi.Remote;
import java.rmi.RemoteException;
import java.rmi.server.UnicastRemoteObject;
import java.rmi.server.RemoteObject;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;

import masteringrmi.agent.interfaces.Agent;
import masteringrmi.agent.interfaces.AgentHost;
import masteringrmi.agent.interfaces.NoSuchAgentException;

/**
 *   This is the implementation of the agent host.
 *      
 *   @see <related>
 *   @author Rickard 謆erg
 *   @version $Revision:$
 */
public class AgentHostImpl
   extends BeanContextServicesSupport
   implements AgentHost
{
   // Constants -----------------------------------------------------
    
   // Attributes ----------------------------------------------------
    
   // Static --------------------------------------------------------
   static AgentHostImpl host;
   
   // Constructors --------------------------------------------------
   public AgentHostImpl()
   {
      super();
      host = this;
   }
   
   // Public --------------------------------------------------------

   // AgentHost implementation --------------------------------------
   public synchronized Agent addAgent(Agent agent)
      throws RemoteException
   {
      // Export agent
      Remote stub = UnicastRemoteObject.exportObject(agent);
      
      // Store agent in list
      add(agent);
      
      System.out.println("Added agent "+agent);
         
      // Return agent -- will be replaced with stub on clientside!
      // This allows the client to talk to it
      return agent;
   }
   
   public synchronized Agent removeAgent(Agent agentStub)
      throws NoSuchAgentException
   {
      // Iterate through the list of agents and compare their stubs with the given stub
      Iterator enum = iterator();
      while(enum.hasNext())
      {
         // Get the next agent
         Agent current = (Agent)enum.next();
         try
         {
            // Compare the agents stub with the given stub
            if (RemoteObject.toStub(current).equals(agentStub))
            {
               // Unexport the agent, forcibly
               UnicastRemoteObject.unexportObject(current, true);
               
               // Remove the agent from the list
               remove(current);
               System.out.println("Removed agent "+current);
               
               // Return the agent
               // Now that it is no longer exported, it will be serialized
               return current;
            }
         } catch (Exception e) {}
      }
      
      // The agent was not found
      throw new NoSuchAgentException();
   }
   
   public synchronized Collection getAgents()
   {
      return new ArrayList(this);
   }
    
   // Y overrides ---------------------------------------------------

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

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

⌨️ 快捷键说明

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