BundleContext methods allow a bundle to:
ServiceReferences from the Framework service registry.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 for a bundle when the bundle
is started. The Bundle object associated with a BundleContext
object is called the context bundle.
The BundleContext object will be passed to the
BundleActivator#start(BundleContext) method during activation of the
context bundle. The same BundleContext object will be passed to the
BundleActivator#stop(BundleContext) method when the context bundle is
stopped. A BundleContext object is generally for the private use of
its associated bundle and is not meant to be shared with other bundles in the
OSGi environment.
The BundleContext object is only valid during the execution of its
context bundle; that is, during the period from when the context bundle is in
the STARTING, STOPPING, and ACTIVE bundle states.
However, the BundleContext object becomes invalid after
BundleActivator#stop(BundleContext) returns (if the bundle has a
Bundle Activator). The BundleContext object becomes invalid before
disposing of any remaining registered services and releasing any remaining
services in use. Since those activities can result in other bundles being
called (for example, ServiceListeners for
ServiceEvent#UNREGISTERING events and ServiceFactorys for
unget operations), those other bundles can observe the stopping bundle in the
STOPPING state but with an invalid BundleContext object. If
the BundleContext object is used after it has become invalid, an
IllegalStateException must be thrown. The BundleContext
object must never be reused after its context bundle is stopped.
Two BundleContext objects are equal if they both refer to the same
execution context of a bundle. The Framework is the only entity that can
create BundleContext objects and they are only valid within the
Framework that created them.
A Bundle can be adapted to its
BundleContext. In order for this to succeed, the caller must have the
appropriate AdminPermission[bundle,CONTEXT] if the Java Runtime
Environment supports permissions.
| Modifier and Type | Method and Description |
|---|---|
| public void | addBundleListener(BundleListener
The listener)BundleListener to be added.Adds the specified |
| public void | addFrameworkListener(FrameworkListener
The listener)FrameworkListener object to be added.Adds the specified |
| public void | addServiceListener(ServiceListener
The listener, String ServiceListener object to be added.The filter criteria. filter)Adds the specified |
| public void | addServiceListener(ServiceListener
The listener)ServiceListener object to be added.Adds the specified |
| public Filter | Returns: AFilter object encapsulating the filter string.The filter string. filter)Creates a |
| public ServiceReference | Returns: An array ofServiceReference objects or null if
no services are registered which satisfy the search.The class name with which the service was registered or
clazz, String null for all services.The filter expression or filter)null for all services.Returns an array of |
| public Bundle | Returns: TheBundle object associated with this
BundleContext.Redeclares org. Returns the |
| public Bundle | Returns: ABundle object or null if the identifier does
not match any installed bundle.The identifier of the bundle to retrieve. id)Returns the bundle with the specified identifier. |
| public Bundle | |
| public Bundle[] | Returns: An array ofBundle objects, one object per installed
bundle.Returns a list of all installed bundles. |
| public File | Returns: AFile object that represents the requested file or
null if the platform does not have file system support.A relative name to the file to be accessed. filename)Creates a |
| public String | Returns: The value of the requested property, ornull if the
property is undefined.The name of the requested property. key)Returns the value of the specified property. |
| public < Type of Service. S> S | Returns: A service object for the service associated withreference or null if the service is not
registered, the service object returned by a
ServiceFactory does not implement the classes under which
it was registered or the ServiceFactory threw an
exception.A reference to the service. reference)Returns the service object for the service referenced by the specified
|
| public < Type of Service. S> ServiceObjects | Returns: AServiceObjects object for the service associated with
the specified reference or null if the service is
not registered.A reference to the service. reference)Returns the |
| public ServiceReference | Returns: AServiceReference object, or null if no services
are registered which implement the named class.The class name with which the service was registered. clazz)Returns a |
| public < Type of Service. S> ServiceReference | Returns: AServiceReference object, or null if no services
are registered which implement the specified class.The class under whose name the service was registered. Must
not be clazz)null.Returns a |
| public ServiceReference | Returns: An array ofServiceReference objects or null if
no services are registered which satisfy the search.The class name with which the service was registered or
clazz, String null for all services.The filter expression or filter)null for all services.Returns an array of |
| public < Type of Service S> Collection | Returns: A collection ofServiceReference objects. May be empty if
no services are registered which satisfy the search.The class under whose name the service was registered. Must
not be clazz, String null.The filter expression or filter)null for all services.Returns a collection of |
| public Bundle | Returns: TheBundle object of the installed bundle.The location identifier of the bundle to install. location, InputStream The input)InputStream object from which this bundle will
be read or null to indicate the Framework must create the
input stream from the specified location identifier. The input
stream must always be closed when this method completes, even if
an exception is thrown.Installs a bundle from the specified |
| public Bundle | Returns: TheBundle object of the installed bundle.The location identifier of the bundle to install. location)Installs a bundle from the specified |
| public ServiceRegistration | Returns: AServiceRegistration object for use by the bundle
registering the service to update the service's properties or to
unregister the service.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 clazzes, Object Constants#OBJECTCLASS.The service object or an object implementing
service, Dictionary<String, ?> ServiceFactory.The properties for this service. The keys in the
properties object must all be properties)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(Dictionary) method must
be called. The set of properties may be null if the
service has no properties.Registers the specified service object with the specified properties under the specified class names into the Framework. |
| public ServiceRegistration | Returns: AServiceRegistration object for use by the bundle
registering the service to update the service's properties or to
unregister the service.The class name under which the service can be located. clazz, Object The service object or an object implementing
service, Dictionary<String, ?> ServiceFactory.The properties for this service. properties)Registers the specified service object with the specified properties under the specified class name with the Framework. |
| public < Type of Service. S> ServiceRegistration | Returns: AServiceRegistration object for use by the bundle
registering the service to update the service's properties or to
unregister the service.The class under whose name the service can be located. clazz, S The service object or an object implementing
service, Dictionary<String, ?> ServiceFactory.The properties for this service. properties)Registers the specified service object with the specified properties under the name of the specified class with the Framework. |
| public < Type of Service. S> ServiceRegistration | Returns: AServiceRegistration object for use by the bundle
registering the service to update the service's properties or to
unregister the service.The class under whose name the service can be located. clazz, ServiceFactory<S> The factory, Dictionary<String, ?> ServiceFactory object.The properties for this service. properties)Registers the specified service factory object with the specified properties under the name of the specified class with the Framework. |
| public void | removeBundleListener(BundleListener
The listener)BundleListener object to be removed.Removes the specified |
| public void | removeFrameworkListener(FrameworkListener
The listener)FrameworkListener object to be removed.Removes the specified |
| public void | removeServiceListener(ServiceListener
The listener)ServiceListener to be removed.Removes the specified |
| public boolean | Returns: false if the context bundle's use count for the service
is zero or if the service has been unregistered; true
otherwise.A reference to the service to be released. reference)Releases the service object for the service referenced by the specified
|
| addBundleListener | back to summary |
|---|---|
| public void addBundleListener(BundleListener listener) Adds the specified
If the context bundle's list of listeners already contains a listener
| |
| addFrameworkListener | back to summary |
|---|---|
| public void addFrameworkListener(FrameworkListener listener) Adds the specified
If the context bundle's list of listeners already contains a listener
| |
| addServiceListener | back to summary |
|---|---|
| public void addServiceListener(ServiceListener listener, String filter) throws InvalidSyntaxException Adds the specified
If the context bundle's list of listeners already contains a listener
The listener is called if the filter criteria is met. To filter based
upon the class of the service, the filter should reference the
When using a
If the Java Runtime Environment supports permissions, the
| |
| addServiceListener | back to summary |
|---|---|
| public void addServiceListener(ServiceListener listener) Adds the specified
This method is the same as calling
| |
| createFilter | back to summary |
|---|---|
| public Filter createFilter(String filter) throws InvalidSyntaxException Creates a
If the filter cannot be parsed, an
| |
| getAllServiceReferences | back to summary |
|---|---|
| public ServiceReference Returns an array of The list is valid at the time of the call to this method. However since the Framework is a very dynamic environment, services can be modified or unregistered at any time.
The specified
The result is an array of
| |
| getBundle | back to summary |
|---|---|
| public Bundle getBundle() Redeclares org. Returns the
| |
| getBundle | back to summary |
|---|---|
| public Bundle getBundle(long id) Returns the bundle with the specified identifier.
| |
| getBundle | back to summary |
|---|---|
| public Bundle getBundle(String location) Returns the bundle with the specified location. | |
| getBundles | back to summary |
|---|---|
| public Bundle[] getBundles() Returns a list of all installed bundles. This method returns a list of all bundles installed in the OSGi environment at the time of the call to this method. However, since the Framework is a very dynamic environment, bundles can be installed or uninstalled at anytime.
| |
| getDataFile | back to summary |
|---|---|
| public File getDataFile(String filename) Creates a
A
If the Java Runtime Environment supports permissions, the Framework will
ensure that the bundle has the
| |
| getProperty | back to summary |
|---|---|
| public String getProperty(String key) Returns the value of the specified property. If the key is not found in
the Framework properties, the system properties are then searched. The
method returns All bundles must have permission to read properties whose names start with "org.osgi.".
| |
| getService | back to summary |
|---|---|
| public <S> S getService(ServiceReference<S> reference) Returns the service object for the service referenced by the specified
A bundle's use of a service object obtained from this method is tracked
by the bundle's use count of that service. Each time the service object
is returned by When a bundle's use count for the service drops to zero, the bundle should no longer use the service object.
This method will always return The following steps are required to get the service object:
| |
| getServiceObjects | back to summary |
|---|---|
| public <S> ServiceObjects Returns the
The
For services with
This method will always return
| |
| getServiceReference | back to summary |
|---|---|
| public ServiceReference Returns a
The returned
This method is the same as calling
If multiple such services exist, the service with the highest priority is
selected. This priority is defined as the service reference with the
highest ranking (as specified in its
If there is a tie in ranking, the service with the lowest service id (as
specified in its
| |
| getServiceReference | back to summary |
|---|---|
| public <S> ServiceReference Returns a
The returned
This method is the same as calling
If multiple such services exist, the service with the highest ranking (as
specified in its
If there is a tie in ranking, the service with the lowest service id (as
specified in its
| |
| getServiceReferences | back to summary |
|---|---|
| public ServiceReference Returns an array of The list is valid at the time of the call to this method. However since the Framework is a very dynamic environment, services can be modified or unregistered at any time.
The specified
The result is an array of
| |
| getServiceReferences | back to summary |
|---|---|
| public <S> Collection Returns a collection of The collection is valid at the time of the call to this method. However since the Framework is a very dynamic environment, services can be modified or unregistered at any time.
The specified
The result is a collection of
| |
| installBundle | back to summary |
|---|---|
| public Bundle installBundle(String location, InputStream input) throws BundleException Installs a bundle from the specified
If the specified
The specified The following steps are required to install a bundle:
| |
| installBundle | back to summary |
|---|---|
| public Bundle installBundle(String location) throws BundleException Installs a bundle from the specified
This method performs the same function as calling
| |
| registerService | back to summary |
|---|---|
| public ServiceRegistration Registers the specified service object with the specified properties
under the specified class names into the Framework. A
A bundle can register a service object that implements the
The following steps are required to register a service:
| |
| registerService | back to summary |
|---|---|
| public ServiceRegistration Registers the specified service object with the specified properties under the specified class name with the Framework.
This method is otherwise identical to
| |
| registerService | back to summary |
|---|---|
| public <S> ServiceRegistration Registers the specified service object with the specified properties under the name of the specified class with the Framework.
This method is otherwise identical to
| |
| registerService | back to summary |
|---|---|
| public <S> ServiceRegistration Registers the specified service factory object with the specified properties under the name of the specified class with the Framework.
This method is otherwise identical to
| |
| removeBundleListener | back to summary |
|---|---|
| public void removeBundleListener(BundleListener listener) Removes the specified
If
| |
| removeFrameworkListener | back to summary |
|---|---|
| public void removeFrameworkListener(FrameworkListener listener) Removes the specified
If
| |
| removeServiceListener | back to summary |
|---|---|
| public void removeServiceListener(ServiceListener listener) Removes the specified
If
| |
| ungetService | back to summary |
|---|---|
| public boolean ungetService(ServiceReference<?> reference) Releases the service object for the service referenced by the specified
The service object must 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 release the service object:
| |