@ThreadSafe @DefaultAnnotation(value=edu.umd.cs.findbugs.annotations.NonNull.class) 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.
| Modifier and Type | Field and Description |
|---|---|
private ClassLoader |
l1 |
| 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.
|
private final ClassLoader l1
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 <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.@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 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.Copyright © 2005-2011 Schlichtherle IT Services. All Rights Reserved.