Package org.exoplatform.container
Class ConcurrentContainer
- java.lang.Object
-
- org.exoplatform.container.AbstractContainer
-
- org.exoplatform.container.AbstractInterceptor
-
- 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 anContainerbased 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
Nested Classes Modifier and Type Class Description static classConcurrentContainer.CreationalContextComponentAdapter<T>This class is used as value holder
-
Field Summary
Fields Modifier and Type Field Description protected Set<ExoContainer>childrenprotected Set<ComponentAdapter<?>>componentAdaptersprotected ConcurrentMap<Object,ComponentAdapter<?>>componentKeyToAdapterCacheprotected CopyOnWriteArrayList<ComponentAdapter<?>>orderedComponentAdapters-
Fields inherited from class org.exoplatform.container.AbstractInterceptor
holder, parent
-
Fields inherited from class org.exoplatform.container.AbstractContainer
delegate
-
-
Constructor Summary
Constructors Constructor Description ConcurrentContainer()Creates a new container with the defaultComponentAdapterFactoryand a parent container.ConcurrentContainer(ExoContainer holder, ExoContainer parent)Creates a new container with the defaultComponentAdapterFactoryand a parent container.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description voidaccept(ContainerVisitor visitor)Accepts a visitor that should visit the child containers, component adapters and component instances.<T> ConcurrentContainer.CreationalContextComponentAdapter<T>addComponentToCtx(Object key)Add theCreationalContextcorresponding to the given key, to the dependency resolution contextprotected voidaddMethods(Class<?> c, Map<String,Method> methodAlreadyRegistered, Map<Class<?>,Collection<Method>> methodsPerClass)protected voidaddOrderedComponentAdapter(ComponentAdapter<?> componentAdapter)<T> TcreateComponent(Class<T> clazz)<T> TcreateComponent(Class<T> clazz, InitParams params)Creates a component corresponding to the givenClasswith the givenInitParamsvoiddispose()Dispose the components of this Container and all its logical child containers.protected CachingContainergetCache()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> TgetComponentInstance(Object componentKey, Class<T> bindType, boolean autoRegistration)Retrieve a component instance registered with a specific key.protected <T> TgetComponentInstanceFromContext(ComponentAdapter<T> componentAdapter, Class<T> bindType)Gets the component instance from the context<T> TgetComponentInstanceOfType(Class<T> componentType, boolean autoRegistration)If noComponentAdaptercan 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 theComponentAdapter.List<Object>getComponentInstances()<T> List<T>getComponentInstancesOfType(Class<T> componentType)Returns a List of components of a certain componentType.protected ComponentAdapterFactorygetDefaultComponentAdapterFactory()ExoContainergetHolder()protected <T> TgetInstance(ComponentAdapter<T> componentAdapter, Class<T> type, boolean autoRegistration)ManagementContextgetManagementContext()Gives the correspondingManagementContextMBeanServergetMBeanServer()Provides theMBeanServerthis method is needed for backward compatibilityObjectNamegetScopingObjectName()Gives the ObjectName of the container build from the scoping datavoidinitialize()Initializes the container<T> booleaninitializeComponent(T instance)Initializes the instance by injecting objects into fields and the methods with the annotationInjectprotected 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.voidremoveComponentFromCtx(Object key)Remove the component corresponding to the given key, from the dependency resolution contextvoidsetHolder(ExoContainer holder)Sets the holder which is mostly used when it is required to be able to go through the chain ofInterceptorvoidstart()Start the components of this Container and all its logical child containers.voidstop()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 defaultComponentAdapterFactoryand a parent container.
-
ConcurrentContainer
public ConcurrentContainer(ExoContainer holder, ExoContainer parent)
Creates a new container with the defaultComponentAdapterFactoryand a parent container.- Parameters:
holder- the holder of the containerparent- the parent container (used for component dependency lookups).
-
-
Method Detail
-
getComponentAdapters
public Collection<ComponentAdapter<?>> getComponentAdapters()
Description copied from class:AbstractContainerRetrieve all the component adapters inside this container. The component adapters from the parent container are not returned.- Specified by:
getComponentAdaptersin interfaceContainer- Overrides:
getComponentAdaptersin classAbstractContainer- 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:AbstractInterceptorSets the holder which is mostly used when it is required to be able to go through the chain ofInterceptor- Specified by:
setHolderin interfaceInterceptor- Overrides:
setHolderin classAbstractInterceptor- 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:AbstractContainerFind 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:
getComponentAdapterin interfaceContainer- Overrides:
getComponentAdapterin classAbstractContainer- 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
nullif 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:AbstractContainerFind 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:
getComponentAdapterOfTypein interfaceContainer- Overrides:
getComponentAdapterOfTypein classAbstractContainer- 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
nullif no component has been registered for the specified key.
-
getComponentAdaptersOfType
public <T> List<ComponentAdapter<T>> getComponentAdaptersOfType(Class<T> componentType)
Description copied from class:AbstractContainerRetrieve 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:
getComponentAdaptersOfTypein interfaceContainer- Overrides:
getComponentAdaptersOfTypein classAbstractContainer- 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:AbstractContainerUnregister a component by key.- Specified by:
unregisterComponentin interfaceContainer- Overrides:
unregisterComponentin classAbstractContainer- 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 anInstanceComponentAdapter.- Specified by:
registerComponentInstancein interfaceContainer- Overrides:
registerComponentInstancein classAbstractContainer- 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 theContainerinterface 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 theComponentAdapterFactorypassed to the container's constructor.- Specified by:
registerComponentImplementationin interfaceContainer- Overrides:
registerComponentImplementationin classAbstractContainer- 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 theContainerinterface can be used to retrieve a reference to the component later on. - Throws:
ContainerException- if registration of the component fails.
-
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:AbstractContainerReturns 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:
getComponentInstancesOfTypein interfaceContainer- Overrides:
getComponentInstancesOfTypein classAbstractContainer- 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:AbstractContainerRetrieve 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:
getComponentInstancein interfaceContainer- Overrides:
getComponentInstancein classAbstractContainer- 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
nullif 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 noComponentAdaptercan 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 theComponentAdapter.- Specified by:
getComponentInstanceOfTypein interfaceContainer- Overrides:
getComponentInstanceOfTypein classAbstractContainer- 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(Class, boolean)
-
addComponentToCtx
public <T> ConcurrentContainer.CreationalContextComponentAdapter<T> addComponentToCtx(Object key)
Add theCreationalContextcorresponding 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 interfaceStartablewill be started.- Specified by:
startin interfaceStartable- Overrides:
startin classAbstractContainer
-
stop
public void stop()
Stop the components of this Container and all its logical child containers. Any component implementing the lifecycle interfaceStartablewill be stopped.- Specified by:
stopin interfaceStartable- Overrides:
stopin classAbstractContainer
-
dispose
public void dispose()
Dispose the components of this Container and all its logical child containers. Any component implementing the lifecycle interfaceDisposablewill be disposed.- Specified by:
disposein interfaceDisposable- Overrides:
disposein classAbstractContainer
-
accept
public void accept(ContainerVisitor visitor)
Description copied from class:AbstractContainerAccepts a visitor that should visit the child containers, component adapters and component instances.- Specified by:
acceptin interfaceContainer- Overrides:
acceptin classAbstractContainer- Parameters:
visitor- the visitor
-
getHolder
public ExoContainer getHolder()
- Returns:
- the holder
-
getManagementContext
public ManagementContext getManagementContext()
Gives the correspondingManagementContext- Specified by:
getManagementContextin interfaceContainer- Overrides:
getManagementContextin classAbstractContainer
-
getMBeanServer
public MBeanServer getMBeanServer()
Provides theMBeanServerthis method is needed for backward compatibility- Specified by:
getMBeanServerin interfaceContainer- Overrides:
getMBeanServerin classAbstractContainer
-
getScopingObjectName
public ObjectName getScopingObjectName()
Gives the ObjectName of the container build from the scoping data- Specified by:
getScopingObjectNamein interfaceContainer- Overrides:
getScopingObjectNamein classAbstractContainer
-
createComponent
public <T> T createComponent(Class<T> clazz, InitParams params) throws Exception
Creates a component corresponding to the givenClasswith the givenInitParams- Specified by:
createComponentin interfaceContainer- Overrides:
createComponentin classAbstractContainer- Parameters:
clazz- the Class of the object to createparams- 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 annotationInject- Returns:
trueif at least Inject annotation has been found,falseotherwise
-
addMethods
protected void addMethods(Class<?> c, Map<String,Method> methodAlreadyRegistered, Map<Class<?>,Collection<Method>> methodsPerClass)
-
initialize
public void initialize()
Initializes the container- Specified by:
initializein interfaceContainer- Overrides:
initializein classAbstractContainer
-
getCache
protected CachingContainer getCache()
Gives the cache if already found otherwise it will get it from the interceptor chain
-
-