2 Replies Latest reply on Feb 15, 2013 3:48 AM by Panasaki

    Access to Apache Felix Configuration from a Servlet or Sling Service

    Panasaki

      Good afternoun,

       

      I have created a Sling Service with different Properties configured from Apache Sling.

       

      I would like to know if there is a way to access to this properties from a Servlet or another Sling Service.

       

      This I am trying to do, to recover the path where the first Service created is stored information and it is configurated from Apache Felix Configuration.

       

      I am looking information about that and I cannot find it.

       

      Thank you for your help in advance.

        • 1. Re: Access to Apache Felix Configuration from a Servlet or Sling Service
          konradhh

          Your Service:

           

          /**

               * A configuration item

               *

               * @scr.property label="Something to configure" value="your default value"

               * description="some description"

               */

              private static final String MY_CONFIGURED_VALUE = "my.configured.value";

           

          private Dictionary<?, ?> properties;

           

          private String myConfiguredValue;

           

          public void activate(ComponentContext context) {

           

               properties = context.getProperties();

               //get your configured value

               myConfiguredValue = (String) this.properties.get(MY_CONFIGURED_VALUE);

           

          }

           

          Create some public getter:

           

          public String getMyConfiguredValue(){

               return myConfiguredValue;

          }

           

           

           

          And in the other Service you can refrence to your service:

           

          /**

               *

               * @scr.reference

               */

              private YourOtherService yourOtherService;

           

          And here you can use this service and all public methods:

           

          yourOtherService.getMyConfiguredValue();

           

           

          Should run^^ give it a try.

          • 2. Re: Access to Apache Felix Configuration from a Servlet or Sling Service
            Panasaki Level 1

            I have achieved the solution in that way:

             

            Creating the Reference in the Sling Component, in my case in the Servlet and in a Job Service different of the Service that define the configuration attributes

             


            @Reference

            private ConfigurationAdmin configurationAdmin;

             

            And recovering in that way:

             

            final String configurationToRetrieve = (String)this.configurationAdmin.getConfiguration(classOfTheService.getCanonicalName()).ge tProperties().get("AttributeName");

             

            Or in the own Service that define the configuration attributes in the Activate Method to check if is correct:

             


            @Activate

            protected void activate(final ComponentContext context) {


                               final String configurationToRetrieve = (String)context.getProperties().get("AttributeName");


             

            Thank you for your help, and I hope our contributions will help to someone.