Class ServiceRegistry
Service providers are looked up from the classpath, under the path
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.
- Version:
- $Id: com/twelvemonkeys/util/service/ServiceRegistry.java#2 $
- Author:
- Harald Kuhr
- See Also:
-
Field Summary
Fields -
Constructor Summary
ConstructorsConstructorDescriptionServiceRegistry
(Iterator<? extends Class<?>> pCategories) Creates aServiceRegistry
instance with a set of categories taken from thepCategories
argument. -
Method Summary
Modifier and TypeMethodDescriptionReturns anIterator
containing all categories in this registry.compatibleCategories
(Object pProvider) Returns anIterator
containing all categories in this registry the givenpProvider
may be registered with.containingCategories
(Object pProvider) Returns anIterator
containing all categories in this registry the givenpProvider
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
protected <T> Iterator<T>
Returns anIterator
containing all providers in the given category.boolean
Registers the given provider for all categories it matches.<T> boolean
Registers the given provider for the given category.void
Registers all provider implementations for thisServiceRegistry
found in the application classpath.
-
Field Details
-
SERVICES
"META-INF/services/"- See Also:
-
-
Constructor Details
-
ServiceRegistry
Creates aServiceRegistry
instance with a set of categories taken from thepCategories
argument.The categories are constant during the lifetime of the registry, and may not be changed after initial creation.
- Parameters:
pCategories
- anIterator
containingClass
objects that defines this registry's categories.- Throws:
IllegalArgumentException
- ifpCategories
isnull
.ClassCastException
- ifpCategories
contains anything butClass
objects.
-
-
Method Details
-
registerApplicationClasspathSPIs
public void registerApplicationClasspathSPIs()Registers all provider implementations for thisServiceRegistry
found in the application classpath.- Throws:
ServiceConfigurationError
- if an error occurred during registration
-
providers
Returns anIterator
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()
) frompCategory
, it does not remove the provider from other categories in the registry.- Parameters:
pCategory
- the category class- Returns:
- an
Iterator
containing all providers in the given category. - Throws:
IllegalArgumentException
- ifpCategory
is not a valid category in this registry
-
categories
Returns anIterator
containing all categories in this registry.The iterator does not support removal.
- Returns:
- an
Iterator
containing all categories in this registry.
-
compatibleCategories
Returns anIterator
containing all categories in this registry the givenpProvider
may be registered with.The iterator does not support removal.
- Parameters:
pProvider
- the provider instance- Returns:
- an
Iterator
containing all categories in this registry the givenpProvider
may be registered with
-
containingCategories
Returns anIterator
containing all categories in this registry the givenpProvider
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 ofnext()
), it does not remove the category itself from the registry.- Parameters:
pProvider
- the provider instance- Returns:
- an
Iterator
containing all categories in this registry the givenpProvider
may be registered with
-
register
Registers the given provider for all categories it matches.- Parameters:
pProvider
- the provider instance- Returns:
true
ifpProvider
is now registered in one or more categories it was not registered in before.- See Also:
-
register
Registers the given provider for the given category.- Parameters:
pProvider
- the provider instancepCategory
- the category class- Returns:
true
ifpProvider
is now registered in the given category
-
deregister
De-registers the given provider from all categories it's currently registered in.- Parameters:
pProvider
- the provider instance- Returns:
true
ifpProvider
was previously registered in any category and is now de-registered.- See Also:
-
deregister
Deregisters the given provider from the given category.- Parameters:
pProvider
- the provider instancepCategory
- the category class- Returns:
true
ifpProvider
was previously registered in the given category
-
main
-