Package org.glassfish.jersey.internal
Class OsgiRegistry
- java.lang.Object
-
- org.glassfish.jersey.internal.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.SynchronousBundleListenerUtility 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 theServiceFinderclass, 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.
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description private static classOsgiRegistry.BundleSpiProvidersLoaderprivate classOsgiRegistry.OsgiServiceFinder
-
Field Summary
Fields Modifier and Type Field Description private org.osgi.framework.BundleContextbundleContextprivate java.util.Map<java.lang.String,org.osgi.framework.Bundle>classToBundleMappingprivate static java.lang.StringCoreBundleSymbolicNAMEprivate java.util.Map<java.lang.Long,java.util.Map<java.lang.String,java.util.concurrent.Callable<java.util.List<java.lang.Class<?>>>>>factoriesprivate static OsgiRegistryinstanceprivate java.util.concurrent.locks.ReadWriteLocklockprivate static java.util.logging.LoggerLOGGERprivate static java.lang.StringWEB_INF_CLASSES
-
Constructor Summary
Constructors Modifier Constructor Description privateOsgiRegistry(org.osgi.framework.BundleContext bundleContext)Creates a new OsgiRegistry instance bound to a particular OSGi runtime.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description voidbundleChanged(org.osgi.framework.BundleEvent event)static java.lang.StringbundleEntryPathToClassName(java.lang.String packagePath, java.lang.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).java.lang.Class<?>classForNameWithException(java.lang.String className)Get the Class from the class name.private static java.util.Enumeration<java.net.URL>findEntries(org.osgi.framework.Bundle bundle, java.lang.String path, java.lang.String fileNamePattern, boolean recursive)static OsgiRegistrygetInstance()Returns anOsgiRegistryinstance.java.util.Enumeration<java.net.URL>getPackageResources(java.lang.String packagePath, java.lang.ClassLoader classLoader, boolean recursive)Get URLs of resources from a given package.java.util.ResourceBundlegetResourceBundle(java.lang.String bundleName)Tries to load resource bundle via OSGi means.(package private) voidhookUp()Will hook up this instance with the OSGi runtime.static booleanisPackageLevelEntry(java.lang.String packagePath, java.lang.String entryPath)Returns whether the given entry path is located directly in the provided package path.private static java.lang.Class<?>loadClass(org.osgi.framework.Bundle bundle, java.lang.String className)private java.util.List<java.lang.Class<?>>locateAllProviders(java.lang.String serviceName)static java.lang.StringnormalizedPackagePath(java.lang.String packagePath)Normalized package returns path that does not start with '/' character and ends with '/' character.private voidregister(org.osgi.framework.Bundle bundle)private voidregisterExistingBundles()private voidsetOSGiServiceFinderIteratorProvider()
-
-
-
Field Detail
-
WEB_INF_CLASSES
private static final java.lang.String WEB_INF_CLASSES
- See Also:
- Constant Field Values
-
CoreBundleSymbolicNAME
private static final java.lang.String CoreBundleSymbolicNAME
- See Also:
- Constant Field Values
-
LOGGER
private static final java.util.logging.Logger LOGGER
-
bundleContext
private final org.osgi.framework.BundleContext bundleContext
-
factories
private final java.util.Map<java.lang.Long,java.util.Map<java.lang.String,java.util.concurrent.Callable<java.util.List<java.lang.Class<?>>>>> factories
-
lock
private final java.util.concurrent.locks.ReadWriteLock lock
-
instance
private static OsgiRegistry instance
-
classToBundleMapping
private final java.util.Map<java.lang.String,org.osgi.framework.Bundle> classToBundleMapping
-
-
Constructor Detail
-
OsgiRegistry
private OsgiRegistry(org.osgi.framework.BundleContext bundleContext)
Creates a new OsgiRegistry instance bound to a particular OSGi runtime. The only parameter must be an instance of aBundleContext.- Parameters:
bundleContext- must be a non-null instance of a BundleContext
-
-
Method Detail
-
getInstance
public static OsgiRegistry getInstance()
Returns anOsgiRegistryinstance. 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
OsgiRegistryinstance.
-
bundleChanged
public void bundleChanged(org.osgi.framework.BundleEvent event)
- Specified by:
bundleChangedin interfaceorg.osgi.framework.BundleListener
-
bundleEntryPathToClassName
public static java.lang.String bundleEntryPathToClassName(java.lang.String packagePath, java.lang.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).- Parameters:
packagePath- The package path where the class is located (even recursively)bundleEntryPath- The bundle path to translate.- Returns:
- Fully qualified class name.
-
isPackageLevelEntry
public static boolean isPackageLevelEntry(java.lang.String packagePath, java.lang.String entryPath)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, thenfalseis 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
public static java.lang.String normalizedPackagePath(java.lang.String packagePath)
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 java.util.Enumeration<java.net.URL> getPackageResources(java.lang.String packagePath, java.lang.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
public java.lang.Class<?> classForNameWithException(java.lang.String className) throws java.lang.ClassNotFoundExceptionGet 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
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.
-
registerExistingBundles
private void registerExistingBundles()
-
setOSGiServiceFinderIteratorProvider
private void setOSGiServiceFinderIteratorProvider()
-
register
private void register(org.osgi.framework.Bundle bundle)
-
locateAllProviders
private java.util.List<java.lang.Class<?>> locateAllProviders(java.lang.String serviceName)
-
loadClass
private static java.lang.Class<?> loadClass(org.osgi.framework.Bundle bundle, java.lang.String className) throws java.lang.ClassNotFoundException- Throws:
java.lang.ClassNotFoundException
-
findEntries
private static java.util.Enumeration<java.net.URL> findEntries(org.osgi.framework.Bundle bundle, java.lang.String path, java.lang.String fileNamePattern, boolean recursive)
-
-