Package org.glassfish.jersey.internal
Class OsgiRegistry
java.lang.Object
org.glassfish.jersey.internal.OsgiRegistry
- All Implemented Interfaces:
EventListener
,org.osgi.framework.BundleListener
,org.osgi.framework.SynchronousBundleListener
public final class OsgiRegistry
extends Object
implements org.osgi.framework.SynchronousBundleListener
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, Adam Lindenthal
-
Method Summary
Modifier and TypeMethodDescriptionvoid
bundleChanged
(org.osgi.framework.BundleEvent event) static String
bundleEntryPathToClassName
(String packagePath, String bundleEntryPath) Translates bundle entry path as returned fromBundle.findEntries(String, String, boolean)
to fully qualified class name that resides in given package path (directly or indirectly in its subpackages).Class<?>
classForNameWithException
(String className) Get the Class from the class name.static OsgiRegistry
Returns anOsgiRegistry
instance.getPackageResources
(String packagePath, ClassLoader classLoader, boolean recursive) Get URLs of resources from a given package.getResourceBundle
(String bundleName) Tries to load resource bundle via OSGi means.static boolean
isPackageLevelEntry
(String packagePath, String entryPath) Returns whether the given entry path is located directly in the provided package path.static String
normalizedPackagePath
(String packagePath) Normalized package returns path that does not start with '/' character and ends with '/' character.
-
Method Details
-
getInstance
Returns anOsgiRegistry
instance. Call this method only if sure that the application is running in OSGi environment, otherwise a call to this method can lead to anClassNotFoundException
.- Returns:
- an
OsgiRegistry
instance.
-
bundleChanged
public void bundleChanged(org.osgi.framework.BundleEvent event) - Specified by:
bundleChanged
in interfaceorg.osgi.framework.BundleListener
-
bundleEntryPathToClassName
Translates bundle entry path as returned fromBundle.findEntries(String, String, boolean)
to fully qualified class name that resides in given package path (directly or indirectly in its subpackages).- Parameters:
packagePath
- The package path where the class is located (even recursively)bundleEntryPath
- The bundle path to translate.- Returns:
- Fully qualified class name.
-
isPackageLevelEntry
Returns whether the given entry path is located directly in the provided package path. That is, if the entry is located in a sub-package, thenfalse
is returned.- Parameters:
packagePath
- Package path which the entry is compared toentryPath
- Entry path- Returns:
- Whether the given entry path is located directly in the provided package path.
-
normalizedPackagePath
Normalized package returns path that does not start with '/' character and ends with '/' character. If the argument is '/' then returned value is empty string "".- Parameters:
packagePath
- package path to normalize.- Returns:
- Normalized package path.
-
getPackageResources
public Enumeration<URL> getPackageResources(String packagePath, ClassLoader classLoader, boolean recursive) Get URLs of resources from a given package.- Parameters:
packagePath
- package.classLoader
- resource class loader.recursive
- whether the given package path should be scanned recursively by OSGi- Returns:
- URLs of the located resources.
-
classForNameWithException
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:
ClassNotFoundException
- if the class cannot be found.
-
getResourceBundle
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
-