TrueZIP Kernel 7.0-rc2

de.schlichtherle.truezip.util
Class ServiceLocator

java.lang.Object
  extended by de.schlichtherle.truezip.util.ServiceLocator

@ThreadSafe
@DefaultAnnotation(value=edu.umd.cs.findbugs.annotations.NonNull.class)
public final class ServiceLocator
extends Object

Provides utility methods for convenient class, resource and service location which is designed to work in both OSGi and JEE environments.

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.

Author:
Christian Schlichtherle

Field Summary
private  ClassLoader l1
           
 
Constructor Summary
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.
 
Method Summary
 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.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

l1

private final ClassLoader l1
Constructor Detail

ServiceLocator

public ServiceLocator()
Equivalent to new ServiceLocator(null).


ServiceLocator

public ServiceLocator(@CheckForNull
                      ClassLoader loader)
Constructs a new service locator which uses the given class loader first to locate classes, resources or services on the class path. If this is null, then the system class loader is used.

Parameters:
loader - the nullable class loader to locate classes, resources and services.
Method Detail

getServices

public <S> Iterator<S> getServices(Class<S> service)
Enumerates all service provider implementation instances. The service provider implementations are resolved as if by calling ServiceLoader.load(java.lang.Class, java.lang.ClassLoader).

This method should be preferred over getService(java.lang.Class, java.lang.Class) if more than one meaningful implementation of a service provider interface is expected at run time.

Type Parameters:
S - The type of the service provider specification.
Parameters:
service - the service provider specification.
Returns:
A concatenated iteration for the service provider implementation instances.
Throws:
ServiceConfigurationError - if an exception occurs.

getService

@Nullable
public <S> S getService(Class<S> service,
                                 @CheckForNull
                                 Class<? extends S> def)
Returns a new service provider implementation instance. The class name of the service provider implementation is resolved by using the value of the 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) if

  1. only one meaningful implementation of a service provider interface is expected at run time, and
  2. creating a new service provider implementation instance on each call is acceptable, and
  3. either getting null as the result or providing a default service provider implementation is acceptable.

Type Parameters:
S - The type of the service provider specification.
Parameters:
service - the service provider specification.
def - the default service provider implementation.
Returns:
A new service provider implementation instance or null if no service provider implementation is known.
Throws:
ServiceConfigurationError - if locating, instantiating or casting the service fails for some reason.

getClass

public Class<?> getClass(String name)
Loads a class according to the algorithm described in the class Javadoc.

Parameters:
name - The fully qualified name of the class to locate.
Returns:
The loaded class.
Throws:
ServiceConfigurationError - if locating the class fails for some reason.

getResources

public Enumeration<URL> getResources(String name)
Enumerates resources according to the algorithm described in the class Javadoc.

Parameters:
name - The fully qualified name of the resources to locate.
Returns:
A concatenated enumeration for the resource on the class path.
Throws:
ServiceConfigurationError - if locating the resources fails for some reason.

TrueZIP Kernel 7.0-rc2

Copyright © 2005-2011 Schlichtherle IT Services. All Rights Reserved.