Writing extensible OSGi applications
If you are writing an OSGi application that is designed to be extensible, you must ensure that your service references are declared such that only the required bundles are provisioned.
This topic describes common scenarios for extensible application design, and provides guidance on how to declare the associated service references to ensure the correct provisioning of bundles.
Scenario 1
Your application is designed to be extended by one or more services that implement a specific interface, but no such services are implemented in the application when it is first deployed.
Define a <reference-list> element in the Blueprint XML file of the client bundle, with the availability attribute set to "optional". Then, when the application is later extended by adding a composite bundle that contains one or more services that implement that interface, the composite bundle will be deployed when the application is updated.
Example
<reference-list id="paymentProviderRef"
interface="com.myco.providers.PaymentProvider"
availability="optional" />
Scenario 2
- A <reference> element for the built-in service, with the availability attribute set to "mandatory".
- A <reference-list> element for the services that will be added later, with the availability attribute set to "optional".
Example
An application is designed to be extended by one or more services that implement the PaymentProvider interface. One such service, CardSample, is built into the application when it is first deployed.
<service id="builtInPaymentProvider" ref="cardSample"
interface="com.myco.providers.PaymentProvider>
<service-properties>
<entry key="type" value="built-in">
</service-properties>
</service>
<bean id="cardSample" class="com.myco.providers.CardSampleImpl" />
The service reference for the built-in service specifies a filter attribute for the custom service property; this ensures that only the built-in service is deployed when the application is deployed.
<reference id="builtInPaymentProviderRef"
interface="com.myco.providers.PaymentProvider"
availability="mandatory"
filter="(type=built-in)"/>
<reference-list id="extensionPaymentProviderRef"
interface="com.myco.providers.PaymentProvider"
availability="optional" />