Interface BundleContext
- All Superinterfaces:
BundleReference
BundleContext
methods allow a bundle to:
- Subscribe to events published by the Framework.
- Register service objects with the Framework service registry.
- Retrieve
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
Bundle
object for a bundle. - Create
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, ServiceListener
s for
ServiceEvent.UNREGISTERING
events and ServiceFactory
s 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.
-
Method Summary
Modifier and TypeMethodDescriptionvoid
addBundleListener
(BundleListener listener) Adds the specifiedBundleListener
object to the context bundle's list of listeners if not already present.void
addFrameworkListener
(FrameworkListener listener) Adds the specifiedFrameworkListener
object to the context bundle's list of listeners if not already present.void
addServiceListener
(ServiceListener listener) Adds the specifiedServiceListener
object to the context bundle's list of listeners.void
addServiceListener
(ServiceListener listener, String filter) Adds the specifiedServiceListener
object with the specifiedfilter
to the context bundle's list of listeners.createFilter
(String filter) Creates aFilter
object.ServiceReference<?>[]
getAllServiceReferences
(String clazz, String filter) Returns an array ofServiceReference
objects.Returns theBundle
object associated with thisBundleContext
.getBundle
(long id) Returns the bundle with the specified identifier.Returns the bundle with the specified location.Bundle[]
Returns a list of all installed bundles.getDataFile
(String filename) Creates aFile
object for a file in the persistent storage area provided for the bundle by the Framework.getProperty
(String key) Returns the value of the specified property.<S> S
getService
(ServiceReference<S> reference) Returns the service object for the service referenced by the specifiedServiceReference
object.<S> ServiceObjects<S>
getServiceObjects
(ServiceReference<S> reference) Returns theServiceObjects
object for the service referenced by the specifiedServiceReference
object.<S> ServiceReference<S>
getServiceReference
(Class<S> clazz) Returns aServiceReference
object for a service that implements and was registered under the name of the specified class.getServiceReference
(String clazz) Returns aServiceReference
object for a service that implements and was registered under the specified class.<S> Collection<ServiceReference<S>>
getServiceReferences
(Class<S> clazz, String filter) Returns a collection ofServiceReference
objects.ServiceReference<?>[]
getServiceReferences
(String clazz, String filter) Returns an array ofServiceReference
objects.installBundle
(String location) Installs a bundle from the specifiedlocation
identifier.installBundle
(String location, InputStream input) Installs a bundle from the specifiedInputStream
object.<S> ServiceRegistration<S>
registerService
(Class<S> clazz, ServiceFactory<S> factory, Dictionary<String, ?> properties) Registers the specified service factory object with the specified properties under the name of the specified class with the Framework.<S> ServiceRegistration<S>
registerService
(Class<S> clazz, S service, Dictionary<String, ?> properties) Registers the specified service object with the specified properties under the name of the specified class with the Framework.registerService
(String[] clazzes, Object service, Dictionary<String, ?> properties) Registers the specified service object with the specified properties under the specified class names into the Framework.registerService
(String clazz, Object service, Dictionary<String, ?> properties) Registers the specified service object with the specified properties under the specified class name with the Framework.void
removeBundleListener
(BundleListener listener) Removes the specifiedBundleListener
object from the context bundle's list of listeners.void
removeFrameworkListener
(FrameworkListener listener) Removes the specifiedFrameworkListener
object from the context bundle's list of listeners.void
removeServiceListener
(ServiceListener listener) Removes the specifiedServiceListener
object from the context bundle's list of listeners.boolean
ungetService
(ServiceReference<?> reference) Releases the service object for the service referenced by the specifiedServiceReference
object.
-
Method Details
-
getProperty
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 returnsnull
if the property is not found.All bundles must have permission to read properties whose names start with "org.osgi.".
- Parameters:
key
- The name of the requested property.- Returns:
- The value of the requested property, or
null
if the property is undefined. - Throws:
SecurityException
- If the caller does not have the appropriatePropertyPermission
to read the property, and the Java Runtime Environment supports permissions.
-
getBundle
Bundle getBundle()Returns theBundle
object associated with thisBundleContext
. This bundle is called the context bundle.- Specified by:
getBundle
in interfaceBundleReference
- Returns:
- The
Bundle
object associated with thisBundleContext
. - Throws:
IllegalStateException
- If this BundleContext is no longer valid.
-
installBundle
Installs a bundle from the specifiedInputStream
object.If the specified
InputStream
isnull
, the Framework must create theInputStream
from which to read the bundle by interpreting, in an implementation dependent manner, the specifiedlocation
.The specified
location
identifier will be used as the identity of the bundle. Every installed bundle is uniquely identified by its location identifier which is typically in the form of a URL.The following steps are required to install a bundle:
- If a bundle containing the same location identifier is already
installed, the
Bundle
object for that bundle is returned. - The bundle's content is read from the input stream. If this fails, a
BundleException
is thrown. - The bundle's associated resources are allocated. The associated
resources minimally consist of a unique identifier and a persistent
storage area if the platform has file system support. If this step fails,
a
BundleException
is thrown. - The bundle's state is set to
INSTALLED
. - A bundle event of type
BundleEvent.INSTALLED
is fired. - The
Bundle
object for the newly or previously installed bundle is returned.
getState()
in {INSTALLED
,RESOLVED
}.- Bundle has a unique ID.
- Bundle is not installed. If there was an existing bundle for the specified location, then that bundle must still be in the state it was prior to calling this method.
- Parameters:
location
- The location identifier of the bundle to install.input
- TheInputStream
object from which this bundle will be read ornull
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.- Returns:
- The
Bundle
object of the installed bundle. - Throws:
BundleException
- If the installation failed. BundleException types thrown by this method include:BundleException.READ_ERROR
,BundleException.DUPLICATE_BUNDLE_ERROR
,BundleException.MANIFEST_ERROR
, andBundleException.REJECTED_BY_HOOK
.SecurityException
- If the caller does not have the appropriateAdminPermission[installed bundle,LIFECYCLE]
, and the Java Runtime Environment supports permissions.IllegalStateException
- If this BundleContext is no longer valid.
- If a bundle containing the same location identifier is already
installed, the
-
installBundle
Installs a bundle from the specifiedlocation
identifier.This method performs the same function as calling
installBundle(String,InputStream)
with the specifiedlocation
identifier and anull
InputStream.- Parameters:
location
- The location identifier of the bundle to install.- Returns:
- The
Bundle
object of the installed bundle. - Throws:
BundleException
- If the installation failed. BundleException types thrown by this method include:BundleException.READ_ERROR
,BundleException.DUPLICATE_BUNDLE_ERROR
,BundleException.MANIFEST_ERROR
, andBundleException.REJECTED_BY_HOOK
.SecurityException
- If the caller does not have the appropriateAdminPermission[installed bundle,LIFECYCLE]
, and the Java Runtime Environment supports permissions.IllegalStateException
- If this BundleContext is no longer valid.- See Also:
-
getBundle
Returns the bundle with the specified identifier.- Parameters:
id
- The identifier of the bundle to retrieve.- Returns:
- A
Bundle
object ornull
if the identifier does not match any installed bundle.
-
getBundles
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.
- Returns:
- An array of
Bundle
objects, one object per installed bundle.
-
addServiceListener
Adds the specifiedServiceListener
object with the specifiedfilter
to the context bundle's list of listeners. SeeFilter
for a description of the filter syntax.ServiceListener
objects are notified when a service has a lifecycle state change.If the context bundle's list of listeners already contains a listener
l
such that(l==listener)
, then this method replaces that listener's filter (which may benull
) with the specified one (which may benull
).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. Iffilter
isnull
, all services are considered to match the filter.When using a
filter
, it is possible that theServiceEvent
s for the complete lifecycle of a service will not be delivered to the listener. For example, if thefilter
only matches when the propertyx
has the value1
, the listener will not be called if the service is registered with the propertyx
not set to the value1
. Subsequently, when the service is modified setting propertyx
to the value1
, the filter will match and the listener will be called with aServiceEvent
of typeMODIFIED
. Thus, the listener will not be called with aServiceEvent
of typeREGISTERED
.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 theServicePermission
to get the service using at least one of the named classes the service was registered under.- Parameters:
listener
- TheServiceListener
object to be added.filter
- The filter criteria.- Throws:
InvalidSyntaxException
- Iffilter
contains an invalid filter string that cannot be parsed.IllegalStateException
- If this BundleContext is no longer valid.- See Also:
-
addServiceListener
Adds the specifiedServiceListener
object to the context bundle's list of listeners.This method is the same as calling
BundleContext.addServiceListener(ServiceListener listener, String filter)
withfilter
set tonull
.- Parameters:
listener
- TheServiceListener
object to be added.- Throws:
IllegalStateException
- If this BundleContext is no longer valid.- See Also:
-
removeServiceListener
Removes the specifiedServiceListener
object from the context bundle's list of listeners.If
listener
is not contained in this context bundle's list of listeners, this method does nothing.- Parameters:
listener
- TheServiceListener
to be removed.- Throws:
IllegalStateException
- If this BundleContext is no longer valid.
-
addBundleListener
Adds the specifiedBundleListener
object to the context bundle's list of listeners if not already present. BundleListener objects are notified when a bundle has a lifecycle state change.If the context bundle's list of listeners already contains a listener
l
such that(l==listener)
, this method does nothing.- Parameters:
listener
- TheBundleListener
to be added.- Throws:
IllegalStateException
- If this BundleContext is no longer valid.SecurityException
- If listener is aSynchronousBundleListener
and the caller does not have the appropriateAdminPermission[context bundle,LISTENER]
, and the Java Runtime Environment supports permissions.- See Also:
-
removeBundleListener
Removes the specifiedBundleListener
object from the context bundle's list of listeners.If
listener
is not contained in the context bundle's list of listeners, this method does nothing.- Parameters:
listener
- TheBundleListener
object to be removed.- Throws:
IllegalStateException
- If this BundleContext is no longer valid.SecurityException
- If listener is aSynchronousBundleListener
and the caller does not have the appropriateAdminPermission[context bundle,LISTENER]
, and the Java Runtime Environment supports permissions.
-
addFrameworkListener
Adds the specifiedFrameworkListener
object to the context bundle's list of listeners if not already present. FrameworkListeners are notified of general Framework events.If the context bundle's list of listeners already contains a listener
l
such that(l==listener)
, this method does nothing.- Parameters:
listener
- TheFrameworkListener
object to be added.- Throws:
IllegalStateException
- If this BundleContext is no longer valid.- See Also:
-
removeFrameworkListener
Removes the specifiedFrameworkListener
object from the context bundle's list of listeners.If
listener
is not contained in the context bundle's list of listeners, this method does nothing.- Parameters:
listener
- TheFrameworkListener
object to be removed.- Throws:
IllegalStateException
- If this BundleContext is no longer valid.
-
registerService
ServiceRegistration<?> registerService(String[] clazzes, Object service, Dictionary<String, ?> properties) Registers the specified service object with the specified properties under the specified class names into the Framework. AServiceRegistration
object is returned. TheServiceRegistration
object is for the private use of the bundle registering the service and should not be shared with other bundles. The registering bundle is defined to be the context bundle. Other bundles can locate the service by using one of thegetServiceReferences(Class, String)
,getServiceReferences(String, String)
,getServiceReference(Class)
orgetServiceReference(String)
methods.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:
- If
service
does not implementServiceFactory
, anIllegalArgumentException
is thrown ifservice
is not aninstanceof
all the specified class names. - The Framework adds the following service properties to the service
properties from the specified
Dictionary
(which may benull
):- A property named
Constants.SERVICE_ID
identifying the registration number of the service - A property named
Constants.OBJECTCLASS
containing all the specified classes. - A property named
Constants.SERVICE_SCOPE
identifying the scope of the service. - A property named
Constants.SERVICE_BUNDLEID
identifying the context bundle.
Dictionary
will be ignored. - A property named
- The service is added to the Framework service registry and may now be used by other bundles.
- A service event of type
ServiceEvent.REGISTERED
is fired. - A
ServiceRegistration
object for this registration is returned.
- 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 keyConstants.OBJECTCLASS
.service
- The service object or an object implementingServiceFactory
.properties
- The properties for this service. The keys in the properties object must all beString
objects. SeeConstants
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 theServiceRegistration.setProperties(Dictionary)
method must be called. The set of properties may benull
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. - Throws:
IllegalArgumentException
- If one of the following is true:service
isnull
.service
does not implementServiceFactory
and is not an instance of all the specified classes.-
properties
contains case variants of the same key name.
SecurityException
- If the caller does not have theServicePermission
to register the service for all the named classes and the Java Runtime Environment supports permissions.IllegalStateException
- If this BundleContext is no longer valid.- See Also:
- If
-
registerService
ServiceRegistration<?> registerService(String clazz, Object service, Dictionary<String, ?> properties) Registers the specified service object with the specified properties under the specified class name with the Framework.This method is otherwise identical to
registerService(String[], Object, Dictionary)
and is provided as a convenience whenservice
will only be registered under a single class name. Note that even in this case the value of the service'sConstants.OBJECTCLASS
property will be an array of string, rather than just a single string.- Parameters:
clazz
- The class name under which the service can be located.service
- The service object or an object implementingServiceFactory
.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. - Throws:
IllegalStateException
- If this BundleContext is no longer valid.- See Also:
-
registerService
<S> ServiceRegistration<S> registerService(Class<S> clazz, S service, Dictionary<String, ?> properties) 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(String, Object, Dictionary)
and is provided to return a type safeServiceRegistration
.- Type Parameters:
S
- Type of Service.- Parameters:
clazz
- The class under whose name the service can be located.service
- The service object or an object implementingServiceFactory
.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. - Throws:
IllegalStateException
- If this BundleContext is no longer valid.- Since:
- 1.6
- See Also:
-
registerService
<S> ServiceRegistration<S> registerService(Class<S> clazz, ServiceFactory<S> factory, Dictionary<String, ?> properties) 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
registerService(Class, Object, Dictionary)
and is provided to return a type safeServiceRegistration
when registering aServiceFactory
.- Type Parameters:
S
- Type of Service.- Parameters:
clazz
- The class under whose name the service can be located.factory
- TheServiceFactory
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. - Throws:
IllegalStateException
- If this BundleContext is no longer valid.- Since:
- 1.8
- See Also:
-
getServiceReferences
ServiceReference<?>[] getServiceReferences(String clazz, String filter) throws InvalidSyntaxException Returns an array ofServiceReference
objects. The returned array ofServiceReference
objects contains services that were registered under the specified class, match the specified filter expression, and the packages for the class names under which the services were registered match the context bundle's packages as defined inServiceReference.isAssignableTo(Bundle, String)
.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
filter
expression is used to select the registered services whose service properties contain keys and values which satisfy the filter expression. SeeFilter
for a description of the filter syntax. If the specifiedfilter
isnull
, all registered services are considered to match the filter. If the specifiedfilter
expression cannot be parsed, anInvalidSyntaxException
will be thrown with a human readable message where the filter became unparsable.The result is an array of
ServiceReference
objects for all services that meet all of the following conditions:- If the specified class name,
clazz
, is notnull
, the service must have been registered with the specified class name. The complete list of class names with which a service was registered is available from the service'sobjectClass
property. - If the specified
filter
is notnull
, the filter expression must match the service. - If the Java Runtime Environment supports permissions, the caller must
have
ServicePermission
with theGET
action for at least one of the class names under which the service was registered. - For each class name with which the service was registered, calling
ServiceReference.isAssignableTo(Bundle, String)
with the context bundle and the class name on the service'sServiceReference
object must returntrue
- Parameters:
clazz
- The class name with which the service was registered ornull
for all services.filter
- The filter expression ornull
for all services.- Returns:
- An array of
ServiceReference
objects ornull
if no services are registered which satisfy the search. - Throws:
InvalidSyntaxException
- If the specifiedfilter
contains an invalid filter expression that cannot be parsed.IllegalStateException
- If this BundleContext is no longer valid.
- If the specified class name,
-
getAllServiceReferences
ServiceReference<?>[] getAllServiceReferences(String clazz, String filter) throws InvalidSyntaxException Returns an array ofServiceReference
objects. The returned array ofServiceReference
objects contains services that were registered under the specified class and match the specified filter expression.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
filter
expression is used to select the registered services whose service properties contain keys and values which satisfy the filter expression. SeeFilter
for a description of the filter syntax. If the specifiedfilter
isnull
, all registered services are considered to match the filter. If the specifiedfilter
expression cannot be parsed, anInvalidSyntaxException
will be thrown with a human readable message where the filter became unparsable.The result is an array of
ServiceReference
objects for all services that meet all of the following conditions:- If the specified class name,
clazz
, is notnull
, the service must have been registered with the specified class name. The complete list of class names with which a service was registered is available from the service'sobjectClass
property. - If the specified
filter
is notnull
, the filter expression must match the service. - If the Java Runtime Environment supports permissions, the caller must
have
ServicePermission
with theGET
action for at least one of the class names under which the service was registered.
- Parameters:
clazz
- The class name with which the service was registered ornull
for all services.filter
- The filter expression ornull
for all services.- Returns:
- An array of
ServiceReference
objects ornull
if no services are registered which satisfy the search. - Throws:
InvalidSyntaxException
- If the specifiedfilter
contains an invalid filter expression that cannot be parsed.IllegalStateException
- If this BundleContext is no longer valid.- Since:
- 1.3
- If the specified class name,
-
getServiceReference
Returns aServiceReference
object for a service that implements and was registered under the specified class.The returned
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 any time.This method is the same as calling
getServiceReferences(String, String)
with anull
filter expression and then finding the reference with the highest priority. 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 priority is selected. This priority is defined as the service reference 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.- Parameters:
clazz
- The class name with which the service was registered.- Returns:
- A
ServiceReference
object, ornull
if no services are registered which implement the named class. - Throws:
IllegalStateException
- If this BundleContext is no longer valid.- See Also:
-
getServiceReference
Returns aServiceReference
object for a service that implements and was registered under the name of the specified class.The returned
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 any time.This method is the same as calling
getServiceReferences(Class, String)
with anull
filter expression. 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.- Type Parameters:
S
- Type of Service.- Parameters:
clazz
- The class under whose name the service was registered. Must not benull
.- Returns:
- A
ServiceReference
object, ornull
if no services are registered which implement the specified class. - Throws:
IllegalStateException
- If this BundleContext is no longer valid.- Since:
- 1.6
- See Also:
-
getServiceReferences
<S> Collection<ServiceReference<S>> getServiceReferences(Class<S> clazz, String filter) throws InvalidSyntaxException Returns a collection ofServiceReference
objects. The returned collection ofServiceReference
objects contains services that were registered under the name of the specified class, match the specified filter expression, and the packages for the class names under which the services were registered match the context bundle's packages as defined inServiceReference.isAssignableTo(Bundle, String)
.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
filter
expression is used to select the registered services whose service properties contain keys and values which satisfy the filter expression. SeeFilter
for a description of the filter syntax. If the specifiedfilter
isnull
, all registered services are considered to match the filter. If the specifiedfilter
expression cannot be parsed, anInvalidSyntaxException
will be thrown with a human readable message where the filter became unparsable.The result is a collection of
ServiceReference
objects for all services that meet all of the following conditions:- The service must have been registered with the name of the specified
class. The complete list of class names with which a service was
registered is available from the service's
objectClass
property. - If the specified
filter
is notnull
, the filter expression must match the service. - If the Java Runtime Environment supports permissions, the caller must
have
ServicePermission
with theGET
action for at least one of the class names under which the service was registered. - For each class name with which the service was registered, calling
ServiceReference.isAssignableTo(Bundle, String)
with the context bundle and the class name on the service'sServiceReference
object must returntrue
- Type Parameters:
S
- Type of Service- Parameters:
clazz
- The class under whose name the service was registered. Must not benull
.filter
- The filter expression ornull
for all services.- Returns:
- A collection of
ServiceReference
objects. May be empty if no services are registered which satisfy the search. - Throws:
InvalidSyntaxException
- If the specifiedfilter
contains an invalid filter expression that cannot be parsed.IllegalStateException
- If this BundleContext is no longer valid.- Since:
- 1.6
- The service must have been registered with the name of the specified
class. The complete list of class names with which a service was
registered is available from the service's
-
getService
Returns the service object for the service referenced by the specifiedServiceReference
object.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
getService(ServiceReference)
the context bundle's use count for the service is incremented by one. Each time the service object is released byungetService(ServiceReference)
the context bundle's use count for the service is decremented by one.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
null
when the service associated with the specifiedreference
has been unregistered.The following steps are required to get the service object:
- If the service has been unregistered,
null
is returned. - If the context bundle's use count for the service is currently zero
and the service has
bundle
orprototype
scope, theServiceFactory.getService(Bundle, ServiceRegistration)
method is called to supply the service object for the context bundle. If the service object returned by theServiceFactory
object isnull
, not aninstanceof
all the classes named when the service was registered or theServiceFactory
object throws an exception or will be recursively called for the context bundle,null
is returned and a Framework event of typeFrameworkEvent.ERROR
containing aServiceException
describing the error is fired. The supplied 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 service object for the context bundle will return the cached service object. - The context bundle's use count for the service is incremented by one.
- The service object for the service is returned.
- Type Parameters:
S
- Type of Service.- Parameters:
reference
- A reference to the service.- Returns:
- A service object for the service associated with
reference
ornull
if the service is not registered, the service object returned by aServiceFactory
does not implement the classes under which it was registered or theServiceFactory
threw an exception. - Throws:
SecurityException
- If the caller does not have theServicePermission
to get the service using at least one of the named classes the service was registered under and the Java Runtime Environment supports permissions.IllegalStateException
- If this BundleContext is no longer valid.IllegalArgumentException
- If the specifiedServiceReference
was not created by the same framework instance as thisBundleContext
.- See Also:
- If the service has been unregistered,
-
ungetService
Releases the service object for the service referenced by the specifiedServiceReference
object. If the context bundle's use count for the service is zero, this method returnsfalse
. Otherwise, the context bundle's use count for the service is decremented by one.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:
- If the context bundle's use count for the service is zero or the
service has been unregistered,
false
is returned. - The context bundle's use count for the service is decremented by one.
- If the context bundle's use count for the service is now zero and the
service has
bundle
orprototype
scope, theServiceFactory.ungetService(Bundle, ServiceRegistration, Object)
method is called to release the service object for the context bundle. true
is returned.
- 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.- Throws:
IllegalStateException
- If this BundleContext is no longer valid.IllegalArgumentException
- If the specifiedServiceReference
was not created by the same framework instance as thisBundleContext
.- See Also:
- If the context bundle's use count for the service is zero or the
service has been unregistered,
-
getServiceObjects
Returns theServiceObjects
object for the service referenced by the specifiedServiceReference
object.The
ServiceObjects
object can be used to obtain multiple service objects for services withprototype
scope.For services with
singleton
orbundle
scope, theServiceObjects.getService()
method behaves the same as thegetService(ServiceReference)
method and theServiceObjects.ungetService(Object)
method behaves the same as theungetService(ServiceReference)
method. That is, only one, use-counted service object is available from theServiceObjects
object.This method will always return
null
when the service associated with the specifiedreference
has been unregistered.- Type Parameters:
S
- Type of Service.- Parameters:
reference
- A reference to the service.- Returns:
- A
ServiceObjects
object for the service associated with the specifiedreference
ornull
if the service is not registered. - Throws:
SecurityException
- If the caller does not have theServicePermission
to get the service using at least one of the named classes the service was registered under and the Java Runtime Environment supports permissions.IllegalStateException
- If this BundleContext is no longer valid.IllegalArgumentException
- If the specifiedServiceReference
was not created by the same framework instance as thisBundleContext
.- Since:
- 1.8
- See Also:
-
getDataFile
Creates aFile
object for a file in the persistent storage area provided for the bundle by the Framework. This method will returnnull
if the platform does not have file system support.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 asfilename
.If the Java Runtime Environment supports permissions, the Framework will ensure that the bundle has the
java.io.FilePermission
with actionsread
,write
,delete
for all files (recursively) in the persistent storage area provided for the context bundle.- Parameters:
filename
- A relative name to the file to be accessed.- Returns:
- A
File
object that represents the requested file ornull
if the platform does not have file system support. - Throws:
IllegalStateException
- If this BundleContext is no longer valid.
-
createFilter
Creates aFilter
object. ThisFilter
object may be used to match aServiceReference
object or aDictionary
object.If the filter cannot be parsed, an
InvalidSyntaxException
will be thrown with a human readable message where the filter became unparsable.- Parameters:
filter
- The filter string.- Returns:
- A
Filter
object encapsulating the filter string. - Throws:
InvalidSyntaxException
- Iffilter
contains an invalid filter string that cannot be parsed.NullPointerException
- Iffilter
is null.IllegalStateException
- If this BundleContext is no longer valid.- Since:
- 1.1
- See Also:
-
- "Framework specification for a description of the filter string syntax."
FrameworkUtil.createFilter(String)
-
getBundle
Returns the bundle with the specified location.- Parameters:
location
- The location of the bundle to retrieve.- Returns:
- A
Bundle
object ornull
if the location does not match any installed bundle. - Since:
- 1.6
-