org.inria.ns.reflex.util
Class Discover

java.lang.Object
  extended by org.inria.ns.reflex.util.Discover

public class Discover
extends Object

A simple service discovery component for retrieving classes.

The lookup is done once for every class name to discover.

An implementation can be render either as a raw Class or as a singleton instance (or both across multiple calls). Singletons are instanciated by their method "newInstance()" if they have one, or by their default zero argument constructor. If the singleton fails to instanciate, the user would still have the possibility to get the raw class and build itself an instance.

Lookup keys and variants

The lookup key is usually the fully qualified name of a class (usually abstract or an interface) : org.acme.Foo, and the resolved class name should be a concrete implementation of it.

The lookup key can be supplied with a variant, in order to bind several implementations (this has to be taken in charge by the caller); for example, org.inria.ns.reflex.xml.SerializerFactory/image/png. Basically, it means that we want a factory that can supply a serializer for "image/png".

Class localization

An implementation is found from the key supplied as follows:

  1. The value of the system property with the name of the key supplied if it exists and is accessible.
  2. The value of the JNDI property with the name of the key supplied prepend with "java:comp/env/" if it exists and is accessible.
  3. The value of the init parameter of the Web application with the name of the key supplied if it exists and is accessible (
    <web-app>
        <context-param>...
    ). During its initialization, the servlet must have invoked WebFileSystemConfigBuilder.registerWebapp(javax.servlet.ServletContext); note that this can lead to security issues but is acceptable in certain productions environments.
  4. The contents of the file "reflex.properties" of the current directory if it exists.
  5. The contents of the file "$USER_HOME/reflex.properties" if it exists.
  6. The contents of the file "$JAVA_HOME/jre/lib/reflex.properties" if it exists.
  7. The Jar Service Provider discovery mechanism specified in the Jar File Specification. A jar file can have a resource (i.e. an embedded file) such as META-INF/services/package.Class (or META-INF/services/package.Class/variant if the key contains a variant) containing the name of the concrete class to instantiate.
  8. The fallback default implementation, which is given by the META-INF/services/ of RefleX (services found with a line of comment before or ending with a comment that contains "default" will be processed at the end)

The first value found is returned. If one of those method fails, the next is tried.

Author:
Philippe Poulard

Constructor Summary
Discover()
           
 
Method Summary
static Class load(String className)
          Load a class.
static Class lookup(String key)
          Return an implementation of a class.
static Object lookupSingleton(String key)
          Return the singleton of a class.
 
Methods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

Discover

public Discover()
Method Detail

load

public static Class load(String className)
                  throws ClassNotFoundException
Load a class. The class loader used is those of the current thread, and if the class not found, with those that was used to load this class.

Parameters:
className - The name of the class to load.
Returns:
The class.
Throws:
ClassNotFoundException - When the class was not found.

lookup

public static Class lookup(String key)
                    throws ClassNotFoundException
Return an implementation of a class.

Parameters:
key - The class name to lookup, with eventually a variant.
Returns:
The implementation of that class.
Throws:
ClassNotFoundException - When the class is not found.

lookupSingleton

public static Object lookupSingleton(String key)
                              throws InstantiationException,
                                     IllegalAccessException,
                                     ClassNotFoundException
Return the singleton of a class.

Parameters:
key - The class name to lookup, with eventually a variant.
Returns:
The singleton instance of that class. The singleton is built with the static method "newInstance()" of the class if it exists, otherwise with the default constructor.
Throws:
InstantiationException - When the class can't be instanciated.
IllegalAccessException - When the class can't be accessed.
ClassNotFoundException - When the class is not found.