OSGi Component's default configuration value contains escaped backslash

1.6k views Asked by At

Below is a code of my simple OSGi component

@Component(metatype = true, label = "My Component", policy = ConfigurationPolicy.REQUIRE)
@Property(label = "My Component's expression", name = "my.expression", value = "/5 * * * * ? *")
public class MyComponent {

    private static final Logger log = LoggerFactory.getLogger(MyComponent.class);

    @Reference
    private Scheduler scheduler;

    @Activate
    public void activate(final ComponentContext context) {
        final String quartzExpression = PropertiesUtil.toString(
                context.getProperties().get("my.expression"), "");
        ScheduleOptions options = scheduler.EXPR(quartzExpression).name("MyJob");
        scheduler.schedule(new Runnable() {

            @Override
            public void run() {
                log.info("Hello World!");

            }
        }, options);
    }
}

What it is supposed to do: it takes a configuration value that is a Quartz expression and uses it to schedule a job that logs a simple greeting.

By default, I want the job to be executed every 5 seconds, which the default value of the property states. Now, since I also specify the ConfigurationPolicy.REQUIRE, I actually need to create the config. To do that, I go to the OSGi console's config manager, find My Component on the list and click it. At this moment, a request is sent to fetch the config dialog data. The response contains stuff like labels, names and, also, default value. In my case, the default value is returned as /5\\ *\\ *\\ *\\ *\\ ?\\ *, and is displayed on the dialog as /5\ *\ *\ *\ *\ ?\ *.

Now, if I manually remove all the backslashes, the configuration is saved, is later extracted properly and makes the component work as expected. My question is, why is the default value specified in the code not the same as what I receive from OSGi in the config manager?

Edit: after further investigation, I think it might be caused by Maven SCR Plugin that I use to generate component descriptors. It generates those two files: .\target\classes\OSGI-INF\MyComponent.xml:

<?xml version="1.0" encoding="UTF-8"?><components xmlns:scr="http://www.osgi.org/xmlns/scr/v1.1.0">
    <scr:component name="MyComponent" configuration-policy="require" activate="activate">
        <implementation class="MyComponent"/>
        <property name="my.expression" value="/5 * * * * ? *"/>
        <property name="service.pid" value="MyComponent"/>
        <reference name="scheduler" interface="org.apache.sling.commons.scheduler.Scheduler" cardinality="1..1" policy="static" bind="bindScheduler" unbind="unbindScheduler"/>
    </scr:component>
</components>

And this one: .\target\classes\OSGI-INF\metatype\MyComponent.xml

<?xml version="1.0" encoding="UTF-8"?><metatype:MetaData xmlns:metatype="http://www.osgi.org/xmlns/metatype/v1.0.0" localization="OSGI-INF/metatype/MyComponent">
    <OCD id="MyComponent" name="%MyComponent.name" description="%MyComponent">
        <AD id="my.expression" type="String" name="%MyComponent.my.expression" />
    </OCD>
    <Designate pid="MyComponent">
        <Object ocdref="MyComponent"/>
    </Designate>
</metatype:MetaData>

It looks like the metatype metadata is not generated properly.

1

There are 1 answers

3
CptBartender On

This is actually an expected behavior of the Maven SCR Plugin that was implemented over two years ago to solve problem with encoding regular expressions as the default values - commit can be found here.

Found it.