eXo Kernel :: Container 2.5.0-GA

org.exoplatform.container
Class ConcurrentContainer

java.lang.Object
  extended by org.exoplatform.container.AbstractContainer
      extended by org.exoplatform.container.AbstractInterceptor
          extended by org.exoplatform.container.ConcurrentContainer
All Implemented Interfaces:
Serializable, Container, Interceptor, Disposable, Startable

public class ConcurrentContainer
extends AbstractInterceptor

This class is aimed to be a ThreadSafe implementation of an Container based on java.util.concurrent collections. Created by The eXo Platform SAS Author : Nicolas Filotto nicolas.filotto@exoplatform.com 6 mai 2010

Version:
$Revision$
See Also:
Serialized Form

Nested Class Summary
static class ConcurrentContainer.CreationalContextComponentAdapter<T>
          This class is used as value holder
 
Field Summary
protected  Set<ExoContainer> children
           
protected  Set<ComponentAdapter<?>> componentAdapters
           
protected  ConcurrentMap<Object,ComponentAdapter<?>> componentKeyToAdapterCache
           
protected  CopyOnWriteArrayList<ComponentAdapter<?>> orderedComponentAdapters
           
 
Fields inherited from class org.exoplatform.container.AbstractInterceptor
holder, parent
 
Fields inherited from class org.exoplatform.container.AbstractContainer
delegate
 
Constructor Summary
ConcurrentContainer()
          Creates a new container with the default ComponentAdapterFactory and a parent container.
ConcurrentContainer(ExoContainer holder, ExoContainer parent)
          Creates a new container with the default ComponentAdapterFactory and a parent container.
 
Method Summary
 void accept(ContainerVisitor visitor)
          Accepts a visitor that should visit the child containers, component adapters and component instances.
<T> ConcurrentContainer.CreationalContextComponentAdapter<T>
addComponentToCtx(Object key)
          Add the CreationalContext corresponding to the given key, to the dependency resolution context
protected  void addMethods(Class<?> c, Map<String,Method> methodAlreadyRegistered, Map<Class<?>,Collection<Method>> methodsPerClass)
           
protected  void addOrderedComponentAdapter(ComponentAdapter<?> componentAdapter)
           
<T> T
createComponent(Class<T> clazz)
           
<T> T
createComponent(Class<T> clazz, InitParams params)
          Creates a component corresponding to the given Class with the given InitParams
 void dispose()
          Dispose the components of this Container and all its logical child containers.
protected  CachingContainer getCache()
          Gives the cache if already found otherwise it will get it from the interceptor chain
<T> ComponentAdapter<T>
getComponentAdapter(Object componentKey, Class<T> bindType, boolean autoRegistration)
          Find a component adapter associated with the specified key.
<T> ComponentAdapter<T>
getComponentAdapterOfType(Class<T> componentType, boolean autoRegistration)
          Find a component adapter associated with the specified type.
 Collection<ComponentAdapter<?>> getComponentAdapters()
          Retrieve all the component adapters inside this container.
<T> List<ComponentAdapter<T>>
getComponentAdaptersOfType(Class<T> componentType)
          Retrieve all component adapters inside this container that are associated with the specified type.
<T> T
getComponentInstance(Object componentKey, Class<T> bindType, boolean autoRegistration)
          Retrieve a component instance registered with a specific key.
protected
<T> T
getComponentInstanceFromContext(ComponentAdapter<T> componentAdapter, Class<T> bindType)
          Gets the component instance from the context
<T> T
getComponentInstanceOfType(Class<T> componentType, boolean autoRegistration)
          If no ConcurrentContainer.CreationalContextComponentAdapter can be found it returns null otherwise it first try to get it from the dependency resolution context if it still cannot be found we get the instance from the ConcurrentContainer.CreationalContextComponentAdapter.
 List<Object> getComponentInstances()
           
<T> List<T>
getComponentInstancesOfType(Class<T> componentType)
          Returns a List of components of a certain componentType.
protected  ComponentAdapterFactory getDefaultComponentAdapterFactory()
           
 ExoContainer getHolder()
           
protected
<T> T
getInstance(ComponentAdapter<T> componentAdapter, Class<T> type, boolean autoRegistration)
           
 ManagementContext getManagementContext()
          Gives the corresponding ManagementContext
 MBeanServer getMBeanServer()
          Provides the MBeanServer this method is needed for backward compatibility
 ObjectName getScopingObjectName()
          Gives the ObjectName of the container build from the scoping data
 void initialize()
          Initializes the container
