Top Description Methods
org.osgi.framework

public Interface ServiceReference<S>

extends Comparable<Object>, BundleReference
Annotations
@ProviderType
Type Parameters
<S>
Type of Service.
Imports
java.util.Dictionary, org.osgi.annotation.versioning.ProviderType

A reference to a service.

The Framework returns ServiceReference objects from the BundleContext.getServiceReference and BundleContext.getServiceReferences methods.

A ServiceReference object may be shared between bundles and can be used to examine the properties of the service and to get the service object.

Every service registered in the Framework has a unique ServiceRegistration object and may have multiple, distinct ServiceReference objects referring to it. ServiceReference objects associated with a ServiceRegistration object have the same hashCode and are considered equal (more specifically, their equals() method will return true when compared).

If the same service object is registered multiple times, ServiceReference objects associated with different ServiceRegistration objects are not equal.

Author
$Id: adb91d7f0922417180e901dc6ec447f467b34921 $
See Also
BundleContext#getServiceReference(Class), BundleContext#getServiceReference(String), BundleContext#getServiceReferences(Class, String), BundleContext#getServiceReferences(String, String), BundleContext#getService(ServiceReference), BundleContext#getServiceObjects(ServiceReference)

Method Summary

Modifier and TypeMethod and Description
public <
The type to which this ServiceReference object is to be adapted.
A
>
A

Returns:

The object, of the specified type, to which this ServiceReference object has been adapted or null if this ServiceReference object cannot be adapted to the specified type.
adapt
(Class<A>
Class object for the type to which this ServiceReference object is to be adapted.
type
)

Adapt this ServiceReference object to the specified type.

public int

Returns:

Returns a negative integer, zero, or a positive integer if this ServiceReference is less than, equal to, or greater than the specified ServiceReference.
compareTo
(Object
The ServiceReference to be compared.
reference
)

Redeclares java.lang.Comparable.compareTo.

Compares this ServiceReference with the specified ServiceReference for order.

public Bundle

Returns:

The bundle that registered the service referenced by this ServiceReference object; null if that service has already been unregistered.
getBundle
()

Redeclares org.osgi.framework.BundleReference.getBundle.

Returns the bundle that registered the service referenced by this ServiceReference object.

public Dictionary<String, Object>

Returns:

A copy of the properties of the service referenced by this ServiceReference object
getProperties
()

Returns a copy of the properties of the service referenced by this ServiceReference object.

public Object

Returns:

The property value to which the key is mapped; null if there is no property named after the key.
getProperty
(String
The property key.
key
)

Returns the property value to which the specified property key is mapped in the properties Dictionary object of the service referenced by this ServiceReference object.

public String[]

Returns:

An array of property keys.
getPropertyKeys
()

Returns an array of the keys in the properties Dictionary object of the service referenced by this ServiceReference object.

public Bundle[]

Returns:

An array of bundles whose usage count for the service referenced by this ServiceReference object is greater than zero; null if no bundles are currently using that service.
getUsingBundles
()

Returns the bundles that are using the service referenced by this ServiceReference object.

public boolean

Returns:

true if the bundle which registered the service referenced by this ServiceReference and the specified bundle use the same source for the package of the specified class name. Otherwise false is returned.
isAssignableTo
(Bundle
The Bundle object to check.
bundle
,
String
The class name to check.
className
)

Tests if the bundle that registered the service referenced by this ServiceReference and the specified bundle use the same source for the package of the specified class name.

Method Detail

adaptback to summary
public <A> A adapt(Class<A> type)

Adapt this ServiceReference object to the specified type.

Adapting this ServiceReference object to the specified type may require certain checks, including security checks, to succeed. If a check does not succeed, then this ServiceReference object cannot be adapted and null is returned.

Parameters
<A>
The type to which this ServiceReference object is to be adapted.
type:Class<A>

Class object for the type to which this ServiceReference object is to be adapted.

Returns:A

The object, of the specified type, to which this ServiceReference object has been adapted or null if this ServiceReference object cannot be adapted to the specified type.

Exceptions
SecurityException:
If the caller does not have the appropriate AdaptPermission[type,this,ADAPT], and the Java Runtime Environment supports permissions.
Since
1.10
compareToback to summary
public int compareTo(Object reference)

Redeclares java.lang.Comparable.compareTo.

Compares this ServiceReference with the specified ServiceReference for order.

