120 Foreign Application Access Specification

120.1 Introduction

The OSGi Framework contains an advanced collaboration model which provides a publish/find/bind model using services. This OSGi service architecture is not natively supported by foreign application models like MIDP, Xlets, Applets, other Java application models. The purpose of this specification is to enable these foreign applications to participate in the OSGi service oriented architecture.

120.1.1 Essentials

  • Interoperability - Full interoperability between foreign application models and OSGi services is required. This requires both getting services, registering services, and listening to Framework events.

  • No Change - The inter-working specification cannot modify the life cycle model of the foreign application models. The foreign application model specifications cannot be changed.

  • Familiarity - Programmers familiar with a foreign application model should be able to leverage the services architecture without much effort.

  • Simplicity - The programming model for using services must be very simple and not require the programmer to learn many new concepts.

  • Management - Support managing the foreign applications; both through proper OSGi APIs and from a remote management server.

120.1.2 Entities

  • Foreign Application - Java Applications, which must be delivered in JAR files, which are not OSGi bundles.

  • Application Container - An Application Container is responsible for controlling a foreign application and providing the appropriate environment. It must interact with the OSGi Framework to give the foreign application instances access to the OSGi services and package sharing.

  • Application Activator - A class in the foreign application JAR file that is used to notify the application of life cycle changes. One JAR file can contain multiple application activators.

  • Framework - A class that provides access to the application container's application context for a given application activator.

  • Application Context - The interface to the application container's functions to inter-work with the OSGi Framework.

  • Application Declaration - An XML resource that must be placed in the application's JAR file at OSGI-INF/app/apps.xml. This is an optional declaration.

  • Application Instance - A launched application. Most foreign application models permit an application to be launched multiple times.

Figure 120.1 Foreign Applications, org.osgi.application package

Foreign Applications, org.osgi.application package

120.1.3 Synopsis

Foreign application JAR files can be installed in an OSGi Framework as if they were normal bundles. Application containers running on the OSGi Framework must detect the installation of recognized foreign applications and provide a bridge to the OSGi Environment. This bridge can include interaction with the Application Admin Specification, as well as provide access to the OSGi services and Framework events.

The Application container reads the application XML resource from the JAR file and treats the foreign application according to this information. When the foreign application is launched, the application container creates an application instance.

Foreign application instances can get an application context through a static method on the Framework class. The Application Context provides access to getting services, registering services and registering listeners.

The foreign application instance's life cycle can be influenced by the application declaration. If desired, an application can be prevented from launching or stopping when required services are, or become, unavailable.

120.2 Foreign Applications

Foreign applications are Java applications that can be installed and managed through the normal OSGi mechanisms. However, they use another application programming model than the bundle programming model. For example: MIDP, MHP, DOJA.

Foreign applications must fulfill the following requirements to be able to inter-work with the OSGi environment:

  • The applications must be written in Java

  • The applications must be delivered in JAR files. This is the common model for Java applications.

  • They must have a clearly defined life cycle with a start and stop state.

  • One or more classes in the application must be available to start and stop the application. For example the Midlet in MIDP or the Xlet in MHP. This object is called the application's activator. As the application container uses this object for life cycle control of the application, the lifetime of this object equals the lifetime of the application.

Foreign applications are managed by application containers. Application containers provide the environment and life cycle management as defined by foreign application model.

This specification does not require any changes in the foreign application model; existing applications must run unmodified. However, to allow the foreign applications to participate as a first class OSGi citizen, a number of additional artifacts in the JAR file are required. These artifacts use Manifest headers and an XML resource in the applications JAR file; these artifacts are permitted and ignored by the foreign application models that are currently known.

120.2.1 Foreign Metadata

There are different types of metadata associated with application models. Descriptive information, for example the name, icon, documentation etc. of the application, is usually provided in an application model specific way. Application models can also define behavioral metadata, that is, prescribe that the application needs to be started automatically at device startup (auto start) or whether multiple instances of an application can be executed concurrently (singleton). These kinds of metadata are supported by different application models to different extent and are not in the scope of this specification. The application container is responsible for interpreting this metadata and treating the foreign application in the appropriate way.

120.2.2 OSGi Manifest Headers

