org.eclipse.osgi.internal.serviceregistry
Class ServiceReferenceImpl<S>

java.lang.Object
  extended by org.eclipse.osgi.internal.serviceregistry.ServiceReferenceImpl<S>
All Implemented Interfaces:
java.lang.Comparable<java.lang.Object>, ServiceReference<S>

public class ServiceReferenceImpl<S>
extends java.lang.Object
implements ServiceReference<S>

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.

See Also:
BundleContext.getServiceReference(java.lang.String), BundleContext.getServiceReferences(java.lang.String, java.lang.String), BundleContext.getService(org.osgi.framework.ServiceReference)

Method Summary
 int compareTo(java.lang.Object object)
          Compares this ServiceReference with the specified ServiceReference for order.
 boolean equals(java.lang.Object obj)
          Indicates whether some other object is "equal to" this one.
 Bundle getBundle()
          Returns the bundle that registered the service referenced by this ServiceReference object.
 java.lang.Object getProperty(java.lang.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.
 java.lang.String[] getPropertyKeys()
          Returns an array of the keys in the properties Dictionary object of the service referenced by this ServiceReference object.
 ServiceRegistrationImpl<S> getRegistration()
          Return the ServiceRegistrationImpl for this ServiceReferenceImpl.
 Bundle[] getUsingBundles()
          Returns the bundles that are using the service referenced by this ServiceReference object.
 int hashCode()
          Returns a hash code value for the object.
 boolean isAssignableTo(Bundle bundle, java.lang.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.
 java.lang.String toString()
          Return a string representation of this reference.
 
Methods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, wait, wait, wait
 

Method Detail

getProperty

public java.lang.Object getProperty(java.lang.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.

Specified by:
getProperty in interface ServiceReference<S>
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

public java.lang.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,java.util.Dictionary) or ServiceRegistration.setProperties(java.util.Dictionary) methods.

Specified by:
getPropertyKeys in interface ServiceReference<S>
Returns:
An array of property keys.

getBundle

public Bundle 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.

Specified by:
getBundle in interface ServiceReference<S>
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,java.util.Dictionary)

getUsingBundles

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.

Specified by:
getUsingBundles in interface ServiceReference<S>
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

public boolean isAssignableTo(Bundle bundle,
                              java.lang.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. Get the package name from the specified class name.
  2. 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 return true if the registrant bundle is equal to the specified bundle; otherwise return false.
  3. If the package source of the registrant bundle is equal to the package source of the specified bundle then return true; otherwise return false.

Specified by:
isAssignableTo in interface ServiceReference<S>
Parameters:
bundle - The Bundle object to check.
className - The class name to check.
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.
Since:
1.3

compareTo

public int compareTo(java.lang.Object object)
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.

Specified by:
compareTo in interface java.lang.Comparable<java.lang.Object>
Specified by:
compareTo in interface ServiceReference<S>
Parameters:
object - The ServiceReference 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 specified ServiceReference.
Since:
1.4

hashCode

public int hashCode()
Returns a hash code value for the object.

Overrides:
hashCode in class java.lang.Object
Returns:
a hash code value for this object.

equals

public boolean equals(java.lang.Object obj)
Indicates whether some other object is "equal to" this one.

Overrides:
equals in class java.lang.Object
Parameters:
obj - the reference object with which to compare.
Returns:
true if this object is the same as the obj argument; false otherwise.

toString

public java.lang.String toString()
Return a string representation of this reference.

Overrides:
toString in class java.lang.Object
Returns:
String

getRegistration

public ServiceRegistrationImpl<S> getRegistration()
Return the ServiceRegistrationImpl for this ServiceReferenceImpl.

Returns:
The ServiceRegistrationImpl for this ServiceReferenceImpl.


Copyright © 2007-2012 FuseSource, Corp.. All Rights Reserved.