Class ClassFactory

java.lang.Object
com.aquima.interactions.foundation.utility.ClassFactory

public final class ClassFactory extends Object
This class provides loading and registering implementation classes for an interface class in such a way that a jvm can be started with -Dlookupkey=nl.everest.myimplementation. This class loader will lookup the implementation and create a new instance for it. One can manually register implementations as well as an optional operation.
Since:
5.0
Author:
F van der Meer, Jon van Leuven
  • Method Details

    • createInstance

      public static <T> T createInstance(String lookUpKey, Class<T> interfaceClass)

      The class factory will create an implementation instance for an interface class when it is configured to know which implementation exists for the interface class.

      When there is no implementation known for the interface class this method will throw an AppException. When the interface class' implementation is found a new instance will be created with the default constructor. When the class is not compatible with the interface class, has no default constructor or is not on the classpath an AppException is raised.

      This method will look in the system properties for a suitable implementation for an interface class. Which gives the option to start java with a -Dlookupkey=nl.everest.myclass option.

      Type Parameters:
      T - the type of the interfaceClass
      Parameters:
      lookUpKey - which should be non-null and greater then zero length.
      interfaceClass - which should point to an interface or non-finalized class.
      Returns:
      An instance of the specified class
      Throws:
      LookupFailureException - When there was no value specified for the lookupKey.
      AppException - When an exception occurs during the load of the class.
      IllegalArgumentException - When the input parameters were incorrect.
    • createInstance

      public static Object createInstance(String className)
      Create an instance of className (using the default constructor).
      Parameters:
      className - The name of the class that should be instantiated.
      Returns:
      New instance of the class specified.
      Throws:
      AppException - This exception is thrown when the instance could not be created.
    • registerImplementation

      public static <T> boolean registerImplementation(String key, Class<T> implementationClass)
      Register a class indicated by the specified key. Another way to do this is starting your JVM with -Dkey=nl.everest.myclass
      Type Parameters:
      T - the type of the implementationClass
      Parameters:
      key - registers an implementation of a certain class under the specified key, which should be non-null and non-zero length.
      implementationClass - the implementation class it will represent.
      Returns:
      boolean indicating whether registration is changed
    • unregisterImplementation

      public static boolean unregisterImplementation(String key)
      Unregister an implementation by its key.
      Parameters:
      key - Unregisters an implementation of a certain class under the specified key, which should be non-null and non-zero length.
      Returns:
      boolean indicating if the operation actually did remove a an implementation.