Foreign applications can import packages by specifying the appropriate OSGi module headers in the manifest. These headers are fully described in OSGi Core Release 7. Their semantics remain unchanged. The following headers must not be used in foreign applications:

  • Export-Package - Exporting packages is forbidden in foreign applications.

  • Bundle-Activator - Foreign applications have their own activator.

  • Service-Component - Service components should be bundles.

Foreign applications that intend to use the OSGi Framework features should have Bundle-SymbolicName and Bundle-Version headers. If they do not have such a header, they can be deployed with Deployment Package, which can assign these headers in the Deployment Package manifest.

Any JAR that uses these headers must not be recognized as a foreign application, even if their manifest is conforming and valid with respect to the foreign application model. This entails that a JAR cannot both be a bundle with activator or exports and a foreign application.

For example, a MIDlet can be extended to import the org.osgi.application package from the OSGi environment. The Import-Package header is used to describe such an import:

Manifest-Version: 1.0
MIDlet-Name: Example
MIDlet-1: Example, , osgi.ExampleMidlet
MIDlet-Version: 1.1.0
MIDlet-Vendor: OSGi
MicroEdition-Configuration: CDC-1.0
MicroEdition-Profile: MIDP-1.0
Bundle-ManifestVersion: 2
Bundle-SymbolicName: osgi.example 
Import-Package: org.osgi.application;version=1.0,
  org.osgi.framework;version=1.3

120.2.3 Interacting with the OSGi Framework

The application container must maintain an application context for each started application, that is, the application instance. This context is related to the application's activator. The Application Context can be acquired using a static getApplicationContext(Object) method on the Framework class. The parameter of this method is the application's activator itself. The getApplicationContext method cannot check if the caller is really the given application; the application activator is therefore a capability, any application that has this object can get the Application Context. The application activator should never be shared with other applications. The Application Context must therefore deny the application activator to be used as a service object.

The getApplicationContext method must not be called from the application activator's constructor; at that time it must not be available yet.

For example, a MIDlet could acquire the application context with the following code:

import org.osgi.framework.*;
import org.osgi.application.*;
import javax.microedition.midlet.*;

public class Example extends MIDlet {
    ApplicationContext     context;
    public void startApp() {
            context = Framework.getApplicationContext(this);
    }

    public void pauseApp() { ... }

    public void destroyApp(boolean unconditional) { ... }
}  

The getApplicationContext method must throw an Illegal Argument Exception if it is called with an object that is not an application's activator.

The ApplicationContext object is singleton for the corresponding application's activator. Subsequent calls to the getApplicationContext method with the same application's activator must return the same ApplicationContext object; therefore, applications are free to forget and get the object any number of times during their lifetime. However, it is an error to get the ApplicationContext object for an application that is already stopped. Existing ApplicationContext objects must be invalidated once the application's activator is stopped.

120.2.4 Introspection

The Application Context provides the following methods about the application:

120.2.5 Access to Services

Foreign applications do not have direct access to the OSGi service registry. However, the application context provides the mechanism to interact with this service registry.

Access to services is more protected and controlled than traditional OSGi access that uses the BundleContext object. The service model is conceptually based on the Declarative Services Specification. It uses the same concepts as that specification. Albeit there are a number of differences due the nature of foreign applications.

Applications can use the locateService or locateServices methods of their associated application context to obtain service objects from the OSGi service registry. Just like OSGi Declarative services, these service objects must be declared a priori in the reference element of the metadata, see Application Descriptor Resource. This metadata declares a number of named references; References contain the criteria which services are eligible for use by the application and how these dependencies should be handled. The foreign application can only use services defined in references; the application context only takes the name of a reference as parameter in the locateService and locateServices methods. That is, a foreign application cannot indiscriminately use the service registry, it is restricted by the application declaration.

A reference selects a subset of services in the service registry. The primary selector is its interface. However, this subset can be further narrowed down with a target filter. The target specifies an OSGi filter expression that is used to additionally qualify the subset of appropriate services.

There are two different methods to access the services selected by the reference:

  • locateService(String) - Return one of the services that is selected by the reference with the given name. If multiple services are selected by the reference, then the service with the highest ranking must be returned. This is compatible with the getServiceReference method in the OSGi Framework's BundleContext class.

  • locateServices(String) - Return all the services that are selected by the reference with the given name.

Once the application instance has obtained a service object, that service is said to be bound to the application instance. There is no method to unbind a service.

For example, a foreign application that wants to log via the Log Service, should declare the following metadata in OSGI-INF/app/apps.xml:

<?xml version="1.0" encoding="UTF-8"?>
    <descriptor xmlns="http://www.osgi.org/xmlns/app/v1.1.0">
        <application class="com.acme.app.SampleMidlet">
        <reference name="log" 
            interface="org.osgi.service.log.LogService"/> 
    </application>
</descriptor>

The code to log could look like:

void log(String msg) {
    ApplicationContext ctxt=    
        Framework.getApplicationContext(this);
    LogService log = (LogService) ctxt.locateService("log");
    log.log( LogService.LOG_INFO, msg );
}

120.2.6 Service Properties

The foreign applications receive the services objects they have access to directly. This means that they cannot access the service properties that are normally associated with the service registrations.

The getServiceProperties(Object) returns a Map object with a copy of these service properties.

120.2.7 Dependencies on Services

The availability of services can influence the life cycle of the foreign application. The life cycle is influenced by the policy and the cardinality.

The policy defines how the unregistration of a bound service must be handled. The following policies are supported:

  • static - The application assumes that bound services will never go away. So if a bound service becomes unregistered, the Application Container must stop the application to prevent it from using a stale service.

  • dynamic - The application must never store service objects and will always get them on demand. Once a service is bound, it can become unregistered without any effect on the application.

Additionally, the cardinality defines if a reference is optional. An optional reference does not influence the life cycle of an application, a mandatory reference does. The cardinality is expressed as one of the following values:

  • 0..1 or 0..n - Optional reference

  • 1..1 or 1..n - Mandatory reference

The multiplicity is only for compatibility with the Declarative Services. Both locateService and locateServices methods can be used regardless of the given multiplicity and return the selected subset for the given reference.

Mandatory references can influence the launching of an application. An application must only be started when a mandatory reference is satisfied. A reference is satisfied when there is at least one registered service selected by the reference.

If a mandatory reference of an application is about to become unsatisfied, due to unregistering a service, the application container must stop the application instance according to corresponding application model semantics.

120.2.8 Registering Services

A common pattern in the OSGi is registering a service to listen to certain events. For example, the Configuration Admin service requires their clients to register a callback Managed Service, so that the service can asynchronously update the client with new configurations. The ApplicationContext interface contains methods that allow the applications to register such services. These services must be automatically unregistered by the application container after the application has been stopped.

The available methods are:

Either method requires that the given object implements all the interfaces that are given. The Dictionary object provides the properties. See the OSGi registerService methods in the BundleContext class. These identical methods specifies the behavior in detail.

The use of the application activator as a service object is explicitly forbidden. Registering the application activator as a service allows other applications in the OSGi environment to access the Application Context using this object and the getApplicationContext method.

Both methods return a ServiceRegistration object that can be used to unregister the service. Services must be automatically unregistered when the application instance is stopped.

120.2.9 Listening to Service Events

The Application Context provides the following methods to listen to service events:

If a Application Service Listener is registered more than once, then the previous registration is removed. Listeners can be removed with removeServiceListener(ApplicationServiceListener). When the application instance is stopped, the listeners are automatically unregistered.

120.2.10 Access to Startup Parameters

Applications can use the getStartupArguments method on the application context to obtain their startup arguments. The startup arguments are represented as map with name and value pairs. The name is a non-null and non-empty ("") String object. The value can be any type of object.

The reason for providing the startup parameters through a special mechanism is that it allows foreign applications access to the parameters of a schedule application, see Scheduling.

This uniform access to the startup parameters provides a uniform way for applications of any foreign application model. This facility does not remove the need for any mechanisms required by the foreign application model for startup parameters access.

120.2.11 Sibling Instances

Most foreign application models allow an application to be launched multiple times, creating multiple instances. In OSGi, a bundle can only be started once, which creates certain assumptions. For example, the Service Factory concept creates a unique service object per bundle.

Each application instance must be seen as a unique bundle while it runs. That is, it should not share anything with other instances. The foreign application container is responsible for this isolation; implementing this isolation requires implementation dependent constructs.

120.3 Application Containers

Application containers:

  • Provide management for the foreign applications

  • Launches application instances in a defined environment

  • Provide a specific a application model context to foreign application instances

  • Interact with the Application Admin service to provide the foreign applications to application managers.

