org.knopflerfish.framework
Class BundleContextImpl

java.lang.Object
  extended by org.knopflerfish.framework.BundleContextImpl
All Implemented Interfaces:
BundleContext

public class BundleContextImpl
extends java.lang.Object
implements BundleContext

Implementation of the BundleContext object.

See Also:
BundleContext

Constructor Summary
BundleContextImpl(BundleImpl bundle)
          Create a BundleContext for specified bundle.
 
Method Summary
 void addBundleListener(BundleListener listener)
          Add a bundle listener.
 void addFrameworkListener(FrameworkListener listener)
          Add a framework listener.
 void addServiceListener(ServiceListener listener)
          Add a service listener.
 void addServiceListener(ServiceListener listener, java.lang.String filter)
          Add a service listener with a filter.
 Filter createFilter(java.lang.String filter)
          Constructs a Filter object.
 ServiceReference[] getAllServiceReferences(java.lang.String clazz, java.lang.String filter)
          Get a list of service references.
 Bundle getBundle()
          Retrieve the Bundle object for the calling bundle.
 Bundle getBundle(long id)
          Retrieve the bundle that has the given unique identifier.
 Bundle[] getBundles()
          Retrieve a list of all installed bundles.
 java.io.File getDataFile(java.lang.String filename)
          Creates a File object for a file in the persistent storage area provided for the bundle.
 java.lang.String getProperty(java.lang.String key)
          Retrieve the value of the named environment property.
 java.lang.Object getService(ServiceReference reference)
          Get the service object.
 ServiceReference getServiceReference(java.lang.String clazz)
          Get a service reference.
 ServiceReference[] getServiceReferences(java.lang.String clazz, java.lang.String filter)
          Get a list of service references.
 Bundle installBundle(java.lang.String location)
          Install a bundle from location.
 Bundle installBundle(java.lang.String location, java.io.InputStream in)
          Install a bundle from an InputStream.
 ServiceRegistration registerService(java.lang.String[] clazzes, java.lang.Object service, java.util.Dictionary properties)
          Register a service with multiple names.
 ServiceRegistration registerService(java.lang.String clazz, java.lang.Object service, java.util.Dictionary properties)
          Register a service with a single name.
 void removeBundleListener(BundleListener listener)
          Remove a bundle listener.
 void removeFrameworkListener(FrameworkListener listener)
          Remove a framework listener.
 void removeServiceListener(ServiceListener listener)
          Remove a service listener.
 boolean ungetService(ServiceReference reference)
          Unget the service object.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

BundleContextImpl

public BundleContextImpl(BundleImpl bundle)
Create a BundleContext for specified bundle.

Method Detail

getProperty

public java.lang.String getProperty(java.lang.String key)
Retrieve the value of the named environment property.

Specified by:
getProperty in interface BundleContext
Parameters:
key - The name of the requested property.
Returns:
The value of the requested property, or null if the property is undefined.
See Also:
BundleContext.getProperty(java.lang.String)

installBundle

public Bundle installBundle(java.lang.String location)
                     throws BundleException
Install a bundle from location.

Specified by:
installBundle in interface BundleContext
Parameters:
location - The location identifier of the bundle to install.
Returns:
The Bundle object of the installed bundle.
Throws:
BundleException - If the installation failed.
See Also:
BundleContext.installBundle(java.lang.String)

installBundle

public Bundle installBundle(java.lang.String location,
                            java.io.InputStream in)
                     throws BundleException
Install a bundle from an InputStream.

Specified by:
installBundle in interface BundleContext
Parameters:
location - The location identifier of the bundle to install.
in - The InputStream object from which this bundle will be read.
Returns:
The Bundle object of the installed bundle.
Throws:
BundleException - If the provided stream cannot be read or the installation failed.
See Also:
BundleContext.installBundle(java.lang.String)

getBundle

public Bundle getBundle()
Retrieve the Bundle object for the calling bundle.