<T> boolean
initializeComponent(T instance)
          Initializes the instance by injecting objects into fields and the methods with the annotation Inject
protected  ComponentAdapter<?> registerComponent(ComponentAdapter<?> componentAdapter)
          Register a component via a ComponentAdapter.
<T> ComponentAdapter<T>
registerComponentImplementation(Object componentKey, Class<T> componentImplementation)
          Register a component.
<T> ComponentAdapter<T>
registerComponentInstance(Object componentKey, T componentInstance)
          Register an arbitrary object as a component in the container.
 void removeComponentFromCtx(Object key)
          Remove the component corresponding to the given key, from the dependency resolution context
 void setHolder(ExoContainer holder)
          Sets the holder which is mostly used when it is required to be able to go through the chain of Interceptor
 void start()
          Start the components of this Container and all its logical child containers.
 void stop()
          Stop the components of this Container and all its logical child containers.
 ComponentAdapter<?> unregisterComponent(Object componentKey)
          Unregister a component by key.
 
Methods inherited from class org.exoplatform.container.AbstractInterceptor
getId, setParent, setSuccessor
 
Methods inherited from class org.exoplatform.container.AbstractContainer
getSuccessor
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 
Methods inherited from interface org.exoplatform.container.spi.Container
getSuccessor
 

Field Detail

componentKeyToAdapterCache

protected final ConcurrentMap<Object,ComponentAdapter<?>> componentKeyToAdapterCache

componentAdapters

protected final Set<ComponentAdapter<?>> componentAdapters

orderedComponentAdapters

protected final CopyOnWriteArrayList<ComponentAdapter<?>> orderedComponentAdapters

children

protected final Set<ExoContainer> children
Constructor Detail

ConcurrentContainer

public ConcurrentContainer()
Creates a new container with the default ComponentAdapterFactory and a parent container.


ConcurrentContainer

public ConcurrentContainer(ExoContainer holder,
                           ExoContainer parent)
Creates a new container with the default ComponentAdapterFactory and a parent container.

Parameters:
holder - the holder of the container
parent - the parent container (used for component dependency lookups).
Method Detail

getComponentAdapters

public Collection<ComponentAdapter<?>> getComponentAdapters()
Description copied from class: AbstractContainer
Retrieve all the component adapters inside this container. The component adapters from the parent container are not returned.

Specified by:
getComponentAdapters in interface Container
Overrides:
getComponentAdapters in class AbstractContainer
Returns:
a collection containing all the ComponentAdapters inside this container. The collection will not be modifiable.
See Also:
a variant of this method which returns the component adapters inside this container that are associated with the specified type.

setHolder

public void setHolder(ExoContainer holder)
Description copied from class: AbstractInterceptor
Sets the holder which is mostly used when it is required to be able to go through the chain of Interceptor

Specified by:
setHolder in interface Interceptor
Overrides:
setHolder in class AbstractInterceptor
Parameters:
holder - the holder of the container

getDefaultComponentAdapterFactory

protected ComponentAdapterFactory getDefaultComponentAdapterFactory()

getComponentAdapter

public <T> ComponentAdapter<T> getComponentAdapter(Object componentKey,
                                                   Class<T> bindType,
                                                   boolean autoRegistration)
                                        throws ContainerException
Description copied from class: AbstractContainer
Find a component adapter associated with the specified key. If a component adapter cannot be found in this container, the parent container (if one exists) will be searched.

Specified by:
getComponentAdapter in interface Container
Overrides:
getComponentAdapter in class AbstractContainer
Parameters:
componentKey - the key that the component was registered with.
bindType - the expected raw type of the adapter if one can be found.
autoRegistration - indicates whether the auto registration should be performed or not
Returns:
the component adapter associated with this key, or null if no component has been registered for the specified key.
Throws:
ContainerException

getComponentAdapterOfType

public <T> ComponentAdapter<T> getComponentAdapterOfType(Class<T> componentType,
                                                         boolean autoRegistration)
Description copied from class: AbstractContainer
Find a component adapter associated with the specified type. If a component adapter cannot be found in this container, the parent container (if one exists) will be searched.

Specified by:
getComponentAdapterOfType in interface Container
Overrides:
getComponentAdapterOfType in class AbstractContainer
Parameters:
componentType - the type of the component.
autoRegistration - indicates whether the auto registration should be performed or not
Returns:
the component adapter associated with this class, or null if no component has been registered for the specified key.

getComponentAdaptersOfType

