4 Replies Latest reply on Aug 14, 2010 10:33 PM by bagavathysivaraman

    Read propertyfile from one bundle to another

    bagavathysivaraman

      Hi,

       

      I need to ready property file from one osgi bundle.

       

      Scenario:

       

      I am having 10 different bundles. Some of them implemented by JAVA DSL and others are SPRING DSL.

       

      Am having 3 different property file for dev,qa and sat. There am defining endpoing based on env.

       

      In stead of duplicating the property file in each bundle, I need to have common bundle which contains the property file.

       

      Example of property file name:

      -


      1. mysample-dev.properties

      2. mysample-qa.properties

      3.mysample-prod.properties

       

      Content of propertyfile:

      -


      FirstQueueUri=activemq:queue:fist.myqueue1

      SecondQueueUri=activemq:queue:fist.myqueue2

      ThirdQueueUri=activemq:queue:fist.myqueue3

      FourthQueueUri=activemq:queue:fist.myqueue4

       

       

      My requirement is :

       

      I need to read the propertyfile which is defined in some other bundle from one bundle.

       

      Am using Servicdemix4.2.

       

      If anybody help in this regard, really it will be very helpful for me.

       

      Regards,

      Saji

        • 1. Re: Read propertyfile from one bundle to another
          ffang

          Hi,

           

          Generally for common used properties files, you can put them in $SMX_HOME/etc folder and use OSGi ConfigAdmin Service to read/modify/save properties.

           

          You can leverage spring dm to inject ConfigAdmin service to your spring context where you define your

           

          And if you use java dsl, you can make the class implement BundleContextAware interface, so that you can access BundleContext, where you can get ConfigAdmin service.

           

          Freeman

          • 2. Re: Read propertyfile from one bundle to another
            bagavathysivaraman

            Hi,

             

            I have created bundle for property file (sample.properties).In pom.xml, I export the package.

            I can able to successfully call that property file from another bundle.For that I used spring DSL. Below are the code to import that property file in camel-context.xml.

             

                 

             

            I can able to read the property file which defined in sample.properties.

             

            IN ONE case am using Java DSL.There am facing the issue.I tried to call it as follows

             

            InputStream propStream = BundleContext.class.getResourceAsStream("com/my/sample/test/sample.properties");

            Properties props = new Properties();

                         try {

                             props.load(propStream);

                             propStream.close();

                         } catch (IOException e) {

                             e.printStackTrace();

                         }

                         SAMPLE_URL_1 = props.getProperty("sample.url");

                          

             

            InputStream propStream = PropertyPlaceholderConfigurer.class.getResourceAsStream("com/my/sample/test/sample.properties");

                try {

                             props.load(propStream);

                             propStream.close();

                         } catch (IOException e) {

                             e.printStackTrace();

                         }

                         SAMPLE_URL_1 = props.getProperty("sample.url");

             

            But both are failed.

             

            Could any one help me to call property file in budle from java dsl.

             

             

            Regards,

            Saji

            • 3. Re: Read propertyfile from one bundle to another
              ffang

              Hi,

               

              As I answered you before, the normal way in OSGi world to handle the properties should use configAdminService. You can make your java dsl class implements BundleContextAware and get configAdminService there.

               

              Freeman

              • 4. Re: Read propertyfile from one bundle to another
                bagavathysivaraman

                Hi Freeman,

                 

                Thanks for your reply.

                 

                I tried as you said..

                 

                Please find below steps what i did :

                 

                1. I put common.properties file in $SMXHOME/etc/ folder.

                 

                 

                2. In camel-context.xml(Spring DSL) i defined '*<osgi:reference id="configAdmin" interface="org.osgi.service.cm.ConfigurationAdmin" />*'

                 

                camel-context.xml:

                <?xml version="1.0" encoding="UTF-8"?>

                 

                 

                 

                <beans xmlns="http://www.springframework.org/schema/beans"

                xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

                xmlns:osgi="http://www.springframework.org/schema/osgi"

                xmlns:osgix="http://www.springframework.org/schema/osgi-compendium"

                xmlns:camel="http://camel.apache.org/schema/osgi"

                xmlns:ctx="http://www.springframework.org/schema/context"

                xsi:schemaLocation="

                http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd

                http://camel.apache.org/schema/spring http://camel.apache.org/schema/spring/camel-spring.xsd

                http://camel.apache.org/schema/osgi http://camel.apache.org/schema/osgi/camel-osgi.xsd

                http://www.springframework.org/schema/osgi http://www.springframework.org/schema/osgi/spring-osgi.xsd

                http://www.springframework.org/schema/osgi-compendium http://www.springframework.org/schema/osgi-compendium/spring-osgi-compendium.xsd

                http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd

                ">

                <import resource="classpath:org/apache/servicemix/camel/nmr/camel-nmr.xml"/>

                 

                 

                 

                *<osgi:reference id="configAdmin" interface="org.osgi.service.cm.ConfigurationAdmin" />*

                 

                 

                 

                <camel:camelContext id="myCamel" xmlns="http://camel.apache.org/schema/spring">

                 

                 

                <endpoint id="sampleFileEndPoint" uri="$"/>

                 

                 

                <!--

                | Define routing rules.

                |-->

                <route>

                <from ref="sampleFileEndPoint" />

                </route>

                </camel:camelContext>

                 

                </beans>

                 

                After install the bundle and when i tried to start that osgi bundle, am getting following exception...

                 

                karaf@root> Exception in thread "SpringOsgiExtenderThread-62" org.apache.camel.RuntimeCamelException: org.apache.camel.FailedToCreateRouteException: Failed to create route route5: Route[[From -> [To[log:com.vzb.ram.... because of Error creating bean with name 'sampleFileEndPoint': Fac toryBean threw exception on object creation; nested exception is org.apache.camel.NoSuchEndpointException: No endpoint could be found for: ${SampleFileEndP oint}, please check your classpath contains the needed camel component jar. at org.apache.camel.util.ObjectHelper.wrapRuntimeCamelException(ObjectHelper.java:1055) at org.apache.camel.spring.SpringCamelContext.onApplicationEvent(SpringCamelContext.java:103) at org.apache.camel.spring.CamelContextFactoryBean.onApplicationEvent(CamelContextFactoryBean.java:593) at org.springframework.context.event.SimpleApplicationEventMulticaster$1.run(SimpleApplicationEventMulticaster.java:78) at org.springframework.core.task.SyncTaskExecutor.execute(SyncTaskExecutor.java:49) at org.springframework.context.event.SimpleApplicationEventMulticaster.multicastEvent(SimpleApplicationEventMulticaster.java:76)

                 

                 

                -


                Then I added the following bean...

                 

                <bean id="placeholderConfig" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"/>

                 

                This time while i start up the osgi bundle there is no exception in kernal. But it throws an exception in servicemix.log.

                 

                17:49:21,328 | ERROR | xtenderThread-63 | ContextLoaderListener | BundleApplicationContextListener 50 | Application context refresh failed (OsgiBundleXmlApplicationContext(bundle=myBundle, config=osgibundle:/META-INF/spring/*.xml)) org.springframework.beans.factory.BeanDefinitionStoreException: Invalid bean definition with name 'sampleFileEndPoint' defined in null: Could not resolve placeholder 'SampleFileEndPoint' at org.springframework.beans.factory.config.PropertyPlaceholderConfigurer.processProperties(PropertyPlaceholderConfigurer.java:268) at org.springframework.beans.factory.config.PropertyResourceConfigurer.postProcessBeanFactory(PropertyResourceConfigurer.java:75) at org.springframework.osgi.context.support.AbstractDelegatedExecutionApplicationContext.invokeBeanFactoryPostProcessors(AbstractDelegatedExecutionApplicationContext.java:479)

                 

                 

                Please give me the steps to read the properties from etc/common.properties file.

                 

                ++++++++++++++++++++++++++++++++++++++++++++++++++++++

                ++++++++++++++++++++++++++++++++++++++++++++++++++++++

                 

                Also, please provide code snippet to read the property from java DSL.

                 

                As you said i implement the interface 'BundleContextAware' and override the method setBundleContext...

                 

                Further i dont know how to read the property file using above.

                 

                Thanks

                Saji

                 

                Edited by: bagavathysivaraman on Aug 15, 2010 2:32 AM