Class FactoryFinder<T>

  • Type Parameters:
    T - The type of the object Factory to locate.

    public class FactoryFinder<T>
    extends java.lang.Object
    A Factory finding helper class used to locate objects that serve as Factories for other Object types. The search an instantiate mechanism is configurable so that in a non-stand-alone environment such as OSGI the finder and be configured to work.
    • Constructor Detail

      • FactoryFinder

        public FactoryFinder​(java.lang.Class<T> factoryType,
                             java.lang.String path)
        Creates a new instance of the FactoryFinder using the given search path.
        Parameters:
        factoryType - The Class that defines what this finder is searching for.
        path - The path to use when searching for the factory definitions.
    • Method Detail

      • getObjectFactory

        public static FactoryFinder.ObjectFactory getObjectFactory()
        Returns:
        the currently configured ObjectFactory instance used to locate the Factory objects.
      • setObjectFactory

        public static void setObjectFactory​(FactoryFinder.ObjectFactory objectFactory)
        Sets the ObjectFactory instance to use when searching for the Factory class. This allows the default instance to be overridden in an environment where the basic version will not work.
        Parameters:
        objectFactory - the new object factory to use when searching for a Factory instance.
      • newInstance

        public T newInstance​(java.lang.String key)
                      throws java.lang.IllegalAccessException,
                             java.lang.InstantiationException,
                             java.io.IOException,
                             java.lang.ClassNotFoundException,
                             java.lang.ClassCastException,
                             ResourceNotFoundException,
                             java.lang.reflect.InvocationTargetException,
                             java.lang.NoSuchMethodException,
                             java.lang.SecurityException
        Creates a new instance of the given key. The method first checks the cache of previously found factory instances for one that matches the key. If no cached version exists then the factory will be searched for using the configured ObjectFactory instance.
        Parameters:
        key - is the key to add to the path to find a text file containing the factory name
        Returns:
        a newly created instance
        Throws:
        java.lang.IllegalAccessException - if an error occurs while accessing the search path.
        java.lang.InstantiationException - if the factory object fails on create.
        ResourceNotFoundException - if the resource with the given key does not exist.
        java.io.IOException - if the search encounter an IO error.
        java.lang.ClassNotFoundException - if the class that is to be loaded cannot be found.
        java.lang.ClassCastException - if the found object is not assignable to the request factory type.
        java.lang.reflect.InvocationTargetException - if the constructor of the found factory throws an exception
        java.lang.NoSuchMethodException - if the factory class found does not have a suitable constructor
        java.lang.SecurityException - if a security error occurs trying to create the factory instance.
      • registerProviderFactory

        public void registerProviderFactory​(java.lang.String scheme,
                                            T factory)
        Allow registration of a Provider factory without wiring via META-INF classes
        Parameters:
        scheme - The URI scheme value that names the target Provider instance.
        factory - The factory to register in this finder.