@Immutable public final class ServiceLocator extends Object
First, the name of a given provider service class is used as the
key string to lookup a system property
.
If this yields a value then it's supposed to name a class which gets loaded
and instantiated by calling its public no-argument constructor.
Otherwise, the class path is searched for any resources with the name
"META-INF/services/"
plus the name of the given locatable
provider class.
If this yields no results, a ServiceConfigurationError
is thrown.
Otherwise the classes with the names contained in these resources get loaded and instantiated by calling their public no-argument constructor. Next, the instances are filtered according to their priority. Only the instance with the highest priority is kept for subsequent use.
Next, the class is searched again for any resources with the name
"META-INF/services/"
plus the name of the given locatable
function class.
If this yields some results, the classes with the names contained in these
resources get loaded and instantiated by calling their public no-argument
constructor.
Next, the instances get sorted in ascending order of their
priority and kept for subsequent use.
Finally, depending on the requesting method either a container or a factory gets created which will use the instantiated provider and functions to obtain a product and map it in order of their priorities.
ServiceLoader
Constructor and Description |
---|
ServiceLocator(Class<?> client)
Constructs a new locator which uses the class loader of the given client
class before using the current thread context's class loader unless the
latter is identical to the former.
|
ServiceLocator(ClassLoader loader)
Constructs a new locator which uses the given class loader before using
the current thread context's class loader unless the latter is identical
to the former.
|
Modifier and Type | Method and Description |
---|---|
<P> Container<P> |
container(Class<? extends LocatableProvider<P>> provider)
Creates a new container with a single product.
|
<P> Container<P> |
container(Class<? extends LocatableProvider<P>> provider,
Class<? extends LocatableDecorator<P>> decorator)
Creates a new container with a single product.
|
<P> Factory<P> |
factory(Class<? extends LocatableFactory<P>> factory)
Creates a new factory for products.
|
<P> Factory<P> |
factory(Class<? extends LocatableFactory<P>> factory,
Class<? extends LocatableFunction<P>> functions)
Creates a new factory for products.
|
public ServiceLocator(Class<?> client)
client
- the class which identifies the calling client.public ServiceLocator(ClassLoader loader)
loader
- the class loader to use before the current thread
context's class loader unless the the latter is identical to the
former.public <P> Container<P> container(Class<? extends LocatableProvider<P>> provider) throws ServiceConfigurationError
P
- the type of the product to contain.provider
- the class of the locatable provider for the product.ServiceConfigurationError
- if loading or instantiating
a located class fails for some reason.public <P> Container<P> container(Class<? extends LocatableProvider<P>> provider, @Nullable Class<? extends LocatableDecorator<P>> decorator) throws ServiceConfigurationError
P
- the type of the product to contain.provider
- the class of the locatable provider for the product.decorator
- the class of the locatable decoractors for the product.ServiceConfigurationError
- if loading or instantiating
a located class fails for some reason.public <P> Factory<P> factory(Class<? extends LocatableFactory<P>> factory) throws ServiceConfigurationError
P
- the type of the products to create.factory
- the class of the locatable factory for the products.ServiceConfigurationError
- if loading or instantiating
a located class fails for some reason.public <P> Factory<P> factory(Class<? extends LocatableFactory<P>> factory, @Nullable Class<? extends LocatableFunction<P>> functions) throws ServiceConfigurationError
P
- the type of the products to create.factory
- the class of the locatable factory for the products.functions
- the class of the locatable functions for the products.ServiceConfigurationError
- if loading or instantiating
a located class fails for some reason.Copyright © 2012–2018 Schlichtherle IT Services. All rights reserved.