Class BundleContextImpl
- java.lang.Object
-
- org.apache.felix.framework.BundleContextImpl
-
- All Implemented Interfaces:
FelixBundleContext,BundleContext,BundleReference
class BundleContextImpl extends java.lang.Object implements FelixBundleContext
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description (package private) classBundleContextImpl.ServiceObjectsImpl<S>
-
Constructor Summary
Constructors Modifier Constructor Description protectedBundleContextImpl(Logger logger, Felix felix, BundleImpl bundle)
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description voidaddBundleListener(BundleListener l)Adds the specifiedBundleListenerobject to the context bundle's list of listeners if not already present.voidaddCapability()voidaddFrameworkListener(FrameworkListener l)Adds the specifiedFrameworkListenerobject to the context bundle's list of listeners if not already present.voidaddRequirement(java.lang.String s)voidaddServiceListener(ServiceListener l)Adds the specifiedServiceListenerobject to the context bundle's list of listeners.voidaddServiceListener(ServiceListener l, java.lang.String s)Adds the specifiedServiceListenerobject with the specifiedfilterto the context bundle's list of listeners.private voidcheckValidity()FiltercreateFilter(java.lang.String expr)Creates aFilterobject.ServiceReference<?>[]getAllServiceReferences(java.lang.String clazz, java.lang.String filter)Returns an array ofServiceReferenceobjects.private ServiceReferencegetBestServiceReference(ServiceReference[] refs)BundlegetBundle()Returns theBundleobject associated with thisBundleContext.BundlegetBundle(long id)Returns the bundle with the specified identifier.BundlegetBundle(java.lang.String location)Returns the bundle with the specified location.Bundle[]getBundles()Returns a list of all installed bundles.java.io.FilegetDataFile(java.lang.String s)Creates aFileobject for a file in the persistent storage area provided for the bundle by the Framework.java.lang.StringgetProperty(java.lang.String name)Returns the value of the specified property.<S> SgetService(ServiceReference<S> ref)Returns the service object for the service referenced by the specifiedServiceReferenceobject.<S> ServiceObjects<S>getServiceObjects(ServiceReference<S> ref)Returns theServiceObjectsobject for the service referenced by the specifiedServiceReferenceobject.<S> ServiceReference<S>getServiceReference(java.lang.Class<S> clazz)Returns aServiceReferenceobject for a service that implements and was registered under the name of the specified class.ServiceReference<?>getServiceReference(java.lang.String clazz)Returns aServiceReferenceobject for a service that implements and was registered under the specified class.<S> java.util.Collection<ServiceReference<S>>getServiceReferences(java.lang.Class<S> clazz, java.lang.String filter)Returns a collection ofServiceReferenceobjects.ServiceReference<?>[]getServiceReferences(java.lang.String clazz, java.lang.String filter)Returns an array ofServiceReferenceobjects.BundleinstallBundle(java.lang.String location)Installs a bundle from the specifiedlocationidentifier.BundleinstallBundle(java.lang.String location, java.io.InputStream is)Installs a bundle from the specifiedInputStreamobject.protected voidinvalidate()<S> ServiceRegistration<S>registerService(java.lang.Class<S> clazz, ServiceFactory<S> factory, java.util.Dictionary<java.lang.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(java.lang.Class<S> clazz, S svcObj, java.util.Dictionary<java.lang.String,?> dict)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 svcObj, java.util.Dictionary<java.lang.String,?> dict)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 svcObj, java.util.Dictionary<java.lang.String,?> dict)Registers the specified service object with the specified properties under the specified class name with the Framework.voidremoveBundleListener(BundleListener l)Removes the specifiedBundleListenerobject from the context bundle's list of listeners.voidremoveCapability()voidremoveFrameworkListener(FrameworkListener l)Removes the specifiedFrameworkListenerobject from the context bundle's list of listeners.voidremoveRequirement()voidremoveServiceListener(ServiceListener l)Removes the specifiedServiceListenerobject from the context bundle's list of listeners.booleanungetService(ServiceReference<?> ref)Releases the service object for the service referenced by the specifiedServiceReferenceobject.
-
-
-
Field Detail
-
m_logger
private Logger m_logger
-
m_felix
private Felix m_felix
-
m_bundle
private BundleImpl m_bundle
-
m_valid
private boolean m_valid
-
-
Constructor Detail
-
BundleContextImpl
protected BundleContextImpl(Logger logger, Felix felix, BundleImpl bundle)
-
-
Method Detail
-
invalidate
protected void invalidate()
-
addRequirement
public void addRequirement(java.lang.String s) throws BundleException- Specified by:
addRequirementin interfaceFelixBundleContext- Throws:
BundleException
-
removeRequirement
public void removeRequirement() throws BundleException- Specified by:
removeRequirementin interfaceFelixBundleContext- Throws:
BundleException
-
addCapability
public void addCapability() throws BundleException- Specified by:
addCapabilityin interfaceFelixBundleContext- Throws:
BundleException
-
removeCapability
public void removeCapability() throws BundleException- Specified by:
removeCapabilityin interfaceFelixBundleContext- Throws:
BundleException
-
getProperty
public java.lang.String getProperty(java.lang.String name)
Description copied from interface:BundleContextReturns the value of the specified property. If the key is not found in the Framework properties, the system properties are then searched. The method returnsnullif the property is not found.All bundles must have permission to read properties whose names start with "org.osgi.".
- Specified by:
getPropertyin interfaceBundleContext- Parameters:
name- The name of the requested property.- Returns:
- The value of the requested property, or
nullif the property is undefined.
-
getBundle
public Bundle getBundle()
Description copied from interface:BundleContextReturns theBundleobject associated with thisBundleContext. This bundle is called the context bundle.- Specified by:
getBundlein interfaceBundleContext- Specified by:
getBundlein interfaceBundleReference- Returns:
- The
Bundleobject associated with thisBundleContext.
-
createFilter
public Filter createFilter(java.lang.String expr) throws InvalidSyntaxException
Description copied from interface:BundleContextCreates aFilterobject. ThisFilterobject may be used to match aServiceReferenceobject or aDictionaryobject.If the filter cannot be parsed, an
InvalidSyntaxExceptionwill be thrown with a human readable message where the filter became unparsable.- Specified by:
createFilterin interfaceBundleContext- Parameters:
expr- The filter string.- Returns:
- A
Filterobject encapsulating the filter string. - Throws:
InvalidSyntaxException- Iffiltercontains an invalid filter string that cannot be parsed.- See Also:
- "Framework specification for a description of the filter string syntax.",
FrameworkUtil.createFilter(String)
-
installBundle
public Bundle installBundle(java.lang.String location) throws BundleException
Description copied from interface:BundleContextInstalls a bundle from the specifiedlocationidentifier.This method performs the same function as calling
BundleContext.installBundle(String,InputStream)with the specifiedlocationidentifier and anullInputStream.- Specified by:
installBundlein interfaceBundleContext- Parameters:
location- The location identifier of the bundle to install.- Returns:
- The
Bundleobject 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.- See Also:
BundleContext.installBundle(String, InputStream)
-
installBundle
public Bundle installBundle(java.lang.String location, java.io.InputStream is) throws BundleException
Description copied from interface:BundleContextInstalls a bundle from the specifiedInputStreamobject.If the specified
InputStreamisnull, the Framework must create theInputStreamfrom which to read the bundle by interpreting, in an implementation dependent manner, the specifiedlocation.The specified
locationidentifier 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
Bundleobject for that bundle is returned. - The bundle's content is read from the input stream. If this fails, a
BundleExceptionis 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
BundleExceptionis thrown. - The bundle's state is set to
INSTALLED. - A bundle event of type
BundleEvent.INSTALLEDis fired. - The
Bundleobject 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.
- Specified by:
installBundlein interfaceBundleContext- Parameters:
location- The location identifier of the bundle to install.is- TheInputStreamobject from which this bundle will be read ornullto 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
Bundleobject 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.
- If a bundle containing the same location identifier is already
installed, the
-
getBundle
public Bundle getBundle(long id)
Description copied from interface:BundleContextReturns the bundle with the specified identifier.- Specified by:
getBundlein interfaceBundleContext- Parameters:
id- The identifier of the bundle to retrieve.- Returns:
- A
Bundleobject ornullif the identifier does not match any installed bundle.
-
getBundle
public Bundle getBundle(java.lang.String location)
Description copied from interface:BundleContextReturns the bundle with the specified location.- Specified by:
getBundlein interfaceBundleContext- Parameters:
location- The location of the bundle to retrieve.- Returns:
- A
Bundleobject ornullif the location does not match any installed bundle.
-
getBundles
public Bundle[] getBundles()
Description copied from interface:BundleContextReturns 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.
- Specified by:
getBundlesin interfaceBundleContext- Returns:
- An array of
Bundleobjects, one object per installed bundle.
-
addBundleListener
public void addBundleListener(BundleListener l)
Description copied from interface:BundleContextAdds the specifiedBundleListenerobject 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
lsuch that(l==listener), this method does nothing.- Specified by:
addBundleListenerin interfaceBundleContext- Parameters:
l- TheBundleListenerto be added.- See Also:
BundleEvent,BundleListener
-
removeBundleListener
public void removeBundleListener(BundleListener l)
Description copied from interface:BundleContextRemoves the specifiedBundleListenerobject from the context bundle's list of listeners.If
listeneris not contained in the context bundle's list of listeners, this method does nothing.- Specified by:
removeBundleListenerin interfaceBundleContext- Parameters:
l- TheBundleListenerobject to be removed.
-
addServiceListener
public void addServiceListener(ServiceListener l)
Description copied from interface:BundleContextAdds the specifiedServiceListenerobject to the context bundle's list of listeners.This method is the same as calling
BundleContext.addServiceListener(ServiceListener listener, String filter)withfilterset tonull.- Specified by:
addServiceListenerin interfaceBundleContext- Parameters:
l- TheServiceListenerobject to be added.- See Also:
BundleContext.addServiceListener(ServiceListener, String)
-
addServiceListener
public void addServiceListener(ServiceListener l, java.lang.String s) throws InvalidSyntaxException
Description copied from interface:BundleContextAdds the specifiedServiceListenerobject with the specifiedfilterto the context bundle's list of listeners. SeeFilterfor a description of the filter syntax.ServiceListenerobjects are notified when a service has a lifecycle state change.If the context bundle's list of listeners already contains a listener
lsuch 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.OBJECTCLASSproperty. Iffilterisnull, all services are considered to match the filter.When using a
filter, it is possible that theServiceEvents for the complete lifecycle of a service will not be delivered to the listener. For example, if thefilteronly matches when the propertyxhas the value1, the listener will not be called if the service is registered with the propertyxnot set to the value1. Subsequently, when the service is modified setting propertyxto the value1, the filter will match and the listener will be called with aServiceEventof typeMODIFIED. Thus, the listener will not be called with aServiceEventof typeREGISTERED.If the Java Runtime Environment supports permissions, the
ServiceListenerobject will be notified of a service event only if the bundle that is registering it has theServicePermissionto get the service using at least one of the named classes the service was registered under.- Specified by:
addServiceListenerin interfaceBundleContext- Parameters:
l- TheServiceListenerobject to be added.s- The filter criteria.- Throws:
InvalidSyntaxException- Iffiltercontains an invalid filter string that cannot be parsed.- See Also:
ServiceEvent,ServiceListener,ServicePermission
-
removeServiceListener
public void removeServiceListener(ServiceListener l)
Description copied from interface:BundleContextRemoves the specifiedServiceListenerobject from the context bundle's list of listeners.If
listeneris not contained in this context bundle's list of listeners, this method does nothing.- Specified by:
removeServiceListenerin interfaceBundleContext- Parameters:
l- TheServiceListenerto be removed.
-
addFrameworkListener
public void addFrameworkListener(FrameworkListener l)
Description copied from interface:BundleContextAdds the specifiedFrameworkListenerobject 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
lsuch that(l==listener), this method does nothing.- Specified by:
addFrameworkListenerin interfaceBundleContext- Parameters:
l- TheFrameworkListenerobject to be added.- See Also:
FrameworkEvent,FrameworkListener
-
removeFrameworkListener
public void removeFrameworkListener(FrameworkListener l)
Description copied from interface:BundleContextRemoves the specifiedFrameworkListenerobject from the context bundle's list of listeners.If
listeneris not contained in the context bundle's list of listeners, this method does nothing.- Specified by:
removeFrameworkListenerin interfaceBundleContext- Parameters:
l- TheFrameworkListenerobject to be removed.
-
registerService
public ServiceRegistration<?> registerService(java.lang.String clazz, java.lang.Object svcObj, java.util.Dictionary<java.lang.String,?> dict)
Description copied from interface:BundleContextRegisters the specified service object with the specified properties under the specified class name with the Framework.This method is otherwise identical to
BundleContext.registerService(String[], Object, Dictionary)and is provided as a convenience whenservicewill only be registered under a single class name. Note that even in this case the value of the service'sConstants.OBJECTCLASSproperty will be an array of string, rather than just a single string.- Specified by:
registerServicein interfaceBundleContext- Parameters:
clazz- The class name under which the service can be located.svcObj- The service object or an object implementingServiceFactory.dict- The properties for this service.- Returns:
- A
ServiceRegistrationobject for use by the bundle registering the service to update the service's properties or to unregister the service. - See Also:
BundleContext.registerService(String[], Object, Dictionary)
-
registerService
public ServiceRegistration<?> registerService(java.lang.String[] clazzes, java.lang.Object svcObj, java.util.Dictionary<java.lang.String,?> dict)
Description copied from interface:BundleContextRegisters the specified service object with the specified properties under the specified class names into the Framework. AServiceRegistrationobject is returned. TheServiceRegistrationobject 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 theBundleContext.getServiceReferences(Class, String),BundleContext.getServiceReferences(String, String),BundleContext.getServiceReference(Class)orBundleContext.getServiceReference(String)methods.A bundle can register a service object that implements the
ServiceFactoryinterface to have more flexibility in providing service objects to other bundles.The following steps are required to register a service:
- If
servicedoes not implementServiceFactory, anIllegalArgumentExceptionis thrown ifserviceis not aninstanceofall 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_IDidentifying the registration number of the service - A property named
Constants.OBJECTCLASScontaining all the specified classes. - A property named
Constants.SERVICE_SCOPEidentifying the scope of the service. - A property named
Constants.SERVICE_BUNDLEIDidentifying the the context bundle.
Dictionarywill 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.REGISTEREDis fired. - A
ServiceRegistrationobject for this registration is returned.
- Specified by:
registerServicein interfaceBundleContext- 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.svcObj- The service object or an object implementingServiceFactory.dict- The properties for this service. The keys in the properties object must all beStringobjects. SeeConstantsfor 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 benullif the service has no properties.- Returns:
- A
ServiceRegistrationobject for use by the bundle registering the service to update the service's properties or to unregister the service. - See Also:
ServiceRegistration,PrototypeServiceFactory,ServiceFactory
- If
-
registerService
public <S> ServiceRegistration<S> registerService(java.lang.Class<S> clazz, S svcObj, java.util.Dictionary<java.lang.String,?> dict)
Description copied from interface:BundleContextRegisters the specified service object with the specified properties under the name of the specified class with the Framework.This method is otherwise identical to
BundleContext.registerService(String, Object, Dictionary)and is provided to return a type safeServiceRegistration.- Specified by:
registerServicein interfaceBundleContext- Type Parameters:
S- Type of Service.- Parameters:
clazz- The class under whose name the service can be located.svcObj- The service object or an object implementingServiceFactory.dict- The properties for this service.- Returns:
- A
ServiceRegistrationobject for use by the bundle registering the service to update the service's properties or to unregister the service. - See Also:
BundleContext.registerService(String, Object, Dictionary)
-
getServiceReference
public ServiceReference<?> getServiceReference(java.lang.String clazz)
Description copied from interface:BundleContextReturns aServiceReferenceobject for a service that implements and was registered under the specified class.The returned
ServiceReferenceobject 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(String, String)with anullfilter 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_RANKINGproperty) is returned.If there is a tie in ranking, the service with the lowest service id (as specified in its
Constants.SERVICE_IDproperty); that is, the service that was registered first is returned.- Specified by:
getServiceReferencein interfaceBundleContext- Parameters:
clazz- The class name with which the service was registered.- Returns:
- A
ServiceReferenceobject, ornullif no services are registered which implement the named class. - See Also:
BundleContext.getServiceReferences(String, String)
-
getServiceReference
public <S> ServiceReference<S> getServiceReference(java.lang.Class<S> clazz)
Description copied from interface:BundleContextReturns aServiceReferenceobject for a service that implements and was registered under the name of the specified class.The returned
ServiceReferenceobject 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 anullfilter 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_RANKINGproperty) is returned.If there is a tie in ranking, the service with the lowest service id (as specified in its
Constants.SERVICE_IDproperty); that is, the service that was registered first is returned.- Specified by:
getServiceReferencein interfaceBundleContext- Type Parameters:
S- Type of Service.- Parameters:
clazz- The class under whose name the service was registered. Must not benull.- Returns:
- A
ServiceReferenceobject, ornullif no services are registered which implement the specified class. - See Also:
BundleContext.getServiceReferences(Class, String)
-
getBestServiceReference
private ServiceReference getBestServiceReference(ServiceReference[] refs)
-
getAllServiceReferences
public ServiceReference<?>[] getAllServiceReferences(java.lang.String clazz, java.lang.String filter) throws InvalidSyntaxException
Description copied from interface:BundleContextReturns an array ofServiceReferenceobjects. The returned array ofServiceReferenceobjects 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
filterexpression is used to select the registered services whose service properties contain keys and values which satisfy the filter expression. SeeFilterfor a description of the filter syntax. If the specifiedfilterisnull, all registered services are considered to match the filter. If the specifiedfilterexpression cannot be parsed, anInvalidSyntaxExceptionwill be thrown with a human readable message where the filter became unparsable.The result is an array of
ServiceReferenceobjects 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'sobjectClassproperty. - If the specified
filteris notnull, the filter expression must match the service. - If the Java Runtime Environment supports permissions, the caller must
have
ServicePermissionwith theGETaction for at least one of the class names under which the service was registered.
- Specified by:
getAllServiceReferencesin interfaceBundleContext- Parameters:
clazz- The class name with which the service was registered ornullfor all services.filter- The filter expression ornullfor all services.- Returns:
- An array of
ServiceReferenceobjects ornullif no services are registered which satisfy the search. - Throws:
InvalidSyntaxException- If the specifiedfiltercontains an invalid filter expression that cannot be parsed.
- If the specified class name,
-
getServiceReferences
public ServiceReference<?>[] getServiceReferences(java.lang.String clazz, java.lang.String filter) throws InvalidSyntaxException
Description copied from interface:BundleContextReturns an array ofServiceReferenceobjects. The returned array ofServiceReferenceobjects 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
filterexpression is used to select the registered services whose service properties contain keys and values which satisfy the filter expression. SeeFilterfor a description of the filter syntax. If the specifiedfilterisnull, all registered services are considered to match the filter. If the specifiedfilterexpression cannot be parsed, anInvalidSyntaxExceptionwill be thrown with a human readable message where the filter became unparsable.The result is an array of
ServiceReferenceobjects 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'sobjectClassproperty. - If the specified
filteris notnull, the filter expression must match the service. - If the Java Runtime Environment supports permissions, the caller must
have
ServicePermissionwith theGETaction 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'sServiceReferenceobject must returntrue
- Specified by:
getServiceReferencesin interfaceBundleContext- Parameters:
clazz- The class name with which the service was registered ornullfor all services.filter- The filter expression ornullfor all services.- Returns:
- An array of
ServiceReferenceobjects ornullif no services are registered which satisfy the search. - Throws:
InvalidSyntaxException- If the specifiedfiltercontains an invalid filter expression that cannot be parsed.
- If the specified class name,
-
getServiceReferences
public <S> java.util.Collection<ServiceReference<S>> getServiceReferences(java.lang.Class<S> clazz, java.lang.String filter) throws InvalidSyntaxException
Description copied from interface:BundleContextReturns a collection ofServiceReferenceobjects. The returned collection ofServiceReferenceobjects 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
filterexpression is used to select the registered services whose service properties contain keys and values which satisfy the filter expression. SeeFilterfor a description of the filter syntax. If the specifiedfilterisnull, all registered services are considered to match the filter. If the specifiedfilterexpression cannot be parsed, anInvalidSyntaxExceptionwill be thrown with a human readable message where the filter became unparsable.The result is a collection of
ServiceReferenceobjects 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
objectClassproperty. - If the specified
filteris notnull, the filter expression must match the service. - If the Java Runtime Environment supports permissions, the caller must
have
ServicePermissionwith theGETaction 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'sServiceReferenceobject must returntrue
- Specified by:
getServiceReferencesin interfaceBundleContext- Type Parameters:
S- Type of Service- Parameters:
clazz- The class under whose name the service was registered. Must not benull.filter- The filter expression ornullfor all services.- Returns:
- A collection of
ServiceReferenceobjects. May be empty if no services are registered which satisfy the search. - Throws:
InvalidSyntaxException- If the specifiedfiltercontains an invalid filter expression that cannot be parsed.
- 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
public <S> S getService(ServiceReference<S> ref)
Description copied from interface:BundleContextReturns the service object for the service referenced by the specifiedServiceReferenceobject.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
BundleContext.getService(ServiceReference)the context bundle's use count for the service is incremented by one. Each time the service object is released byBundleContext.ungetService(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
nullwhen the service associated with the specifiedreferencehas been unregistered.The following steps are required to get the service object:
- If the service has been unregistered,
nullis returned. - If the context bundle's use count for the service is currently zero
and the service has
bundleorprototypescope, theServiceFactory.getService(Bundle, ServiceRegistration)method is called to supply the service object for the context bundle. If the service object returned by theServiceFactoryobject isnull, not aninstanceofall the classes named when the service was registered or theServiceFactoryobject throws an exception or will be recursively called for the context bundle,nullis returned and a Framework event of typeFrameworkEvent.ERRORcontaining aServiceExceptiondescribing 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.
- Specified by:
getServicein interfaceBundleContext- Type Parameters:
S- Type of Service.- Parameters:
ref- A reference to the service.- Returns:
- A service object for the service associated with
referenceornullif the service is not registered, the service object returned by aServiceFactorydoes not implement the classes under which it was registered or theServiceFactorythrew an exception. - See Also:
BundleContext.ungetService(ServiceReference),ServiceFactory
- If the service has been unregistered,
-
ungetService
public boolean ungetService(ServiceReference<?> ref)
Description copied from interface:BundleContextReleases the service object for the service referenced by the specifiedServiceReferenceobject. 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,
falseis 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
bundleorprototypescope, theServiceFactory.ungetService(Bundle, ServiceRegistration, Object)method is called to release the service object for the context bundle. trueis returned.
- Specified by:
ungetServicein interfaceBundleContext- Parameters:
ref- A reference to the service to be released.- Returns:
falseif the context bundle's use count for the service is zero or if the service has been unregistered;trueotherwise.- See Also:
BundleContext.getService(ServiceReference),ServiceFactory
- If the context bundle's use count for the service is zero or the
service has been unregistered,
-
getDataFile
public java.io.File getDataFile(java.lang.String s)
Description copied from interface:BundleContextCreates aFileobject for a file in the persistent storage area provided for the bundle by the Framework. This method will returnnullif the platform does not have file system support.A
Fileobject 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.FilePermissionwith actionsread,write,deletefor all files (recursively) in the persistent storage area provided for the context bundle.- Specified by:
getDataFilein interfaceBundleContext- Parameters:
s- A relative name to the file to be accessed.- Returns:
- A
Fileobject that represents the requested file ornullif the platform does not have file system support.
-
checkValidity
private void checkValidity()
-
registerService
public <S> ServiceRegistration<S> registerService(java.lang.Class<S> clazz, ServiceFactory<S> factory, java.util.Dictionary<java.lang.String,?> properties)
Description copied from interface:BundleContextRegisters 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
BundleContext.registerService(Class, Object, Dictionary)and is provided to return a type safeServiceRegistrationwhen registering aServiceFactory.- Specified by:
registerServicein interfaceBundleContext- Type Parameters:
S- Type of Service.- Parameters:
clazz- The class under whose name the service can be located.factory- TheServiceFactoryobject.properties- The properties for this service.- Returns:
- A
ServiceRegistrationobject for use by the bundle registering the service to update the service's properties or to unregister the service. - See Also:
BundleContext.registerService(java.lang.Class, org.osgi.framework.ServiceFactory, java.util.Dictionary)
-
getServiceObjects
public <S> ServiceObjects<S> getServiceObjects(ServiceReference<S> ref)
Description copied from interface:BundleContextReturns theServiceObjectsobject for the service referenced by the specifiedServiceReferenceobject.The
ServiceObjectsobject can be used to obtain multiple service objects for services withprototypescope.For services with
singletonorbundlescope, theServiceObjects.getService()method behaves the same as theBundleContext.getService(ServiceReference)method and theServiceObjects.ungetService(Object)method behaves the same as theBundleContext.ungetService(ServiceReference)method. That is, only one, use-counted service object is available from theServiceObjectsobject.This method will always return
nullwhen the service associated with the specifiedreferencehas been unregistered.- Specified by:
getServiceObjectsin interfaceBundleContext- Type Parameters:
S- Type of Service.- Parameters:
ref- A reference to the service.- Returns:
- A
ServiceObjectsobject for the service associated with the specifiedreferenceornullif the service is not registered. - See Also:
BundleContext.getServiceObjects(org.osgi.framework.ServiceReference)
-
-