org.apache.tuscany.sca.core.scope
Class AbstractScopeContainer<KEY>

java.lang.Object
  extended by org.apache.tuscany.sca.core.scope.AbstractScopeContainer<KEY>
All Implemented Interfaces:
java.util.EventListener, ScopeContainer<KEY>, RuntimeEventListener
Direct Known Subclasses:
CompositeScopeContainer, ConversationalScopeContainer, HttpSessionScopeContainer, RequestScopeContainer, StatelessScopeContainer

public abstract class AbstractScopeContainer<KEY>
extends java.lang.Object
implements ScopeContainer<KEY>

Implements functionality common to scope contexts.


Field Summary
protected  RuntimeComponent component
           
protected  int lifecycleState
           
protected  Scope scope
           
protected  java.util.Map<KEY,InstanceWrapper<?>> wrappers
           
 
Fields inherited from interface org.apache.tuscany.sca.core.scope.ScopeContainer
CONFIG_ERROR, ERROR, INITIALIZED, INITIALIZING, RUNNING, STOPPED, STOPPING, UNINITIALIZED
 
Constructor Summary
AbstractScopeContainer(Scope scope, RuntimeComponent component)
           
 
Method Summary
 void addWrapperReference(KEY existingContextId, KEY newContextId)
          Allows a component to be registered against more than one context id.
protected  void checkInit()
           
protected  InstanceWrapper createInstanceWrapper()
          Creates a new physical instance of a component, wrapped in an InstanceWrapper.
 InstanceWrapper getAssociatedWrapper(KEY contextId)
          Returns an implementation instance associated with the current scope context.
 RuntimeComponent getComponent()
           
 int getLifecycleState()
          Returns the lifecycle state
 Scope getScope()
          Returns the Scope that this container supports.
 InstanceWrapper getWrapper(KEY contextId)
          Returns an instance wrapper associated with the current scope context, creating one if necessary
protected  boolean isEagerInit()
           
 void onEvent(Event event)
          Accepts and event and acts on it
 void registerWrapper(InstanceWrapper wrapper, KEY contextId)
          Register an existing instance against a context id.
 void remove(KEY contextId)
          Default implementation of remove which does nothing
 void returnWrapper(InstanceWrapper wrapper, KEY contextId)
          Return a wrapper after use (for example, after invoking the instance).
 void setComponent(RuntimeComponent component)
           
protected  void setLifecycleState(int lifecycleState)
          Set the current state of the Lifecycle.
 void start()
          Starts the Lifecycle.
 void startContext(KEY contextId)
          Start a new context with the supplied ID.
 void stop()
          Stops the Lifecycle.
 void stopContext(KEY contextId)
          Stop the context with the supplied ID.
 java.lang.String toString()
           
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

wrappers

protected java.util.Map<KEY,InstanceWrapper<?>> wrappers

scope

protected final Scope scope

component

protected RuntimeComponent component

lifecycleState

protected volatile int lifecycleState
Constructor Detail

AbstractScopeContainer

public AbstractScopeContainer(Scope scope,
                              RuntimeComponent component)
Method Detail

checkInit

protected void checkInit()

createInstanceWrapper

protected InstanceWrapper createInstanceWrapper()
                                         throws TargetResolutionException
Creates a new physical instance of a component, wrapped in an InstanceWrapper.

Parameters:
component - the component whose instance should be created
Returns:
a wrapped instance that has been injected but not yet started
Throws:
TargetResolutionException - if there was a problem creating the instance

getAssociatedWrapper

public InstanceWrapper getAssociatedWrapper(KEY contextId)
                                     throws TargetResolutionException
Description copied from interface: ScopeContainer
Returns an implementation instance associated with the current scope context. If no instance is found, a TargetNotFoundException is thrown.

Specified by:
getAssociatedWrapper in interface ScopeContainer<KEY>
Parameters:
contextId - the id for the scope context
Returns:
the wrapper for the target instance
Throws:
TargetResolutionException - if there was a problem instantiating the target instance

getScope

public Scope getScope()
Description copied from interface: ScopeContainer
Returns the Scope that this container supports.

