Configurable filter for OSGi service reference in blueprint

70 Views Asked by At

I develop an OSGi-based application running in apache karaf.

I have several OSGi-services, that implement the same interface. Each of them reads data from a different source. In many other bundles, I just get a reference to one of these reader-services. Usually the referenced service is fix, so I use a specific service-filter in the blueprint-file.

But for testing-purposes I sometimes have to use a different service for one bundle. Is it possible to make the service-filter configurable, so that I just have to change a config-file at runtime? If it is not possible do you have other ideas how to change the service without rebuilding the bundles?

Here is an example blueprint-file

<?xml version="1.0" encoding="UTF-8"?>
<blueprint xmlns="www.osgi.org/xmlns/blueprint/v1.0.0" xmlns:xsi="www.w3.org/2001/XMLSchema-instance"
           xmlns:cm="aries.apache.org/blueprint/xmlns/blueprint-cm/v1.1.0"
           xmlns:ext="aries.apache.org/blueprint/xmlns/blueprint-ext/v1.0.0"
           xsi:schemaLocation="www.osgi.org/xmlns/blueprint/v1.0.0 www.osgi.org/xmlns/blueprint/v1.0.0/blueprint.xsd
                               camel.apache.org/schema/blueprint camel.apache.org/schema/blueprint/camel-blueprint.xsd">

  <ext:property-placeholder/>

  <cm:property-placeholder persistent-id="com.example.MyBundle"
                           placeholder-prefix="$(" placeholder-suffix=")" update-strategy="reload">
    <cm:default-properties>
      <cm:property name="readServiceName" value="local-read"/>
    </cm:default-properties>
  </cm:property-placeholder>

  <reference id="MyBundleReadService" interface="com.example.MyInterface" availability="mandatory" filter="(name=$(readServiceName)"/>
</blueprint>

Hint

I had to remove all http:// from the <blueprint...>-tag because otherwise my question was marked as spam and was not published.

0

There are 0 best solutions below