Evaluating Software Design Patterns
— the "Gang of Four" patterns implemented in Java 6

dk.rode.thesis.singleton
Class LoadableSingletonRegistry<T>

java.lang.Object
  extended by dk.rode.thesis.singleton.StatefullSingletonRegistry<T>
      extended by dk.rode.thesis.singleton.LoadableSingletonRegistry<T>
Type Parameters:
T - The super type of all singleton types handled by this registry. Use Object if any type of singleton type can be handled.
All Implemented Interfaces:
SingletonRegistry<T>, Iterable<T>

public abstract class LoadableSingletonRegistry<T>
extends StatefullSingletonRegistry<T>

A loadable singleton registry is a registry that offers the same functionality as StatefullSingletonRegistry, but also allows loading of the actual singleton classes if not already loaded using the getInstance(String) method.

All loaded singleton classes will be loaded using the class loader that loaded this class, or by a specific class loader supplied at construction time. Only singleton classes matching the bound supplied as T can be loaded, regardless if T is generic or not.

The class is abstract in order to preserve the generic information, and must be created in the following manner:

   LoadableSingletonRegistry<Foo<? extends CharSequence>> registry = 
     new LoadableSingletonRegistry<Foo<? extends CharSequence>>(){}; // default: StatelessSingletonRegistry is used
 
   ClassLoader classLoader = ..;
   SingletonRegistry<Foo<? extends CharSequence>> aggregate = ..;
   LoadableSingletonRegistry<Foo<? extends CharSequence>> registry = 
     new LoadableSingletonRegistry<Foo<? extends CharSequence>>(classLoader, aggregate){};
 
In the above example, only singleton types conforming to Foo<? extends CharSequence> can be loaded.

Implementation notes:
This class cannot load singleton types implemented using enumerations, because generic information from super-classes is used and all enumerations extend java.lang.Enum. They can still be acquired using the StatefullSingletonRegistry.getInstance(Class) method, though.

Author:
Gunni Rode / rode.dk

Field Summary
private  ClassLoader classLoader
          The class loader used by this registry.
 
Constructor Summary
protected LoadableSingletonRegistry()
          Constructor.
protected LoadableSingletonRegistry(ClassLoader classLoader)
          Constructor.
protected LoadableSingletonRegistry(ClassLoader classLoader, SingletonRegistry<T> registry)
          Constructor.
 
Method Summary
 T getInstance(String className)
          Loads the singleton type represented by the class name supplied as className using the class loader that loaded this registry, and then invokes StatefullSingletonRegistry.getInstance(Class) using the loaded singleton type to return the singleton instance.
 
Methods inherited from class dk.rode.thesis.singleton.StatefullSingletonRegistry
discard, getInstance, isSingleton, iterator, toString
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

classLoader

private final ClassLoader classLoader
The class loader used by this registry.

Default class loader is the class loader that loaded this class.

Never null.

Constructor Detail

LoadableSingletonRegistry

protected LoadableSingletonRegistry()
Constructor.

An instance of the StatelessSingletonRegistry type will be used to acquire the singleton instances.

The class loader that loaded this class will be used by this registry.


LoadableSingletonRegistry

protected LoadableSingletonRegistry(ClassLoader classLoader)
Constructor.

An instance of the StatelessSingletonRegistry type will be used to acquire the singleton instances.

Parameters:
classLoader - The class loader to use; cannot be null.
Throws:
NullPointerException - If classLoader is null.

LoadableSingletonRegistry

protected LoadableSingletonRegistry(ClassLoader classLoader,
                                    SingletonRegistry<T> registry)
Constructor.

Parameters:
classLoader - The class loader to use; cannot be null.
registry - The registry that will be used to acquire the singleton instances; cannot be null.
Throws:
NullPointerException - If either argument is null.
Method Detail

getInstance

public T getInstance(String className)
              throws ClassNotFoundException
Loads the singleton type represented by the class name supplied as className using the class loader that loaded this registry, and then invokes StatefullSingletonRegistry.getInstance(Class) using the loaded singleton type to return the singleton instance.

A cast is required to obtain the exact singleton type.

Parameters:
className - The class name of the singleton type; cannot be null.
Returns:
The singleton instance; never null.
Throws:
NullPointerException - If className is null.
ClassNotFoundException - If className is illegal.
ClassCastException - If className represents a type that is not an instance of T, including if T is generic.
SingletonException - If the class represented by className is not a singleton type handled by this registry, or if the type cannot be created when first requested.

Gunni Rode / rode.dk

Feel free to use and/or modify the Java 6 source code developed for this thesis AT YOUR OWN RISK, but note that the source code comes WITHOUT ANY — and I do mean WITHOUT ANY — form of warranty WHAT SO EVER!

The original thesis and source code are available at rode.dk/thesis.