Class OsgiRegistry

  • All Implemented Interfaces:
    java.util.EventListener, org.osgi.framework.BundleListener, org.osgi.framework.SynchronousBundleListener

    public final class OsgiRegistry
    extends java.lang.Object
    implements org.osgi.framework.SynchronousBundleListener
    Taken from Jersey 2. Utility class to deal with OSGi runtime specific behavior. This is mainly to handle META-INF/services lookup and generic/application class lookup issue in OSGi.

    When OSGi runtime is detected by the ServiceFinder class, an instance of OsgiRegistry is created and associated with given OSGi BundleContext. META-INF/services entries are then being accessed via the OSGi Bundle API as direct ClassLoader#getResource() method invocation does not work in this case within OSGi.

    Author:
    Jakub Podlesak (jakub.podlesak at oracle.com)
    • Method Summary

      All Methods Static Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      void bundleChanged​(org.osgi.framework.BundleEvent event)  
      java.lang.Class<?> classForNameWithException​(java.lang.String className)
      Get the Class from the class name.
      static OsgiRegistry getInstance()
      Returns an OsgiRegistry instance.
      java.util.Enumeration<java.net.URL> getPackageResources​(java.lang.String packagePath, java.lang.ClassLoader classLoader)  
      java.util.ResourceBundle getResourceBundle​(java.lang.String bundleName)
      Tries to load resource bundle via OSGi means.
      void hookUp()
      Will hook up this instance with the OSGi runtime.
      • Methods inherited from class java.lang.Object

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

      • getInstance

        public static OsgiRegistry getInstance()
        Returns an OsgiRegistry instance. Call this method only if sure that the application is running in OSGi environment, otherwise a call to this method can lead to an ClassNotFoundException.
        Returns:
        an OsgiRegistry instance.
      • bundleChanged

        public void bundleChanged​(org.osgi.framework.BundleEvent event)
        Specified by:
        bundleChanged in interface org.osgi.framework.BundleListener
      • getPackageResources

        public java.util.Enumeration<java.net.URL> getPackageResources​(java.lang.String packagePath,
                                                                       java.lang.ClassLoader classLoader)
      • classForNameWithException

        public java.lang.Class<?> classForNameWithException​(java.lang.String className)
                                                     throws java.lang.ClassNotFoundException
        Get the Class from the class name.

        The context class loader will be utilized if accessible and non-null. Otherwise the defining class loader of this class will be utilized.

        Parameters:
        className - the class name.
        Returns:
        the Class, otherwise null if the class cannot be found.
        Throws:
        java.lang.ClassNotFoundException - if the class cannot be found.
      • getResourceBundle

        public java.util.ResourceBundle getResourceBundle​(java.lang.String bundleName)
        Tries to load resource bundle via OSGi means. No caching involved here, as localization properties are being cached in Localizer class already.
        Parameters:
        bundleName - name of the resource bundle to load
        Returns:
        resource bundle instance if found, null otherwise
      • hookUp

        public void hookUp()
        Will hook up this instance with the OSGi runtime. This is to actually update SPI provider lookup and class loading mechanisms in Jersey to utilize OSGi features.