Class DefaultFactories

java.lang.Object
org.apache.sis.internal.system.SystemListener
org.apache.sis.internal.system.DefaultFactories
All Implemented Interfaces:
EventListener

public final class DefaultFactories extends SystemListener
Default factories defined in the sis-utility module. This is a temporary placeholder until we leverage the "dependency injection" pattern. A candidate replacement is JSR-330.
Since:
0.3
Version:
1.2
See Also:
  • Field Details

    • FACTORIES

      private static final Map<Class<?>,Object> FACTORIES
      Cache of factories found by ServiceLoader from META-INF/services files content.
  • Constructor Details

    • DefaultFactories

      private DefaultFactories()
      For the singleton system listener only.
  • Method Details

    • classpathChanged

      protected void classpathChanged()
      Discards cached factories when the classpath has changed.
      Specified by:
      classpathChanged in class SystemListener
    • isDefaultInstance

      public static <T> boolean isDefaultInstance(Class<T> type, T factory)
      Returns true if the default factory of the given type is the given instance. A null factory is interpreted as the default one.
      Type Parameters:
      T - the interface type.
      Parameters:
      type - the interface type.
      factory - the factory implementation to test, or null.
      Returns:
      true if the given factory implementation is the default instance.
    • forClass

      public static <T> T forClass(Class<T> type)
      Returns the default factory implementing the given interface. This method gives preference to Apache SIS implementation of factories if present. This is a temporary mechanism while we are waiting for a real dependency injection mechanism.
      Type Parameters:
      T - the interface type.
      Parameters:
      type - the interface type.
      Returns:
      a factory implementing the given interface, or null if none.
    • forBuildin

      public static <T> T forBuildin(Class<T> type)
      Returns a factory which is guaranteed to be present. If the factory is not found, this will be considered a configuration error (corrupted JAR files of incorrect classpath).
      Type Parameters:
      T - the interface type.
      Parameters:
      type - the interface type.
      Returns:
      a factory implementing the given interface.
      Since:
      0.6
    • forBuildin

      public static <T, I extends T> I forBuildin(Class<T> type, Class<I> impl)
      Returns a factory of the given type, making sure that it is an implementation of the given class. Use this method only when we know that Apache SIS registers only one implementation of a given service.
      Type Parameters:
      T - the interface type.
      I - the requested implementation class.
      Parameters:
      type - the interface type.
      impl - the requested implementation class.
      Returns:
      a factory implementing the given interface.
      Since:
      0.6
    • createServiceLoader

      public static <T> ServiceLoader<T> createServiceLoader(Class<T> service)
      Returns a service loader for the given type using the default class loader. The default is the current thread context class loader, provided that it can access at least the Apache SIS stores.
      Type Parameters:
      T - the compile-time value of service argument.
      Parameters:
      service - the interface or abstract class representing the service.
      Returns:
      a new service loader for the given service type.
      Since:
      0.8
    • getContextClassLoader

      public static ClassLoader getContextClassLoader() throws SecurityException
      Returns the context class loader, but makes sure that it has Apache SIS on its classpath. First, this method invokes Thread.getContextClassLoader() for the current thread. Then this method scans over all Apache SIS classes on the stack trace. For each SIS class, its loader is compared to the above-cited context class loader. If the context class loader is equal or is a child of the SIS loader, then it is left unchanged. Otherwise the context class loader is replaced by the SIS one.

      The intent of this method is to ensure that ServiceLoader.load(Class) will find the Apache SIS services even in an environment that defined an unsuitable context class loader.

      Returns:
      the context class loader if suitable, or another class loader otherwise.
      Throws:
      SecurityException - if this method is not allowed to get the current thread context class loader or one of its parent.
      Since:
      0.8