Interface ServiceReference<S>
-
- Type Parameters:
S
- Type of Service.
- All Superinterfaces:
java.lang.Comparable<java.lang.Object>
@ProviderType public interface ServiceReference<S> extends java.lang.Comparable<java.lang.Object>
A reference to a service.The Framework returns
ServiceReference
objects from theBundleContext.getServiceReference
andBundleContext.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, distinctServiceReference
objects referring to it.ServiceReference
objects associated with aServiceRegistration
object have the samehashCode
and are considered equal (more specifically, theirequals()
method will returntrue
when compared).If the same service object is registered multiple times,
ServiceReference
objects associated with differentServiceRegistration
objects are not equal.- Author:
- $Id: a56f8df70e8c74a76a1ef02b714b4612bc064ecc $
- See Also:
BundleContext.getServiceReference(Class)
,BundleContext.getServiceReference(String)
,BundleContext.getServiceReferences(Class, String)
,BundleContext.getServiceReferences(String, String)
,BundleContext.getService(ServiceReference)
,BundleContext.getServiceObjects(ServiceReference)
-
-
Method Summary
All Methods Instance Methods Abstract Methods Modifier and Type Method Description int
compareTo(java.lang.Object reference)
Compares thisServiceReference
with the specifiedServiceReference
for order.Bundle
getBundle()
Returns the bundle that registered the service referenced by thisServiceReference
object.java.lang.Object
getProperty(java.lang.String key)
Returns the property value to which the specified property key is mapped in the propertiesDictionary
object of the service referenced by thisServiceReference
object.java.lang.String[]
getPropertyKeys()
Returns an array of the keys in the propertiesDictionary
object of the service referenced by thisServiceReference
object.Bundle[]
getUsingBundles()
Returns the bundles that are using the service referenced by thisServiceReference
object.boolean
isAssignableTo(Bundle bundle, java.lang.String className)
Tests if the bundle that registered the service referenced by thisServiceReference
and the specified bundle use the same source for the package of the specified class name.
-
-
-
Method Detail
-
getProperty
java.lang.Object getProperty(java.lang.String key)
Returns the property value to which the specified property key is mapped in the propertiesDictionary
object of the service referenced by thisServiceReference
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
- The property key.- Returns:
- The property value to which the key is mapped;
null
if there is no property named after the key.
-
getPropertyKeys
java.lang.String[] getPropertyKeys()
Returns an array of the keys in the propertiesDictionary
object of the service referenced by thisServiceReference
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 theBundleContext.registerService(String[],Object,Dictionary)
orServiceRegistration.setProperties(Dictionary)
methods.- Returns:
- An array of property keys.
-
getBundle
Bundle getBundle()
Returns the bundle that registered the service referenced by thisServiceReference
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:
- The bundle that registered the service referenced by this
ServiceReference
object;null
if that service has already been unregistered. - See Also:
BundleContext.registerService(String[],Object,Dictionary)
-
getUsingBundles
Bundle[] getUsingBundles()
Returns the bundles that are using the service referenced by thisServiceReference
object. Specifically, this method returns the bundles whose usage count for that service is greater than zero.- 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. - Since:
- 1.1
-
isAssignableTo
boolean isAssignableTo(Bundle bundle, java.lang.String className)
Tests if the bundle that registered the service referenced by thisServiceReference
and the specified bundle use the same source for the package of the specified class name.This method performs the following checks:
- Get the package name from the specified class name.
- For the bundle that registered the service referenced by this
ServiceReference
(registrant bundle); find the source for the package. If no source is found then returntrue
if the registrant bundle is equal to the specified bundle; otherwise returnfalse
. - If the package source of the registrant bundle is equal to the
package source of the specified bundle then return
true
; otherwise returnfalse
.
- Parameters:
bundle
- TheBundle
object to check.className
- The class name to check.- Returns:
true
if the bundle which registered the service referenced by thisServiceReference
and the specified bundle use the same source for the package of the specified class name. Otherwisefalse
is returned.- Throws:
java.lang.IllegalArgumentException
- If the specifiedBundle
was not created by the same framework instance as thisServiceReference
.- Since:
- 1.3
-
compareTo
int compareTo(java.lang.Object reference)
Compares thisServiceReference
with the specifiedServiceReference
for order.If this
ServiceReference
and the specifiedServiceReference
have the sameservice id
they are equal. ThisServiceReference
is less than the specifiedServiceReference
if it has a lowerservice ranking
and greater if it has a higher service ranking. Otherwise, if thisServiceReference
and the specifiedServiceReference
have the sameservice ranking
, thisServiceReference
is less than the specifiedServiceReference
if it has a higherservice id
and greater if it has a lower service id.- Specified by:
compareTo
in interfacejava.lang.Comparable<S>
- Parameters:
reference
- TheServiceReference
to be compared.- Returns:
- Returns a negative integer, zero, or a positive integer if this
ServiceReference
is less than, equal to, or greater than the specifiedServiceReference
. - Throws:
java.lang.IllegalArgumentException
- If the specifiedServiceReference
was not created by the same framework instance as thisServiceReference
.- Since:
- 1.4
-
-