com.almende.eve.agent
Class AgentFactory

java.lang.Object
  extended by com.almende.eve.agent.AgentFactory

public class AgentFactory
extends Object

The AgentFactory is a factory to instantiate and invoke Eve Agents within the configured state. The AgentFactory can invoke local as well as remote agents. An AgentFactory must be instantiated with a valid Eve configuration file. This configuration is needed to load the configured agent classes and instantiate a state for each agent. Example usage: // generic constructor Config config = new Config("eve.yaml"); AgentFactory factory = new AgentFactory(config); // construct in servlet InputStream is = getServletContext().getResourceAsStream("/WEB-INF/eve.yaml"); Config config = new Config(is); AgentFactory factory = new AgentFactory(config); // create or get a shared instance of the AgentFactory AgentFactory factory = AgentFactory.createInstance(namespace, config); AgentFactory factory = AgentFactory.getInstance(namespace); // invoke a local agent by its id response = factory.invoke(agentId, request); // invoke a local or remote agent by its url response = factory.send(senderId, receiverUrl, request); // create a new agent Agent agent = factory.createAgent(agentType, agentId); String desc = agent.getDescription(); // use the agent agent.destroy(); // neatly shutdown the agents state // instantiate an existing agent Agent agent = factory.getAgent(agentId); String desc = agent.getDescription(); // use the agent agent.destroy(); // neatly shutdown the agents state

Author:
jos

Constructor Summary
AgentFactory()
           
AgentFactory(Config config)
          Construct an AgentFactory and initialize the configuration
 
Method Summary
 void addAgents(Config config)
          Create agents from a config (only when they do not yet exist).
 void addTransportService(TransportService transportService)
          Add a new transport service
 void addTransportServices(Config config)
          Load transport services for incoming and outgoing messages from a config (for example http and xmpp services).
 Agent createAgent(Class<?> agentType, String agentId)
          Create an agent.
 Agent createAgent(String agentType, String agentId)
          Create an agent.
<T> T
createAgentProxy(String senderId, String receiverUrl, Class<T> agentInterface)
          Create an agent proxy from an java interface
static AgentFactory createInstance()
          Create a shared AgentFactory instance with the default namespace "default"
static AgentFactory createInstance(Config config)
          Create a shared AgentFactory instance with the default namespace "default"
static AgentFactory createInstance(String namespace)
          Create a shared AgentFactory instance with a specific namespace
static AgentFactory createInstance(String namespace, Config config)
          Create a shared AgentFactory instance with a specific namespace
 void deleteAgent(String agentId)
          Delete an agent
 Agent getAgent(String agentId)
          Get an agent by its id.
 Config getConfig()
          Get the loaded config file
static String getEnvironment()
          Retrieve the current environment, using the configured State.
 EventLogger getEventLogger()
          Get the event logger.
static AgentFactory getInstance()
          Get a shared AgentFactory instance with the default namespace "default"
static AgentFactory getInstance(String namespace)
          Get a shared AgentFactory instance with a specific namespace
 List<Object> getMethods(Agent agent, Boolean asJSON)
           
 Scheduler getScheduler(String agentId)
          create a scheduler for an agent
 StateFactory getStateFactory()
          Get the configured state factory.
 TransportService getTransportService(String protocol)
          Get the first registered transport service which supports given protocol.
 List<TransportService> getTransportServices()
          Get all registered transport services
 List<TransportService> getTransportServices(String protocol)
          Get all registered transport services which can handle given protocol
 boolean hasAgent(String agentId)
          Test if an agent exists
 JSONResponse invoke(String receiverId, JSONRequest request, RequestParams requestParams)
          Invoke a local agent
 void removeTransportService(TransportService transportService)
          Remove a registered a transport service
 JSONResponse send(String senderId, String receiverUrl, JSONRequest request)
          Invoke a local or remote agent.
 void sendAsync(String senderId, String receiverUrl, JSONRequest request, AsyncCallback<JSONResponse> callback)
          Asynchronously invoke a request on an agent.
 void setConfig(Config config)
          Get the loaded config file
static void setEnvironment(String env)
          Programmatically set the environment
 void setSchedulerFactory(Config config)
          Load a scheduler factory from a config file
 void setSchedulerFactory(SchedulerFactory schedulerFactory)
          Set a scheduler factory.
 void setStateFactory(Config config)
          Load a state factory from config
 void setStateFactory(StateFactory stateFactory)
          Set a state factory.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

AgentFactory

public AgentFactory()

AgentFactory

public AgentFactory(Config config)
             throws Exception
Construct an AgentFactory and initialize the configuration

Parameters:
config -
Throws:
Exception
Method Detail

getInstance

public static AgentFactory getInstance()
Get a shared AgentFactory instance with the default namespace "default"

Returns:
factory Returns the factory instance, or null when not existing

getInstance

public static AgentFactory getInstance(String namespace)
Get a shared AgentFactory instance with a specific namespace

Parameters:
namespace - If null, "default" namespace will be loaded.
Returns:
factory Returns the factory instance, or null when not existing

createInstance

public static AgentFactory createInstance()
                                   throws Exception
Create a shared AgentFactory instance with the default namespace "default"

Returns:
factory
Throws:
Exception

createInstance

public static AgentFactory createInstance(Config config)
                                   throws Exception
Create a shared AgentFactory instance with the default namespace "default"

Parameters:
config -
Returns:
factory
Throws:
Exception

createInstance

public static AgentFactory createInstance(String namespace)
                                   throws Exception
Create a shared AgentFactory instance with a specific namespace

Parameters:
namespace -
Returns:
factory
Throws:
Exception

createInstance

public static AgentFactory createInstance(String namespace,
                                          Config config)
                                   throws Exception
Create a shared AgentFactory instance with a specific namespace

