Top Description Inners Fields Constructors Methods
org.glassfish.tyrus.core

public final Class OsgiRegistry

extends Object
implements SynchronousBundleListener
Class Inheritance
All Implemented Interfaces
org.osgi.framework.SynchronousBundleListener, org.osgi.framework.BundleListener, java.util.EventListener
Imports
java.io.BufferedReader, .IOException, .InputStream, .InputStreamReader, java.net.URI, .URISyntaxException, .URL, java.util.ArrayList, .Collections, .Enumeration, .HashMap, .Iterator, .LinkedList, .List, .Map, .PropertyResourceBundle, .ResourceBundle, java.util.concurrent.Callable, .ConcurrentHashMap, java.util.concurrent.locks.ReadWriteLock, .ReentrantReadWriteLock, java.util.jar.JarEntry, .JarInputStream, java.util.logging.Level, .Logger, org.glassfish.tyrus.core.l10n.LocalizationMessages, org.osgi.framework.Bundle, .BundleContext, .BundleEvent, .BundleReference, .FrameworkUtil, .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

Nested and Inner Type Summary

Modifier and TypeClass and Description
private static class
private class

Field Summary

Modifier and TypeField and Description
private final BundleContext
private Map<String, Bundle>
private static final String
private final Map<Long, Map<String, Callable<List<Class<?>>>>>
private static OsgiRegistry
private final ReadWriteLock
private static final Logger

Constructor Summary

AccessConstructor and Description
private
OsgiRegistry(BundleContext
must be a non-null instance of a BundleContext
bundleContext
)

Creates a new OsgiRegistry instance bound to a particular OSGi runtime.

Method Summary

Modifier and TypeMethod and Description
public void
bundleChanged(BundleEvent
The BundleEvent.
event
)

Implements org.osgi.framework.BundleListener.bundleChanged.

Receives notification that a bundle has had a lifecycle change.

public Class<?>

Returns:

the Class, otherwise null if the class cannot be found.
classForNameWithException
(final String
the class name.
className
)

Get the Class from the class name.

public static synchronized OsgiRegistry

Returns:

an OsgiRegistry instance.
getInstance
()

Returns an OsgiRegistry instance.

public Enumeration<URL>
getPackageResources(final String packagePath, final ClassLoader classLoader)

public ResourceBundle

Returns:

resource bundle instance if found, null otherwise
getResourceBundle
(final String
name of the resource bundle to load
bundleName
)

Tries to load resource bundle via OSGi means.

public void
hookUp()

Will hook up this instance with the OSGi runtime.

private List<Class<?>>
private void
register(final Bundle bundle)

private void
private void
Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

bundleContextback to summary
private final BundleContext bundleContext
classToBundleMappingback to summary
private Map<String, Bundle> classToBundleMapping
CoreBundleSymbolicNAMEback to summary
private static final String CoreBundleSymbolicNAME
factoriesback to summary
private final Map<Long, Map<String, Callable<List<Class<?>>>>> factories
instanceback to summary
private static OsgiRegistry instance
lockback to summary
private final ReadWriteLock lock
LOGGERback to summary
private static final Logger LOGGER

Constructor Detail

OsgiRegistryback to summary
private OsgiRegistry(BundleContext bundleContext)

Creates a new OsgiRegistry instance bound to a particular OSGi runtime. The only parameter must be an instance of a BundleContext.

Parameters
bundleContext:BundleContext

must be a non-null instance of a BundleContext

Method Detail

bundleChangedback to summary
public void bundleChanged(BundleEvent event)

Implements org.osgi.framework.BundleListener.bundleChanged.

Doc from org.osgi.framework.BundleListener.bundleChanged.

Receives notification that a bundle has had a lifecycle change.

Parameters
event:BundleEvent

The BundleEvent.

Annotations
@Override
classForNameWithExceptionback to summary
public Class<?> classForNameWithException(final String className) throws 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:String

the class name.

Returns:Class<?>

the Class, otherwise null if the class cannot be found.

Exceptions
ClassNotFoundException:
if the class cannot be found.
getInstanceback to summary
public static synchronized 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:OsgiRegistry

