cz.cuni.amis.pogamut.base.component.controller
Class AbstractComponentControllerBase<COMPONENT extends IComponent>

java.lang.Object
  extended by cz.cuni.amis.pogamut.base.component.controller.AbstractComponentControllerBase<COMPONENT>
All Implemented Interfaces:
IComponentControllerBase<COMPONENT>, IComponent
Direct Known Subclasses:
ComponentController, SharedComponentController

public abstract class AbstractComponentControllerBase<COMPONENT extends IComponent>
extends java.lang.Object
implements IComponentControllerBase<COMPONENT>

Abstract class implementing some methods from IComponentControllerBase.

Namely:

And all methods from IComponent.

Suitable for creating custom IComponentControllers or ISharedComponentControllers.


Nested Class Summary
protected static class AbstractComponentControllerBase.AwaitState
          Used for filtering states we're awaiting on.
 
Field Summary
protected  boolean broadcastingEvents
          Tells whether the controller sends events about the state of the component, i.e., whether it should automatically send starting/stopping events or not.
protected  COMPONENT component
          Component controlled by this controller.
protected  Flag<ComponentState> componentState
          State of the controlled component.
protected  IComponentControlHelper control
          Method providing means for direct control of the component.
protected  IToken controllerId
          Unique (in context of one agent) id of this controller.
protected  java.util.logging.Logger log
          Log used by the class, is never null.
 
Constructor Summary
AbstractComponentControllerBase(COMPONENT component, IComponentControlHelper componentControlHelper, java.util.logging.Logger log)
          Initialize the controller.
AbstractComponentControllerBase(IToken componentControllerId, COMPONENT component, IComponentControlHelper componentControlHelper, java.util.logging.Logger log)
          Initialize controller with specific componentControllerId.
 
Method Summary
 ComponentState awaitState(ComponentState... states)
          Waits until the component reaches one of 'states' or KILLING / KILLED state is reached.
 ComponentState awaitState(long timeoutMillis, ComponentState... states)
          Waits until the component reaches one of 'states' or KILLING / KILLED state is reached.
 COMPONENT getComponent()
          Returns controlled component instance.
 IComponentControlHelper getComponentControl()
          Returns component control with lifecycle methods of the component controlled by this instance.
 IToken getComponentId()
          Unique identification of the component.
 java.util.logging.Logger getLog()
           
 ImmutableFlag<ComponentState> getState()
          Returns state of the controlled component (state of the component life-cycle).
protected  java.lang.String id(IComponent component)
          Returns component id or null.
 boolean inState(ComponentState... states)
          Whether the component is in one of 'states'.
 boolean isBroadcastingEvents()
          Tells whether the controller sends events about the state of the component, i.e., whether it should automatically send starting/stopping events or not.
 boolean isPaused()
          Whether the component is paused (or is pausing/resuming).
 boolean isRunning()
          Whether the component has been started, is not stopped or killed, may be paused.
 boolean notInState(ComponentState... states)
          Whether the component is not in any of 'states'.
 void setBroadcastingEvents(boolean broadcastingEvents)
          Enables (== true) / Disables (== false) sending events about the state of the component, i.e., whether it should automatically send starting/stopping events or not.
protected  void setState(ComponentState state)
          Changes the componentState to desired state.
 java.lang.String toString()
           
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 
Methods inherited from interface cz.cuni.amis.pogamut.base.component.controller.IComponentControllerBase
fatalError, fatalError, getFatalError, manualKill, manualPause, manualResume, manualStart, manualStartPaused, manualStop
 

Field Detail

controllerId

protected IToken controllerId
Unique (in context of one agent) id of this controller.


control

protected IComponentControlHelper control
Method providing means for direct control of the component. This object is passed from the outside of the controller, usually created by the component itself.


component

protected COMPONENT extends IComponent component
Component controlled by this controller.


log

protected java.util.logging.Logger log
Log used by the class, is never null.


componentState

protected Flag<ComponentState> componentState
State of the controlled component.

Use setState(ComponentState) to alter the value of the flag.

Should not be set manually, use setState(ComponentState) instead.


broadcastingEvents

protected boolean broadcastingEvents
Tells whether the controller sends events about the state of the component, i.e., whether it should automatically send starting/stopping events or not.

DEFAULT: TRUE (the controller is broadcasting events as default)

Exception: IComponentControllerBase.fatalError(String) and IComponentControllerBase.fatalError(String, Throwable) must always send fatal error!

Constructor Detail

AbstractComponentControllerBase

public AbstractComponentControllerBase(COMPONENT component,
                                       IComponentControlHelper componentControlHelper,
                                       java.util.logging.Logger log)
