public class BundleContextImpl extends java.lang.Object implements BundleContext
BundleContext
构造器和说明 |
---|
BundleContextImpl(BundleImpl bundle)
Create a BundleContext for specified bundle.
|
限定符和类型 | 方法和说明 |
---|---|
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 |
getBundle(java.lang.String location)
Returns the bundle with the specified location.
|
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.
|
<S> S |
getService(ServiceReference<S> reference)
Get the service object.
|
<S> ServiceReference<S> |
getServiceReference(java.lang.Class<S> clazz)
Returns a
ServiceReference object for a service that implements
and was registered under the name of the specified class. |
ServiceReference<?> |
getServiceReference(java.lang.String clazz)
Get a service reference.
|
<S> java.util.Collection<ServiceReference<S>> |
getServiceReferences(java.lang.Class<S> clazz,
java.lang.String filter)
Returns a collection of
ServiceReference objects. |
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.
|
<S> ServiceRegistration<S> |
registerService(java.lang.Class<S> clazz,
S service,
java.util.Dictionary<java.lang.String,?> properties)
Registers the specified service object with the specified properties
under the name of the specified class with the Framework.
|
ServiceRegistration<?> |
registerService(java.lang.String[] clazzes,
java.lang.Object service,
java.util.Dictionary<java.lang.String,?> properties)
Register a service with multiple names.
|
ServiceRegistration<?> |
registerService(java.lang.String clazz,
java.lang.Object service,
java.util.Dictionary<java.lang.String,?> 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.
|
public BundleContextImpl(BundleImpl bundle)
public java.lang.String getProperty(java.lang.String key)
getProperty
在接口中 BundleContext
key
- The name of the requested property.null
if the
property is undefined.BundleContext.getProperty(java.lang.String)
public Bundle installBundle(java.lang.String location) throws BundleException
installBundle
在接口中 BundleContext
location
- The location identifier of the bundle to install.Bundle
object of the installed bundle.BundleException
- If the installation failed. BundleException types
thrown by this method include: BundleException.READ_ERROR
, BundleException.DUPLICATE_BUNDLE_ERROR
,
BundleException.MANIFEST_ERROR
, and
BundleException.REJECTED_BY_HOOK
.BundleContext.installBundle(java.lang.String, java.io.InputStream)
public Bundle installBundle(java.lang.String location, java.io.InputStream in) throws BundleException
installBundle
在接口中 BundleContext
location
- The location identifier of the bundle to install.in
- The 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.Bundle
object of the installed bundle.BundleException
- If the installation failed. BundleException types
thrown by this method include: BundleException.READ_ERROR
, BundleException.DUPLICATE_BUNDLE_ERROR
,
BundleException.MANIFEST_ERROR
, and
BundleException.REJECTED_BY_HOOK
.BundleContext.installBundle(java.lang.String, java.io.InputStream)
public Bundle getBundle()
getBundle
在接口中 BundleContext
getBundle
在接口中 BundleReference
Bundle
object associated with this
BundleContext
.BundleContext.getBundle()
public Bundle getBundle(long id)
getBundle
在接口中 BundleContext
id
- The identifier of the bundle to retrieve.Bundle
object or null
if the identifier does
not match any installed bundle.BundleContext.getBundle()
public Bundle[] getBundles()
getBundles
在接口中 BundleContext
Bundle
objects, one object per installed
bundle.BundleContext.getBundles()
public void addServiceListener(ServiceListener listener, java.lang.String filter) throws InvalidSyntaxException
addServiceListener
在接口中 BundleContext
listener
- The ServiceListener
object to be added.filter
- The filter criteria.InvalidSyntaxException
- If filter
contains an invalid
filter string that cannot be parsed.BundleContext.addServiceListener(org.osgi.framework.ServiceListener, java.lang.String)
public void addServiceListener(ServiceListener listener)
addServiceListener
在接口中 BundleContext
listener
- The ServiceListener
object to be added.BundleContext.addServiceListener(org.osgi.framework.ServiceListener, java.lang.String)
public void removeServiceListener(ServiceListener listener)
removeServiceListener
在接口中 BundleContext
listener
- The ServiceListener
to be removed.BundleContext.removeServiceListener(org.osgi.framework.ServiceListener)
public void addBundleListener(BundleListener listener)
addBundleListener
在接口中 BundleContext
listener
- The BundleListener
to be added.BundleContext.addBundleListener(org.osgi.framework.BundleListener)
public void removeBundleListener(BundleListener listener)
removeBundleListener
在接口中 BundleContext
listener
- The BundleListener
object to be removed.BundleContext.removeBundleListener(org.osgi.framework.BundleListener)
public void addFrameworkListener(FrameworkListener listener)
addFrameworkListener
在接口中 BundleContext
listener
- The FrameworkListener
object to be added.BundleContext.addFrameworkListener(org.osgi.framework.FrameworkListener)
public void removeFrameworkListener(FrameworkListener listener)
removeFrameworkListener
在接口中 BundleContext
listener
- The FrameworkListener
object to be removed.BundleContext.removeFrameworkListener(org.osgi.framework.FrameworkListener)
public ServiceRegistration<?> registerService(java.lang.String[] clazzes, java.lang.Object service, java.util.Dictionary<java.lang.String,?> properties)
registerService
在接口中 BundleContext
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(Dictionary)
method must
be called. The set of properties may be null
if the
service has no properties.ServiceRegistration
object for use by the bundle
registering the service to update the service's properties or to
unregister the service.BundleContext.registerService(java.lang.String[], java.lang.Object, java.util.Dictionary<java.lang.String, ?>)
public ServiceRegistration<?> registerService(java.lang.String clazz, java.lang.Object service, java.util.Dictionary<java.lang.String,?> properties)
registerService
在接口中 BundleContext
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.ServiceRegistration
object for use by the bundle
registering the service to update the service's properties or to
unregister the service.BundleContext.registerService(java.lang.String[], java.lang.Object, java.util.Dictionary<java.lang.String, ?>)
public ServiceReference<?>[] getServiceReferences(java.lang.String clazz, java.lang.String filter) throws InvalidSyntaxException
getServiceReferences
在接口中 BundleContext
clazz
- The class name with which the service was registered or
null
for all services.filter
- The filter expression or null
for all services.ServiceReference
objects or null
if
no services are registered which satisfy the search.InvalidSyntaxException
- If the specified filter
contains
an invalid filter expression that cannot be parsed.BundleContext.getServiceReferences(java.lang.String, java.lang.String)
public ServiceReference<?>[] getAllServiceReferences(java.lang.String clazz, java.lang.String filter) throws InvalidSyntaxException
getAllServiceReferences
在接口中 BundleContext
clazz
- The class name with which the service was registered or
null
for all services.filter
- The filter expression or null
for all services.ServiceReference
objects or null
if
no services are registered which satisfy the search.InvalidSyntaxException
- If the specified filter
contains
an invalid filter expression that cannot be parsed.BundleContext.getAllServiceReferences(java.lang.String, java.lang.String)
public ServiceReference<?> getServiceReference(java.lang.String clazz)
getServiceReference
在接口中 BundleContext
clazz
- The class name with which the service was registered.ServiceReference
object, or null
if no services
are registered which implement the named class.BundleContext.getServiceReference(java.lang.String)
public <S> S getService(ServiceReference<S> reference)
getService
在接口中 BundleContext
S
- Type of Service.reference
- A reference to the service.reference
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.BundleContext.getService(org.osgi.framework.ServiceReference<S>)
public boolean ungetService(ServiceReference<?> reference)
ungetService
在接口中 BundleContext
reference
- A reference to the service to be released.false
if the context bundle's use count for the service
is zero or if the service has been unregistered; true
otherwise.BundleContext.ungetService(org.osgi.framework.ServiceReference<?>)
public java.io.File getDataFile(java.lang.String filename)
getDataFile
在接口中 BundleContext
filename
- A relative name to the file to be accessed.File
object that represents the requested file or
null
if the platform does not have file system support.BundleContext.getDataFile(java.lang.String)
public Filter createFilter(java.lang.String filter) throws InvalidSyntaxException
ServiceReference
or a Dictionary.createFilter
在接口中 BundleContext
filter
- the filter string.InvalidSyntaxException
- If the filter parameter contains
an invalid filter string which cannot be parsed.FrameworkUtil.createFilter(String)
public <S> ServiceRegistration<S> registerService(java.lang.Class<S> clazz, S service, java.util.Dictionary<java.lang.String,?> properties)
BundleContext
This method is otherwise identical to
BundleContext.registerService(String, Object, Dictionary)
and is provided to
return a type safe ServiceRegistration
.
registerService
在接口中 BundleContext
S
- Type of Service.clazz
- The class under whose name the service can be located.service
- The service object or a ServiceFactory
object.properties
- The properties for this service.ServiceRegistration
object for use by the bundle
registering the service to update the service's properties or to
unregister the service.BundleContext.registerService(String, Object, Dictionary)
public <S> ServiceReference<S> getServiceReference(java.lang.Class<S> clazz)
BundleContext
ServiceReference
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
BundleContext.getServiceReferences(Class, String)
with a null
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.
getServiceReference
在接口中 BundleContext
S
- Type of Service.clazz
- The class under whose name the service was registered. Must
not be null
.ServiceReference
object, or null
if no services
are registered which implement the specified class.BundleContext.getServiceReferences(Class, String)
public <S> java.util.Collection<ServiceReference<S>> getServiceReferences(java.lang.Class<S> clazz, java.lang.String filter) throws InvalidSyntaxException
BundleContext
ServiceReference
objects. The returned
collection of ServiceReference
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 in ServiceReference.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. See Filter
for a description of the filter
syntax. If the specified filter
is null
, all registered
services are considered to match the filter. If the specified
filter
expression cannot be parsed, an
InvalidSyntaxException
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:
objectClass
property.
filter
is not null
, the filter
expression must match the service.
ServicePermission
with the GET
action for at least
one of the class names under which the service was registered.
ServiceReference.isAssignableTo(Bundle, String)
with the context
bundle and the class name on the service's ServiceReference
object must return true
getServiceReferences
在接口中 BundleContext
S
- Type of Serviceclazz
- The class under whose name the service was registered. Must
not be null
.filter
- The filter expression or null
for all services.ServiceReference
objects. May be empty if
no services are registered which satisfy the search.InvalidSyntaxException
- If the specified filter
contains
an invalid filter expression that cannot be parsed.public Bundle getBundle(java.lang.String location)
BundleContext
getBundle
在接口中 BundleContext
location
- The location of the bundle to retrieve.Bundle
object or null
if the location does not
match any installed bundle.