Parameters:
namespace - If null, "default" namespace will be loaded.
config - If null, a non-configured AgentFactory will be created.
Returns:
factory
Throws:
Exception

getAgent

public Agent getAgent(String agentId)
               throws Exception
Get an agent by its id. Returns null if the agent does not exist Before deleting the agent, the method agent.destroy() must be executed to neatly shutdown the instantiated state.

Parameters:
agentId -
Returns:
agent
Throws:
Exception

createAgentProxy

public <T> T createAgentProxy(String senderId,
                              String receiverUrl,
                              Class<T> agentInterface)
Create an agent proxy from an java interface

Parameters:
senderId - Internal id of the sender agent. Not required for all transport services (for example not for outgoing HTTP requests)
receiverUrl - Url of the receiving agent
agentInterface - A java Interface, extending AgentInterface
Returns:

createAgent

public Agent createAgent(String agentType,
                         String agentId)
                  throws Exception
Create an agent. Before deleting the agent, the method agent.destroy() must be executed to neatly shutdown the instantiated state.

Parameters:
agentType - full class path
agentId -
Returns:
Throws:
Exception

createAgent

public Agent createAgent(Class<?> agentType,
                         String agentId)
                  throws Exception
Create an agent. Before deleting the agent, the method agent.destroy() must be executed to neatly shutdown the instantiated state.

Parameters:
agentType -
agentId -
Returns:
Throws:
Exception

deleteAgent

public void deleteAgent(String agentId)
                 throws Exception
Delete an agent

Parameters:
agentId -
Throws:
Exception

hasAgent

public boolean hasAgent(String agentId)
                 throws Exception
Test if an agent exists

Parameters:
agentId -
Returns:
true if the agent exists
Throws:
Exception

getEventLogger

public EventLogger getEventLogger()
Get the event logger. The event logger is used to temporary log triggered events, and display them on the agents web interface.

Returns:
eventLogger

invoke

public JSONResponse invoke(String receiverId,
                           JSONRequest request,
                           RequestParams requestParams)
                    throws Exception
Invoke a local agent

Parameters:
receiverId - Id of the receiver agent
request -
requestParams -
Returns:
Throws:
Exception

send

public JSONResponse send(String senderId,
                         String receiverUrl,
                         JSONRequest request)
                  throws Exception
Invoke a local or remote agent. In case of an local agent, the agent is invoked immediately. In case of an remote agent, an HTTP Request is sent to the concerning agent.

Parameters:
senderId - Internal id of the sender agent Not required for all transport services (for example not for outgoing HTTP requests)
receiverUrl -
request -
Returns:
Throws:
Exception

sendAsync

public void sendAsync(String senderId,
                      String receiverUrl,
                      JSONRequest request,
                      AsyncCallback<JSONResponse> callback)
               throws Exception
Asynchronously invoke a request on an agent.

Parameters:
senderId - Internal id of the sender agent. Not required for all transport services (for example not for outgoing HTTP requests)
receiverUrl -
request -
callback -
Throws:
Exception

getEnvironment

public static String getEnvironment()
Retrieve the current environment, using the configured State. Can return values like "Production", "Development". If no environment variable is found, "Production" is returned.

Returns:
environment

setEnvironment

public static void setEnvironment(String env)
Programmatically set the environment

Parameters:
env - The environment, for example "Production" or "Development"

setConfig

public void setConfig(Config config)
Get the loaded config file


getConfig

public Config getConfig()
Get the loaded config file

Returns:
config A configuration file

setStateFactory

public void setStateFactory(Config config)
Load a state factory from config

Parameters:
config -
Throws:
Exception

addAgents

public void addAgents(Config config)
Create agents from a config (only when they do not yet exist). Agents will be read from the configuration path bootstrap.agents, which must contain a map where the keys are agentId's and the values are the agent types (full java class path).

Parameters:
config -

setStateFactory

public void setStateFactory(StateFactory stateFactory)
Set a state factory. The state factory is used to get/create/delete an agents state.

Parameters:
stateFactory -

getStateFactory

public StateFactory getStateFactory()
                             throws Exception
Get the configured state factory.

Returns:
stateFactory
Throws:
Exception

setSchedulerFactory

public void setSchedulerFactory(Config config)
Load a scheduler factory from a config file

Parameters:
config -
Throws:
Exception

addTransportServices

public void addTransportServices(Config config)
Load transport services for incoming and outgoing messages from a config (for example http and xmpp services).

Parameters:
config -

addTransportService

public void addTransportService(TransportService transportService)
Add a new transport service

Parameters:
transportService -

removeTransportService

public void removeTransportService(TransportService transportService)
Remove a registered a transport service

Parameters:
transportService -

getTransportServices

public List<TransportService> getTransportServices()
Get all registered transport services

Returns:
transportService

getTransportServices

public List<TransportService> getTransportServices(String protocol)
Get all registered transport services which can handle given protocol

Parameters:
protocol - A protocol, for example "http" or "xmpp"
Returns:
transportService

getTransportService

public TransportService getTransportService(String protocol)
Get the first registered transport service which supports given protocol. Returns null when none of the registered transport services can handle the protocol.

Parameters:
protocol - A protocol, for example "http" or "xmpp"
Returns:
service

getMethods

public List<Object> getMethods(Agent agent,
                               Boolean asJSON)

setSchedulerFactory

public void setSchedulerFactory(SchedulerFactory schedulerFactory)
Set a scheduler factory. The scheduler factory is used to get/create/delete an agents scheduler.

Parameters:
schedulerFactory -

getScheduler

public Scheduler getScheduler(String agentId)
create a scheduler for an agent

Parameters:
agentId -
Returns:
scheduler


Copyright © 2013 Almende BV. All Rights Reserved.