Search code examples
aem

Unable to fetch OSGi Configuration Values


I have created an event handler and used OSGi configuration as below.

@Component(immediate = true,
        service=EventHandler.class,
        property= {
                EventConstants.EVENT_TOPIC + "=" + ReplicationAction.EVENT_TOPIC
        }
)
    @Designate(ocd = PagePublishEventHandler.Configuration.class)
    public class PagePublishEventHandler implements EventHandler {

        private static String rootPage = "";
        @Override
        public void handleEvent(final Event event) {

        }

        @Activate
        @Modified
        public void activate(Configuration config) {
            String rootPage = config.getPath();
            logger.info("********ConfigurationPropertyInterface**********activate**********************");
            logger.info("********rootPage********",rootPage);        
        }

        @ObjectClassDefinition(name="AEM Plugin OSGi Configuration")
        public @interface Configuration {
            @AttributeDefinition(
                    name = "Root Page For Web Site",
                    description = "Configurable paths for root page",
                    type = AttributeType.STRING
            )
            String getPath() default "/content";


        }

    }

Inside the activate method, Value of rootPage is always blank. Do anyone has the solution on this.

I Thanks


Solution

  • I ran your code in my machine and I don't see any issues with it except for the following things which I noticed.

    1. The rootPage is defined as a static variable. Though this is not the cause of the issue in question, it might cause issues during runtime.
    2. You are not printing the value of the rootPage in your log (probably that is why you think that the value is null?). In order to print it, use format specifiers as shown below.

    logger.info("********rootPage******** {}",rootPage);