public class ServiceRegistry extends Object
META-INF/services/
<full-class-name>.
For example:META-INF/services/com.company.package.spi.MyService
.
The file should contain a list of fully-qualified concrete class names,
one per line.
The full-class-name represents an interface or (typically) an
abstract class, and is the same class used as the category for this registry.
Note that only one instance of a concrete subclass may be registered with a
specific category at a time.
Implementation detail: This class is a clean room implementation of
a service registry and does not use the proprietary sun.misc.Service
class that is referred to in the JAR File specification.
This class should work on any Java platform.
RegisterableService
,
JAR File SpecificationModifier and Type | Field | Description |
---|---|---|
static String |
SERVICES |
"META-INF/services/"
|
Constructor | Description |
---|---|
ServiceRegistry(Iterator<? extends Class<?>> pCategories) |
Creates a
ServiceRegistry instance with a set of categories
taken from the pCategories argument. |
Modifier and Type | Method | Description |
---|---|---|
protected Iterator<Class<?>> |
categories() |
Returns an
Iterator containing all categories in this registry. |
protected Iterator<Class<?>> |
compatibleCategories(Object pProvider) |
Returns an
Iterator containing all categories in this registry
the given pProvider may be registered with. |
protected Iterator<Class<?>> |
containingCategories(Object pProvider) |
Returns an
Iterator containing all categories in this registry
the given pProvider is currently registered with. |
boolean |
deregister(Object pProvider) |
De-registers the given provider from all categories it's currently
registered in.
|
boolean |
deregister(Object pProvider,
Class<?> pCategory) |
Deregisters the given provider from the given category.
|
static void |
main(String[] pArgs) |
|
protected <T> Iterator<T> |
providers(Class<T> pCategory) |
Returns an
Iterator containing all providers in the given
category. |
boolean |
register(Object pProvider) |
Registers the given provider for all categories it matches.
|
<T> boolean |
register(T pProvider,
Class<? super T> pCategory) |
Registers the given provider for the given category.
|
void |
registerApplicationClasspathSPIs() |
Registers all provider implementations for this
ServiceRegistry
found in the application classpath. |
public static final String SERVICES
public ServiceRegistry(Iterator<? extends Class<?>> pCategories)
ServiceRegistry
instance with a set of categories
taken from the pCategories
argument.
The categories are constant during the lifetime of the registry, and may
not be changed after initial creation.pCategories
- an Iterator
containing
Class
objects that defines this registry's categories.IllegalArgumentException
- if pCategories
is null
.ClassCastException
- if pCategories
contains anything
but Class
objects.public void registerApplicationClasspathSPIs()
ServiceRegistry
found in the application classpath.ServiceConfigurationError
- if an error occurred during registrationprotected <T> Iterator<T> providers(Class<T> pCategory)
Iterator
containing all providers in the given
category.
The iterator supports removal.
NOTE: Removing a provider from the iterator, deregisters the current
provider (as returned by the last invocation of next()
) from
pCategory
, it does not remove the provider
from other categories in the registry.
pCategory
- the category classIterator
containing all providers in the given
category.IllegalArgumentException
- if pCategory
is not a valid
category in this registryprotected Iterator<Class<?>> categories()
Iterator
containing all categories in this registry.
The iterator does not support removal.Iterator
containing all categories in this registry.protected Iterator<Class<?>> compatibleCategories(Object pProvider)
Iterator
containing all categories in this registry
the given pProvider
may be registered with.
The iterator does not support removal.pProvider
- the provider instanceIterator
containing all categories in this registry
the given pProvider
may be registered withprotected Iterator<Class<?>> containingCategories(Object pProvider)
Iterator
containing all categories in this registry
the given pProvider
is currently registered with.
The iterator supports removal.
NOTE: Removing a category from the iterator, de-registers
pProvider
from the current category (as returned by the last
invocation of next()
), it does not remove the category
itself from the registry.
pProvider
- the provider instanceIterator
containing all categories in this registry
the given pProvider
may be registered withpublic boolean register(Object pProvider)
pProvider
- the provider instancetrue
if pProvider
is now registered in
one or more categories it was not registered in before.compatibleCategories(Object)
public <T> boolean register(T pProvider, Class<? super T> pCategory)
pProvider
- the provider instancepCategory
- the category classtrue
if pProvider
is now registered in
the given categorypublic boolean deregister(Object pProvider)
pProvider
- the provider instancetrue
if pProvider
was previously registered in
any category and is now de-registered.containingCategories(Object)
public boolean deregister(Object pProvider, Class<?> pCategory)
pProvider
- the provider instancepCategory
- the category classtrue
if pProvider
was previously registered in
the given categorypublic static void main(String[] pArgs)
Copyright © 2018. All rights reserved.