Interface ServiceReference<S>
-
- Type Parameters:
S- Type of Service.
- All Superinterfaces:
BundleReference,java.lang.Comparable<java.lang.Object>
@ProviderType public interface ServiceReference<S> extends java.lang.Comparable<java.lang.Object>, BundleReference
A reference to a service.The Framework returns
ServiceReferenceobjects from theBundleContext.getServiceReferenceandBundleContext.getServiceReferencesmethods.A
ServiceReferenceobject 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
ServiceRegistrationobject and may have multiple, distinctServiceReferenceobjects referring to it.ServiceReferenceobjects associated with aServiceRegistrationobject have the samehashCodeand are considered equal (more specifically, theirequals()method will returntruewhen compared).If the same service object is registered multiple times,
ServiceReferenceobjects associated with differentServiceRegistrationobjects are not equal.
-
-
Method Summary
All Methods Instance Methods Abstract Methods Modifier and Type Method Description <A> Aadapt(java.lang.Class<A> type)Adapt thisServiceReferenceobject to the specified type.intcompareTo(java.lang.Object reference)Compares thisServiceReferencewith the specifiedServiceReferencefor order.BundlegetBundle()Returns the bundle that registered the service referenced by thisServiceReferenceobject.java.util.Dictionary<java.lang.String,java.lang.Object>getProperties()Returns a copy of the properties of the service referenced by thisServiceReferenceobject.java.lang.ObjectgetProperty(java.lang.String key)Returns the property value to which the specified property key is mapped in the propertiesDictionaryobject of the service referenced by thisServiceReferenceobject.java.lang.String[]getPropertyKeys()Returns an array of the keys in the propertiesDictionaryobject of the service referenced by thisServiceReferenceobject.Bundle[]getUsingBundles()Returns the bundles that are using the service referenced by thisServiceReferenceobject.booleanisAssignableTo(Bundle bundle, java.lang.String className)Tests if the bundle that registered the service referenced by thisServiceReferenceand 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 propertiesDictionaryobject of the service referenced by thisServiceReferenceobject.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,
ServiceReferenceobjects stored in the log) can still be interrogated.- Parameters:
key- The property key.- Returns:
- The property value to which the key is mapped;
nullif there is no property named after the key.
-
getPropertyKeys
java.lang.String[] getPropertyKeys()
Returns an array of the keys in the propertiesDictionaryobject of the service referenced by thisServiceReferenceobject.This method will continue to return the keys after the service has been unregistered. This is so references to unregistered services (for example,
ServiceReferenceobjects 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
Dictionarythat 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 thisServiceReferenceobject.This method must return
nullwhen the service has been unregistered. This can be used to determine if the service has been unregistered.- Specified by:
getBundlein interfaceBundleReference- Returns:
- The bundle that registered the service referenced by this
ServiceReferenceobject;nullif 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 thisServiceReferenceobject. 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
ServiceReferenceobject is greater than zero;nullif 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 thisServiceReferenceand the specified bundle use the same source for the package of the specified class name.This method performs the following checks:
- If the specified bundle is equal to the bundle that registered the
service referenced by this
ServiceReference(registrant bundle) returntrue. - Get the package name from the specified class name.
- 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). - For the registrant bundle; find the source for the package. If the
package source is found then return
trueif the package source equals the package source of the specified bundle; otherwise returnfalse. - 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
ServiceFactoryand the factory implementation is not from the registrant bundle returntrue; 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 returntrue; otherwise returnfalse.
- Parameters:
bundle- TheBundleobject to check.className- The class name to check.- Returns:
trueif the bundle which registered the service referenced by thisServiceReferenceand the specified bundle use the same source for the package of the specified class name. Otherwisefalseis returned.- Throws:
java.lang.IllegalArgumentException- If the specifiedBundlewas not created by the same framework instance as thisServiceReference.- Since:
- 1.3
- If the specified bundle is equal to the bundle that registered the
service referenced by this
-
compareTo
int compareTo(java.lang.Object reference)
Compares thisServiceReferencewith the specifiedServiceReferencefor order.If this
ServiceReferenceand the specifiedServiceReferencehave the sameservice idthey are equal. ThisServiceReferenceis less than the specifiedServiceReferenceif it has a lowerservice rankingand greater if it has a higher service ranking. Otherwise, if thisServiceReferenceand the specifiedServiceReferencehave the sameservice ranking, thisServiceReferenceis less than the specifiedServiceReferenceif it has a higherservice idand greater if it has a lower service id.- Specified by:
compareToin interfacejava.lang.Comparable<S>- Parameters:
reference- TheServiceReferenceto be compared.- Returns:
- Returns a negative integer, zero, or a positive integer if this
ServiceReferenceis less than, equal to, or greater than the specifiedServiceReference. - Throws:
java.lang.IllegalArgumentException- If the specifiedServiceReferencewas not created by the same framework instance as thisServiceReference.- Since:
- 1.4
-
getProperties
java.util.Dictionary<java.lang.String,java.lang.Object> getProperties()
Returns a copy of the properties of the service referenced by thisServiceReferenceobject.This method will continue to return the properties after the service has been unregistered. This is so references to unregistered services (for example,
ServiceReferenceobjects stored in the log) can still be interrogated.The returned
Dictionaryobject:- Must map property values by using property keys in a case-insensitive manner.
- Must return property keys is a case-preserving manner. This
means that the keys must have the same case as the corresponding key in
the properties
Dictionarythat was passed to theBundleContext.registerService(String[],Object,Dictionary)orServiceRegistration.setProperties(Dictionary)methods. - Is the property of the caller and can be modified by the caller but
any changes are not reflected in the properties of the service.
ServiceRegistration.setProperties(Dictionary)must be called to modify the properties of the service.
- Returns:
- A copy of the properties of the service referenced by this
ServiceReferenceobject - Since:
- 1.9
-
adapt
<A> A adapt(java.lang.Class<A> type)
Adapt thisServiceReferenceobject to the specified type.Adapting this
ServiceReferenceobject to the specified type may require certain checks, including security checks, to succeed. If a check does not succeed, then thisServiceReferenceobject cannot be adapted andnullis returned.- Type Parameters:
A- The type to which thisServiceReferenceobject is to be adapted.- Parameters:
type- Class object for the type to which thisServiceReferenceobject is to be adapted.- Returns:
- The object, of the specified type, to which this
ServiceReferenceobject has been adapted ornullif thisServiceReferenceobject cannot be adapted to the specified type. - Throws:
java.lang.SecurityException- If the caller does not have the appropriateAdaptPermission[type,this,ADAPT], and the Java Runtime Environment supports permissions.- Since:
- 1.10
-
-