Specified by:
getBundle in interface BundleContext
Returns:
The Bundle object associated with this BundleContext.
See Also:
BundleContext.getBundle()

getBundle

public Bundle getBundle(long id)
Retrieve the bundle that has the given unique identifier.

Specified by:
getBundle in interface BundleContext
Parameters:
id - The identifier of the bundle to retrieve.
Returns:
A Bundle object or null if the identifier does not match any installed bundle.
See Also:
BundleContext.getBundle()

getBundles

public Bundle[] getBundles()
Retrieve a list of all installed bundles.

Specified by:
getBundles in interface BundleContext
Returns:
An array of Bundle objects, one object per installed bundle.
See Also:
BundleContext.getBundles()

addServiceListener

public void addServiceListener(ServiceListener listener,
                               java.lang.String filter)
                        throws InvalidSyntaxException
Add a service listener with a filter.

Specified by:
addServiceListener in interface BundleContext
Parameters:
listener - The ServiceListener object to be added.
filter - The filter criteria.
Throws:
InvalidSyntaxException - If filter contains an invalid filter string that cannot be parsed.
See Also:
BundleContext.addServiceListener(org.osgi.framework.ServiceListener, java.lang.String)

addServiceListener

public void addServiceListener(ServiceListener listener)
Add a service listener.

Specified by:
addServiceListener in interface BundleContext
Parameters:
listener - The ServiceListener object to be added.
See Also:
BundleContext.addServiceListener(org.osgi.framework.ServiceListener, java.lang.String)

removeServiceListener

public void removeServiceListener(ServiceListener listener)
Remove a service listener.

Specified by:
removeServiceListener in interface BundleContext
Parameters:
listener - The ServiceListener to be removed.
See Also:
BundleContext.removeServiceListener(org.osgi.framework.ServiceListener)

addBundleListener

public void addBundleListener(BundleListener listener)
Add a bundle listener.

Specified by:
addBundleListener in interface BundleContext
Parameters:
listener - The BundleListener to be added.
See Also:
BundleContext.addBundleListener(org.osgi.framework.BundleListener)

removeBundleListener

public void removeBundleListener(BundleListener listener)
Remove a bundle listener.

Specified by:
removeBundleListener in interface BundleContext
Parameters:
listener - The BundleListener object to be removed.
See Also:
BundleContext.removeBundleListener(org.osgi.framework.BundleListener)

addFrameworkListener

public void addFrameworkListener(FrameworkListener listener)
Add a framework listener.

Specified by:
addFrameworkListener in interface BundleContext
Parameters:
listener - The FrameworkListener object to be added.
See Also:
BundleContext.addFrameworkListener(org.osgi.framework.FrameworkListener)

removeFrameworkListener

public void removeFrameworkListener(FrameworkListener listener)
Remove a framework listener.

Specified by:
removeFrameworkListener in interface BundleContext
Parameters:
listener - The FrameworkListener object to be removed.
See Also:
BundleContext.removeFrameworkListener(org.osgi.framework.FrameworkListener)

registerService

public ServiceRegistration registerService(java.lang.String[] clazzes,
                                           java.lang.Object service,
                                           java.util.Dictionary properties)
Register a service with multiple names.

Specified by:
registerService in interface BundleContext
Parameters:
clazzes - The class names under which the service can be located. The class names in this array will be stored in the service's properties under the key Constants.OBJECTCLASS.
service - The service object or a ServiceFactory object.
properties - The properties for this service. The keys in the properties object must all be String objects. See Constants for a list of standard service property keys. Changes should not be made to this object after calling this method. To update the service's properties the ServiceRegistration.setProperties(java.util.Dictionary) method must be called. The set of properties may be null if the service has no properties.
Returns:
A ServiceRegistration object for use by the bundle registering the service to update the service's properties or to unregister the service.
See Also:
BundleContext.registerService(java.lang.String[], java.lang.Object, java.util.Dictionary)

