You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@karaf.apache.org by "z8sbk.yahoo.com.ar via user" <us...@karaf.apache.org> on 2023/06/09 15:21:08 UTC

issue with Declarative Service and blueprint?

Hi, I'm trying to understand how declarative service works. In a bundle, I have these two clasess, very basic:
@Componentpublic class App implements IApp { 
@Referenceprivate IConf conf;
 public App() {}
public void doIt() {


System.out.println("foo=" + conf.getFoo());


}
 }
@Component(name = "Conf", immediate = true, configurationPid = "confByDS")public class Conf implements IConf { 
private String foo = "bar";


public Conf() {}@Override
 
public String getFoo() {


return foo;


}


@Activate@Modifiedpublic void activate(ComponentContext context) {


foo = context.getProperties().get("foo") != null ? (String) context.getProperties().get("foo") : this.foo;


}
  }
Then, I have this blueprint/camel route:
<?xml version="1.0" encoding="UTF-8"?><blueprint xmlns="http://www.osgi.org/xmlns/blueprint/v1.0.0">
 <bean id="app" class="com.app.App" />     <camelContext xmlns="http://camel.apache.org/schema/blueprint">        <route>            <from uri="timer:simple?period=5000"/>                        <to uri="bean:app?method=doIt"/>        </route>    </camelContext>
