org.eclipse.osgi.framework.internal.core
Class ServiceReferenceImpl

java.lang.Object
  extended by org.eclipse.osgi.framework.internal.core.ServiceReferenceImpl
All Implemented Interfaces:
java.lang.Comparable, ServiceReference

public class ServiceReferenceImpl
extends java.lang.Object
implements ServiceReference, java.lang.Comparable

A reference to a service. The framework returns ServiceReference objects from the BundleContext.getServiceReference and BundleContext.getServiceReferences methods.

A ServiceReference may be shared between bundles and can be used to examine the properties of the service and to get the service object (See BundleContext.getService).

A registered service may have multiple, distinct ServiceReference objects which refer to it. However these ServiceReference objects will have the same hashCode and the equals method will return true when compared.


Field Summary
protected  ServiceRegistrationImpl registration
          Registered Service object.
 
Constructor Summary
protected ServiceReferenceImpl(ServiceRegistrationImpl registration)
          Construct a reference.
 
Method Summary
 int compareTo(java.lang.Object object)
          Compares two service objects and returns an indication as to which is higher ranked based on service ranking and service_ID.
 boolean equals(java.lang.Object obj)
          Indicates whether some other object is "equal to" this one.
 Bundle getBundle()
          Return the bundle which registered the service.
protected  java.lang.String[] getClasses()
          Return the classes under which the referenced service was registered.
protected  long getId()
          Return the serviceid of the ServiceRegistration.
 java.lang.Object getProperty(java.lang.String key)
          Get the value of a service's property.
 java.lang.String[] getPropertyKeys()
          Get the list of key names for the service's properties.
protected  int getRanking()
          Return the serviceranking of the ServiceRegistration.
 Bundle[] getUsingBundles()
          Return the list of bundles which are using the service.
 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
 

Field Detail

registration

protected ServiceRegistrationImpl registration
Registered Service object.

Constructor Detail

ServiceReferenceImpl

protected ServiceReferenceImpl(ServiceRegistrationImpl registration)
Construct a reference.

Method Detail

getProperty

public java.lang.Object getProperty(java.lang.String key)
Get the value of a service's property.

This method will continue to return property values after the service has been unregistered. This is so that references to unregistered service can be interrogated. (For example: ServiceReference objects stored in the log.)

Specified by:
getProperty in interface ServiceReference
Parameters:
key - Name of the property.
Returns:
Value of the property or null if there is no property by that name.

getPropertyKeys

public java.lang.String[] getPropertyKeys()
Get the list of key names for the service's properties.

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

Specified by:
getPropertyKeys in interface ServiceReference
Returns:
The list of property key names.

getBundle

public Bundle getBundle()
Return the bundle which registered the service.

This method will always 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
Returns:
The bundle which registered the service.
See Also:
BundleContext.registerService(String[],Object,java.util.Dictionary)

getUsingBundles

public Bundle[] getUsingBundles()
Return the list of bundles which are using the service.

This method will always return null when the service has been unregistered.

Specified by:
getUsingBundles in interface ServiceReference
Returns:
The array of bundles using the service or null if no bundles are using the service.
See Also:
BundleContextImpl.getService(org.osgi.framework.ServiceReference)

getClasses

protected java.lang.String[] getClasses()
Return the classes under which the referenced service was registered.

Returns:
array of class names.

getId

protected long getId()
Return the serviceid of the ServiceRegistration.

Returns:
service.id of the service

getRanking

protected int getRanking()
Return the serviceranking of the ServiceRegistration.

Returns:
service.ranking of the service

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

compareTo

public int compareTo(java.lang.Object object)
Compares two service objects and returns an indication as to which is higher ranked based on service ranking and service_ID. The service with the higher service ranking (as specified in its service.ranking property) is defined to be higher ranked. If there is a tie in ranking, the service with the lowest service ID (as specified in its service.id property); that is, the service that was registered first is returned. This is the same algorithm used by BundleContext.getServiceReference.

Specified by:
compareTo in interface java.lang.Comparable
Specified by:
compareTo in interface ServiceReference
Parameters:
object - an object to compare the receiver to
Returns:
int int = 0 if this object's ranking and service id are equivalent int < 0 if this object's ranking is lower than the argument's ranking or if the rankings are equal, this object's service id is greater than the argument's service id. int > 0 if this object's ranking is higher than than the argument's ranking or if the rankings are equal, this object's service id is less than the argument's service id.
Throws:
java.lang.ClassCastException - if the argument can not be converted into something comparable with the receiver.

isAssignableTo

public boolean isAssignableTo(Bundle bundle,
                              java.lang.String className)
Description copied from interface: ServiceReference
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
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.