A single OSGi environment can host multiple application containers.

120.3.1 Installation

Applications are installed into the system using OSGi bundle installation mechanism (i.e. installBundle method of the BundleContext interface). This allows including application JARs to Deployment Packages without any changes to the Deployment Package format or Deployment Admin behavior. It also allows the OSGi framework to process the dependency information (the package dependencies) included in the application metadata.

The application container can listen to the BundleEvent.INSTALLED events and examine the installed JARs whether they contain applications supported by the particular container. After the installation, the application container is responsible for registering the corresponding Application Descriptor as defined in the Application Admin Specification. Similarly, the container can recognize the removal of the package by listening to BundleEvent.UNINSTALLED events and then it can unregister the corresponding descriptors. Additionally, application container must check the bundle registry for changes when they are started.

Receiving BundleEvent.INSTALLED events via a Synchronous Bundle Listener makes it possible for the application container to examine the package content during installation. A foreign application must not become available for execution unless it is started as a bundle. This mechanism allows foreign applications to be installed but not yet recognized as a foreign application.

120.4 Application Descriptor Resource

Applications' dependencies on services must be declared in the OSGI-INF/app/apps.xml resource. The XML file must use the http://www.osgi.org/xmlns/app/v1.1.0 namespace. The preferred abbreviation is app. The XML schema definition can be found at Component Description Schema. The apps.xml file is optional if a foreign application does not require any dependencies.

The structure of the XML must conform to the description below.

<descriptor>     ::= <application> +
<application>    ::= <reference> *              

120.4.1 Descriptor Element

The descriptor is the top level element. The descriptor element has no attributes.

120.4.2 Application Element

A JAR file can contain multiple application activators. The application element can therefore be repeated one or more times in the descriptor element.

The application element has the following attribute:

  • class - The class attribute of the application element must contain the fully qualified name of the application's activator.

120.4.3 Reference Element

A reference element represents the applications use of a particular service. All services that an application uses must be declared in a reference element.

A reference element has the following attributes:

  • name - A reference element is identified by a name. This name can be used in the locateService or locateService, see Access to Services. This name must be unique within an application element.

  • interface - The fully qualified name of the interface or class that defines the selected service.

  • policy - The choice of action when a bound services becomes unregistered while an application instance is running. It can have the following values:

    • static - If a bound service becomes unregistered, the application instance must be stopped but the corresponding Application Descriptor is still launchable.

    • dynamic - If a bound service becomes unregistered, the application can continue to run if the mandatory reference can still be satisfied by another service.

  • cardinality - Defines the optionality of the reference. If it starts with a 0, an application can handle that the reference selects no service. That is, locateService method can return a null. If it starts with 1, the reference is mandatory and at least one service must be available before an application instance can be launched. The cardinality can have one of the following values:

    • 0..1 or 0..n - Optional reference

    • 1..1 or 1..n - Mandatory reference

  • target - The optional target attribute of the element can be used to further narrow which services are acceptable for the application by providing an OSGi filter on the properties of the services.

120.4.4 Example XML

The following example is an application declaration for a MIDlet application that depends on the OSGi Log Service and another service:

<?xml version="1.0" encoding="UTF-8"?>
<descriptor xmlns="http://www.osgi.org/xmlns/app/v1.1.0">
  <application class="com.acme.apps.SampleMidlet">
    <reference name="log" interface="org.osgi.service.log"/> 
    <reference name="foo"   
      interface="com.acme.service.FooService" 
      policy="dynamic" 
      cardinality="0..n" /> 
  </application>
</descriptor>

A similar example for an imaginary Xlet, with different dependencies:

<?xml version="1.0" encoding="UTF-8"?>
<descriptor xmlns="http://www.osgi.org/xmlns/app/v1.1.0">
  <application class="com.acme.apps.SampleXlet">
    <reference name="log" interface="org.osgi.service.log"/> 
    <reference name="bar" 
          interface="com.acme.service.BarService"
          policy="static" cardinality="1..n" /> 
  </application>
</descriptor>

120.5 Component Description Schema

This XML Schema defines the component description grammar.

