?? agenthostimpl.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 -------------------------------------------------
}
?? 快捷鍵說明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -