@ThreadSafe public final class ServiceLocator extends Object
If the class loader provided to the constructor is the current thread's context class loader, then the methods of this class will locate classes, resources or services by using this class loader only. Otherwise, the given class loader is used first. Second, the current thread's context class loader is used.
When loading a class, the search stops on the first hit, so if the given class loader finds the class, the current thread's context class loader is not used.
When enumerating resources and services, the results of both class loaders are concatenated, so a resource or a service may get enumerated twice! If this is not acceptable, you should create a set from the enumeration results.
Constructor and Description |
---|
ServiceLocator()
Equivalent to
new ServiceLocator(null) . |
ServiceLocator(ClassLoader loader)
Constructs a new service locator which uses the given class loader first
to locate classes, resources or services on the class path.
|
Modifier and Type | Method and Description |
---|---|
Class<?> |
getClass(String name)
Loads a class according to the algorithm described in the class Javadoc.
|
Enumeration<URL> |
getResources(String name)
Enumerates resources according to the algorithm described in the class
Javadoc.
|
<S> S |
getService(Class<S> service,
Class<? extends S> def)
Returns a new service provider implementation instance.
|
<S> Iterator<S> |
getServices(Class<S> service)
Enumerates all service provider implementation instances.
|
static <T> T |
promote(Object object,
Class<T> type)
Promotes the given
object to an instance of the given
type . |
public ServiceLocator()
new ServiceLocator(null)
.public ServiceLocator(@CheckForNull ClassLoader loader)
null
, then the system class loader is used.loader
- the nullable class loader to locate classes, resources and
services.public Class<?> getClass(String name)
name
- The fully qualified name of the class to locate.ServiceConfigurationError
- if locating the class fails for some
reason.public Enumeration<URL> getResources(String name)
name
- The fully qualified name of the resources to locate.ServiceConfigurationError
- if locating the resources fails for
some reason.@Nullable public <S> S getService(Class<S> service, @CheckForNull Class<? extends S> def)
system property
with
the service provider interface class name as the key and the given
service provider implementation class name as the default value.
The service provider implementation class is then loaded using
getClass(java.lang.String)
and finally instantiated by calling its no-arg
constructor.
This method should be preferred over getServices(java.lang.Class<S>)
if
null
as the result or providing a default
service provider implementation is acceptable.
S
- The type of the service provider specification.service
- the service provider specification.def
- the default service provider implementation.null
if no service provider implementation is known.ServiceConfigurationError
- if locating, instantiating or casting
the service fails for some reason.public <S> Iterator<S> getServices(Class<S> service)
ServiceLoader.load(java.lang.Class, java.lang.ClassLoader)
.
This method should be preferred over getService(java.lang.Class<S>, java.lang.Class<? extends S>)
if more than
one meaningful implementation of a service provider interface is
expected at run time.
S
- The type of the service provider specification.service
- the service provider specification.ServiceConfigurationError
- if an exception occurs.@CheckForNull public static <T> T promote(@CheckForNull Object object, Class<T> type)
object
to an instance of the given
type
.
The following steps are consecutively applied in order to promote the
given object by this utility method:
object
is an instance of String
and type
is
not the String
class instance, then object
is considered
to name a class, which is loaded by using a new ServiceLocator
with the class loader of type
as the primary class loader.
object
is an instance of Class
and type
is not the Class
class instance, then it gets instantiated by
calling it's public no-argument constructor.
object
is cast to T
and returned.
T
- the desired type of the object.object
- the object to promote.type
- the class describing the desired type.null
if and only if
object
is null
.IllegalArgumentException
- if any promotion step fails.Copyright © 2005–2018 Schlichtherle IT Services. All rights reserved.