<schema xmlns="http://www.w3.org/2001/XMLSchema"
    xmlns:app="http://www.osgi.org/xmlns/app/v1.1.0"
    targetNamespace="http://www.osgi.org/xmlns/app/v1.1.0"
    version="1.1.1">

    <element name="descriptor" type="app:Tdescriptor">
        <annotation>
            <documentation xml:lang="en">
                descriptor element encloses the application descriptors
                provided in a document
            </documentation>
        </annotation>
    </element>

    <complexType name="Tdescriptor">
        <sequence>
            <element name="application" type="app:Tapplication"
                minOccurs="1" maxOccurs="unbounded" />
            <!-- It is non-deterministic, per W3C XML Schema 1.0: http://www.w3.org/TR/xmlschema-1/#cos-nonambig
                to use namespace="##any" below. -->
            <any namespace="##other" processContents="lax" minOccurs="0"
                maxOccurs="unbounded" />
        </sequence>
        <anyAttribute processContents="lax" />
    </complexType>

    <complexType name="Tapplication">
        <annotation>
            <documentation xml:lang="en">
                describes the service dependencies of an application
            </documentation>
        </annotation>
        <sequence>
            <element name="reference" minOccurs="0"
                maxOccurs="unbounded" type="app:Treference" />
            <!-- It is non-deterministic, per W3C XML Schema 1.0: http://www.w3.org/TR/xmlschema-1/#cos-nonambig
                to use namespace="##any" below. -->
            <any namespace="##other" processContents="lax" minOccurs="0"
                maxOccurs="unbounded" />
        </sequence>
        <attribute name="class" type="string" />
        <anyAttribute processContents="lax" />
    </complexType>

    <complexType name="Treference">
        <sequence>
            <any namespace="##any" processContents="lax" minOccurs="0"
                maxOccurs="unbounded" />
        </sequence>
        <attribute name="name" type="NMTOKEN" use="required" />
        <attribute name="interface" type="string" use="required" />
        <attribute name="cardinality" default="1..1" use="optional"
            type="app:Tcardinality" />
        <attribute name="policy" use="optional" default="static"
            type="app:Tpolicy" />
        <attribute name="target" type="string" use="optional" />
        <anyAttribute processContents="lax" />
    </complexType>

    <simpleType name="Tcardinality">
        <restriction base="string">
            <enumeration value="0..1" />
            <enumeration value="0..n" />
            <enumeration value="1..1" />
            <enumeration value="1..n" />
        </restriction>
    </simpleType>

    <simpleType name="Tpolicy">
        <restriction base="string">
            <enumeration value="static" />
            <enumeration value="dynamic" />
        </restriction>
    </simpleType>
    
    <attribute name="must-understand" type="boolean">
        <annotation>
            <documentation xml:lang="en">
                This attribute should be used by extensions to documents
                to require that the document consumer understand the
                extension.
            </documentation>
        </annotation>
    </attribute>
</schema>

120.6 Security

120.6.1 Application Context Access

The getApplicationContext method provides access to the Application Context of a given application activator. The application activator is therefore a capability; any party that has access to this object can potentially get its related Application Context and use it in intended ways.

A common pattern in small applications is to (ab)use the application activator class for all tasks, among them as service object. However, registering the application activator as a service will allow any party that can use that service to use it as the parameter to the getApplicationContext method.

The Application Context must therefore be protected to not allow the registration of the application activator.

120.6.2 Signing

Application models can include the definition of a security model. For example, MIDP 2 defines a security model different from the standard Java 2 security model. If the foreign application model defines a security model different from Java 2 security, then it is the responsibility of the application container to implement this model and enforce it.

OSGi services are protected by Java 2 permissions. Applications wishing to use such services must have the appropriate permissions for those services.

Java 2 permissions are assigned during class loading based on the location of the code, the JAR signatures, and possibly based on other conditions, when using the Conditional Permission framework.

Signing is a very common technique to handle the granting of permissions. It requires that the JAR be signed according to the JAR Signing model. Therefore, OSGi-aware application packages should be signed by JAR signing. However, some foreign application models have alternative signing models in place. However, it is unlikely that this conflicts because JAR signing uses well defined separate files and manifest headers. If the foreign application model changes the JAR file outside the META-INF directory, then the signing according to the foreign application model must be performed before the standard JAR signing.

For example, in the case of MIDP signing and both models are used, the JAR signature should be put to the file first as it modifies the content of the file, and MIDP signing should be applied afterwards.

