Class ModuleLoader

java.lang.Object
com.github.cafapi.common.util.moduleloader.ModuleLoader

public final class ModuleLoader extends Object
Utility class that provides methods for finding and returning components at runtime using the Java ServiceLoader.
  • Method Summary

    Modifier and Type
    Method
    Description
    static <T> T
    getService(Class<T> intf)
    Determine the first advertised service implementation for the specified interface.
    static <T> T
    getService(Class<T> intf, Class<? extends T> defaultImpl)
    Determine the first advertised service implementation for the specified interface.
    static <T> T
    getServiceOrElse(Class<T> intf, T defaultObj)
    Determine the first advertised service implementation for the specified interface.
    static <T> List<T>
    getServices(Class<T> intf)
    Get all advertised service implementations of the specified interface.

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Method Details

    • getService

      public static <T> T getService(Class<T> intf) throws ModuleLoaderException
      Determine the first advertised service implementation for the specified interface. The implementations are advertised via the Java "ServiceLoader" mechanism.
      Type Parameters:
      T - the interface
      Parameters:
      intf - the interface to find an advertised service implementation for
      Returns:
      an advertised implementation of intf of type T
      Throws:
      ModuleLoaderException - if there is no advertised implementation available
    • getService

      public static <T> T getService(Class<T> intf, Class<? extends T> defaultImpl) throws ModuleLoaderException
      Determine the first advertised service implementation for the specified interface. The implementations are advertised via the Java "ServiceLoader" mechanism.
      Type Parameters:
      T - the interface
      Parameters:
      intf - the interface to find an advertised service implementation for
      defaultImpl - the default implementation class if an advertised one is not found, may be null
      Returns:
      an advertised implementation of intf of type T
      Throws:
      ModuleLoaderException - if the implementation is missing and no defaultImpl is specified
    • getServiceOrElse

      public static <T> T getServiceOrElse(Class<T> intf, T defaultObj)
      Determine the first advertised service implementation for the specified interface. The implementations are advertised via the Java "ServiceLoader" mechanism.
      Type Parameters:
      T - the interface
      Parameters:
      intf - the interface to find an advertised service implementation for
      defaultObj - the default object to return if an advertised one is not found, may be null
      Returns:
      an advertised implementation of intf of type T, or the default object if one is not found
    • getServices

      public static <T> List<T> getServices(Class<T> intf)
      Get all advertised service implementations of the specified interface.
      Type Parameters:
      T - the interface
      Parameters:
      intf - the interface to find advertised service implementations of
      Returns:
      a collection of implementations of the specified interface