Class ServiceProvider


  • public final class ServiceProvider
    extends Object
    A provider of Silverpeas services such as repositories, controllers, transactional services, and so on. This provider is an adaptor to the underlying IoD subsystem used to manage the life-cycle and the dependencies of the objects in Silverpeas. Usually, defining injection points is enough for a bean to access the services in Silverpeas, but these injection points work only for beans managed themselves by the IoD subsystem. Hence, this provider is for unmanaged beans in order they have access all of the services available in Silverpeas. The provider can be either used directly by the unmanaged beans or through a more typed-specific provider. This latest solution is our preferred way. The service provider doesn't use directly the IoD subsystem to fetch the asked service. Instead it delegates the service fetching to another object, a BeanContainer instance that is a wrapper of the actual IoD subsystem in use. 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 ServiceProvider class.
    Author:
    mmoquillon
    • Method Summary

      All Methods Static Methods Concrete Methods 
      Modifier and Type Method Description
      static <T> Set<T> getAllServices​(Class<T> type, Annotation... qualifiers)
      Gets an instance of all implementations of mandatory given type and qualifier if any.
      Please be attentive about that this method does not return result when trying to get instance implementations of interfaces which are dealing with typed types (ApplicationService<T> for example).
      To deal with this kind of interfaces, please use the Instance method.
      static <T> T getService​(Class<T> type, Annotation... qualifiers)
      Gets an instance of the single implementation of mandatory given type and qualifier if any.
      Please be attentive about that this method does not return result when trying to get instance of an implementation of interfaces which are dealing with typed types (ApplicationService<T> for example).
      To deal with this kind of interfaces, please use the Instance method.
      static <T> T getService​(String name)
      Gets an instance of the single implementation that is qualified by the specified name.
      Please be attentive about that this method does not return result when trying to get instance of an implementation of interfaces which are dealing with typed types (ApplicationService<T> for example).
      To deal with this kind of interfaces, please use the Instance method.
      static <T> T getServiceByComponentInstanceAndNameSuffix​(String componentInstanceIdOrComponentName, String nameSuffix)
      Some API are defined at a high level, core-api for example, and be implemented by several services or component services.
      static <T> T getSingleton​(Class<T> type, Annotation... qualifiers)
      Gets the service instance as getService(Class, Annotation...) provides it.
      static <T> T getSingleton​(String name)
      Gets the service instance as getService(String) provides it.
    • Method Detail

      • getService

        public static <T> T getService​(Class<T> type,
                                       Annotation... qualifiers)
        Gets an instance of the single implementation of mandatory given type and qualifier if any.
        Please be attentive about that this method does not return result when trying to get instance of an implementation of interfaces which are dealing with typed types (ApplicationService<T> for example).
        To deal with this kind of interfaces, please use the Instance method. An example can be found into core-library project at ApplicationServiceTest class.
        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:
        IllegalStateException - if no bean of the specified type and with the specified qualifiers can be found.
        See Also:
        BeanContainer.getBeanByType(Class, java.lang.annotation.Annotation...)
      • getSingleton

        public static <T> T getSingleton​(Class<T> type,
                                         Annotation... qualifiers)
        Gets the service instance as getService(Class, Annotation...) provides it.

        But in that case, the caller is knowingly requesting a singleton. It has to be SURE that the service it requests can be processed as a singleton behavior.
        Indeed, a lot of service implementations are Singleton annotated, but in some cases, for memory reasons or others, some implementations are not.

        The service instance provided by this method has a singleton behavior into the current thread execution.

        The advantage of this method over getService(Class, Annotation...) is that ThreadCacheService is used in order to perform only one time the search into CDI containers per thread execution.

        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 singleton bean satisfying the expected type and, if any, the expected qualifiers.
        Throws:
        IllegalStateException - if no bean of the specified type and with the specified qualifiers can be found.
        See Also:
        getService(Class, java.lang.annotation.Annotation...)
      • getService

        public static <T> T getService​(String name)
        Gets an instance of the single implementation that is qualified by the specified name.
        Please be attentive about that this method does not return result when trying to get instance of an implementation of interfaces which are dealing with typed types (ApplicationService<T> for example).
        To deal with this kind of interfaces, please use the Instance method. An example can be found into core-library project at ApplicationServiceTest class.
        Type Parameters:
        T - the type of the bean to return.
        Parameters:
        name - the name of the bean.
        Returns:
        the bean matching the specified name.
        Throws:
        IllegalStateException - if no bean can be found with the specified name.
        See Also:
        BeanContainer.getBeanByName(String)
      • getSingleton

        public static <T> T getSingleton​(String name)
        Gets the service instance as getService(String) provides it.

        But in that case, the caller is knowingly requesting a singleton. It has to be SURE that the service it requests can be processed as a singleton behavior.
        Indeed, a lot of service implementations are Singleton annotated, but in some cases, for memory reasons or others, some implementations are not.

        The service instance provided by this method has a singleton behavior into the current thread execution.

        The advantage of this method over getService(String) is that ThreadCacheService is used in order to perform only one time the search into CDI containers per thread execution.

        Type Parameters:
        T - the type of the bean to return.
        Parameters:
        name - the name of the bean.
        Returns:
        the bean matching the specified name.
        Throws:
        IllegalStateException - if no bean can be found with the specified name.
        See Also:
        getService(String)
      • getAllServices

        public static <T> Set<T> getAllServices​(Class<T> type,
                                                Annotation... qualifiers)
        Gets an instance of all implementations of mandatory given type and qualifier if any.
        Please be attentive about that this method does not return result when trying to get instance implementations of interfaces which are dealing with typed types (ApplicationService<T> for example).
        To deal with this kind of interfaces, please use the Instance method. An example can be found into core-library project at ApplicationServiceTest class.
        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.
        See Also:
        BeanContainer.getAllBeansByType(Class, java.lang.annotation.Annotation...)
      • getServiceByComponentInstanceAndNameSuffix

        public static <T> T getServiceByComponentInstanceAndNameSuffix​(String componentInstanceIdOrComponentName,
                                                                       String nameSuffix)

        Some API are defined at a high level, core-api for example, and be implemented by several services or component services.

        This method offers to provide an implementation of an API by observing following convention of service naming:
        [COMPONENT NAME][SERVICE NAME SUFFIX]
        kmeliaInstancePostConstruction for example, where kmelia the component name and InstancePostConstruction the service name suffix.

        To be provided by this way, an implementation of an API must use Named annotation and fill Named#value() in case where the implementation class name does not correspond to [COMPONENT NAME][SERVICE NAME SUFFIX] concatenation.

        This way of service implementation getting take care about silverpeas components which have a workflow behavior. In case of workflow component, the implementation provided will be the one satisfying the service name processManager[SERVICE NAME SUFFIX].

        Type Parameters:
        T - the type of the bean to return.
        Parameters:
        componentInstanceIdOrComponentName - a component instance identifier of a component name.
        nameSuffix - a service name suffix.
        Returns:
        the bean matching the specified name.
        Throws:
        IllegalStateException - if no bean can be found with the specified name.
        IllegalArgumentException - if componentInstanceIdOrComponentName is not found.
        See Also:
        BeanContainer.getBeanByName(String)