cz.cuni.amis.pogamut.base.factory.guice
Class GuiceAgentFactory<AGENT extends IAgent,PARAMS extends IAgentParameters>

java.lang.Object
  extended by cz.cuni.amis.pogamut.base.factory.guice.AbstractGuiceAgentFactory
      extended by cz.cuni.amis.pogamut.base.factory.guice.GuiceAgentFactory<AGENT,PARAMS>
Type Parameters:
AGENT -
PARAMS -
All Implemented Interfaces:
IAgentFactory<AGENT,PARAMS>
Direct Known Subclasses:
GuiceRemoteAgentFactory, UT2004AnalyzerFactory, UT2004BotFactory, UT2004ObserverFactory, UT2004ServerFactory

public class GuiceAgentFactory<AGENT extends IAgent,PARAMS extends IAgentParameters>
extends AbstractGuiceAgentFactory
implements IAgentFactory<AGENT,PARAMS>

Guice-based IAgent factory that instantiates the agent according to the bindigs that are found inside the GuiceAgentModule, which is provided during the construction.

Additionally to the classic Injector.getInstance(Class) method, the class offers a place where to slip runtime dependencies. I.e., if you are unable to specify some dependencies during class coding, you need to create Providers for the runtime dependency (such as desired IAgentId for the IAgent). These dependencies are injected into the module via the method configureModule(IAgentParameters) that is called before the new instance is created.

Additionally, the factory method newAgent(IAgentParameters) clears the agent scope that is defined by the GuiceAgentModule.

NOTE: if you are going to extend the implementation, than the only place that should suffice you for hacking is configureModule(IAgentParameters) where you slip runtime dependencies that the base GuiceAgentFactory is unaware of, taking these dependencies from your custom IAgentParameters implementation.

NOTE: you might not need to override the configureModule(IAgentParameters) as the module configuration might be also done inside GuiceAgentModule.prepareNewAgent(IAgentParameters) which is implicitly called from the configureModule(IAgentParameters).

THREAD-SAFE


Constructor Summary
GuiceAgentFactory(GuiceAgentModule module)
          Creates a Guice-based factory that will use Injector created using the 'module'.
 
Method Summary
protected  void configureModule(PARAMS agentParameters)
          Called from within the newAgent(IAgentParameters) to configure the AbstractGuiceAgentFactory.getAgentModule() with variables from 'agentParams'.
 AGENT newAgent(PARAMS agentParameters)
          Creates a new instance of the IAgent interface that is cast to AGENT parameter.
 
Methods inherited from class cz.cuni.amis.pogamut.base.factory.guice.AbstractGuiceAgentFactory
getAgentModule, getInjector, setAgentModule
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

GuiceAgentFactory

public GuiceAgentFactory(GuiceAgentModule module)
Creates a Guice-based factory that will use Injector created using the 'module'.

The module MUST specify bindings for the IAgent interface as that is what's going to be instantiated using the injector from AbstractGuiceAgentFactory.getInjector().

Parameters:
module - module that configures bindings between classes, may be null (specify module later using AbstractGuiceAgentFactory.setAgentModule(GuiceAgentModule))
Method Detail

configureModule

protected void configureModule(PARAMS agentParameters)
Called from within the newAgent(IAgentParameters) to configure the AbstractGuiceAgentFactory.getAgentModule() with variables from 'agentParams'.

Just calls GuiceAgentModule.prepareNewAgent(IAgentParameters).

NOTE: You will probably need to override this method in subclasses. If you do - do not forget to call super.configureModule(agentParameters) first! So other runtime-dependencies can be set too.

Parameters:
agentParameters -

newAgent

public AGENT newAgent(PARAMS agentParameters)
                              throws PogamutException
Creates a new instance of the IAgent interface that is cast to AGENT parameter.

Firstly, it calls configureModule(IAgentParameters) to configure run-time dependencies of the module and prepare it for the new agent instance, secondly, it instantiates a new IAgent instance.

NOTE: that the GuiceAgentFactory must be correctly instantiated, i.e., the module passed into the constructor must bind IAgent interface to the AGENT (or descendant) class, otherwise you may experience ClassCastException.

Specified by:
newAgent in interface IAgentFactory<AGENT extends IAgent,PARAMS extends IAgentParameters>
Parameters:
agentParameters -
Returns:
agent instance configured with 'agentParameters'
Throws:
PogamutException