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
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:
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic classThis class is used as value holder -
Field Summary
FieldsModifier and TypeFieldDescriptionprotected final Set<ExoContainer>protected final Set<ComponentAdapter<?>>protected final ConcurrentMap<Object,ComponentAdapter<?>> protected final CopyOnWriteArrayList<ComponentAdapter<?>>Fields inherited from class org.exoplatform.container.AbstractInterceptor
holder, parentFields inherited from class org.exoplatform.container.AbstractContainer
delegate -
Constructor Summary
ConstructorsConstructorDescriptionCreates 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
Modifier and TypeMethodDescriptionvoidaccept(ContainerVisitor visitor) Accepts a visitor that should visit the child containers, component adapters and component instances.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.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 returnsnullotherwise it first try to get it from the dependency resolution context if it still cannot be found we get the instance from theComponentAdapter.<T> List<T>getComponentInstancesOfType(Class<T> componentType) Returns a List of components of a certain componentType.protected ComponentAdapterFactoryprotected <T> TgetInstance(ComponentAdapter<T> componentAdapter, Class<T> type, boolean autoRegistration) Gives the correspondingManagementContextProvides theMBeanServerthis method is needed for backward compatibilityGives the ObjectName of the container build from the scoping datavoidInitializes 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.voidRemove 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.unregisterComponent(Object componentKey) Unregister a component by key.Methods inherited from class org.exoplatform.container.AbstractInterceptor
getId, setParent, setSuccessorMethods inherited from class org.exoplatform.container.AbstractContainer
getSuccessorMethods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, waitMethods inherited from interface org.exoplatform.container.spi.Container
getSuccessor
-
Field Details
-
componentKeyToAdapterCache
-
componentAdapters
-
orderedComponentAdapters
-
children
-
-
Constructor Details
-
ConcurrentContainer
public ConcurrentContainer()Creates a new container with the defaultComponentAdapterFactoryand a parent container. -
ConcurrentContainer
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 Details
-
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:
-
setHolder
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
-
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
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
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
-
getComponentInstances
- Throws:
ContainerException
-
getComponentInstancesOfType
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
If noComponentAdaptercan be found it returnsnullotherwise 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:
-
addComponentToCtx
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
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
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
- Returns:
- the holder
-
getManagementContext
Gives the correspondingManagementContext- Specified by:
getManagementContextin interfaceContainer- Overrides:
getManagementContextin classAbstractContainer
-
getMBeanServer
Provides theMBeanServerthis method is needed for backward compatibility- Specified by:
getMBeanServerin interfaceContainer- Overrides:
getMBeanServerin classAbstractContainer
-
getScopingObjectName
Gives the ObjectName of the container build from the scoping data- Specified by:
getScopingObjectNamein interfaceContainer- Overrides:
getScopingObjectNamein classAbstractContainer
-
createComponent
- Throws:
Exception
-
createComponent
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
-
initialize
public void initialize()Initializes the container- Specified by:
initializein interfaceContainer- Overrides:
initializein classAbstractContainer
-
getCache
Gives the cache if already found otherwise it will get it from the interceptor chain
-