6 Replies Latest reply on Aug 30, 2010 1:09 AM by Freeman(Yue) Fang

    Call / Import bundle in java dsl

    bagavathysivaraman Newbie

      Hi,

       

      I need to call / import the another bundle in camel java DSL.

       

      I could easily achieve the same in SpringDSL.

       

      Below is code snippet in spring dsl:

       

       

       

       

      But I am not able to call / import the bundle in camel java dsl.

       

      If anybody have an idea on it, please help me.

       

      Thanks,

      Saji

        • 1. Re: Call / Import bundle in java dsl
          Freeman(Yue) Fang Master

          Hi,

           

          In OSGi world, call another bundle means call the services from another bundle. If the bundle you want to call already publish some services, you can easily get the servicereference from your java dsl.

          Just let your java dsl code implement BundleContextAware inferface, and then you can get the serivcereference from the BundleContext.

           

          Freeman

          • 2. Re: Call / Import bundle in java dsl
            bagavathysivaraman Newbie

            Hi Freeman,

             

            Thanks for you reply.

             

            As you said I have tried to get the ServiceReference of another bundle which already published / providing services. But am getting nullpointer exception.

             

            Below are the steps that I have followed:

             

            1. Implement BundleContextAware interface in camel java dsl.

            2. Override the method setBundleContext method

            3. I have tried to access the another bunlde service as below :

                           3.1. ServiceReference srRef = bndleCtx.getServiceReference("SMX :: ESB :: MY :: SAMLE :: MyProperties");

                                    Where  SMX :: ESB :: MY :: SAMLE :: MyProperties is another bundle name which already published (am getting it from another bundle's MANIFEST.MF)

             

                           3.2. InputStream propStream = srRef.getBundle().getClass().getResourceAsStream("my.properties");  

                           3.3. Using java.util.Properties, am trying to get the properties defined in my.properties.

             

            4. I can able to build and while deploy SMX4.2 am getting below said exception.

             

            Please help me with sample code to to get the another bundle from camel java dsl.(As per my project requirement i cannt keep these property in SMXHOME/etc folder).

             

            Believe there should be some way to access the another bundle property from camel java dsl.

             

            Please help me in this regard.

             

            karaf@root> Exception in thread "SpringOsgiExtenderThread-62" org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'MyCamelJavaDsl': Invocation of init method failed; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name ' crMessageSwithToGC' defined in URL bundleentry://211.fwk32755292/META-INF/spring/camel-context.xml

            •        at com.my.sample.teswt.MyCamelJavaDsl.setBundleContext(MyCamelJavaDsl.java:98)*
            •        at org.springframework.osgi.context.support.BundleContextAwareProcessor.postProcessBeforeInitialization(BundleContextAwareProcessor.java:48)*
            •        at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.applyBeanPostProcessorsBeforeInitialization(AbstractAutowireCapable*
            : Initialization of bean failed; nested exception is ja va.lang.NullPointerException         at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1338)         at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:473)         at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory$1.run(AbstractAutowireCapableBeanFactory.java:409)         at java.security.AccessController.doPrivileged(Native Method)         at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:380)         at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:264)         at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:222)         at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:261)         at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:185)         at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:164)         at org.springframework.beans.factory.support.DefaultListableBeanFactory.getBeansOfType(DefaultListableBeanFactory.java:308)         at org.springframework.context.support.AbstractApplicationContext.getBeansOfType(AbstractApplicationContext.java:947)         at org.springframework.context.support.AbstractApplicationContext.registerListeners(AbstractApplicationContext.java:701)         at org.springframework.osgi.context.support.AbstractDelegatedExecutionApplicationContext.access$1500(AbstractDelegatedExecutionApplicationContext.j ava:69)         at org.springframework.osgi.context.support.AbstractDelegatedExecutionApplicationContext$4.run(AbstractDelegatedExecutionApplicationContext.java:35 1)         at org.springframework.osgi.util.internal.PrivilegedUtils.executeWithCustomTCCL(PrivilegedUtils.java:85)         at org.springframework.osgi.context.support.AbstractDelegatedExecutionApplicationContext.completeRefresh(AbstractDelegatedExecutionApplicationConte xt.java:320)         at org.springframework.osgi.extender.internal.dependencies.startup.DependencyWaiterApplicationContextExecutor$CompleteRefreshTask.run(DependencyWai terApplicationContextExecutor.java:136)         at java.lang.Thread.run(Thread.java:619) Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'myCamelJavaDsl' defined in URL [bundleentry://211.fw k32755292/META-INF/spring/camel-context.xml]: Initialization of bean failed; nested exception is java.lang.NullPointerException         at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:480)         at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory$1.run(AbstractAutowireCapableBeanFactory.java:409)         at java.security.AccessController.doPrivileged(Native Method)         at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:380)         at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:264)         at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:222)         at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:261)         at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:185)         at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:168)         at org.springframework.context.support.AbstractApplicationContext.getBean(AbstractApplicationContext.java:884)         at org.apache.camel.spring.spi.ApplicationContextRegistry.lookup(ApplicationContextRegistry.java:41)         at org.apache.camel.osgi.CompositeRegistry.lookup(CompositeRegistry.java:47)         at org.apache.camel.util.CamelContextHelper.lookup(CamelContextHelper.java:104)         at org.apache.camel.model.RouteBuilderDefinition.createRouteBuilder(RouteBuilderDefinition.java:64)         at org.apache.camel.spring.CamelContextFactoryBean.installRoutes(CamelContextFactoryBean.java:877)         at org.apache.camel.spring.CamelContextFactoryBean.afterPropertiesSet(CamelContextFactoryBean.java:359)         at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1369)         at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1335)         ... 18 more Caused by: java.lang.NullPointerException BeanFactory.java:350)         at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1331)         at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:473)         ... 35 more

             

            Edited by: bagavathysivaraman on Aug 29, 2010 11:17 PM

            • 3. Re: Call / Import bundle in java dsl
              Willem Jiang Master

              Hi,

               

              The NPE is thrown from you route builder (MyCamelJavaDsl), can past the code for it.

               

              BTW, if you want to use the properties in the camel route, you can take a look at camel-properties[1].

               

              https://cwiki.apache.org/CAMEL/properties.html

               

              Willem

              • 4. Re: Call / Import bundle in java dsl
                bagavathysivaraman Newbie

                Hi William,

                 

                Thanks for your reply.

                 

                Yeah, the NPE is thrown from MyCamelJavaDsl.java. Herewith I past the code.

                 

                -


                public class MyCamelJavaDsl extends RouteBuilder implements BundleContextAware{

                 

                private static String XSL_URI;

                 

                /**

                • Configure the route.

                *

                • @throws java.lang.Exception

                */

                public void configure() throws Exception {

                 

                from("nmr:myMessageTransform").

                to("xslt:" + XSL_URI + "message/transform/myrequest/FromSystem1-ToSystem2.xsl").

                to("log:After Transaltion?showAll=true&level=DEBUG").

                end();

                }

                 

                @Override

                public void setBundleContext(BundleContext bndleCtx) {

                // TODO Auto-generated method stub

                ServiceReference srf= bndleCtx.getServiceReference("SMX :: ESB :: MY :: SAMLE :: MyProperties");

                InputStream propStream = srf.getBundle().getClass().getResourceAsStream("common.properties");

                Properties props = new Properties();

                try {

                props.load(propStream);

                propStream.close();

                } catch (IOException e) {

                e.printStackTrace();

                }

                XSL_URI = props.getProperty("xsl.uri");

                }

                 

                }

                ----


                 

                I created myproperties bundle and deployed in SMX4.2. I can able to read the properties defined in my.properties using Spring DSL. I havnt faced any issue with spring DSL . When I try to get the propetry from Camel Java DSL, am facing above said issue.

                 

                Herewith I past the MyProperties bundle's MANIFEST.MF content to get an idea / clue.

                 

                -


                Manifest-Version: 1.0

                Export-Package: com.my.sample.common.resources

                Built-By: ax45689

                Build-Jdk: 1.6.0_21

                Bundle-Version: 0.0.0.main-SNAPSHOT

                Tool: Bnd-0.0.357

                Bundle-Name: SMX :: ESB :: MY :: SAMLE :: MyProperties

                Bnd-LastModified: 1280434965500

                 

                Created-By: Apache Maven Bundle Plugin

                Bundle-ManifestVersion: 2

                Bundle-SymbolicName: MyProperties

                 

                -


                Could you help me to fix above said issue and how to read the properties from property file whic is an another bundle.

                 

                If  explore the MyProperties-main-SNAPSHOT.jar, my.properties are available in package com.my.sample.common.resources. So I import / access that package from camel java DSL, I can get the properties defined in my.properties.

                 

                 

                -


                -


                -


                As you said, I have tried to use camel-properties.But it is available as of camel2.3.

                 

                But already my project is running on ServiceMix4.2. As by default it contains the features came-core-2.2.0-fuse-02-00,I cannt start bundle (which implements camel-properties using camel 2.3). Despite, I install the camel-core2.3.0(to support camel properties) features.

                But while deploy the bundle into Servicemix4.2, am getting .... Package uses conflict: Import-Package: org.apache.servicemix.camel.nmr; version="0.0.0", Package uses conflict: Import-Package: org.apache.camel.osgi; version="0.0.0" ....etc.All are due to camel2.3.

                 

                Is there any way to upgrade ALL the Servicemix4.2 packages to camel2.3 compatible.

                or

                It is better to go for Servicemix4.3 in order to use camel2.3.

                 

                As of now we not in the position to upgarade from 4.2 to 4.3(since project deliverty date is very near :(.

                 

                So, please help me to get the properties which is in another bundle from camel javadsl in SMX4.2.

                 

                 

                Regards,

                Saji

                 

                Edited by: bagavathysivaraman on Aug 30, 2010 12:33 AM

                • 5. Re: Call / Import bundle in java dsl
                  Claus Ibsen Master

                  FUSE ESB (ServiceMix) 4.3 has been release which contains Camel 2.4

                  • 6. Re: Call / Import bundle in java dsl
                    Freeman(Yue) Fang Master

                    Hi,

                     

                    What you do isn't correct.

                     

                    1. You should simply override the method setBundleContext method like

                    this.bundleContext = bundleContext;

                    This method generally get invoked by the OSGi container so that can inject bundleContext to your class.

                    You shouldn't put extra logic in setBundleContext generally.

                     

                    2. As now you have bundleContext, you can get the ServiceReference already published by other bundles.

                    I don't think get services through bundle name like

                    ServiceReference srf= bndleCtx.getServiceReference("SMX :: ESB :: MY :: SAMLE :: MyProperties");

                    make any sense here, you should get ServiceReference from the interface that Service implemented.

                    For example, the way to get ConfigAdmin ServiceReference

                    ServiceReference ref =     bndleCtx.getServiceReference(ConfigurationAdmin.class.getName());

                     

                    You may need google to get some basic concept about how OSGi service works.

                     

                    3. If you want to read properties from other bundles, you can use

                    a. ConfigAdmin service, this may need you put property files into SMX_HOME/etc folder

                    or

                    b. From the bundle which you have property file, create a method like

                    readProprties() , and publish it as a OSGi serivce, then in your camel java dsl bundle, you can retrieve this OSGi service and call readProperties method.

                     

                    Freeman