120.6.3 Permission Management

Applications that use OSGi services must have the corresponding Java 2 permissions granted. In order to simplify the policy management, and ensure that the overall device policy is consistent, application containers should not define separate policy management for each application model; rather they should use the existing OSGi policy management and express the complete security policy by the means of Java 2 permissions with the Conditional Permission Admin service. This way, policy administrator can define the boundaries of the sandbox available for a particular application based on its location, signer or other condition. The application container is responsible for enforcing both the foreign application specific security mechanisms as well as the OSGi granted permissions.

Applications can package permissions as described in the Conditional Permission Admin. These permissions will restrict the foreign application's permissions to maximally the permissions in this file scoped by the signer's permissions.

120.7 org.osgi.application

Version 1.0

Foreign Application Package Version 1.0.

Bundles wishing to use this package must list the package in the Import-Package header of the bundle's manifest. This package has two types of users: the consumers that use the API in this package and the providers that implement the API in this package.

Example import for consumers using the API in this package:

Import-Package: org.osgi.application; version="[1.0,2.0)"

Example import for providers implementing the API in this package:

Import-Package: org.osgi.application; version="[1.0,1.1)"

120.7.1 Summary

120.7.2 public interface ApplicationContext

ApplicationContext is the access point for an OSGi-aware application to the features of the OSGi Service Platform. Each application instance will have its own ApplicationContext instance, which will not be reused after destroying the corresponding application instance.

Application instances can obtain their ApplicationContext using the Framework.getApplicationContext(Object) method.

The lifecycle of an ApplicationContext instance is bound to the lifecycle of the corresponding application instance. The ApplicationContext becomes 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 (except getApplicationId() and getInstanceId()) to an invalidated context object result an IllegalStateException.

org.osgi.application.Framework

120.7.2.1 public void addServiceListener(ApplicationServiceListener listener, String referenceName) throws IllegalArgumentException

The org.osgi.application.ApplicationServiceListener to be added. It must not be null

the reference name of a service from the descriptor of the corresponding application. It must not be null.

Adds the specified ApplicationServiceListener object to this context application instance's list of listeners. The specified referenceName is a reference name specified in the descriptor of the corresponding application. The registered listener will only receive the ApplicationServiceEvents related to the referred service.

If the listener was already added, calling this method will overwrite the previous registration.

IllegalStateException– If this context application instance has stopped.

NullPointerException– If listener or referenceName is null

IllegalArgumentException– If there is no service in the application descriptor with the specified referenceName.

120.7.2.2 public void addServiceListener(ApplicationServiceListener listener, String[] referenceNames) throws IllegalArgumentException

The org.osgi.application.ApplicationServiceListener to be added. It must not be null

and array of service reference names from the descriptor of the corresponding application. It must not be null and it must not be empty.

Adds the specified ApplicationServiceListener object to this context application instance's list of listeners. The referenceNames parameter is an array of reference name specified in the descriptor of the corresponding application. The registered listener will only receive the ApplicationServiceEvents related to the referred services.

If the listener was already added, calling this method will overwrite the previous registration.

IllegalStateException– If this context application instance has stopped.

NullPointerException– If listener or referenceNames is null

IllegalArgumentException– If referenceNames array is empty or it contains unknown references

120.7.2.3 public 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 ApplicationContext as well.

the identifier of the application type.

org.osgi.service.application.ApplicationDescriptor.getApplicationId()

120.7.2.4 public 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 invalid ApplicationContext as well.

the unique identifier of the corresponding application instance

org.osgi.service.application.ApplicationHandle.getInstanceId()

120.7.2.5 public Map getServiceProperties(Object serviceObject)

A service object the application is bound to. It must not be null.

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 calling locateService or locateServices methods.

The service properties associated with the specified service object.

NullPointerException– if the specified serviceObject is null

IllegalArgumentException– if the application is not bound to the specified service object or it is not a service object at all.

IllegalStateException– If this context application instance has stopped.

120.7.2.6 public Map getStartupParameters()

Returns the startup parameters specified when calling the org.osgi.service.application.ApplicationDescriptor#launch(Map) method.

Startup arguments can be specified as name, value pairs. The name must be of type java.lang.String, which must not be null or empty java.lang.String (""), the value can be any object including null.