public <T> List<ComponentAdapter<T>> getComponentAdaptersOfType(Class<T> componentType)
Description copied from class: AbstractContainer
Retrieve all component adapters inside this container that are associated with the specified type. The component adapters from the parent container are not returned.

Specified by:
getComponentAdaptersOfType in interface Container
Overrides:
getComponentAdaptersOfType in class AbstractContainer
Parameters:
componentType - the type of the components.
Returns:
a collection containing all the ComponentAdapters inside this container that are associated with the specified type. Changes to this collection will not be reflected in the container itself.

registerComponent

protected ComponentAdapter<?> registerComponent(ComponentAdapter<?> componentAdapter)
                                         throws ContainerException
Register a component via a ComponentAdapter. Use this if you need fine grained control over what ComponentAdapter to use for a specific component.

Parameters:
componentAdapter - the adapter
Returns:
the same adapter that was passed as an argument.
Throws:
ContainerException - if registration fails.

unregisterComponent

public ComponentAdapter<?> unregisterComponent(Object componentKey)
Description copied from class: AbstractContainer
Unregister a component by key.

Specified by:
unregisterComponent in interface Container
Overrides:
unregisterComponent in class AbstractContainer
Parameters:
componentKey - key of the component to unregister.
Returns:
the ComponentAdapter that was associated with this component.

registerComponentInstance

public <T> ComponentAdapter<T> registerComponentInstance(Object componentKey,
                                                         T componentInstance)
                                              throws ContainerException
Register an arbitrary object as a component in the container. This is handy when other components in the same container have dependencies on this kind of object, but where letting the container manage and instantiate it is impossible.

Beware that too much use of this method is an antipattern. The returned ComponentAdapter will be an InstanceComponentAdapter.

Specified by:
registerComponentInstance in interface Container
Overrides:
registerComponentInstance in class AbstractContainer
Parameters:
componentKey - a key that identifies the component. Must be unique within the container. The type of the key object has no semantic significance unless explicitly specified in the implementing container.
componentInstance - an arbitrary object.
Returns:
the ComponentAdapter that has been associated with this component. In the majority of cases, this return value can be safely ignored, as one of the getXXX() methods of the Container interface can be used to retrieve a reference to the component later on.
Throws:
ContainerException - if registration fails.

registerComponentImplementation

public <T> ComponentAdapter<T> registerComponentImplementation(Object componentKey,
                                                               Class<T> componentImplementation)
                                                    throws ContainerException
Register a component. The returned ComponentAdapter will be instantiated by the ComponentAdapterFactory passed to the container's constructor.

Specified by:
registerComponentImplementation in interface Container
Overrides:
registerComponentImplementation in class AbstractContainer
Parameters:
componentKey - a key that identifies the component. Must be unique within the container. The type of the key object has no semantic significance unless explicitly specified in the documentation of the implementing container.
componentImplementation - the component's implementation class. This must be a concrete class (ie, a class that can be instantiated).
Returns:
the ComponentAdapter that has been associated with this component. In the majority of cases, this return value can be safely ignored, as one of the getXXX() methods of the Container interface can be used to retrieve a reference to the component later on.
Throws:
ContainerException

addOrderedComponentAdapter

protected void addOrderedComponentAdapter(ComponentAdapter<?> componentAdapter)

getComponentInstances

public List<Object> getComponentInstances()
                                   throws ContainerException
Throws:
ContainerException

getComponentInstancesOfType

public <T> List<T> getComponentInstancesOfType(Class<T> componentType)
                                    throws ContainerException
Description copied from class: AbstractContainer
Returns a List of components of a certain componentType. The list is ordered by instantiation order, starting with the components instantiated first at the beginning.

Specified by:
getComponentInstancesOfType in interface Container
Overrides:
getComponentInstancesOfType in class AbstractContainer
Parameters:
componentType - the searched type.
Returns:
a List of components.
Throws:
ContainerException

getComponentInstance

public <T> T getComponentInstance(Object componentKey,
                                  Class<T> bindType,
                                  boolean autoRegistration)
                       throws ContainerException
Description copied from class: AbstractContainer
Retrieve a component instance registered with a specific key. If a component cannot be found in this container, the parent container (if one exists) will be searched.

Specified by:
getComponentInstance in interface Container
Overrides:
getComponentInstance in class AbstractContainer
Parameters:
componentKey - the key that the component was registered with.
bindType - the expected type of the instance if one can be found.
autoRegistration - indicates whether the auto registration should be performed or not
Returns:
an instantiated component, or null if no component has been registered for the specified key.
Throws:
ContainerException

