|
OSGi™ Service Platform Release 2 |
|||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
A bundle's execution context within the Framework. The context is used to grant access to other methods so that this bundle can interact with the Framework.
BundleContext methods allow a bundle to:
Bundle
object for a bundle.
File
objects for files in a
persistent storage area provided for the bundle by the Framework.
A BundleContext object will be created and provided
to this bundle when it is started using the BundleActivator.start(org.osgi.framework.BundleContext)
method.
The same BundleContext object will be passed to this bundle when it is
stopped using the BundleActivator.stop(org.osgi.framework.BundleContext)
method.
BundleContext is generally for the private use of this bundle and
is not meant to be shared with other bundles in the OSGi environment. BundleContext is used
when resolving ServiceListeners and EventListeners.
The BundleContext object is only valid during an execution instance of this bundle; that is, during the period from when this bundle is called by BundleActivator.start until after this bundle is called and returns from BundleActivator.stop (or if BundleActivator.start terminates with an exception). If the BundleContext object is used subsequently, an IllegalStateException may be thrown. When this bundle is restarted, a new BundleContext object will be created.
The Framework is the only entity that can create BundleContext objects and they are only valid within the Framework that created them.
Note: A single virtual machine may host multiple Framework instances at any given time, but objects created by one Framework instance cannot be used by bundles running in the execution context of another Framework instance.
Method Summary | |
void |
addBundleListener(BundleListener listener)
Adds the specified BundleListener object to this context bundle's list of listeners if not already present. |
void |
addFrameworkListener(FrameworkListener listener)
Adds the specified FrameworkListener object to this context bundle's list of listeners if not already present. |
void |
addServiceListener(ServiceListener listener)
Adds the specified ServiceListener object to this context bundle's list of listeners. |
void |
addServiceListener(ServiceListener listener,
java.lang.String filter)
Adds the specified ServiceListener object with the specified filter to this context bundle's list of listeners. |
Filter |
createFilter(java.lang.String filter)
Creates a Filter object. |
Bundle |
getBundle()
Returns the Bundle object for this context bundle. |
Bundle |
getBundle(long id)
Returns the bundle with the specified identifier. |
Bundle[] |
getBundles()
Returns 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 by the Framework. |
java.lang.String |
getProperty(java.lang.String key)
Returns the value of the specified environment property. |
java.lang.Object |
getService(ServiceReference reference)
Returns the specified service object for a service. |
ServiceReference |
getServiceReference(java.lang.String clazz)
Returns a ServiceReference object for a service that implements, and was registered under, the specified class. |
ServiceReference[] |
getServiceReferences(java.lang.String clazz,
java.lang.String filter)
Returns a list of ServiceReference objects. |
Bundle |
installBundle(java.lang.String location)
Installs the bundle from the specified location string. |
Bundle |
installBundle(java.lang.String location,
java.io.InputStream in)
Installs the bundle from the specified InputStream object. |
ServiceRegistration |
registerService(java.lang.String[] clazzes,
java.lang.Object service,
java.util.Dictionary properties)
Registers the specified service object with the specified properties under the specified class names into the Framework. |
ServiceRegistration |
registerService(java.lang.String clazz,
java.lang.Object service,
java.util.Dictionary properties)
Registers the specified service object with the specified properties under the specified class name into the Framework. |
void |
removeBundleListener(BundleListener listener)
Removes the specified BundleListener object from this context bundle's list of listeners. |
void |
removeFrameworkListener(FrameworkListener listener)
Removes the specified FrameworkListener object from this context bundle's list of listeners. |
void |
removeServiceListener(ServiceListener listener)
Removes the specified ServiceListener object from this context bundle's list of listeners. |
boolean |
ungetService(ServiceReference reference)
Releases the service object referenced by the specified ServiceReference object. |
Method Detail |
public java.lang.String getProperty(java.lang.String key)
The following standard property keys are valid:
Constants.FRAMEWORK_VERSION
Constants.FRAMEWORK_VENDOR
Constants.FRAMEWORK_LANGUAGE
Constants.FRAMEWORK_OS_NAME
Constants.FRAMEWORK_OS_VERSION
Constants.FRAMEWORK_PROCESSOR
Note: The last four standard properties are used by the
Constants.BUNDLE_NATIVECODE
Manifest header's matching
algorithm for selecting native language code.
key
- The name of the requested property.
null
if the property is undefined.public Bundle getBundle()
The context bundle is defined as the bundle that was assigned this BundleContext in its BundleActivator.
java.lang.IllegalStateException
- If this context bundle has stopped.public Bundle installBundle(java.lang.String location) throws BundleException
Every installed bundle is uniquely identified by its location string, typically in the form of a URL.
The following steps are required to install a bundle:
BundleException
is thrown.
BundleEvent.INSTALLED
is broadcast.
location
- The location identifier of the bundle to install.
BundleException
- If the installation failed.
java.lang.SecurityException
- If the caller does not have
the appropriate AdminPermission, and the Java Runtime Environment supports permissions.public Bundle installBundle(java.lang.String location, java.io.InputStream in) throws BundleException
This method performs all of the steps listed in BundleContext.installBundle(String location), except that the bundle's content will be read from the InputStream object. The location identifier string specified will be used as the identity of the bundle.
This method will always close the InputStream object, even if an exception is thrown.
location
- The location identifier of the bundle to install.in
- The InputStream object from which this bundle will be read.
BundleException
- If the provided stream cannot be read.installBundle(java.lang.String)
public Bundle getBundle(long id)
id
- The identifier of the bundle to retrieve.
public Bundle[] getBundles()
This method returns a list of all bundles installed in the OSGi environment at the time of the call to this method. However, as the Framework is a very dynamic environment, bundles can be installed or uninstalled at anytime.
public void addServiceListener(ServiceListener listener, java.lang.String filter) throws InvalidSyntaxException
See
getBundle()
for a definition of context bundle, and
Filter
for a description of the filter syntax.
ServiceListener objects are notified when a service has a lifecycle state
change.
If this context bundle's list of listeners already contains a listener l such that (l==listener), this method replaces that listener's filter (which may be null) with the specified one (which may be null).
The listener is called if the filter criteria is met.
To filter based upon the class of the service, the filter
should reference the Constants.OBJECTCLASS
property.
If filter is null, all services
are considered to match the filter.
If the Java Runtime Environment supports permissions, the ServiceListener object will be notified of a service event only if the bundle that is registering it has the ServicePermission to get the service using at least one of the named classes the service was registered under.
listener
- The ServiceListener object to be added.filter
- The filter criteria.
InvalidSyntaxException
- If filter contains
an invalid filter string which cannot be parsed.
java.lang.IllegalStateException
- If this context bundle has stopped.ServiceEvent
,
ServiceListener
,
ServicePermission
public void addServiceListener(ServiceListener listener)
This method is the same as calling BundleContext.addServiceListener(ServiceListener listener, String filter) with filter set to null.
listener
- The ServiceListener object to be added.
java.lang.IllegalStateException
- If this context bundle has stopped.addServiceListener(ServiceListener, String)
public void removeServiceListener(ServiceListener listener)
getBundle()
for a definition of context bundle.
If listener is not contained in this context bundle's list of listeners, this method does nothing.
listener
- The ServiceListener to be removed.
java.lang.IllegalStateException
- If this context bundle has stopped.public void addBundleListener(BundleListener listener)
getBundle()
for a definition of context bundle.
BundleListener objects are notified when a bundle has a lifecycle state change.
If this context bundle's list of listeners already contains a listener l such that (l==listener), this method does nothing.
listener
- The BundleListener to be added.
java.lang.IllegalStateException
- If this context bundle has stopped.BundleEvent
,
BundleListener
public void removeBundleListener(BundleListener listener)
getBundle()
for a definition of context bundle.
If listener is not contained in this context bundle's list of listeners, this method does nothing.
listener
- The BundleListener object to be removed.
java.lang.IllegalStateException
- If this context bundle has stopped.public void addFrameworkListener(FrameworkListener listener)
getBundle()
for a definition of context bundle.
FrameworkListeners are notified of general Framework events.
If this context bundle's list of listeners already contains a listener l such that (l==listener), this method does nothing.
listener
- The FrameworkListener object to be added.
java.lang.IllegalStateException
- If this context bundle has stopped.FrameworkEvent
,
FrameworkListener
public void removeFrameworkListener(FrameworkListener listener)
getBundle()
for a definition of context bundle.
If listener is not contained in this context bundle's list of listeners, this method does nothing.
listener
- The FrameworkListener object to be removed.
java.lang.IllegalStateException
- If this context bundle has stopped.public ServiceRegistration registerService(java.lang.String[] clazzes, java.lang.Object service, java.util.Dictionary properties)
getBundle()
for a definition of context bundle.
Other bundles can locate the service by using either the
getServiceReferences(java.lang.String, java.lang.String)
or getServiceReference(java.lang.String)
method.
A bundle can register a service object that implements the
ServiceFactory
interface to have more flexibility in providing service objects to other
bundles.
The following steps are required to register a service:
instanceof
all the classes named.
Constants.SERVICE_ID
identifying the
registration number of the service, and a property named
Constants.OBJECTCLASS
containing all the specified
classes. If any of these properties have already been specified by the
registering bundle, their values will be overwritten by the Framework.
ServiceEvent.REGISTERED
is synchronously sent.
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 Strings. 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.
properties may be null
if the service has no properties.
java.lang.IllegalArgumentException
- If one of the following is true:
java.lang.SecurityException
- If the caller does not have the
ServicePermission to register the service for all the named classes and
the Java Runtime Environment supports permissions.
java.lang.IllegalStateException
- If this context bundle has stopped.ServiceRegistration
,
ServiceFactory
public ServiceRegistration registerService(java.lang.String clazz, java.lang.Object service, java.util.Dictionary properties)
This method is otherwise identical to
registerService(java.lang.String[], java.lang.Object,
java.util.Dictionary)
and is provided as a convenience when service will only
be registered under a single class name. Note that even in this case the value of the service's
Constants.OBJECTCLASS
property will be an array of strings, rather than just a single string.
registerService(java.lang.String[], java.lang.Object,
java.util.Dictionary)
public ServiceReference[] getServiceReferences(java.lang.String clazz, java.lang.String filter) throws InvalidSyntaxException
The list is valid at the time of the call to this method, however as the Framework is a very dynamic environment, services can be modified or unregistered at anytime.
filter is used to select the registered service whose
properties objects contain keys and values which satisfy the filter.
See Filter
for a description of the filter string syntax.
If filter is null, all registered services are considered to match the filter.
If filter cannot be parsed, an InvalidSyntaxException
will
be thrown with a human readable message where the filter became unparsable.
The following steps are required to select a service:
clazz
is not null, the set is further reduced to
those services which are an instanceof and were registered under the specified class.
The complete list of classes of which a service is an instance and which
were specified when the service was registered is available from the
service's Constants.OBJECTCLASS
property.
clazz
- The class name with which the service was registered, or
null for all services.filter
- The filter criteria.
InvalidSyntaxException
- If filter contains
an invalid filter string which cannot be parsed.public ServiceReference getServiceReference(java.lang.String clazz)
This ServiceReference object is valid at the time of the call to this method, however as the Framework is a very dynamic environment, services can be modified or unregistered at anytime.
This method is the same as calling getServiceReferences(java.lang.String, java.lang.String)
with a
null
filter string.
It is provided as a convenience for when the caller is interested in any service that
implements the specified class.
If multiple such services exist, the service
with the highest ranking (as specified in its Constants.SERVICE_RANKING
property) is
returned.
If there is a tie in ranking, the service with the lowest
service ID (as specified in its Constants.SERVICE_ID
property); that is,
the service that was registered first is returned.
clazz
- The class name with which the service was registered.
getServiceReferences(java.lang.String, java.lang.String)
public java.lang.Object getService(ServiceReference reference)
A bundle's use of a service is tracked by the bundle's use
count of that service. Each time a service's service object is returned by
getService(org.osgi.framework.ServiceReference)
the context bundle's use count for that service
is incremented by one. Each time the service is released by
ungetService(org.osgi.framework.ServiceReference)
the context bundle's use count for that service is decremented by one.
When a bundle's use count for a service drops to zero, the bundle should no longer use that service.
See getBundle()
for a definition of context bundle.
This method will always return null when the service associated with this reference has been unregistered.
The following steps are required to get the service object:
ServiceFactory.getService(org.osgi.framework.Bundle, org.osgi.framework.ServiceRegistration)
method is called to create a service object
for the context bundle.
This service object is cached by the Framework.
While the context bundle's use count for the service is greater than
zero, subsequent calls to get the services's service object for the
context bundle will return the cached service object.
instanceof
all the classes named when the service was registered or
the ServiceFactory object throws an exception, null is returned and a
Framework event of type FrameworkEvent.ERROR
is broadcast.
reference
- A reference to target service object's service.
java.lang.SecurityException
- If the caller does not have
the ServicePermission to get the service using at least one of the named classes
the service was registered under, and the Java Runtime Environment supports permissions.
java.lang.IllegalStateException
- If the context bundle has stopped.ungetService(org.osgi.framework.ServiceReference)
,
ServiceFactory
public boolean ungetService(ServiceReference reference)
getBundle()
for a definition of context bundle.
The service's service object should no longer be used and all references to it should be destroyed when a bundle's use count for the service drops to zero.
The following steps are required to unget the service object:
ServiceFactory.ungetService(org.osgi.framework.Bundle, org.osgi.framework.ServiceRegistration, java.lang.Object)
method is called to release the service object
for the context bundle.
reference
- A reference to the service to be released.
java.lang.IllegalStateException
- If the context bundle has stopped.getService(org.osgi.framework.ServiceReference)
,
ServiceFactory
public java.io.File getDataFile(java.lang.String filename)
A File object for the base directory of the
persistent storage area provided for the context bundle by the Framework
can be obtained by calling this method with an empty string (" ")
as filename.
See getBundle()
for a definition of context bundle.
If the Java Runtime Environment supports permissions, the Framework will ensure that the bundle has the java.io.FilePermission with actions read, write, execute, delete for all files (recursively) in the persistent storage area provided for the context bundle.
filename
- A relative name to the file to be accessed.
java.lang.IllegalStateException
- If the context bundle has stopped.public Filter createFilter(java.lang.String filter) throws InvalidSyntaxException
Filter
for a description of the filter string syntax.
If the filter cannot be parsed, an InvalidSyntaxException
will be thrown
with a human readable message where the filter became unparsable.
filter
- The filter string.
InvalidSyntaxException
- If filter contains
an invalid filter string that cannot be parsed.
|
OSGi™ Service Platform Release 2 |
|||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |