You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@karaf.apache.org by Steinar Bang <sb...@dod.no> on 2017/12/06 19:43:27 UTC

How's config connected to a bundle

How's karaf config connected to a bundle?

I tried matching on bundle name.  Ie. I have a bundle with symbolic-name
no.priv.bang.sonar.sonar-collector-webhook so I tried creating a config
with a PID identical to this, ie.
 config:edit no.priv.bang.sonar.sonar-collector-webhook
 config:property-set sonar.collector.jdbc.url "jdbc:postgresql:///sonarcollector"
 config:property-set sonar.collector.jdbc.user "karaf"
 config:update

but the resulting config isn't fed to a component in the bundle.  All I
get there, is the stuff sett in the attributes.

Ie. what I received in the activate method is this:
 {alias=/sonar-collector, component.name=no.priv.bang.sonar.collector.webhook.SonarCollectorServlet, component.id=2}

I tried adding the pid like I found in this example:
 https://github.com/paremus/hello-examples/blob/37c9b2b1ac430227f0d92bf336be6cf5a59c079a/helloworld/helloworld-ds/src/main/java/com/example/hello/impl/GreetingImpl.java#L19
ie. like so:
 @Component(service={Servlet.class}, property={"alias=/sonar-collector"}, configurationPid = "no.priv.bang.sonar.sonar-collector-webhook" )
 public class SonarCollectorServlet extends HttpServlet {
    ...
    @Activate
    public void activate(Map<String, Object> config) {
    ...
but that didn't help.

The config can be found but show up as without a null BundleLocation:
 karaf@root()> config:list "(service.pid=no.priv.bang.sonar.sonar-collector-webhook)"
 ----------------------------------------------------------------
 Pid:            no.priv.bang.sonar.sonar-collector-webhook
 BundleLocation: null
 Properties:
    service.pid = no.priv.bang.sonar.sonar-collector-webhook
    sonar.collector.jdbc.url = jdbc:postgresql:///sonarcollector
    sonar.collector.jdbc.user = karaf
 karaf@root()>

Would it have helped if my bundle had shown up in the BundleLocation?
What does it take to get a bundle there?

Thanks!


- Steinar


Re: How's config connected to a bundle

Posted by Steinar Bang <sb...@dod.no>.
>>>>> David Jencks <da...@gmail.com>:

> I’d expect your component with a specified PID to work, are you sure
> it didn’t?  It turns out that DS cannot reliably set the bundle
> location; when it tried to it turns out there is an unavoidable race
> condition.  Therefore DS does not try to set the bundle location, so
> don’t expect it to change.

Right! So looking at the BundleLocation was just a red herring...?

> I generally use the default component pid (the fully qualified class
> name or component name if specified) or give it a name distinct from
> the bundle name unless the config is shared by several components in
> the same bundle.

When I used the fully qualified class name, rather than the bundle name,
things started working.

If I do this:
 config:edit no.priv.bang.sonar.collector.webhook.SonarCollectorServlet
 config:property-set sonar.collector.jdbc.url "jdbc:postgresql:///sonarcollector"
 config:property-set sonar.collector.jdbc.user "karaf
 config:update

Then I get the following in the config parameter to the activate() method:
 {service.pid=no.priv.bang.sonar.collector.webhook.SonarCollectorServlet, sonar.collector.jdbc.user=karaf, alias=/sonar-collector, component.name=no.priv.bang.sonar.collector.webhook.SonarCollectorServlet, configurationPid=no.priv.bang.sonar.sonar-collector-webhook, component.id=2, sonar.collector.jdbc.url=jdbc:postgresql:///sonarcollector}

And that meant the configuration is now working!

Thanks! :-)


Re: How's config connected to a bundle

Posted by Steinar Bang <sb...@dod.no>.
>>>>> Timothy Ward <ti...@paremus.com>:

> That does look like it should be working - what do the scr commands
> display when you look at your component?

I've got it working now: the trick was to use the fully qualified class
name as the PID.

But thanks for the heads up on the "scr:*" commands.  Lots of
interesting information there, from commands like:
 scr:list
 scr:components
 scr:details no.priv.bang.sonar.collector.webhook.SonarCollectorServlet
 scr:info no.priv.bang.sonar.collector.webhook.SonarCollectorServlet
gavve a lot of intomation.

> Note that your component will be activated without any configuration
> (for example if your configuration doesn’t exist when the component’s
> bundle is started) because it is using the default configuration
> policy of optional. This is why you see the component start up with
> just the default properties. If starting without configuration causes
> your component to fail then you should change this policy.

No I'm good with that.

The plan is for the normal case the database will work without any
external configuration, exepecting to connect to a PostgreSQL database
running on localhost as a PostgreSQL user with user name matching the
username of the karaf process (my own user when running a development
system and user "karaf" on a debian server).

But I want users to be able to configure the database connection in
karaf config if they want to eg. connect to a PostgreSQL server running
on a different host.

And now it looks like it's working so I'll just do a little cleanup and
push. :-)


Re: How's config connected to a bundle

Posted by Timothy Ward <ti...@paremus.com>.
Hi Steinar,

That does look like it should be working - what do the scr commands display when you look at your component? 

Note that your component will be activated without any configuration (for example if your configuration doesn’t exist when the component’s bundle is started) because it is using the default configuration policy of optional. This is why you see the component start up with just the default properties. If starting without configuration causes your component to fail then you should change this policy.

Tim

> On 6 Dec 2017, at 21:36, David Jencks <da...@gmail.com> wrote:
> 
> I’d expect your component with a specified PID to work, are you sure it didn’t?  It turns out that DS cannot reliably set the bundle location; when it tried to it turns out there is an unavoidable race condition.  Therefore DS does not try to set the bundle location, so don’t expect it to change.
> 
> I generally use the default component pid (the fully qualified class name or component name if specified) or give it a name distinct from the bundle name unless the config is shared by several components in the same bundle.
> 
> david jencks
> 
> 
>> On Dec 6, 2017, at 11:43 AM, Steinar Bang <sb...@dod.no> wrote:
>> 
>> How's karaf config connected to a bundle?
>> 
>> I tried matching on bundle name.  Ie. I have a bundle with symbolic-name
>> no.priv.bang.sonar.sonar-collector-webhook so I tried creating a config
>> with a PID identical to this, ie.
>> config:edit no.priv.bang.sonar.sonar-collector-webhook
>> config:property-set sonar.collector.jdbc.url "jdbc:postgresql:///sonarcollector"
>> config:property-set sonar.collector.jdbc.user "karaf"
>> config:update
>> 
>> but the resulting config isn't fed to a component in the bundle.  All I
>> get there, is the stuff sett in the attributes.
>> 
>> Ie. what I received in the activate method is this:
>> {alias=/sonar-collector, component.name=no.priv.bang.sonar.collector.webhook.SonarCollectorServlet, component.id=2}
>> 
>> I tried adding the pid like I found in this example:
>> https://github.com/paremus/hello-examples/blob/37c9b2b1ac430227f0d92bf336be6cf5a59c079a/helloworld/helloworld-ds/src/main/java/com/example/hello/impl/GreetingImpl.java#L19
>> ie. like so:
>> @Component(service={Servlet.class}, property={"alias=/sonar-collector"}, configurationPid = "no.priv.bang.sonar.sonar-collector-webhook" )
>> public class SonarCollectorServlet extends HttpServlet {
>>   ...
>>   @Activate
>>   public void activate(Map<String, Object> config) {
>>   ...
>> but that didn't help.
>> 
>> The config can be found but show up as without a null BundleLocation:
>> karaf@root()> config:list "(service.pid=no.priv.bang.sonar.sonar-collector-webhook)"
>> ----------------------------------------------------------------
>> Pid:            no.priv.bang.sonar.sonar-collector-webhook
>> BundleLocation: null
>> Properties:
>>   service.pid = no.priv.bang.sonar.sonar-collector-webhook
>>   sonar.collector.jdbc.url = jdbc:postgresql:///sonarcollector
>>   sonar.collector.jdbc.user = karaf
>> karaf@root()>
>> 
>> Would it have helped if my bundle had shown up in the BundleLocation?
>> What does it take to get a bundle there?
>> 
>> Thanks!
>> 
>> 
>> - Steinar
>> 
> 


Re: How's config connected to a bundle

Posted by David Jencks <da...@gmail.com>.
I’d expect your component with a specified PID to work, are you sure it didn’t?  It turns out that DS cannot reliably set the bundle location; when it tried to it turns out there is an unavoidable race condition.  Therefore DS does not try to set the bundle location, so don’t expect it to change.

I generally use the default component pid (the fully qualified class name or component name if specified) or give it a name distinct from the bundle name unless the config is shared by several components in the same bundle.

david jencks


> On Dec 6, 2017, at 11:43 AM, Steinar Bang <sb...@dod.no> wrote:
> 
> How's karaf config connected to a bundle?
> 
> I tried matching on bundle name.  Ie. I have a bundle with symbolic-name
> no.priv.bang.sonar.sonar-collector-webhook so I tried creating a config
> with a PID identical to this, ie.
> config:edit no.priv.bang.sonar.sonar-collector-webhook
> config:property-set sonar.collector.jdbc.url "jdbc:postgresql:///sonarcollector"
> config:property-set sonar.collector.jdbc.user "karaf"
> config:update
> 
> but the resulting config isn't fed to a component in the bundle.  All I
> get there, is the stuff sett in the attributes.
> 
> Ie. what I received in the activate method is this:
> {alias=/sonar-collector, component.name=no.priv.bang.sonar.collector.webhook.SonarCollectorServlet, component.id=2}
> 
> I tried adding the pid like I found in this example:
> https://github.com/paremus/hello-examples/blob/37c9b2b1ac430227f0d92bf336be6cf5a59c079a/helloworld/helloworld-ds/src/main/java/com/example/hello/impl/GreetingImpl.java#L19
> ie. like so:
> @Component(service={Servlet.class}, property={"alias=/sonar-collector"}, configurationPid = "no.priv.bang.sonar.sonar-collector-webhook" )
> public class SonarCollectorServlet extends HttpServlet {
>    ...
>    @Activate
>    public void activate(Map<String, Object> config) {
>    ...
> but that didn't help.
> 
> The config can be found but show up as without a null BundleLocation:
> karaf@root()> config:list "(service.pid=no.priv.bang.sonar.sonar-collector-webhook)"
> ----------------------------------------------------------------
> Pid:            no.priv.bang.sonar.sonar-collector-webhook
> BundleLocation: null
> Properties:
>    service.pid = no.priv.bang.sonar.sonar-collector-webhook
>    sonar.collector.jdbc.url = jdbc:postgresql:///sonarcollector
>    sonar.collector.jdbc.user = karaf
> karaf@root()>
> 
> Would it have helped if my bundle had shown up in the BundleLocation?
> What does it take to get a bundle there?
> 
> Thanks!
> 
> 
> - Steinar
>