Package com.tnsai.spi

Class SpiLoader

java.lang.Object
com.tnsai.spi.SpiLoader

public final class SpiLoader extends Object
Classloader-scoped cache for Java SPI providers.

Providers are discovered once for each service type and effective thread context classloader. The returned list is immutable and retains provider order from ServiceLoader. Both cache-key referents and cached snapshots are weak: an application redeploy can reclaim its classloader even when this utility was loaded by a parent classloader.

A snapshot may be rediscovered after it becomes otherwise unreachable and is reclaimed. Callers that need deterministic refresh should use invalidate(Class) or invalidateAll().

Since:
0.14.0
  • Method Summary

    Modifier and Type
    Method
    Description
    static <T> Optional<T>
    findFirst(Class<T> serviceType)
    Returns the first provider visible to the effective thread context classloader without constructing later providers.
    static <T> Optional<T>
    findFirst(Class<T> serviceType, ClassLoader classLoader)
    Returns the first provider visible to classLoader without constructing later providers.
    static <T> void
    invalidate(Class<T> serviceType)
    Invalidates every classloader-scoped snapshot for serviceType.
    static void
    Clears every cached provider snapshot.
    static <T> List<T>
    load(Class<T> serviceType)
    Returns the cached providers visible to the effective thread context classloader.
    static <T> List<T>
    load(Class<T> serviceType, ClassLoader classLoader)
    Returns the cached providers visible to classLoader.

    Methods inherited from class java.lang.Object

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

    • load

      public static <T> List<T> load(Class<T> serviceType)
      Returns the cached providers visible to the effective thread context classloader.

      If the current thread has no context classloader, the service type's defining classloader is used, followed by the system classloader for a bootstrap-defined service type.

      Type Parameters:
      T - service type
      Parameters:
      serviceType - service interface or abstract class
      Returns:
      immutable providers in ServiceLoader order
      Throws:
      ServiceConfigurationError - when provider discovery fails
    • findFirst

      public static <T> Optional<T> findFirst(Class<T> serviceType)
      Returns the first provider visible to the effective thread context classloader without constructing later providers.

      This preserves ServiceLoader.findFirst() semantics for APIs that select a single provider while still caching the result.

      Type Parameters:
      T - service type
      Parameters:
      serviceType - service interface or abstract class
      Returns:
      cached first provider, or empty when none is registered
      Throws:
      ServiceConfigurationError - when first-provider discovery fails
    • findFirst

      public static <T> Optional<T> findFirst(Class<T> serviceType, ClassLoader classLoader)
      Returns the first provider visible to classLoader without constructing later providers.
      Type Parameters:
      T - service type
      Parameters:
      serviceType - service interface or abstract class
      classLoader - provider classloader
      Returns:
      cached first provider, or empty when none is registered
      Throws:
      ServiceConfigurationError - when first-provider discovery fails
    • load

      public static <T> List<T> load(Class<T> serviceType, ClassLoader classLoader)
      Returns the cached providers visible to classLoader.
      Type Parameters:
      T - service type
      Parameters:
      serviceType - service interface or abstract class
      classLoader - provider classloader
      Returns:
      immutable providers in ServiceLoader order
      Throws:
      ServiceConfigurationError - when provider discovery fails
    • invalidate

      public static <T> void invalidate(Class<T> serviceType)
      Invalidates every classloader-scoped snapshot for serviceType. The next load performs fresh discovery.
      Type Parameters:
      T - service type
      Parameters:
      serviceType - service interface or abstract class
    • invalidateAll

      public static void invalidateAll()
      Clears every cached provider snapshot.