getComponentInstanceFromContext

protected <T> T getComponentInstanceFromContext(ComponentAdapter<T> componentAdapter,
                                                Class<T> bindType)
Gets the component instance from the context


getComponentInstanceOfType

public <T> T getComponentInstanceOfType(Class<T> componentType,
                                        boolean autoRegistration)
If no ConcurrentContainer.CreationalContextComponentAdapter can be found it returns null otherwise it first try to get it from the dependency resolution context if it still cannot be found we get the instance from the ConcurrentContainer.CreationalContextComponentAdapter.

Specified by:
getComponentInstanceOfType in interface Container
Overrides:
getComponentInstanceOfType in class AbstractContainer
Parameters:
componentType - the type of the component.
autoRegistration - indicates whether the auto registration should be performed or not
Returns:
the adapter matching the class.
See Also:
Container#getComponentInstanceOfType(java.lang.Class)

addComponentToCtx

public <T> ConcurrentContainer.CreationalContextComponentAdapter<T> addComponentToCtx(Object key)
Add the CreationalContext corresponding to the given key, to the dependency resolution context

Parameters:
key - The key of the component to add to the resolution context

removeComponentFromCtx

public void removeComponentFromCtx(Object key)
Remove the component corresponding to the given key, from the dependency resolution context

Parameters:
key - The key of the component to remove from the context

getInstance

protected <T> T getInstance(ComponentAdapter<T> componentAdapter,
                            Class<T> type,
                            boolean autoRegistration)

start

public void start()
Start the components of this Container and all its logical child containers. Any component implementing the lifecycle interface Startable will be started.

Specified by:
start in interface Startable
Overrides:
start in class AbstractContainer

stop

public void stop()
Stop the components of this Container and all its logical child containers. Any component implementing the lifecycle interface Startable will be stopped.

Specified by:
stop in interface Startable
Overrides:
stop in class AbstractContainer

dispose

public void dispose()
Dispose the components of this Container and all its logical child containers. Any component implementing the lifecycle interface Disposable will be disposed.

Specified by:
dispose in interface Disposable
Overrides:
dispose in class AbstractContainer

accept

public void accept(ContainerVisitor visitor)
Description copied from class: AbstractContainer
Accepts a visitor that should visit the child containers, component adapters and component instances.

Specified by:
accept in interface Container
Overrides:
accept in class AbstractContainer
Parameters:
visitor - the visitor

getHolder

public ExoContainer getHolder()
Returns:
the holder

getManagementContext

public ManagementContext getManagementContext()
Gives the corresponding ManagementContext

Specified by:
getManagementContext in interface Container
Overrides:
getManagementContext in class AbstractContainer

getMBeanServer

public MBeanServer getMBeanServer()
Provides the MBeanServer this method is needed for backward compatibility

Specified by:
getMBeanServer in interface Container
Overrides:
getMBeanServer in class AbstractContainer

getScopingObjectName

public ObjectName getScopingObjectName()
Gives the ObjectName of the container build from the scoping data

Specified by:
getScopingObjectName in interface Container
Overrides:
getScopingObjectName in class AbstractContainer

createComponent

public <T> T createComponent(Class<T> clazz)
                  throws Exception
Throws:
Exception

createComponent

public <T> T createComponent(Class<T> clazz,
                             InitParams params)
                  throws Exception
Creates a component corresponding to the given Class with the given InitParams

Specified by:
createComponent in interface Container
Overrides:
createComponent in class AbstractContainer
Parameters:
clazz - the Class of the object to create
params - the parameters to use to create the component
Returns:
an instance of the component
Throws:
Exception - if any issue occurs while creating the component.

initializeComponent

public <T> boolean initializeComponent(T instance)
Initializes the instance by injecting objects into fields and the methods with the annotation Inject

Returns:
true if at least Inject annotation has been found, false otherwise

addMethods

protected void addMethods(Class<?> c,
                          Map<String,Method> methodAlreadyRegistered,
                          Map<Class<?>,Collection<Method>> methodsPerClass)

initialize

public void initialize()
Initializes the container

Specified by:
initialize in interface Container
Overrides:
initialize in class AbstractContainer

getCache

protected CachingContainer getCache()
Gives the cache if already found otherwise it will get it from the interceptor chain


eXo Kernel :: Container 2.5.0-GA

Copyright © 2014 eXo Platform SAS. All Rights Reserved.