</blueprint>
After deploying the bundle, when I put the blueprint in /deploy, throws a NullPointerException in conf.getFoo(), which means that the Conf reference is not injected:
Message History---------------------------------------------------------------------------------------------------------------------------------------RouteId              ProcessorId          Processor                                                                        Elapsed (ms)[route3            ] [route3            ] [timer://simple?period=5000                                                    ] [         7][route3            ] [to2               ] [bean:app?method=doIt                                                          ] [         0]
Stacktrace---------------------------------------------------------------------------------------------------------------------------------------java.lang.NullPointerException: null        at com.app.App.doIt(App.java:17) ~[!/:?]        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[?:1.8.0_321]        at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[?:1.8.0_321]        at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[?:1.8.0_321]
I think I'm misunderstanding how the DI in Karaf works plus DS, could you please tell me what am I missing? Does DS work with Camel XML DSL routes?
Thanks in advanced.Nick.


Re: issue with Declarative Service and blueprint?

Posted by "z8sbk.yahoo.com.ar via user" <us...@karaf.apache.org>.
 Now I get it!This seems to work fine:
<?xml version="1.0" encoding="UTF-8"?><blueprint xmlns="http://www.osgi.org/xmlns/blueprint/v1.0.0">
<reference id="app" interface="com.app.IApp"/>    <camelContext xmlns="http://camel.apache.org/schema/blueprint">        <route>            <from uri="timer:simple?period=5000"/>                        <bean ref="app" method="doIt"/>        </route>    </camelContext>
</blueprint>
I didn't know I could use reference to actually reference a component registered using @Component.
Many thanks Paul!

    El viernes, 9 de junio de 2023, 19:47:42 ART, Paul McCulloch <pk...@gmail.com> escribió:  
 
 Use reference to access your App component that Ds creates for you. You currently have two instances of App - one from DS and one from your <bean>.
On Fri, 9 Jun 2023, 23:33 z8sbk.yahoo.com.ar via user, <us...@karaf.apache.org> wrote:

 Hi Paul,I'm not sure what you mean with using <reference> instead of <bean>. I tried this:

<?xml version="1.0" encoding="UTF-8"?><blueprint xmlns="http://www.osgi.org/xmlns/blueprint/v1.0.0">

<reference id="conf" interface="com.conf.IConf"/>
 
<bean id="app" class="com.app.App" >


<property name="conf" ref="conf"/>


</bean>

    <camelContext xmlns="http://camel.apache.org/schema/blueprint">        <route>            <from uri="timer:simple?period=5000"/>                        <to uri="bean:app?method=doIt"/>        </route>    </camelContext>
</blueprint>
Now the error is:
19:21:14.974 ERROR [xxxx\apache-karaf-4.2.15/deploy] Unable to start container for blueprint bundle confByDS.xml/0.0.0org.osgi.service.blueprint.container.ComponentDefinitionException: Unable to find property descriptor conf on com.app.App         at org.apache.aries.blueprint.container.BeanRecipe.getPropertyDescriptor(BeanRecipe.java:831) ~[!/:1.10.3]        at org.apache.aries.blueprint.container.BeanRecipe.setProperty(BeanRecipe.java:813) ~[!/:1.10.3]        at org.apache.aries.blueprint.container.BeanRecipe.setProperties(BeanRecipe.java:784) ~[!/:1.10.3]        at org.apache.aries.blueprint.container.BeanRecipe.setProperties(BeanRecipe.java:765) ~[!/:1.10.3]        at org.apache.aries.blueprint.container.BeanRecipe.internalCreate2(BeanRecipe.java:699) ~[!/:1.10.3]        at org.apache.aries.blueprint.container.BeanRecipe.internalCreate(BeanRecipe.java:666) ~[!/:1.10.3]        at org.apache.aries.blueprint.di.AbstractRecipe$1.call(AbstractRecipe.java:81) ~[!/:1.10.3]
Could you please fix my previous blueprint? Thanks in advanced.
    El viernes, 9 de junio de 2023, 17:25:31 ART, Paul McCulloch <pk...@gmail.com> escribió:  
 
 You need to use <reference> in your blueprint to access your ds component. Using <bean> creates a new instance, but doesn't wire it into the ds infrastructure.
On Fri, 9 Jun 2023, 16:22 z8sbk.yahoo.com.ar via user, <us...@karaf.apache.org> wrote:

Hi, I'm trying to understand how declarative service works. In a bundle, I have these two clasess, very basic:
@Componentpublic class App implements IApp { 
@Referenceprivate IConf conf;
 public App() {}
public void doIt() {


System.out.println("foo=" + conf.getFoo());


}
 }
@Component(name = "Conf", immediate = true, configurationPid = "confByDS")public class Conf implements IConf { 
private String foo = "bar";


public Conf() {}@Override
 
public String getFoo() {


return foo;


}


@Activate@Modifiedpublic void activate(ComponentContext context) {


foo = context.getProperties().get("foo") != null ? (String) context.getProperties().get("foo") : this.foo;


}
  }
Then, I have this blueprint/camel route:
<?xml version="1.0" encoding="UTF-8"?><blueprint xmlns="http://www.osgi.org/xmlns/blueprint/v1.0.0">
 <bean id="app" class="com.app.App" />     <camelContext xmlns="http://camel.apache.org/schema/blueprint">        <route>            <from uri="timer:simple?period=5000"/>                        <to uri="bean:app?method=doIt"/>        </route>    </camelContext>
</blueprint>
After deploying the bundle, when I put the blueprint in /deploy, throws a NullPointerException in conf.getFoo(), which means that the Conf reference is not injected:
Message History---------------------------------------------------------------------------------------------------------------------------------------RouteId              ProcessorId          Processor                                                                        Elapsed (ms)[route3            ] [route3            ] [timer://simple?period=5000                                                    ] [         7][route3            ] [to2               ] [bean:app?method=doIt                                                          ] [         0]
Stacktrace---------------------------------------------------------------------------------------------------------------------------------------java.lang.NullPointerException: null        at com.app.App.doIt(App.java:17) ~[!/:?]        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[?:1.8.0_321]        at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[?:1.8.0_321]        at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[?:1.8.0_321]
I think I'm misunderstanding how the DI in Karaf works plus DS, could you please tell me what am I missing? Does DS work with Camel XML DSL routes?
Thanks in advanced.Nick.


  
  

Re: issue with Declarative Service and blueprint?

Posted by Paul McCulloch <pk...@gmail.com>.
Use reference to access your App component that Ds creates for you. You
currently have two instances of App - one from DS and one from your <bean>.

On Fri, 9 Jun 2023, 23:33 z8sbk.yahoo.com.ar via user, <
user@karaf.apache.org> wrote:

> Hi Paul,
> I'm not sure what you mean with using <reference> instead of <bean>. I
> tried this:
>
>
> <?xml version="1.0" encoding="UTF-8"?>
> <blueprint xmlns="http://www.osgi.org/xmlns/blueprint/v1.0.0">
>
> <reference id="conf" interface="com.conf.IConf"/>
>
> <bean id="app" class="com.app.App" >
>
> <property name="conf" ref="conf"/>
>
> </bean>
>
>
>     <camelContext xmlns="http://camel.apache.org/schema/blueprint">
>         <route>
>             <from uri="timer:simple?period=5000"/>
>             <to uri="bean:app?method=doIt"/>
>         </route>
>     </camelContext>
>
> </blueprint>
>
> Now the error is:
>
> 19:21:14.974 ERROR [xxxx\apache-karaf-4.2.15/deploy] Unable to start
> container for blueprint bundle confByDS.xml/0.0.0
> org.osgi.service.blueprint.container.ComponentDefinitionException: Unable
> to find property descriptor conf on com.app.App
>         at
> org.apache.aries.blueprint.container.BeanRecipe.getPropertyDescriptor(BeanRecipe.java:831)
> ~[!/:1.10.3]
>         at
> org.apache.aries.blueprint.container.BeanRecipe.setProperty(BeanRecipe.java:813)
> ~[!/:1.10.3]
>         at
> org.apache.aries.blueprint.container.BeanRecipe.setProperties(BeanRecipe.java:784)
> ~[!/:1.10.3]
>         at
> org.apache.aries.blueprint.container.BeanRecipe.setProperties(BeanRecipe.java:765)
> ~[!/:1.10.3]
>         at
> org.apache.aries.blueprint.container.BeanRecipe.internalCreate2(BeanRecipe.java:699)
> ~[!/:1.10.3]
>         at
> org.apache.aries.blueprint.container.BeanRecipe.internalCreate(BeanRecipe.java:666)
> ~[!/:1.10.3]
>         at
> org.apache.aries.blueprint.di.AbstractRecipe$1.call(AbstractRecipe.java:81)
> ~[!/:1.10.3]
>
> Could you please fix my previous blueprint?
> Thanks in advanced.
>
> El viernes, 9 de junio de 2023, 17:25:31 ART, Paul McCulloch <
> pkmcculloch@gmail.com> escribió:
>
>
> You need to use <reference> in your blueprint to access your ds component.
> Using <bean> creates a new instance, but doesn't wire it into the ds
> infrastructure.
>
> On Fri, 9 Jun 2023, 16:22 z8sbk.yahoo.com.ar via user, <
> user@karaf.apache.org> wrote:
>
> Hi,
> I'm trying to understand how declarative service works. In a bundle, I
> have these two clasess, very basic:
>
> @Component
> public class App implements IApp {
>
> @Reference
> private IConf conf;
>
> public App() {}
>
> public void doIt() {
>
> System.out.println("foo=" + conf.getFoo());
>
> }
>
> }
>
> @Component(name = "Conf", immediate = true, configurationPid = "confByDS")
> public class Conf implements IConf {
>
> private String foo = "bar";
>
>
> public Conf() {}
> @Override
>
> public String getFoo() {
>
> return foo;
>
> }
>
> @Activate
> @Modified
> public void activate(ComponentContext context) {
>
> foo = context.getProperties().get("foo") != null ? (String)
> context.getProperties().get("foo") : this.foo;
>
> }
>
> }
>
> Then, I have this blueprint/camel route:
>
> <?xml version="1.0" encoding="UTF-8"?>
> <blueprint xmlns="http://www.osgi.org/xmlns/blueprint/v1.0.0">
>
> <bean id="app" class="com.app.App" />
>     <camelContext xmlns="http://camel.apache.org/schema/blueprint">
>         <route>
>             <from uri="timer:simple?period=5000"/>
>             <to uri="bean:app?method=doIt"/>
>         </route>
>     </camelContext>
>
> </blueprint>
>
> After deploying the bundle, when I put the blueprint in /deploy, throws a
> NullPointerException in conf.getFoo(), which means that the Conf reference
> is not injected:
>
> Message History
>
> ---------------------------------------------------------------------------------------------------------------------------------------
> RouteId              ProcessorId          Processor
>                                                 Elapsed (ms)
> [route3            ] [route3            ] [timer://simple?period=5000
>                                               ] [         7]
> [route3            ] [to2               ] [bean:app?method=doIt
>                                               ] [         0]
>
> Stacktrace
>
> ---------------------------------------------------------------------------------------------------------------------------------------
> java.lang.NullPointerException: null
>         at com.app.App.doIt(App.java:17) ~[!/:?]
>         at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
> ~[?:1.8.0_321]
>         at
> sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
> ~[?:1.8.0_321]
>         at
> sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
> ~[?:1.8.0_321]
>
> I think I'm misunderstanding how the DI in Karaf works plus DS, could you
> please tell me what am I missing? Does DS work with Camel XML DSL routes?
>
> Thanks in advanced.
> Nick.
>
>
>

Re: issue with Declarative Service and blueprint?

Posted by "z8sbk.yahoo.com.ar via user" <us...@karaf.apache.org>.
 Hi Paul,I'm not sure what you mean with using <reference> instead of <bean>. I tried this:

<?xml version="1.0" encoding="UTF-8"?><blueprint xmlns="http://www.osgi.org/xmlns/blueprint/v1.0.0">

<reference id="conf" interface="com.conf.IConf"/>
 
<bean id="app" class="com.app.App" >


<property name="conf" ref="conf"/>


</bean>

    <camelContext xmlns="http://camel.apache.org/schema/blueprint">        <route>            <from uri="timer:simple?period=5000"/>                        <to uri="bean:app?method=doIt"/>        </route>    </camelContext>
</blueprint>
Now the error is:
19:21:14.974 ERROR [xxxx\apache-karaf-4.2.15/deploy] Unable to start container for blueprint bundle confByDS.xml/0.0.0org.osgi.service.blueprint.container.ComponentDefinitionException: Unable to find property descriptor conf on com.app.App         at org.apache.aries.blueprint.container.BeanRecipe.getPropertyDescriptor(BeanRecipe.java:831) ~[!/:1.10.3]        at org.apache.aries.blueprint.container.BeanRecipe.setProperty(BeanRecipe.java:813) ~[!/:1.10.3]        at org.apache.aries.blueprint.container.BeanRecipe.setProperties(BeanRecipe.java:784) ~[!/:1.10.3]        at org.apache.aries.blueprint.container.BeanRecipe.setProperties(BeanRecipe.java:765) ~[!/:1.10.3]        at org.apache.aries.blueprint.container.BeanRecipe.internalCreate2(BeanRecipe.java:699) ~[!/:1.10.3]        at org.apache.aries.blueprint.container.BeanRecipe.internalCreate(BeanRecipe.java:666) ~[!/:1.10.3]        at org.apache.aries.blueprint.di.AbstractRecipe$1.call(AbstractRecipe.java:81) ~[!/:1.10.3]
Could you please fix my previous blueprint? Thanks in advanced.
    El viernes, 9 de junio de 2023, 17:25:31 ART, Paul McCulloch <pk...@gmail.com> escribió:  
 
 You need to use <reference> in your blueprint to access your ds component. Using <bean> creates a new instance, but doesn't wire it into the ds infrastructure.
On Fri, 9 Jun 2023, 16:22 z8sbk.yahoo.com.ar via user, <us...@karaf.apache.org> wrote:

Hi, I'm trying to understand how declarative service works. In a bundle, I have these two clasess, very basic:
@Componentpublic class App implements IApp { 
@Referenceprivate IConf conf;
 public App() {}
public void doIt() {


System.out.println("foo=" + conf.getFoo());


}
 }
@Component(name = "Conf", immediate = true, configurationPid = "confByDS")public class Conf implements IConf { 
private String foo = "bar";


public Conf() {}@Override
 
public String getFoo() {


return foo;


}


@Activate@Modifiedpublic void activate(ComponentContext context) {


foo = context.getProperties().get("foo") != null ? (String) context.getProperties().get("foo") : this.foo;


}
  }
Then, I have this blueprint/camel route:
<?xml version="1.0" encoding="UTF-8"?><blueprint xmlns="http://www.osgi.org/xmlns/blueprint/v1.0.0">
 <bean id="app" class="com.app.App" />     <camelContext xmlns="http://camel.apache.org/schema/blueprint">        <route>            <from uri="timer:simple?period=5000"/>                        <to uri="bean:app?method=doIt"/>        </route>    </camelContext>
</blueprint>
After deploying the bundle, when I put the blueprint in /deploy, throws a NullPointerException in conf.getFoo(), which means that the Conf reference is not injected:
Message History---------------------------------------------------------------------------------------------------------------------------------------RouteId              ProcessorId          Processor                                                                        Elapsed (ms)[route3            ] [route3            ] [timer://simple?period=5000                                                    ] [         7][route3            ] [to2               ] [bean:app?method=doIt                                                          ] [         0]
Stacktrace---------------------------------------------------------------------------------------------------------------------------------------java.lang.NullPointerException: null        at com.app.App.doIt(App.java:17) ~[!/:?]        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[?:1.8.0_321]        at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[?:1.8.0_321]        at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[?:1.8.0_321]
I think I'm misunderstanding how the DI in Karaf works plus DS, could you please tell me what am I missing? Does DS work with Camel XML DSL routes?
Thanks in advanced.Nick.


  

Re: issue with Declarative Service and blueprint?

Posted by Paul McCulloch <pk...@gmail.com>.
You need to use <reference> in your blueprint to access your ds component.
Using <bean> creates a new instance, but doesn't wire it into the ds
infrastructure.

On Fri, 9 Jun 2023, 16:22 z8sbk.yahoo.com.ar via user, <
user@karaf.apache.org> wrote:

> Hi,
> I'm trying to understand how declarative service works. In a bundle, I
> have these two clasess, very basic:
>
> @Component
> public class App implements IApp {
>
> @Reference
> private IConf conf;
>
> public App() {}
>
> public void doIt() {
>
> System.out.println("foo=" + conf.getFoo());
>
> }
>
> }
>
> @Component(name = "Conf", immediate = true, configurationPid = "confByDS")
> public class Conf implements IConf {
>
> private String foo = "bar";
>
>
> public Conf() {}
> @Override
>
> public String getFoo() {
>
> return foo;
>
> }
>
> @Activate
> @Modified
> public void activate(ComponentContext context) {
>
> foo = context.getProperties().get("foo") != null ? (String)
> context.getProperties().get("foo") : this.foo;
>
> }
>
> }
>
> Then, I have this blueprint/camel route:
>
> <?xml version="1.0" encoding="UTF-8"?>
> <blueprint xmlns="http://www.osgi.org/xmlns/blueprint/v1.0.0">
>
> <bean id="app" class="com.app.App" />
>     <camelContext xmlns="http://camel.apache.org/schema/blueprint">
>         <route>
>             <from uri="timer:simple?period=5000"/>
>             <to uri="bean:app?method=doIt"/>
>         </route>
>     </camelContext>
>
> </blueprint>
>
> After deploying the bundle, when I put the blueprint in /deploy, throws a
> NullPointerException in conf.getFoo(), which means that the Conf reference
> is not injected:
>
> Message History
>
> ---------------------------------------------------------------------------------------------------------------------------------------
> RouteId              ProcessorId          Processor
>                                                 Elapsed (ms)
> [route3            ] [route3            ] [timer://simple?period=5000
>                                               ] [         7]
> [route3            ] [to2               ] [bean:app?method=doIt
>                                               ] [         0]
>
> Stacktrace
>
> ---------------------------------------------------------------------------------------------------------------------------------------
> java.lang.NullPointerException: null
>         at com.app.App.doIt(App.java:17) ~[!/:?]
>         at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
> ~[?:1.8.0_321]
>         at
> sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
> ~[?:1.8.0_321]
>         at
> sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
> ~[?:1.8.0_321]
>
> I think I'm misunderstanding how the DI in Karaf works plus DS, could you
> please tell me what am I missing? Does DS work with Camel XML DSL routes?
>
> Thanks in advanced.
> Nick.
>
>
>

Re: issue with Declarative Service and blueprint?

Posted by "z8sbk.yahoo.com.ar via user" <us...@karaf.apache.org>.
Thanks Benjamin!

Enviado desde Yahoo Mail para Android 
 
  El sáb., 10 de jun. de 2023 a la(s) 14:52, Benjamin Reed<ra...@opennms.org> escribió:   It’s a pretty dry read, but all of this is basically part of the blueprint spec: https://docs.osgi.org/specification/osgi.cmpn/7.0.0/service.blueprint.html


On Jun 10, 2023, at 8:48 AM, z8sbk.yahoo.com.ar via user <us...@karaf.apache.org> wrote:



 Hi JB!Yes, I could make it work after I changed the <bean> declaration for <reference>. I didn't know this was possible, is there an "official" (or not so official) documentation that explains DS, dependency injection in Karaf, and how to use it in a blueprint? 
Thanks!
    El sábado, 10 de junio de 2023, 01:54:23 ART, Jean-Baptiste Onofré <jb...@nanthrax.net> escribió:  
 
 Hi

In blueprint, a bean (<bean/>) is searched inside the blueprint container.

If you want to use a service declared outside of the blueprint
container (DS in your case), you have to use <reference/>: it will
create a bean proxying the OSGi service available in the OSGi registry
(registered with OSGi "classic" code, DS, blueprint, whatever).

Regards
JB

On Fri, Jun 9, 2023 at 5:21 PM z8sbk.yahoo.com.ar via user
<us...@karaf.apache.org> wrote:
>
> Hi,
> I'm trying to understand how declarative service works. In a bundle, I have these two clasess, very basic:
>
> @Component
> public class App implements IApp {
>
> @Reference
> private IConf conf;
>
> public App() {}
>
> public void doIt() {
>
> System.out.println("foo=" + conf.getFoo());
>
> }
>
> }
>
> @Component(name = "Conf", immediate = true, configurationPid = "confByDS")
> public class Conf implements IConf {
>
> private String foo = "bar";
>
>
> public Conf() {}
> @Override
>
> public String getFoo() {
>
> return foo;
>
> }
>
> @Activate
> @Modified
> public void activate(ComponentContext context) {
>
> foo = context.getProperties().get("foo") != null ? (String) context.getProperties().get("foo") : this.foo;
>
> }
>
> }
>
> Then, I have this blueprint/camel route:
>
> <?xml version="1.0" encoding="UTF-8"?>
> <blueprint xmlns="http://www.osgi.org/xmlns/blueprint/v1.0.0">
>
> <bean id="app" class="com.app.App" />
>    <camelContext xmlns="http://camel.apache.org/schema/blueprint">
>        <route>
>            <from uri="timer:simple?period=5000"/>
>            <to uri="bean:app?method=doIt"/>
>        </route>
>    </camelContext>
>
> </blueprint>
>
> After deploying the bundle, when I put the blueprint in /deploy, throws a NullPointerException in conf.getFoo(), which means that the Conf reference is not injected:
>
> Message History
> ---------------------------------------------------------------------------------------------------------------------------------------
> RouteId              ProcessorId          Processor                                                                        Elapsed (ms)
> [route3            ] [route3            ] [timer://simple?period=5000                                                    ] [        7]
> [route3            ] [to2              ] [bean:app?method=doIt                                                          ] [        0]
>
> Stacktrace
> ---------------------------------------------------------------------------------------------------------------------------------------
> java.lang.NullPointerException: null
>        at com.app.App.doIt(App.java:17) ~[!/:?]
>        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[?:1.8.0_321]
>        at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[?:1.8.0_321]
>        at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[?:1.8.0_321]
>
> I think I'm misunderstanding how the DI in Karaf works plus DS, could you please tell me what am I missing? Does DS work with Camel XML DSL routes?
>
> Thanks in advanced.
> Nick.
>
>
  
  

Re: issue with Declarative Service and blueprint?

Posted by Benjamin Reed <ra...@opennms.org>.
It’s a pretty dry read, but all of this is basically part of the blueprint
spec:
<https://docs.osgi.org/specification/osgi.cmpn/7.0.0/service.blueprint.html>

  

  

> On Jun 10, 2023, at 8:48 AM, z8sbk.yahoo.com.ar via user
> <us...@karaf.apache.org> wrote:  
>  
>

> 
>
> Hi JB!
>
> Yes, I could make it work after I changed the <bean> declaration for
> <reference>. I didn't know this was possible, is there an "official" (or not
> so official) documentation that explains DS, dependency injection in Karaf,
> and how to use it in a blueprint?
>
>  
> Thanks!
>
>  
>
>
> El sábado, 10 de junio de 2023, 01:54:23 ART, Jean-Baptiste Onofré
> <jb...@nanthrax.net> escribió:
>
>  
>
>
>  
>
>
> Hi  
>  
> In blueprint, a bean (<bean/>) is searched inside the blueprint container.  
>  
> If you want to use a service declared outside of the blueprint  
> container (DS in your case), you have to use <reference/>: it will  
> create a bean proxying the OSGi service available in the OSGi registry  
> (registered with OSGi "classic" code, DS, blueprint, whatever).  
>  
> Regards  
> JB  
>
>
>  
> On Fri, Jun 9, 2023 at 5:21 PM z8sbk.yahoo.com.ar via user  
> <[user@karaf.apache.org](mailto:user@karaf.apache.org)> wrote:  
> >  
> > Hi,  
> > I'm trying to understand how declarative service works. In a bundle, I
> have these two clasess, very basic:  
> >  
> > @Component  
> > public class App implements IApp {  
> >  
> > @Reference  
> > private IConf conf;  
> >  
> > public App() {}  
> >  
> > public void doIt() {  
> >  
> > System.out.println("foo=" + conf.getFoo());  
> >  
> > }  
> >  
> > }  
> >  
> > @Component(name = "Conf", immediate = true, configurationPid = "confByDS")  
> > public class Conf implements IConf {  
> >  
> > private String foo = "bar";  
> >  
> >  
> > public Conf() {}  
> > @Override  
> >  
> > public String getFoo() {  
> >  
> > return foo;  
> >  
> > }  
> >  
> > @Activate  
> > @Modified  
> > public void activate(ComponentContext context) {  
> >  
> > foo = context.getProperties().get("foo") != null ? (String)
> context.getProperties().get("foo") : this.foo;  
> >  
> > }  
> >  
> > }  
> >  
> > Then, I have this blueprint/camel route:  
> >  
> > <?xml version="1.0" encoding="UTF-8"?>  
> > <blueprint xmlns="http://www.osgi.org/xmlns/blueprint/v1.0.0">  
> >  
> > <bean id="app" class="com.app.App" />  
> >    <camelContext xmlns="http://camel.apache.org/schema/blueprint">  
> >        <route>  
> >            <from uri="timer:simple?period=5000"/>  
> >            <to uri="bean:app?method=doIt"/>  
> >        </route>  
> >    </camelContext>  
> >  
> > </blueprint>  
> >  
> > After deploying the bundle, when I put the blueprint in /deploy, throws a
> NullPointerException in conf.getFoo(), which means that the Conf reference
> is not injected:  
> >  
> > Message History  
> >
> \---------------------------------------------------------------------------------------------------------------------------------------  
> > RouteId              ProcessorId          Processor
> Elapsed (ms)  
> > [route3            ] [route3            ] [timer://simple?period=5000
> ] [        7]  
> > [route3            ] [to2              ] [bean:app?method=doIt
> ] [        0]  
> >  
> > Stacktrace  
> >
> \---------------------------------------------------------------------------------------------------------------------------------------  
> > java.lang.NullPointerException: null  
> >        at com.app.App.doIt(App.java:17) ~[!/:?]  
> >        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
> ~[?:1.8.0_321]  
> >        at
> sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
> ~[?:1.8.0_321]  
> >        at
> sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
> ~[?:1.8.0_321]  
> >  
> > I think I'm misunderstanding how the DI in Karaf works plus DS, could you
> please tell me what am I missing? Does DS work with Camel XML DSL routes?  
> >  
> > Thanks in advanced.  
> > Nick.  
> >  
> >  
>


Re: issue with Declarative Service and blueprint?

Posted by "z8sbk.yahoo.com.ar via user" <us...@karaf.apache.org>.
 Hi JB!Yes, I could make it work after I changed the <bean> declaration for <reference>. I didn't know this was possible, is there an "official" (or not so official) documentation that explains DS, dependency injection in Karaf, and how to use it in a blueprint? 
Thanks!
    El sábado, 10 de junio de 2023, 01:54:23 ART, Jean-Baptiste Onofré <jb...@nanthrax.net> escribió:  
 
 Hi

In blueprint, a bean (<bean/>) is searched inside the blueprint container.

If you want to use a service declared outside of the blueprint
container (DS in your case), you have to use <reference/>: it will
create a bean proxying the OSGi service available in the OSGi registry
(registered with OSGi "classic" code, DS, blueprint, whatever).

Regards
JB

On Fri, Jun 9, 2023 at 5:21 PM z8sbk.yahoo.com.ar via user
<us...@karaf.apache.org> wrote:
>
> Hi,
> I'm trying to understand how declarative service works. In a bundle, I have these two clasess, very basic:
>
> @Component
> public class App implements IApp {
>
> @Reference
> private IConf conf;
>
> public App() {}
>
> public void doIt() {
>
> System.out.println("foo=" + conf.getFoo());
>
> }
>
> }
>
> @Component(name = "Conf", immediate = true, configurationPid = "confByDS")
> public class Conf implements IConf {
>
> private String foo = "bar";
>
>
> public Conf() {}
> @Override
>
> public String getFoo() {
>
> return foo;
>
> }
>
> @Activate
> @Modified
> public void activate(ComponentContext context) {
>
> foo = context.getProperties().get("foo") != null ? (String) context.getProperties().get("foo") : this.foo;
>
> }
>
> }
>
> Then, I have this blueprint/camel route:
>
> <?xml version="1.0" encoding="UTF-8"?>
> <blueprint xmlns="http://www.osgi.org/xmlns/blueprint/v1.0.0">
>
> <bean id="app" class="com.app.App" />
>    <camelContext xmlns="http://camel.apache.org/schema/blueprint">
>        <route>
>            <from uri="timer:simple?period=5000"/>
>            <to uri="bean:app?method=doIt"/>
>        </route>
>    </camelContext>
>
> </blueprint>
>
> After deploying the bundle, when I put the blueprint in /deploy, throws a NullPointerException in conf.getFoo(), which means that the Conf reference is not injected:
>
> Message History
> ---------------------------------------------------------------------------------------------------------------------------------------
> RouteId              ProcessorId          Processor                                                                        Elapsed (ms)
> [route3            ] [route3            ] [timer://simple?period=5000                                                    ] [        7]
> [route3            ] [to2              ] [bean:app?method=doIt                                                          ] [        0]
>
> Stacktrace
> ---------------------------------------------------------------------------------------------------------------------------------------
> java.lang.NullPointerException: null
>        at com.app.App.doIt(App.java:17) ~[!/:?]
>        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[?:1.8.0_321]
>        at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[?:1.8.0_321]
>        at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[?:1.8.0_321]
>
> I think I'm misunderstanding how the DI in Karaf works plus DS, could you please tell me what am I missing? Does DS work with Camel XML DSL routes?
>
> Thanks in advanced.
> Nick.
>
>
  

Re: issue with Declarative Service and blueprint?

Posted by Jean-Baptiste Onofré <jb...@nanthrax.net>.
Hi

In blueprint, a bean (<bean/>) is searched inside the blueprint container.

If you want to use a service declared outside of the blueprint
container (DS in your case), you have to use <reference/>: it will
create a bean proxying the OSGi service available in the OSGi registry
(registered with OSGi "classic" code, DS, blueprint, whatever).

Regards
JB

On Fri, Jun 9, 2023 at 5:21 PM z8sbk.yahoo.com.ar via user
<us...@karaf.apache.org> wrote:
>
> Hi,
> I'm trying to understand how declarative service works. In a bundle, I have these two clasess, very basic:
>
> @Component
> public class App implements IApp {
>
> @Reference
> private IConf conf;
>
> public App() {}
>
> public void doIt() {
>
> System.out.println("foo=" + conf.getFoo());
>
> }
>
> }
>
> @Component(name = "Conf", immediate = true, configurationPid = "confByDS")
> public class Conf implements IConf {
>
> private String foo = "bar";
>
>
> public Conf() {}
> @Override
>
> public String getFoo() {
>
> return foo;
>
> }
>
> @Activate
> @Modified
> public void activate(ComponentContext context) {
>
> foo = context.getProperties().get("foo") != null ? (String) context.getProperties().get("foo") : this.foo;
>
> }
>
> }
>
> Then, I have this blueprint/camel route:
>
> <?xml version="1.0" encoding="UTF-8"?>
> <blueprint xmlns="http://www.osgi.org/xmlns/blueprint/v1.0.0">
>
> <bean id="app" class="com.app.App" />
>     <camelContext xmlns="http://camel.apache.org/schema/blueprint">
>         <route>
>             <from uri="timer:simple?period=5000"/>
>             <to uri="bean:app?method=doIt"/>
>         </route>
>     </camelContext>
>
> </blueprint>
>
> After deploying the bundle, when I put the blueprint in /deploy, throws a NullPointerException in conf.getFoo(), which means that the Conf reference is not injected:
>
> Message History
> ---------------------------------------------------------------------------------------------------------------------------------------
> RouteId              ProcessorId          Processor                                                                        Elapsed (ms)
> [route3            ] [route3            ] [timer://simple?period=5000                                                    ] [         7]
> [route3            ] [to2               ] [bean:app?method=doIt                                                          ] [         0]
>
> Stacktrace
> ---------------------------------------------------------------------------------------------------------------------------------------
> java.lang.NullPointerException: null
>         at com.app.App.doIt(App.java:17) ~[!/:?]
>         at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[?:1.8.0_321]
>         at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[?:1.8.0_321]
>         at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[?:1.8.0_321]
>
> I think I'm misunderstanding how the DI in Karaf works plus DS, could you please tell me what am I missing? Does DS work with Camel XML DSL routes?
>
> Thanks in advanced.
> Nick.
>
>