Class ServiceRegistry


  • @InternalApi
    public final class ServiceRegistry
    extends Object
    Registry for service instances loaded from ServiceLoader. This abstracts the differences between using a flat classpath, a module path, and OSGi modules.
    Since:
    3.0.0
    • Method Detail

      • getInstance

        public static ServiceRegistry getInstance()
        Returns the singleton ServiceRegistry instance.
      • getServices

        public <S> List<S> getServices​(Class<S> serviceType,
                                       MethodHandles.Lookup lookup,
                                       Predicate<S> validator)
        Gets service instances loaded from the calling context and any previously registered bundle services. The loaderCallerContext parameter is provided to ensure the caller can specify which calling function context to load services.
        Type Parameters:
        S - type of service
        Parameters:
        serviceType - service class
        lookup - MethodHandle lookup created in same module as caller context to use for loading services
        validator - if non-null, used to validate service instances, removing invalid instances from the returned list
        Returns:
        loaded service instances
      • loadServicesFromBundle

        public <S> void loadServicesFromBundle​(Class<S> serviceType,
                                               long bundleId,
                                               ClassLoader bundleClassLoader)
        Loads and registers services from an OSGi context.
        Type Parameters:
        S - type of service
        Parameters:
        serviceType - service class
        bundleId - bundle id to load services from
        bundleClassLoader - bundle ClassLoader to load services from
      • registerBundleServices

        public <S> void registerBundleServices​(Class<S> serviceType,
                                               long bundleId,
                                               List<S> services)
        Registers a list of service instances from an OSGi context.
        Type Parameters:
        S - type of service
        Parameters:
        serviceType - service class
        bundleId - bundle id where services are being registered
        services - list of services to register for this bundle
      • unregisterBundleServices

        public void unregisterBundleServices​(long bundleId)
        Unregisters all services instances from an OSGi context.
        Parameters:
        bundleId - bundle id where services are being unregistered