a java.util.Map containing the startup arguments. It can be null.

IllegalStateException– If this context application instance has stopped.

120.7.2.7 public Object locateService(String referenceName)

The name of a reference as specified in a reference element in this context application's description. It must not be null

This method returns the service object for the specified referenceName. 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 its org.osgi.framework.Constants.SERVICE_RANKING property) is returned. If there is a tie in ranking, the service with the lowest service ID (as specified in its org.osgi.framework.Constants.SERVICE_ID property); that is, the service that was registered first is returned.

A service object for the referenced service or null if the reference cardinality is 0..1 or 0..n and no bound service is available.

NullPointerException– If referenceName is null.

IllegalArgumentException– If there is no service in the application descriptor with the specified referenceName.

IllegalStateException– If this context application instance has stopped.

120.7.2.8 public Object[] locateServices(String referenceName)

The name of a reference as specified in a reference element in this context application's description. It must not be null.

This method returns the service objects for the specified referenceName.

An array of service object for the referenced service or null if the reference cardinality is 0..1 or 0..n and no bound service is available.

NullPointerException– If referenceName is null.

IllegalArgumentException– If there is no service in the application descriptor with the specified referenceName.

IllegalStateException– If this context application instance has stopped.

120.7.2.9 public ServiceRegistration registerService(String[] clazzes, Object service, Dictionary properties)

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 org.osgi.framework.Constants.OBJECTCLASS. This parameter must not be null.

The service object or a ServiceFactory object.

The properties for this service. The keys in the properties object must all be String objects. See org.osgi.framework.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 org.osgi.framework.ServiceRegistration.setProperties(Dictionary) method must be called. The set of properties may be null if the service has no properties.

Registers the specified service object with the specified properties under the specified class names into the Framework. A org.osgi.framework.ServiceRegistration object is returned. The org.osgi.framework.ServiceRegistration object 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 the org.osgi.framework.BundleContext.getServiceReferences(String, String) or org.osgi.framework.BundleContext.getServiceReference(String) method. Other applications can locate this service by using locateService(String) or locateServices(String) method, if they declared their dependence on the registered service.

An application can register a service object that implements the org.osgi.framework.ServiceFactory interface to have more flexibility in providing service objects to other applications or bundles.

The following steps are required to register a service:

  1. If service is not a ServiceFactory, an IllegalArgumentException is thrown if service is not an instanceof all the classes named.

  2. The Framework adds these service properties to the specified Dictionary (which may be null): a property named org.osgi.framework.Constants.SERVICE_ID identifying the registration number of the service and a property named org.osgi.framework.Constants.OBJECTCLASS containing 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.

  3. The service is added to the Framework service registry and may now be used by others.

  4. A service event of type org.osgi.framework.ServiceEvent.REGISTERED is fired. This event triggers the corresponding ApplicationServiceEvent to be delivered to the applications that registered the appropriate listener.

  5. A ServiceRegistration object for this registration is returned.

A org.osgi.framework.ServiceRegistration object for use by the application registering the service to update the service's properties or to unregister the service.

IllegalArgumentException– If one of the following is true:

  • service is null.

  • service is not a ServiceFactory object and is not an instance of all the named classes in clazzes.

  • properties contains case variants of the same key name.

NullPointerException– if clazzes is null

SecurityException– If the caller does not have the ServicePermission to register the service for all the named classes and the Java Runtime Environment supports permissions.

IllegalStateException– If this ApplicationContext is no longer valid.

org.osgi.framework.BundleContext.registerService(java.lang.String[], java.lang.Object, java.util.Dictionary), org.osgi.framework.ServiceRegistration, org.osgi.framework.ServiceFactory

120.7.2.10 public ServiceRegistration registerService(String clazz, Object service, Dictionary properties)

The class name under which the service can be located. It must not be null

The service object or a ServiceFactory object.

The properties for this service.

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 when service will only be registered under a single class name. Note that even in this case the value of the service's Constants.OBJECTCLASS property will be an array of strings, rather than just a single string.

A ServiceRegistration object for use by the application registering the service to update the service's properties or to unregister the service.

IllegalArgumentException– If one of the following is true:

  • service is null.

  • service is not a ServiceFactory object and is not an instance of the named class in clazz.

  • properties contains case variants of the same key name.