Specified by:
getScope in interface ScopeContainer<KEY>
Returns:
the Scope that this container supports

getWrapper

public InstanceWrapper getWrapper(KEY contextId)
                           throws TargetResolutionException
Description copied from interface: ScopeContainer
Returns an instance wrapper associated with the current scope context, creating one if necessary

Specified by:
getWrapper in interface ScopeContainer<KEY>
Parameters:
contextId - the id for the scope context
Returns:
the wrapper for the target instance
Throws:
TargetResolutionException - if there was a problem instantiating the target instance

addWrapperReference

public void addWrapperReference(KEY existingContextId,
                                KEY newContextId)
                         throws TargetResolutionException
Description copied from interface: ScopeContainer
Allows a component to be registered against more than one context id. This is required in the case of stateful callbacks where we want to identify the originating client component instance as the callback target but we don't want to reuse the clients original conversation id

Specified by:
addWrapperReference in interface ScopeContainer<KEY>
Parameters:
existingContextId - an id that identifies an existing component instance
newContextId - a new id against which this component will also be registered
Throws:
TargetResolutionException

registerWrapper

public void registerWrapper(InstanceWrapper wrapper,
                            KEY contextId)
                     throws TargetResolutionException
Description copied from interface: ScopeContainer
Register an existing instance against a context id. This is needed for a stateful callback where the service reference for the forward call contains a callback object that is not a service reference.

Specified by:
registerWrapper in interface ScopeContainer<KEY>
Parameters:
wrapper - the instance wrapper for the instance to be registered
contextId - the id for the scope context
Throws:
TargetResolutionException

onEvent

public void onEvent(Event event)
Description copied from interface: RuntimeEventListener
Accepts and event and acts on it

Specified by:
onEvent in interface RuntimeEventListener
Parameters:
event - The event to act on

isEagerInit

protected boolean isEagerInit()

returnWrapper

public void returnWrapper(InstanceWrapper wrapper,
                          KEY contextId)
                   throws TargetDestructionException
Description copied from interface: ScopeContainer
Return a wrapper after use (for example, after invoking the instance).

Specified by:
returnWrapper in interface ScopeContainer<KEY>
Parameters:
wrapper - the wrapper for the target instance being returned
contextId - the id for the scope context
Throws:
TargetDestructionException - if there was a problem returning the target instance

remove

public void remove(KEY contextId)
            throws TargetDestructionException
Default implementation of remove which does nothing

Specified by:
remove in interface ScopeContainer<KEY>
Parameters:
contextId - the identifier of the context to remove.
Throws:
TargetDestructionException

start

public void start()
Description copied from interface: ScopeContainer
Starts the Lifecycle.

Specified by:
start in interface ScopeContainer<KEY>

startContext

public void startContext(KEY contextId)
Description copied from interface: ScopeContainer
Start a new context with the supplied ID.

Specified by:
startContext in interface ScopeContainer<KEY>
Parameters:
contextId - an ID that uniquely identifies the context.

stop

public void stop()
Description copied from interface: ScopeContainer
Stops the Lifecycle.

Specified by:
stop in interface ScopeContainer<KEY>

stopContext

public void stopContext(KEY contextId)
Description copied from interface: ScopeContainer
Stop the context with the supplied ID.

Specified by:
stopContext in interface ScopeContainer<KEY>
Parameters:
contextId - an ID that uniquely identifies the context.

toString

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

getComponent

public RuntimeComponent getComponent()

setComponent

public void setComponent(RuntimeComponent component)

getLifecycleState

public int getLifecycleState()
Description copied from interface: ScopeContainer
Returns the lifecycle state

Specified by:
getLifecycleState in interface ScopeContainer<KEY>
See Also:
ScopeContainer.UNINITIALIZED, ScopeContainer.INITIALIZING, ScopeContainer.INITIALIZED, ScopeContainer.RUNNING, ScopeContainer.STOPPING, ScopeContainer.STOPPED

setLifecycleState

protected void setLifecycleState(int lifecycleState)
Set the current state of the Lifecycle.

Parameters:
lifecycleState - the new state