registerService

public ServiceRegistration registerService(java.lang.String clazz,
                                           java.lang.Object service,
                                           java.util.Dictionary properties)
Register a service with a single name.

Specified by:
registerService in interface BundleContext
Parameters:
clazz - The class name under which the service can be located.
service - The service object or a ServiceFactory object.
properties - The properties for this service.
Returns:
A ServiceRegistration object for use by the bundle registering the service to update the service's properties or to unregister the service.
See Also:
BundleContext.registerService(java.lang.String[], java.lang.Object, java.util.Dictionary)

getServiceReferences

public ServiceReference[] getServiceReferences(java.lang.String clazz,
                                               java.lang.String filter)
                                        throws InvalidSyntaxException
Get a list of service references.

Specified by:
getServiceReferences in interface BundleContext
Parameters:
clazz - The class name with which the service was registered or null for all services.
filter - The filter criteria.
Returns:
An array of ServiceReference objects or null if no services are registered which satisfy the search.
Throws:
InvalidSyntaxException - If filter contains an invalid filter string that cannot be parsed.
See Also:
BundleContext.getServiceReferences(java.lang.String, java.lang.String)

getAllServiceReferences

public ServiceReference[] getAllServiceReferences(java.lang.String clazz,
                                                  java.lang.String filter)
                                           throws InvalidSyntaxException
Get a list of service references.

Specified by:
getAllServiceReferences in interface BundleContext
Parameters:
clazz - The class name with which the service was registered or null for all services.
filter - The filter criteria.
Returns:
An array of ServiceReference objects or null if no services are registered which satisfy the search.
Throws:
InvalidSyntaxException - If filter contains an invalid filter string that cannot be parsed.
See Also:
BundleContext.getAllServiceReferences(java.lang.String, java.lang.String)

getServiceReference

public ServiceReference getServiceReference(java.lang.String clazz)
Get a service reference.

Specified by:
getServiceReference in interface BundleContext
Parameters:
clazz - The class name with which the service was registered.
Returns:
A ServiceReference object, or null if no services are registered which implement the named class.
See Also:
BundleContext.getServiceReference(java.lang.String)

getService

public java.lang.Object getService(ServiceReference reference)
Get the service object.

Specified by:
getService in interface BundleContext
Parameters:
reference - A reference to the service.
Returns:
A service object for the service associated with reference or null if the service is not registered or does not implement the classes under which it was registered in the case of a ServiceFactory.
See Also:
BundleContext.getService(org.osgi.framework.ServiceReference)

ungetService

public boolean ungetService(ServiceReference reference)
Unget the service object.

Specified by:
ungetService in interface BundleContext
Parameters:
reference - A reference to the service to be released.
Returns:
false if the context bundle's use count for the service is zero or if the service has been unregistered; true otherwise.
See Also:
BundleContext.ungetService(org.osgi.framework.ServiceReference)

getDataFile

public java.io.File getDataFile(java.lang.String filename)
Creates a File object for a file in the persistent storage area provided for the bundle.

Specified by:
getDataFile in interface BundleContext
Parameters:
filename - A relative name to the file to be accessed.
Returns:
A File object that represents the requested file or null if the platform does not have file system support.
See Also:
BundleContext.getDataFile(java.lang.String)

createFilter

public Filter createFilter(java.lang.String filter)
                    throws InvalidSyntaxException
Constructs a Filter object. This filter object may be used to match a ServiceReference or a Dictionary.

Specified by:
createFilter in interface BundleContext
Parameters:
filter - the filter string.
Returns:
the Filter object encapsulating the filter string.
Throws:
InvalidSyntaxException - If the filter parameter contains an invalid filter string which cannot be parsed.
Since:
1.1
See Also:
"Framework specification for a description of the filter string syntax.", FrameworkUtil.createFilter(String)