public class ServiceRegistry extends Object implements ServiceLoader
ServiceLoader
contract. This registry uses underlying
ServiceLoader
implementation to load the service while storing cached instances in a local map.Constructor and Description |
---|
ServiceRegistry(ServiceLoader serviceLoader)
Creates a service registry with a
ServiceLoader to load service instances until they are cached |
Modifier and Type | Method and Description |
---|---|
<T> Collection<T> |
all(Class<T> serviceClass)
Loads all registered services for given
serviceClass . |
static ServiceRegistry |
getInstance()
Returns
ServiceRegistry instance. |
<T> T |
onlyOne(Class<T> serviceClass)
Loads a registered service for given
serviceClass . |
<T> T |
onlyOne(Class<T> serviceClass,
Class<? extends T> defaultImplementationClass)
Loads a registered service for given
serviceClass . |
static void |
register(ServiceRegistry registry)
Registers an instance of
ServiceRegistry . |
public ServiceRegistry(ServiceLoader serviceLoader)
ServiceLoader
to load service instances until they are cachedserviceLoader
- public static ServiceRegistry getInstance() throws IllegalStateException
ServiceRegistry
instance. Instance must be registered first.IllegalStateException
- If no service registry was registered yetpublic static void register(ServiceRegistry registry)
ServiceRegistry
.registry
- public <T> Collection<T> all(Class<T> serviceClass) throws IllegalArgumentException
ServiceLoader
serviceClass
.all
in interface ServiceLoader
serviceClass
- The service class.IllegalArgumentException
- If serviceClass
is null
.public <T> T onlyOne(Class<T> serviceClass) throws IllegalArgumentException, IllegalStateException
ServiceLoader
serviceClass
.onlyOne
in interface ServiceLoader
serviceClass
- The service class.IllegalArgumentException
- If serviceClass
is null
.IllegalStateException
- If more than a single service is registered.public <T> T onlyOne(Class<T> serviceClass, Class<? extends T> defaultImplementationClass) throws IllegalArgumentException, IllegalStateException
ServiceLoader
serviceClass
. Reverts to the defaultImplementationClass
if no
other service is registered. If defaultImplemenationClass
is registered as well, it simply ignores it during
resolution.onlyOne
in interface ServiceLoader
serviceClass
- The service class.defaultImplementationClass
- The default implementation class.IllegalArgumentException
- If either serviceClass
or defaultImplementationClass
is null
IllegalStateException
- If more than a single service is registeredCopyright © 2022 JBoss by Red Hat. All rights reserved.