Class ProviderLocator


  • public class ProviderLocator
    extends java.lang.Object
    • Field Summary

      Fields 
      Modifier and Type Field Description
      private static org.osgi.framework.BundleContext context  
      private static java.lang.Object registryTracker  
    • Constructor Summary

      Constructors 
      Modifier Constructor Description
      private ProviderLocator()  
    • Method Summary

      All Methods Static Methods Concrete Methods 
      Modifier and Type Method Description
      static void destroy()
      Cleanup resources on bundle shutdown.
      private static java.lang.Object getRegistry()
      Retrieve the registry from the tracker if it is available, all without causing the interface class to load.
      static java.lang.Object getService​(java.lang.String iface, java.lang.Class<?> contextClass, java.lang.ClassLoader loader)
      Get a single service instance that matches an interface definition.
      static java.lang.Class<?> getServiceClass​(java.lang.String iface, java.lang.Class<?> contextClass, java.lang.ClassLoader loader)
      Locate a service class that matches an interface definition.
      static java.util.List<java.lang.Class<?>> getServiceClasses​(java.lang.String iface, java.lang.Class<?> contextClass, java.lang.ClassLoader loader)
      Get a list of service class implementations that match an interface name.
      static java.util.List<java.lang.Object> getServices​(java.lang.String iface, java.lang.Class<?> contextClass, java.lang.ClassLoader loader)
      Get a list of services that match a given interface name.
      static void init​(org.osgi.framework.BundleContext c)
      initialize the tracker statics for this bundle
      static java.lang.Class<?> loadClass​(java.lang.String className)
      Utility class for locating a class with OSGi registry support.
      static java.lang.Class<?> loadClass​(java.lang.String className, java.lang.Class<?> contextClass)
      Utility class for locating a class with OSGi registry support.
      static java.lang.Class<?> loadClass​(java.lang.String className, java.lang.Class<?> contextClass, java.lang.ClassLoader loader)
      Standardized utility method for performing class lookups with support for OSGi registry lookups.
      static java.lang.Class<?> locate​(java.lang.String providerId)
      Locate a class by its provider id indicator.
      static java.util.List<java.lang.Class<?>> locateAll​(java.lang.String providerId)
      Locate all class files that match a given factory id.
      private static java.lang.Class<?> locateServiceClass​(java.lang.String iface, java.lang.Class<?> contextClass, java.lang.ClassLoader loader)
      Locate the first class for a META-INF/services definition of a given interface class.
      private static java.util.Collection<java.lang.Class<?>> locateServiceClasses​(java.lang.String iface, java.lang.Class<?> contextClass, java.lang.ClassLoader loader)
      Locate all classes that map to a given provider class definition.
      private static java.lang.String locateServiceClassName​(java.lang.String iface, java.lang.Class<?> contextClass, java.lang.ClassLoader loader)
      Locate the first class name for a META-INF/services definition of a given class.
      private static java.lang.String locateServiceClassName​(java.lang.String iface, java.lang.ClassLoader loader)
      Locate a classpath-define service mapping.
      private static java.util.Collection<java.lang.String> locateServiceClassNames​(java.lang.String iface, java.lang.Class<?> contextClass, java.lang.ClassLoader loader)
      Locate all class names name for a META-INF/services definition of a given class.
      (package private) static void locateServiceClassNames​(java.lang.String iface, java.lang.ClassLoader loader, java.util.Set names)
      Locate all class names name for a META-INF/services definition of a given class.
      static java.lang.String lookupByJREPropertyFile​(java.lang.String path, java.lang.String property)
      Perform a service class discovery by looking for a property in a target properties file located in the java.home directory.
      private static java.util.List<java.lang.String> parseServiceDefinition​(java.net.URL u)
      Parse a definition file and return the names of all included implementation classes contained within the file.
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Field Detail

      • context

        private static org.osgi.framework.BundleContext context
      • registryTracker

        private static java.lang.Object registryTracker
    • Constructor Detail

      • ProviderLocator

        private ProviderLocator()
    • Method Detail

      • init

        public static void init​(org.osgi.framework.BundleContext c)
        initialize the tracker statics for this bundle
        Parameters:
        c - The starup BundleContext.
      • destroy

        public static void destroy()
        Cleanup resources on bundle shutdown.
      • locate

        public static java.lang.Class<?> locate​(java.lang.String providerId)
        Locate a class by its provider id indicator. .
        Parameters:
        providerId - The provider id (generally, a fully qualified class name).
        Returns:
        The Class corresponding to this provider id. Returns null if this is not registered or the indicated class can't be loaded.
      • locateAll

        public static java.util.List<java.lang.Class<?>> locateAll​(java.lang.String providerId)
        Locate all class files that match a given factory id.
        Parameters:
        providerId - The target provider identifier.
        Returns:
        A List containing the class objects corresponding to the provider identifier. Returns an empty list if no matching classes can be located.
      • loadClass

        public static java.lang.Class<?> loadClass​(java.lang.String className)
                                            throws java.lang.ClassNotFoundException
        Utility class for locating a class with OSGi registry support. Uses the thread context classloader as part of the search order.
        Parameters:
        className - The name of the target class.
        Returns:
        The loaded class.
        Throws:
        java.lang.ClassNotFoundException - Thrown if the class cannot be located.
      • loadClass

        public static java.lang.Class<?> loadClass​(java.lang.String className,
                                                   java.lang.Class<?> contextClass)
                                            throws java.lang.ClassNotFoundException
        Utility class for locating a class with OSGi registry support. Uses the thread context classloader as part of the search order.
        Parameters:
        className - The name of the target class.
        Returns:
        The loaded class.
        Throws:
        java.lang.ClassNotFoundException - Thrown if the class cannot be located.
      • loadClass

        public static java.lang.Class<?> loadClass​(java.lang.String className,
                                                   java.lang.Class<?> contextClass,
                                                   java.lang.ClassLoader loader)
                                            throws java.lang.ClassNotFoundException
        Standardized utility method for performing class lookups with support for OSGi registry lookups.
        Parameters:
        className - The name of the target class.
        loader - An optional class loader.
        Returns:
        The loaded class
        Throws:
        java.lang.ClassNotFoundException - Thrown if the class cannot be loaded.
      • getService

        public static java.lang.Object getService​(java.lang.String iface,
                                                  java.lang.Class<?> contextClass,
                                                  java.lang.ClassLoader loader)
                                           throws java.lang.Exception
        Get a single service instance that matches an interface definition.
        Parameters:
        iface - The name of the required interface.
        contextClass - The class requesting the lookup (used for class resolution).
        loader - A class loader to use for searching for service definitions and loading classes.
        Returns:
        The service instance, or null if no matching services can be found.
        Throws:
        java.lang.Exception - Thrown for any classloading or exceptions thrown trying to instantiate a service instance.
      • getServiceClass

        public static java.lang.Class<?> getServiceClass​(java.lang.String iface,
                                                         java.lang.Class<?> contextClass,
                                                         java.lang.ClassLoader loader)
                                                  throws java.lang.ClassNotFoundException
        Locate a service class that matches an interface definition.
        Parameters:
        iface - The name of the required interface.
        contextClass - The class requesting the lookup (used for class resolution).
        loader - A class loader to use for searching for service definitions and loading classes.
        Returns:
        The located class, or null if no matching services can be found.
        Throws:
        java.lang.Exception - Thrown for any classloading exceptions thrown trying to load the class.
        java.lang.ClassNotFoundException
      • getServices

        public static java.util.List<java.lang.Object> getServices​(java.lang.String iface,
                                                                   java.lang.Class<?> contextClass,
                                                                   java.lang.ClassLoader loader)
                                                            throws java.lang.Exception
        Get a list of services that match a given interface name. This searches both the current class path and the global repository for matches.
        Parameters:
        iface - The name of the required interface.
        contextClass - The class requesting the lookup (used for class resolution).
        loader - A class loader to use for searching for service definitions and loading classes.
        Returns:
        A list of matching services. Returns an empty list if there are no matches.
        Throws:
        java.lang.Exception - Thrown for any classloading or exceptions thrown trying to instantiate a service instance.
      • getServiceClasses

        public static java.util.List<java.lang.Class<?>> getServiceClasses​(java.lang.String iface,
                                                                           java.lang.Class<?> contextClass,
                                                                           java.lang.ClassLoader loader)
                                                                    throws java.lang.Exception
        Get a list of service class implementations that match an interface name. This searches both the current class path and the global repository for matches.
        Parameters:
        iface - The name of the required interface.
        contextClass - The class requesting the lookup (used for class resolution).
        loader - A class loader to use for searching for service definitions and loading classes.
        Returns:
        A list of matching provider classes. Returns an empty list if there are no matches.
        Throws:
        java.lang.Exception - Thrown for any classloading exceptions thrown trying to load a provider class.
      • locateServiceClassName

        private static java.lang.String locateServiceClassName​(java.lang.String iface,
                                                               java.lang.Class<?> contextClass,
                                                               java.lang.ClassLoader loader)
        Locate the first class name for a META-INF/services definition of a given class. The first matching provider is returned.
        Parameters:
        iface - The interface class name used for the match.
        loader - The classloader for locating resources.
        Returns:
        The mapped provider name, if found. Returns null if no mapping is located.
      • locateServiceClassName

        private static java.lang.String locateServiceClassName​(java.lang.String iface,
                                                               java.lang.ClassLoader loader)
        Locate a classpath-define service mapping.
        Parameters:
        iface - The required interface name.
        loader - The ClassLoader instance to use to locate the service.
        Returns:
        The mapped class name, if one is found. Returns null if the mapping is not located.
      • locateServiceClass

        private static java.lang.Class<?> locateServiceClass​(java.lang.String iface,
                                                             java.lang.Class<?> contextClass,
                                                             java.lang.ClassLoader loader)
                                                      throws java.lang.ClassNotFoundException
        Locate the first class for a META-INF/services definition of a given interface class. The first matching provider is returned.
        Parameters:
        iface - The interface class name used for the match.
        loader - The classloader for locating resources.
        Returns:
        The mapped provider class, if found. Returns null if no mapping is located.
        Throws:
        java.lang.ClassNotFoundException
      • locateServiceClassNames

        private static java.util.Collection<java.lang.String> locateServiceClassNames​(java.lang.String iface,
                                                                                      java.lang.Class<?> contextClass,
                                                                                      java.lang.ClassLoader loader)
        Locate all class names name for a META-INF/services definition of a given class.
        Parameters:
        iface - The interface class name used for the match.
        loader - The classloader for locating resources.
        Returns:
        The mapped provider name, if found. Returns null if no mapping is located.
      • locateServiceClassNames

        static void locateServiceClassNames​(java.lang.String iface,
                                            java.lang.ClassLoader loader,
                                            java.util.Set names)
        Locate all class names name for a META-INF/services definition of a given class.
        Parameters:
        iface - The interface class name used for the match.
        loader - The classloader for locating resources.
      • locateServiceClasses

        private static java.util.Collection<java.lang.Class<?>> locateServiceClasses​(java.lang.String iface,
                                                                                     java.lang.Class<?> contextClass,
                                                                                     java.lang.ClassLoader loader)
                                                                              throws java.lang.ClassNotFoundException
        Locate all classes that map to a given provider class definition. This will search both the services directories, as well as the provider classes from the OSGi provider registry.
        Parameters:
        iface - The interface class name used for the match.
        loader - The classloader for locating resources.
        Returns:
        A list of all mapped classes, if found. Returns an empty list if no mappings are found.
        Throws:
        java.lang.ClassNotFoundException
      • parseServiceDefinition

        private static java.util.List<java.lang.String> parseServiceDefinition​(java.net.URL u)
        Parse a definition file and return the names of all included implementation classes contained within the file.
        Parameters:
        u - The URL of the file
        Returns:
        A list of all matching classes. Returns an empty list if no matches are found.
      • lookupByJREPropertyFile

        public static java.lang.String lookupByJREPropertyFile​(java.lang.String path,
                                                               java.lang.String property)
                                                        throws java.io.IOException
        Perform a service class discovery by looking for a property in a target properties file located in the java.home directory.
        Parameters:
        path - The relative path to the desired properties file.
        property - The name of the required property.
        Returns:
        The value of the named property within the properties file. Returns null if the property doesn't exist or the properties file doesn't exist.
        Throws:
        java.io.IOException
      • getRegistry

        private static java.lang.Object getRegistry()
        Retrieve the registry from the tracker if it is available, all without causing the interface class to load.
        Returns:
        The registry service instance, or null if it is not available for any reason.