If this ServiceReference and the specified ServiceReference have the same service id they are equal. This ServiceReference is less than the specified ServiceReference if it has a lower service ranking and greater if it has a higher service ranking. Otherwise, if this ServiceReference and the specified ServiceReference have the same service ranking, this ServiceReference is less than the specified ServiceReference if it has a higher service id and greater if it has a lower service id.

Parameters
reference:Object

The ServiceReference to be compared.

Returns:int

Returns a negative integer, zero, or a positive integer if this ServiceReference is less than, equal to, or greater than the specified ServiceReference.

Annotations
@Override
Exceptions
IllegalArgumentException:
If the specified ServiceReference was not created by the same framework instance as this ServiceReference.
Since
1.4
getBundleback to summary
public Bundle getBundle()

Redeclares org.osgi.framework.BundleReference.getBundle.

Returns the bundle that registered the service referenced by this ServiceReference object.

This method must return null when the service has been unregistered. This can be used to determine if the service has been unregistered.

Returns:Bundle

The bundle that registered the service referenced by this ServiceReference object; null if that service has already been unregistered.

Annotations
@Override
See Also
BundleContext#registerService(String[], Object, Dictionary)
getPropertiesback to summary
public Dictionary<String, Object> getProperties()

Returns a copy of the properties of the service referenced by this ServiceReference object.

This method will continue to return the properties after the service has been unregistered. This is so references to unregistered services (for example, ServiceReference objects stored in the log) can still be interrogated.

The returned Dictionary object:

Returns:Dictionary<String, Object>

A copy of the properties of the service referenced by this ServiceReference object

Since
1.9
getPropertyback to summary
public Object getProperty(String key)

Returns the property value to which the specified property key is mapped in the properties Dictionary object of the service referenced by this ServiceReference object.

Property keys are case-insensitive.

This method must continue to return property values after the service has been unregistered. This is so references to unregistered services (for example, ServiceReference objects stored in the log) can still be interrogated.

Parameters
key:String

The property key.

Returns:Object

The property value to which the key is mapped; null if there is no property named after the key.

getPropertyKeysback to summary
public String[] getPropertyKeys()

Returns an array of the keys in the properties Dictionary object of the service referenced by this ServiceReference object.

This method will continue to return the keys after the service has been unregistered. This is so references to unregistered services (for example, ServiceReference objects stored in the log) can still be interrogated.

This method is case-preserving ; this means that every key in the returned array must have the same case as the corresponding key in the properties Dictionary that was passed to the BundleContext#registerService(String[], Object, Dictionary) or ServiceRegistration#setProperties(Dictionary) methods.

Returns:String[]

An array of property keys.

getUsingBundlesback to summary
public Bundle[] getUsingBundles()

Returns the bundles that are using the service referenced by this ServiceReference object. Specifically, this method returns the bundles whose usage count for that service is greater than zero.

Returns:Bundle[]

An array of bundles whose usage count for the service referenced by this ServiceReference object is greater than zero; null if no bundles are currently using that service.

Since
1.1
isAssignableToback to summary
public boolean isAssignableTo(Bundle bundle, String className)

Tests if the bundle that registered the service referenced by this ServiceReference and the specified bundle use the same source for the package of the specified class name.

This method performs the following checks:

  1. If the specified bundle is equal to the bundle that registered the service referenced by this ServiceReference (registrant bundle) return true.
  2. Get the package name from the specified class name.
  3. For the specified bundle; find the source for the package. If no source is found then return true (use of reflection is assumed by the specified bundle).
  4. For the registrant bundle; find the source for the package. If the package source is found then return true if the package source equals the package source of the specified bundle; otherwise return false.
  5. If no package source is found for the registrant bundle then determine the package source based on the service object. If the service object is a ServiceFactory and the factory implementation is not from the registrant bundle return true; otherwise attempt to find the package source based on the service object class. If the package source is found and is equal to package source of the specified bundle return true; otherwise return false.
Parameters
bundle:Bundle

The Bundle object to check.

className:String

The class name to check.

Returns:boolean

true if the bundle which registered the service referenced by this ServiceReference and the specified bundle use the same source for the package of the specified class name. Otherwise false is returned.

Exceptions
IllegalArgumentException:
If the specified Bundle was not created by the same framework instance as this ServiceReference.
Since
1.3