2 Replies Latest reply on Feb 22, 2011 3:21 AM by Linus Brimstedt

    Configuration file with maps

    Linus Brimstedt Newbie



      I have created a service unit which makes use of a configuration file from the etc directory (servicemix 4.x). It works fine, the properties from the file are set on my object which is then injected into my RouteBuilder.


      However, I would like to have a Map<> in my settings, is this possible?


      I.e. either by using xml for the cfg file, or by doing something like

      x.y = 234

      x.z = 456

      p.y = 999

      p.z = 666


      and in my pojo get a map like:


        y => 234

        z => 456


        y => 999

        z => zzz


      As long as I can keep each y/z pair together, and add an "unlimited" number of items, im happy.


      Any thoughts or hints for me?


      Another option for me would be if it was possible to deploy the same unit several times with different config files, is this possible within the same instance?


      br and thanks in advance!



        • 1. Re: Configuration file with maps
          Linus Brimstedt Newbie

          Im not sure if my question wasn't understandable or if it's not possible to do what Im asking.


          An alternative for me would be to read the configuration files from etc dir myself.

          Is there an easy way to get hold of them?




          • 2. Re: Configuration file with maps
            Linus Brimstedt Newbie

            Ok, this  is how I did it in the end:


            Implement BundleContextAware

            public class XxxRouteBuilder extends RouteBuilder implements BundleContextAware


            Implement the required method and assign bundleContext to a local field:

            public void setBundleContext(BundleContext bundleContext)


            this.bundleContext = bundleContext;




            In the configure() method, you now have access to the properties in the config file:

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

            ConfigurationAdmin admin = (ConfigurationAdmin) bundleContext.getService(ref);



            Configuration config = admin.getConfiguration("com.viskan.integration.moveexecutearchive");

            properties = (Dictionary&lt;String, String&gt;) config.getProperties();



            You can loop and get the properties and build you map or whatever you want:

            Enumeration&lt;String&gt; keys = properties.keys();

            while (keys.hasMoreElements())


            String key = keys.nextElement();

            String value = properties.get(key);


            storeSetting(key, value);



            In the end I decided I didnt need this right now anyway, but I wanted to post here for future reference..