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.
{@code BundleContext} methods allow a bundle to:
- Subscribe to events published by the Framework.
- Register service objects with the Framework service registry.
- Retrieve {@code ServiceReferences} from the Framework service registry.
- Get and release service objects for a referenced service.
- Install new bundles in the Framework.
- Get the list of bundles installed in the Framework.
- Get the {@link Bundle} object for a bundle.
- Create {@code File} objects for files in a persistent storage areaprovided for the bundle by the Framework.
A {@code BundleContext} object will be created for a bundle when the bundleis started. The {@code Bundle} object associated with a {@code BundleContext}object is called the context bundle.
The {@code BundleContext} object will be passed to the{@link BundleActivator#start(BundleContext)} method during activation of thecontext bundle. The same {@code BundleContext} object will be passed to the{@link BundleActivator#stop(BundleContext)} method when the context bundle isstopped. A {@code BundleContext} object is generally for the private use ofits associated bundle and is not meant to be shared with other bundles in the OSGi environment.
The {@code BundleContext} object is only valid during the execution of itscontext bundle; that is, during the period from when the context bundle is in the {@code STARTING}, {@code STOPPING}, and {@code ACTIVE} bundle states.However, the {@code BundleContext} object becomes invalid after{@link BundleActivator#stop(BundleContext)} returns (if the bundle has aBundle Activator). The {@code BundleContext} object becomes invalid beforedisposing of any remaining registered services and releasing any remaining services in use. Since those activities can result in other bundles being called (for example, {@link ServiceListener}s for {@link ServiceEvent#UNREGISTERING} events and {@link ServiceFactory}s for unget operations), those other bundles can observe the stopping bundle in the {@code STOPPING} state but with an invalid {@code BundleContext} object. Ifthe {@code BundleContext} object is used after it has become invalid, an{@code IllegalStateException} must be thrown. The {@code BundleContext}object must never be reused after its context bundle is stopped.
Two {@code BundleContext} objects are equal if they both refer to the sameexecution context of a bundle. The Framework is the only entity that can create {@code BundleContext} objects and they are only valid within theFramework that created them.
A {@link Bundle} can be {@link Bundle#adapt(Class) adapted} to its{@code BundleContext}. In order for this to succeed, the caller must have the appropriate {@code AdminPermission[bundle,CONTEXT]} if the Java RuntimeEnvironment supports permissions.
@ThreadSafe
@author $Id: fbf0b18296a0b85d628ee8c47d0f0f213a914e48 $