Class 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:
  • Field Details

  • Constructor Details

    • 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 Details

    • 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:
    • 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.
    • registerComponentAdapter

      public <T> void registerComponentAdapter(ComponentAdapter<T> componentAdapter)
      Description copied from interface: Container
      Register a component adapter.
      Specified by:
      registerComponentAdapter in interface Container
      Overrides:
      registerComponentAdapter in class AbstractContainer
      Parameters:
      componentAdapter - ComponentAdapter to register.
    • 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 - 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: 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 ComponentAdapter 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 ComponentAdapter.
      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:
    • 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