NullPointerException– if clazz is null

SecurityException– If the caller does not have the ServicePermission to register the service the named class and the Java Runtime Environment supports permissions.

IllegalStateException– If this ApplicationContext is no longer valid.

registerService(java.lang.String[], java.lang.Object, java.util.Dictionary)

120.7.2.11 public void removeServiceListener(ApplicationServiceListener listener)

The org.osgi.application.ApplicationServiceListener object to be removed.

Removes the specified org.osgi.application.ApplicationServiceListener object from this context application instance's list of listeners.

If listener is not contained in this context application instance's list of listeners, this method does nothing.

IllegalStateException– If this context application instance has stopped.

120.7.3 public class ApplicationServiceEvent
extends ServiceEvent

An event from the Framework describing a service lifecycle change.

ApplicationServiceEvent objects are delivered to a ApplicationServiceListener objects when a change occurs in this service's lifecycle. The delivery of an ApplicationServiceEvent is always triggered by a org.osgi.framework.ServiceEvent. ApplicationServiceEvent extends the content of ServiceEvent with the service object the event is referring to as applications has no means to find the corresponding service object for a org.osgi.framework.ServiceReference. A type code is used to identify the event type for future extendability. The available type codes are defined in org.osgi.framework.ServiceEvent.

OSGi Alliance reserves the right to extend the set of types.

org.osgi.framework.ServiceEvent, ApplicationServiceListener

120.7.3.1 public ApplicationServiceEvent(int type, ServiceReference reference, Object serviceObject)

The event type. Available type codes are defines in org.osgi.framework.ServiceEvent

A ServiceReference object to the service that had a lifecycle change. This reference will be used as the source in the java.util.EventObject base class, therefore, it must not be null.

The service object bound to this application instance. It can be null if this application is not bound to this service yet.

Creates a new application service event object.

IllegalArgumentException– if the specified reference is null.

120.7.3.2 public Object getServiceObject()

This method returns the service object of this service bound to the listener application instance. A service object becomes bound to the application when it first obtains a service object reference to that service by calling the ApplicationContext.locateService or locateServices methods. If the application is not bound to the service yet, this method returns null.

the service object bound to the listener application or null if it isn't bound to this service yet.

120.7.4 public interface ApplicationServiceListener
extends EventListener

An ApplicationServiceEvent listener. When a ServiceEvent is fired, it is converted to an ApplictionServiceEvent and it is synchronously delivered to an ApplicationServiceListener.

ApplicationServiceListener is a listener interface that may be implemented by an application developer.

An ApplicationServiceListener object is registered with the Framework using the ApplicationContext.addServiceListener method. ApplicationServiceListener objects are called with an ApplicationServiceEvent object when a service is registered, modified, or is in the process of unregistering.

ApplicationServiceEvent object delivery to ApplicationServiceListener objects is filtered by the filter specified when the listener was registered. If the Java Runtime Environment supports permissions, then additional filtering is done. ApplicationServiceEvent objects are only delivered to the listener if the application which defines the listener object's class has the appropriate ServicePermission to get the service using at least one of the named classes the service was registered under, and the application specified its dependence on the corresponding service in the application metadata.

ApplicationServiceEvent object delivery to ApplicationServiceListener objects is further filtered according to package sources as defined in ServiceReference.isAssignableTo(Bundle, String).

ApplicationServiceEvent, ServicePermission

120.7.4.1 public void serviceChanged(ApplicationServiceEvent event)

The ApplicationServiceEvent object.

Receives notification that a service has had a lifecycle change.

120.7.5 public final class Framework

Using this class, OSGi-aware applications can obtain their ApplicationContext.

120.7.5.1 public static ApplicationContext getApplicationContext(Object applicationInstance)

is the activator object of an application instance

This method needs an argument, an object that represents the application instance. An application consists of a set of object, however there is a single object, which is used by the corresponding application container to manage the lifecycle on the application instance. The lifetime of this object equals the lifetime of the application instance; therefore, it is suitable to represent the instance.

The returned ApplicationContext object is singleton for the specified application instance. Subsequent calls to this method with the same application instance must return the same context object

the ApplicationContext of the specified application instance.

NullPointerException– If applicationInstance is null

IllegalArgumentException– if called with an object that is not the activator object of an application.