Class PriorityServiceLoader<S>

java.lang.Object
org.jboss.resteasy.spi.PriorityServiceLoader<S>
All Implemented Interfaces:
Iterable<S>

public class PriorityServiceLoader<S> extends Object implements Iterable<S>

A service loader which loads classes aggressively sorting the implementations by the value in the Priority annotation. If the implementation does not include the annotation Integer.MAX_VALUE is used for the priority. The instances themselves are lazily created.


Constructor

If a constructor function is used the argument passed to the function is the resolved type. The function is free to construct the type however it sees fit.

Example:


 PriorityServiceLoader.load(Service.class, (service) -> {
     try {
         return service.getConstructor(String.class).newInstance("init value");
     } catch (Exception e) {
         throw new RuntimeException(e);
     }
 });
 

Since:
6.1
Author:
James R. Perkins
See Also:
  • Method Details

    • load

      public static <S> PriorityServiceLoader<S> load(Class<S> type)
      Creates a new service loader for the type.

      To resolve the class loader this first attempts to get the current threads context class loader. If that is null the services class loader is used. Finally if the class loader from the service is null then the system class loader is used.

      Parameters:
      type - the type to load the services for
      Returns:
      a new service loader
      Throws:
      SecurityException - if the security manager is enabled there is a security issue loading the class or retrieving the class loader
    • load

      public static <S> PriorityServiceLoader<S> load(Class<S> type, Function<Class<? extends S>,S> constructor)
      Creates a new service loader for the type.

      To resolve the class loader this first attempts to get the current threads context class loader. If that is null the services class loader is used. Finally if the class loader from the service is null then the system class loader is used.

      Parameters:
      type - the type to load the services for
      constructor - an optional constructor used to construct the type
      Returns:
      a new service loader
      Throws:
      SecurityException - if the security manager is enabled there is a security issue loading the class or retrieving the class loader
    • load

      public static <S> PriorityServiceLoader<S> load(Class<S> type, ClassLoader cl)
      Creates a new service loader for the type and class loader.
      Parameters:
      type - the type to load the services for
      cl - the class loader used to load the found services
      Returns:
      a new service loader
      Throws:
      SecurityException - if the security manager is enabled and there is a security issue loading the class
    • load

      public static <S> PriorityServiceLoader<S> load(Class<S> type, ClassLoader cl, Function<Class<? extends S>,S> constructor)
      Creates a new service loader for the type and class loader.
      Parameters:
      type - the type to load the services for
      cl - the class loader used to load the found services
      constructor - an optional constructor used to construct the type
      Returns:
      a new service loader
      Throws:
      SecurityException - if the security manager is enabled and there is a security issue loading the class
    • first

      public Optional<S> first()
      If there are services available the first one is returned.
      Returns:
      the first service or an empty optional
      Throws:
      SecurityException - if the security manager is enabled and there is a security error instantiating the object
    • last

      public Optional<S> last()
      If there are services available the last one is returned.
      Returns:
      the last service or an empty optional
      Throws:
      SecurityException - if the security manager is enabled and there is a security error instantiating the object
    • getTypes

      public Set<Class<S>> getTypes()
      Returns the types found for this service.

      Note if accessed before the iterator the types are not actually constructed.

      Returns:
      the types found for this service
    • iterator

      public Iterator<S> iterator()
      Specified by:
      iterator in interface Iterable<S>
      Throws:
      SecurityException - if the security manager is enabled and there is a security error instantiating the object
    • toString

      public String toString()
      Overrides:
      toString in class Object