Initialize the controller. This constructor is auto-creating generic controllerId (adding suffix "-controller" to the IComponent.getComponentId().

Parameters:
component -
componentControlHelper -
log -

AbstractComponentControllerBase

public AbstractComponentControllerBase(IToken componentControllerId,
                                       COMPONENT component,
                                       IComponentControlHelper componentControlHelper,
                                       java.util.logging.Logger log)
Initialize controller with specific componentControllerId.

Parameters:
componentControllerId -
component -
componentControlHelper -
log -
Method Detail

toString

public java.lang.String toString()
Overrides:
toString in class java.lang.Object

getComponentId

public IToken getComponentId()
Description copied from interface: IComponent
Unique identification of the component.

Specified by:
getComponentId in interface IComponent
Returns:

getLog

public java.util.logging.Logger getLog()

getComponent

public COMPONENT getComponent()
Description copied from interface: IComponentControllerBase
Returns controlled component instance.

Specified by:
getComponent in interface IComponentControllerBase<COMPONENT extends IComponent>
Returns:
controlled component

isBroadcastingEvents

public boolean isBroadcastingEvents()
Description copied from interface: IComponentControllerBase
Tells whether the controller sends events about the state of the component, i.e., whether it should automatically send starting/stopping events or not.

DEFAULT: TRUE (the controller is broadcasting events as default)

Exception: IComponentControllerBase.fatalError(String) and IComponentControllerBase.fatalError(String, Throwable) must always send fatal error!

Specified by:
isBroadcastingEvents in interface IComponentControllerBase<COMPONENT extends IComponent>
Returns:

setBroadcastingEvents

public void setBroadcastingEvents(boolean broadcastingEvents)
Description copied from interface: IComponentControllerBase
Enables (== true) / Disables (== false) sending events about the state of the component, i.e., whether it should automatically send starting/stopping events or not.

Exception: IComponentControllerBase.fatalError(String) and IComponentControllerBase.fatalError(String, Throwable) must always send fatal error!

Specified by:
setBroadcastingEvents in interface IComponentControllerBase<COMPONENT extends IComponent>
Parameters:
broadcastingEvents - Enables (== true) / Disables (== false)

getComponentControl

public IComponentControlHelper getComponentControl()
Description copied from interface: IComponentControllerBase
Returns component control with lifecycle methods of the component controlled by this instance. I.e., methods that are directly controlling the component IComponentControllerBase.getComponent().

IT IS DISCOURAGED TO USE METHODS OF THE IComponentControlHelper DIRECTLY! IT DEFIES THE PURPOSE OF THE CONTROLLER TOTALLY AND THE CONTROLLER WILL PROBABLY WON'T COPE WITH SUCH BEHAVIOR.

But what the hell, if it solves your problem, go ahead ;-)

Specified by:
getComponentControl in interface IComponentControllerBase<COMPONENT extends IComponent>
Returns:

isRunning

public boolean isRunning()
Description copied from interface: IComponentControllerBase
Whether the component has been started, is not stopped or killed, may be paused.

Specified by:
isRunning in interface IComponentControllerBase<COMPONENT extends IComponent>
Returns:
component is running

isPaused

public boolean isPaused()
Description copied from interface: IComponentControllerBase
Whether the component is paused (or is pausing/resuming).

Specified by:
isPaused in interface IComponentControllerBase<COMPONENT extends IComponent>
Returns:

getState

public ImmutableFlag<ComponentState> getState()
Description copied from interface: IComponentControllerBase
Returns state of the controlled component (state of the component life-cycle).

It returns flag - therefore you may use WaitForFlagChange to synchronize on the flag changes in other threads or use awaitState() method.

Specified by:
getState in interface IComponentControllerBase<COMPONENT extends IComponent>
Returns:
immutable flag

inState

public boolean inState(ComponentState... states)
Description copied from interface: IComponentControllerBase
Whether the component is in one of 'states'.

Specified by:
inState in interface IComponentControllerBase<COMPONENT extends IComponent>
Returns:

notInState

public boolean notInState(ComponentState... states)
Description copied from interface: IComponentControllerBase
Whether the component is not in any of 'states'.

Specified by:
notInState in interface IComponentControllerBase<COMPONENT extends IComponent>
Returns:

awaitState

public ComponentState awaitState(ComponentState... states)
                          throws ComponentKilledException
Description copied from interface: IComponentControllerBase
Waits until the component reaches one of 'states' or KILLING / KILLED state is reached.

If KILLING / KILLED state is not among 'states' then reaching of this state will throw ComponentKilledException exception.

If interrupted, PogamutInterruptedException is thrown.

Specified by:
awaitState in interface IComponentControllerBase<COMPONENT extends IComponent>
Returns:
reached component state
Throws:
ComponentKilledException
ComponentKilledException

awaitState

public ComponentState awaitState(long timeoutMillis,
                                 ComponentState... states)
                          throws ComponentKilledException
Description copied from interface: IComponentControllerBase
Waits until the component reaches one of 'states' or KILLING / KILLED state is reached.

If KILLING / KILLED state is not among 'states' then reaching of this state will throw ComponentKilledException exception.

If interrupted, PogamutInterruptedException is thrown.

If times out, null is returned.

Specified by:
awaitState in interface IComponentControllerBase<COMPONENT extends IComponent>
Returns:
reached component state
Throws:
ComponentKilledException
ComponentKilledException

id

protected java.lang.String id(IComponent component)
Returns component id or null.

Parameters:
component -
Returns:

setState

protected void setState(ComponentState state)
Changes the componentState to desired state.

Parameters:
state -