an OsgiRegistry instance.

getPackageResourcesback to summary
public Enumeration<URL> getPackageResources(final String packagePath, final ClassLoader classLoader)
Annotations
@SuppressWarnings:unchecked
getResourceBundleback to summary
public ResourceBundle getResourceBundle(final 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:String

name of the resource bundle to load

Returns:ResourceBundle

resource bundle instance if found, null otherwise

hookUpback to summary
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.

locateAllProvidersback to summary
private List<Class<?>> locateAllProviders(String serviceName)
registerback to summary
private void register(final Bundle bundle)
registerExistingBundlesback to summary
private void registerExistingBundles()
setOSGiServiceFinderIteratorProviderback to summary
private void setOSGiServiceFinderIteratorProvider()
org.glassfish.tyrus.core back to summary

private Class OsgiRegistry.BundleSpiProvidersLoader

extends Object
implements Callable<List<Class<?>>>
Class Inheritance
  • java.lang.Object
  • org.glassfish.tyrus.core.OsgiRegistry.BundleSpiProvidersLoader
All Implemented Interfaces
java.util.concurrent.Callable

Field Summary

Modifier and TypeField and Description
private final Bundle
private final String
private final URI

Constructor Summary

AccessConstructor and Description
pack-priv
BundleSpiProvidersLoader(final String spi, final URI spiRegistryUri, final Bundle bundle)

Method Summary

Modifier and TypeMethod and Description
public List<Class<?>>
call()

Implements java.util.concurrent.Callable.call.

Computes a result, or throws an exception if unable to do so.

public boolean
equals(Object
the reference object with which to compare.
obj
)

Overrides java.lang.Object.equals.

Indicates whether some other object is "equal to" this one.

public int
hashCode()

Overrides java.lang.Object.hashCode.

Returns a hash code value for this object.

public String
toString()

Overrides java.lang.Object.toString.

Returns a string representation of the object.

Inherited from java.lang.Object:
clonefinalizegetClassnotifynotifyAllwaitwaitwait

Field Detail

bundleback to summary
private final Bundle bundle
spiback to summary
private final String spi
spiRegistryUriback to summary
private final URI spiRegistryUri

Constructor Detail

BundleSpiProvidersLoaderback to summary
pack-priv BundleSpiProvidersLoader(final String spi, final URI spiRegistryUri, final Bundle bundle)

Method Detail

callback to summary
public List<Class<?>> call() throws Exception

Implements java.util.concurrent.Callable.call.

Doc from java.util.concurrent.Callable.call.

Computes a result, or throws an exception if unable to do so.

Returns:List<Class<?>>

computed result

Annotations
@Override
Exceptions
Exception:
if unable to compute a result
equalsback to summary
public boolean equals(Object obj)

Overrides java.lang.Object.equals.

Doc from java.lang.Object.equals.

Indicates whether some other object is "equal to" this one.

The equals method implements an equivalence relation on non-null object references:

  • It is reflexive: for any non-null reference value x, x.equals(x) should return true.
  • It is symmetric: for any non-null reference values x and y, x.equals(y) should return true if and only if y.equals(x) returns true.
  • It is transitive: for any non-null reference values x, y, and z, if x.equals(y) returns true and y.equals(z) returns true, then x.equals(z) should return true.
  • It is consistent: for any non-null reference values x and y, multiple invocations of x.equals(y) consistently return true or consistently return false, provided no information used in equals comparisons on the objects is modified.
  • For any non-null reference value x, x.equals(null) should return false.

An equivalence relation partitions the elements it operates on into equivalence classes; all the members of an equivalence class are equal to each other. Members of an equivalence class are substitutable for each other, at least for some purposes.

Parameters
obj:Object

the reference object with which to compare.

Returns:boolean

true if this object is the same as the obj argument; false otherwise.

Annotations
@Override
hashCodeback to summary
public int hashCode()

Overrides java.lang.Object.hashCode.

Doc from java.lang.Object.hashCode.

Returns a hash code value for this object. This method is supported for the benefit of hash tables such as those provided by java.util.HashMap.

The general contract of hashCode is:

  • Whenever it is invoked on the same object more than once during an execution of a Java application, the hashCode method must consistently return the same integer, provided no information used in equals comparisons on the object is modified. This integer need not remain consistent from one execution of an application to another execution of the same application.
  • If two objects are equal according to the equals method, then calling the hashCode method on each of the two objects must produce the same integer result.
  • It is not required that if two objects are unequal according to the equals method, then calling the hashCode method on each of the two objects must produce distinct integer results. However, the programmer should be aware that producing distinct integer results for unequal objects may improve the performance of hash tables.
Returns:int

a hash code value for this object

Annotations
@Override
toStringback to summary
public String toString()

Overrides java.lang.Object.toString.

Doc from java.lang.Object.toString.

Returns a string representation of the object. Satisfying this method's contract implies a non-null result must be returned.

Returns:String

a string representation of the object

Annotations
@Override
org.glassfish.tyrus.core back to summary

private final Class OsgiRegistry.OsgiServiceFinder

extends ServiceIteratorProvider
Class Inheritance

Field Summary

Modifier and TypeField and Description
pack-priv final ServiceFinder.ServiceIteratorProvider

Constructor Summary

AccessConstructor and Description
private

Method Summary

Modifier and TypeMethod and Description
public <T> Iterator<Class<T>>
createClassIterator(Class<T>
the service class.
service
,
String
the service name.
serviceName
,
ClassLoader
the class loader to utilize when loading provider classes.
loader
,
boolean
if true ignore the provider class if cannot be found, otherwise throw a ClassNotFoundException.
ignoreOnClassNotFound
)

Implements abstract org.glassfish.tyrus.core.ServiceFinder.ServiceIteratorProvider.createClassIterator.

Iterate over provider classes of a service.

public <T> Iterator<T>
createIterator(final Class<T>
the service class.
serviceClass
,
final String
the service name.
serviceName
,
ClassLoader
the class loader to utilize when loading provider classes.
loader
,
boolean
if true ignore an instance if the corresponding provider class if cannot be found, otherwise throw a ClassNotFoundException.
ignoreOnClassNotFound
)

Implements abstract org.glassfish.tyrus.core.ServiceFinder.ServiceIteratorProvider.createIterator.

Iterate over provider instances of a service.

Field Detail

defaultIteratorback to summary
pack-priv final ServiceFinder.ServiceIteratorProvider defaultIterator

Constructor Detail

OsgiServiceFinderback to summary
private OsgiServiceFinder()

Method Detail

createClassIteratorback to summary
public <T> Iterator<Class<T>> createClassIterator(Class<T> service, String serviceName, ClassLoader loader, boolean ignoreOnClassNotFound)

Implements abstract org.glassfish.tyrus.core.ServiceFinder.ServiceIteratorProvider.createClassIterator.

Doc from org.glassfish.tyrus.core.ServiceFinder.ServiceIteratorProvider.createClassIterator.

Iterate over provider classes of a service.

Parameters
<T>
the type of the service.
service:Class<T>

the service class.

serviceName:String

the service name.

loader:ClassLoader

the class loader to utilize when loading provider classes.

ignoreOnClassNotFound:boolean

if true ignore the provider class if cannot be found, otherwise throw a ClassNotFoundException.

Returns:Iterator<Class<T>>

the provider class iterator.

Annotations
@Override
createIteratorback to summary
public <T> Iterator<T> createIterator(final Class<T> serviceClass, final String serviceName, ClassLoader loader, boolean ignoreOnClassNotFound)

Implements abstract org.glassfish.tyrus.core.ServiceFinder.ServiceIteratorProvider.createIterator.

Doc from org.glassfish.tyrus.core.ServiceFinder.ServiceIteratorProvider.createIterator.

Iterate over provider instances of a service.

Parameters
<T>
the type of the service.
serviceClass:Class<T>

the service class.

serviceName:String

the service name.

loader:ClassLoader

the class loader to utilize when loading provider classes.

ignoreOnClassNotFound:boolean

if true ignore an instance if the corresponding provider class if cannot be found, otherwise throw a ClassNotFoundException.

Returns:Iterator<T>

the provider instance iterator.

Annotations
@Override