how to read OSGI configuration via JAVA | Community
Skip to main content
October 19, 2017
Solved

how to read OSGI configuration via JAVA

  • October 19, 2017
  • 6 replies
  • 5612 views

so I have the code below

I have a breakpoint in this line 27 and another one in line 29.

when debugging my code (eclipse user here), the program stops at the first breakpoint. And when I resume (F8), it never stops in the second breakpoint. I suppose something happened to it (fatal error perhaps) and my page loads completely as if nothing happened.

This is how I call the JAVA code:

<sly data-sly-use.protectedflag="${'com.mycompany.core.impl.view.tools.checker' @ path=currentPage.path}"/>

Any ideas what I could be doing wrong? Thanks

package com.mycompany.core.impl.view.tools;

import com.adobe.cq.sightly.WCMUsePojo; 

import java.io.IOException;

import java.util.Dictionary;

import java.util.Hashtable;

import java.util.Map;

import org.apache.commons.lang.StringUtils;

import org.apache.felix.scr.annotations.Component;

import org.apache.felix.scr.annotations.Reference;

import org.apache.felix.scr.annotations.Service;

import org.apache.sling.commons.osgi.PropertiesUtil;

import org.osgi.service.cm.Configuration;

import org.osgi.service.cm.ConfigurationAdmin;

public class checker extends WCMUsePojo { 

    private boolean protectedPage;

   

    @Reference

    private ConfigurationAdmin configAdmin;

   

    @Override 

    public void activate() { 

        try {

            Configuration conf = configAdmin.getConfiguration("org.apache.sling.engine.impl.auth.SlingAuthenticator");

           

            String path =  get("path", String.class);

            @SuppressWarnings("unchecked")

            Dictionary<String, Object> props = conf.getProperties();

            protectedPage = false;

        } catch (IOException e) {

            protectedPage = false;

        }

    }

   

    public boolean getProtectedPage() { 

        return protectedPage; 

    } 

}

This post is no longer active and is closed to new replies. Need help? Start a new post to ask your question.
Best answer by Feike_Visser1

Both @Reference or @Inject won't work with WCMUsePojo.

To get a Service-reference, you need to use getSlingScriptHelper()

6 replies

BrijeshYadav
October 19, 2017

You should use @Inject annotation instead of @Reference to initialize service while extending WCMUsePojo

@Inject

private ConfigurationAdmin configurationAdmin;

/Brijesh Yadav

Feike_Visser1
Adobe Employee
Feike_Visser1Adobe EmployeeAccepted solution
Adobe Employee
October 19, 2017

Both @Reference or @Inject won't work with WCMUsePojo.

To get a Service-reference, you need to use getSlingScriptHelper()

VeenaVikraman
Community Advisor
Community Advisor
October 19, 2017

As Feike suggested you might have to get the service using the below code snippet

smacdonald2008
October 20, 2017

To see what Feike suggested in a full example - see this article where we get a reference to another AEM Servicve from a Class that uses WCMUsePojo -- Scott's Digital Community: Creating an AEM HTL component that queries the JCR

sachind88574992
February 9, 2019

Question:

Will the activate() gets called when we update the config?

I believe activate() is only called once when the service gets activated or does it have a different way of working with WCMusePojo?

joerghoh
Adobe Employee
Adobe Employee
February 9, 2019

Hi,

WcmUsePojos are no services, but -- as already indicated by the name -- simple pojos. Therefor all mechanics which are available for OSGI components are not available. activate() is never called during the rendering by the framework.