Class ManagedBeanProvider

java.lang.Object
org.silverpeas.kernel.ManagedBeanProvider

public final class ManagedBeanProvider extends Object
A provider of objects whose life-cycle is managed by an underlying IoC by IoD container that must satisfy the BeanContainer interface. This provider is just an access point to the beans that are eligible for automatic life-cycle management. The election of the beans is usually done by a mechanism of registration of their classes in the bean container which is left to the implementation of the IoC by IoD solution.

Usually, the dependencies between beans managed by the container are resolved during their instantiation and this mechanism is done transparently by the IoC/IoD solution itself. Nevertheless, some IoC by IoD solutions resolves them at compilation time instead of at runtime. And some supports a lazy dependency resolution whereas others not. So, the resolution of the dependencies on other managed objects is expected to be automatically performed by the container. Nevertheless, some objects non managed by the container (like objects got from data source) can require to access some of the beans in the container. This provider is for them; it allows them to get the expected bean in the container.

For managed beans, according to the IoC system used, their dependency resolution should be left to the underlying container because it is less expensive in time and CPU than using this provider to access the dependencies. This is why this provider should be used only by unmanaged beans. Anyway, to improve lightly the performances, a cache mechanism is used to store single beans of singletons and of any classes annotated with Cacheable, avoiding then to invoke explicitly the underlying bean container. It is when such an asked bean isn't found in the cache, the container is involved to get it. In order to avoid excessive memory usage, the beans in the cache are stored as soft references, meaning they will be collected by the garbage collector and specifically before any out-of-memory situations.

Author:
mmoquillon
Implementation Note:
The BeanContainer interface is a wrapper of the underlying IoC subsystem (Inversion of Control) used to manage the life-cycle of some objects as well as the resolution of their dependencies. (The IoC subsystem is built upon an IoD (Injection of Dependencies) mechanism.) The bind between the BeanContainer interface and its implementation is performed by the Java SPI (Java Service Provider Interface). Only the first available bean container implementation is loaded by the ManagedBeanProvider class.

This managed bean provider manages a cache to access more quickly managed objects of singletons and of classes annotated with Cacheable. With this mechanism, several asks the underlying bean container for a given bean can be avoided. For doing, to figure out a bean is the single eligible to be cached, this provider walks across the annotations tree of the bean until to find either the @Singleton or the Cacheable annotation. This is the default behavior.

  • Method Details

    • getInstance

      public static ManagedBeanProvider getInstance()
      Gets the single instance of this ManagedBeanProvider class.
      Returns:
      the ManagedBeanProvider single instance.
    • getManagedBean

      @NonNull public <T> T getManagedBean(Class<T> type, Annotation... qualifiers)
      Gets an instance of the single implementation matching the specified type and satisfying the given qualifiers if any. The bean is first looked for in the cache of the current thead before asking it to the bean container. If the implementation is a singleton, and it's not yet cached, then the bean is put in the cache of the current thread for further retrieval.
      Type Parameters:
      T - the type of the bean to return.
      Parameters:
      type - the type of the bean.
      qualifiers - zero, one or more qualifiers annotating the bean to look for.
      Returns:
      the bean satisfying the expected type and, if any, the expected qualifiers.
      Throws:
      NotFoundException - if no bean of the specified type and with the specified qualifiers can be found.
      MultipleCandidateException - if there is more than one bean matching the given type and qualifiers as there is an ambiguous decision in selecting the bean to return.
      ExpectationViolationException - if the expectations of the underlying container on the bean or on the qualifiers aren't fulfilled.
      IllegalStateException - if the underlying container isn't in a valid state when asking a managed bean.
      See Also:
    • getManagedBean

      @NonNull public <T> T getManagedBean(String name)
      Gets an object of the single type implementation that is qualified with the specified name. The bean is first looked for in the cache of the current thead before asking it to the bean container. If the implementation is a singleton, and it's not yet cached, then the bean is put in the cache of the current thread for further retrieval.
      Type Parameters:
      T - the type of the bean to return.
      Parameters:
      name - the unique name identifying the bean in the container.
      Returns:
      the bean matching the specified name.
      Throws:
      NotFoundException - if no bean can be found with the specified name.
      MultipleCandidateException - if there is more than one bean with the given name as the name must be unique for each bean.
      ExpectationViolationException - if the expectations of the underlying container on the bean aren't fulfilled.
      IllegalStateException - if the underlying container isn't in a valid state when asking a managed bean.
      See Also:
    • getAllManagedBeans

      public <T> Set<T> getAllManagedBeans(Class<T> type, Annotation... qualifiers)
      Gets an instance of each implementation of the specified type and satisfying the given qualifiers if any.
      Type Parameters:
      T - the type of the bean to return.
      Parameters:
      type - the type of the bean.
      qualifiers - zero, one or more qualifiers annotating the bean to look for.
      Returns:
      a set of beans satisfying the expected type and, if any, the expected qualifiers, or an empty set otherwise.
      Throws:
      ExpectationViolationException - if the expectations of the underlying container on the beans or on the qualifiers aren't fulfilled.
      IllegalStateException - if the underlying container isn't in a valid state when asking a managed bean.
      See Also: