Interface ApplicationContext
-
public interface ApplicationContextApplicationContextis the access point for an OSGi-aware application to the features of the OSGi Service Platform. Each application instance will have its ownApplicationContextinstance, which will not be reused after destroying the corresponding application instance.Application instances can obtain their
ApplicationContextusing theFramework.getApplicationContext(Object)method.The lifecycle of an
ApplicationContextinstance is bound to the lifecycle of the corresponding application instance. TheApplicationContextbecomes available when the application is started and it is invalidated when the application instance is stopped (i.e. the "stop" method of the application activator object returned). All method calls (exceptgetApplicationId()andgetInstanceId()) to an invalidated context object result anIllegalStateException.- See Also:
Framework
-
-
Method Summary
All Methods Instance Methods Abstract Methods Modifier and Type Method Description voidaddServiceListener(ApplicationServiceListener listener, java.lang.String referenceName)Adds the specifiedApplicationServiceListenerobject to this context application instance's list of listeners.voidaddServiceListener(ApplicationServiceListener listener, java.lang.String[] referenceNames)Adds the specifiedApplicationServiceListenerobject to this context application instance's list of listeners.java.lang.StringgetApplicationId()This method return the identifier of the corresponding application type.java.lang.StringgetInstanceId()This method returns the identifier of the corresponding application instance.java.util.MapgetServiceProperties(java.lang.Object serviceObject)Application can query the service properties of a service object it is bound to.java.util.MapgetStartupParameters()Returns the startup parameters specified when calling theorg.osgi.service.application.ApplicationDescriptor#launch(Map)method.java.lang.ObjectlocateService(java.lang.String referenceName)This method returns the service object for the specifiedreferenceName.java.lang.Object[]locateServices(java.lang.String referenceName)This method returns the service objects for the specifiedreferenceName.org.osgi.framework.ServiceRegistrationregisterService(java.lang.String[] clazzes, java.lang.Object service, java.util.Dictionary properties)Registers the specified service object with the specified properties under the specified class names into the Framework.org.osgi.framework.ServiceRegistrationregisterService(java.lang.String clazz, java.lang.Object service, java.util.Dictionary properties)Registers the specified service object with the specified properties under the specified class name with the Framework.voidremoveServiceListener(ApplicationServiceListener listener)Removes the specifiedApplicationServiceListenerobject from this context application instance's list of listeners.
-
-
-
Method Detail
-
addServiceListener
void addServiceListener(ApplicationServiceListener listener, java.lang.String referenceName) throws java.lang.IllegalArgumentException
Adds the specifiedApplicationServiceListenerobject to this context application instance's list of listeners. The specifiedreferenceNameis a reference name specified in the descriptor of the corresponding application. The registeredlistenerwill only receive theApplicationServiceEvents related to the referred service.If the
listenerwas already added, calling this method will overwrite the previous registration.- Parameters:
listener- TheApplicationServiceListenerto be added. It must not benullreferenceName- the reference name of a service from the descriptor of the corresponding application. It must not benull.- Throws:
java.lang.IllegalStateException- If this context application instance has stopped.java.lang.NullPointerException- IflistenerorreferenceNameisnulljava.lang.IllegalArgumentException- If there is no service in the application descriptor with the specifiedreferenceName.
-
addServiceListener
void addServiceListener(ApplicationServiceListener listener, java.lang.String[] referenceNames) throws java.lang.IllegalArgumentException
Adds the specifiedApplicationServiceListenerobject to this context application instance's list of listeners. ThereferenceNamesparameter is an array of reference name specified in the descriptor of the corresponding application. The registeredlistenerwill only receive theApplicationServiceEvents related to the referred services.If the
listenerwas already added, calling this method will overwrite the previous registration.- Parameters:
listener- TheApplicationServiceListenerto be added. It must not benullreferenceNames- and array of service reference names from the descriptor of the corresponding application. It must not benulland it must not be empty.- Throws:
java.lang.IllegalStateException- If this context application instance has stopped.java.lang.NullPointerException- IflistenerorreferenceNamesisnulljava.lang.IllegalArgumentException- IfreferenceNamesarray is empty or it contains unknown references
-
removeServiceListener
void removeServiceListener(ApplicationServiceListener listener)
Removes the specifiedApplicationServiceListenerobject from this context application instance's list of listeners.If
listeneris not contained in this context application instance's list of listeners, this method does nothing.- Parameters:
listener- TheApplicationServiceListenerobject to be removed.- Throws:
java.lang.IllegalStateException- If this context application instance has stopped.
-
getInstanceId
java.lang.String getInstanceId()
This method returns the identifier of the corresponding application instance. This identifier is guaranteed to be unique within the scope of the device. Note: this method can safely be called on an invalidApplicationContextas well.- Returns:
- the unique identifier of the corresponding application instance
- See Also:
ApplicationHandle.getInstanceId()
-
getApplicationId
java.lang.String getApplicationId()
This method return the identifier of the corresponding application type. This identifier is the same for the different instances of the same application but it is different for different application type.Note: this method can safely be called on an invalid
ApplicationContextas well.- Returns:
- the identifier of the application type.
- See Also:
ApplicationDescriptor.getApplicationId()
-
locateService
java.lang.Object locateService(java.lang.String referenceName)
This method returns the service object for the specifiedreferenceName. If the cardinality of the reference is 0..n or 1..n and multiple services are bound to the reference, the service with the highest ranking (as specified in itsConstants.SERVICE_RANKINGproperty) is returned. If there is a tie in ranking, the service with the lowest service ID (as specified in itsConstants.SERVICE_IDproperty); that is, the service that was registered first is returned.- Parameters:
referenceName- The name of a reference as specified in a reference element in this context application's description. It must not benull- Returns:
- A service object for the referenced service or
nullif the reference cardinality is 0..1 or 0..n and no bound service is available. - Throws:
java.lang.NullPointerException- IfreferenceNameisnull.java.lang.IllegalArgumentException- If there is no service in the application descriptor with the specifiedreferenceName.java.lang.IllegalStateException- If this context application instance has stopped.
-
locateServices
java.lang.Object[] locateServices(java.lang.String referenceName)
This method returns the service objects for the specifiedreferenceName.- Parameters:
referenceName- The name of a reference as specified in a reference element in this context application's description. It must not benull.- Returns:
- An array of service object for the referenced service or
nullif the reference cardinality is 0..1 or 0..n and no bound service is available. - Throws:
java.lang.NullPointerException- IfreferenceNameisnull.java.lang.IllegalArgumentException- If there is no service in the application descriptor with the specifiedreferenceName.java.lang.IllegalStateException- If this context application instance has stopped.
-
getStartupParameters
java.util.Map getStartupParameters()
Returns the startup parameters specified when calling theorg.osgi.service.application.ApplicationDescriptor#launch(Map)method.Startup arguments can be specified as name, value pairs. The name must be of type
String, which must not benullor emptyString(""), the value can be any object includingnull.- Returns:
- a
Mapcontaining the startup arguments. It can benull. - Throws:
java.lang.IllegalStateException- If this context application instance has stopped.
-
getServiceProperties
java.util.Map getServiceProperties(java.lang.Object serviceObject)
Application can query the service properties of a service object it is bound to. Application gets bound to a service object when it first obtains a reference to the service by callinglocateServiceorlocateServicesmethods.- Parameters:
serviceObject- A service object the application is bound to. It must not be null.- Returns:
- The service properties associated with the specified service object.
- Throws:
java.lang.NullPointerException- if the specifiedserviceObjectisnulljava.lang.IllegalArgumentException- if the application is not bound to the specified service object or it is not a service object at all.java.lang.IllegalStateException- If this context application instance has stopped.
-
registerService
org.osgi.framework.ServiceRegistration registerService(java.lang.String[] clazzes, java.lang.Object service, java.util.Dictionary properties)Registers 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 application registering the service and should not be shared with other applications. The registering application is defined to be the context application. Bundles can locate the service by using either theBundleContext.getServiceReferences(String, String)orBundleContext.getServiceReference(String)method. Other applications can locate this service by usinglocateService(String)orlocateServices(String)method, if they declared their dependence on the registered service.An application can register a service object that implements the
ServiceFactoryinterface to have more flexibility in providing service objects to other applications or bundles.The following steps are required to register a service:
- If
serviceis not aServiceFactory, anIllegalArgumentExceptionis thrown ifserviceis not aninstanceofall the classes named. - The Framework adds these service properties to the specified
Dictionary(which may benull): a property namedConstants.SERVICE_IDidentifying the registration number of the service and a property namedConstants.OBJECTCLASScontaining all the specified classes. If any of these properties have already been specified by the registering bundle, their values will be overwritten by the Framework. - The service is added to the Framework service registry and may now be used by others.
- A service event of type
ServiceEvent.REGISTEREDis fired. This event triggers the correspondingApplicationServiceEventto be delivered to the applications that registered the appropriate listener. - A
ServiceRegistrationobject 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. This parameter must not benull.service- The service object or aServiceFactoryobject.properties- 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 application registering the service to update the service's properties or to unregister the service. - Throws:
java.lang.IllegalArgumentException- If one of the following is true:serviceisnull.serviceis not aServiceFactoryobject and is not an instance of all the named classes inclazzes.propertiescontains case variants of the same key name.
java.lang.NullPointerException- ifclazzesisnulljava.lang.SecurityException- If the caller does not have theServicePermissionto register the service for all the named classes and the Java Runtime Environment supports permissions.java.lang.IllegalStateException- If this ApplicationContext is no longer valid.- See Also:
BundleContext.registerService(java.lang.String[], java.lang.Object, java.util.Dictionary),ServiceRegistration,ServiceFactory
- If
-
registerService
org.osgi.framework.ServiceRegistration registerService(java.lang.String clazz, java.lang.Object service, java.util.Dictionary 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(java.lang.String[], java.lang.Object, java.util.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 strings, rather than just a single string.- Parameters:
clazz- The class name under which the service can be located. It must not benullservice- The service object or aServiceFactoryobject.properties- The properties for this service.- Returns:
- A
ServiceRegistrationobject for use by the application registering the service to update the service's properties or to unregister the service. - Throws:
java.lang.IllegalArgumentException- If one of the following is true:serviceisnull.serviceis not aServiceFactoryobject and is not an instance of the named class inclazz.propertiescontains case variants of the same key name.
java.lang.NullPointerException- ifclazzisnulljava.lang.SecurityException- If the caller does not have theServicePermissionto register the service the named class and the Java Runtime Environment supports permissions.java.lang.IllegalStateException- If this ApplicationContext is no longer valid.- See Also:
registerService(java.lang.String[], java.lang.Object, java.util.Dictionary)
-
-