Skip to main content
Level 2
Oct 19, 2017 5:48 AM UTC
Solved

how to read OSGI configuration via JAVA

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.
Feike_Visser1
Feike_Visser1Accepted solution
Adobe Employee
Oct 19, 2017 6:19 PM UTC

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

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

6 replies

BrijeshYadav
Level 5
Oct 19, 2017 8:37 AM UTC

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

@Inject

private ConfigurationAdmin configurationAdmin;

/Brijesh Yadav

Translate
Feike_Visser1
Adobe Employee
Feike_Visser1Adobe EmployeeAccepted solution
Adobe Employee
Oct 19, 2017 6:19 PM UTC

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

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

Translate
VeenaVikraman
Community Advisor
Community Advisor
Oct 19, 2017 7:25 PM UTC

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

Translate
smacdonald2008
Level 10
Oct 20, 2017 1:10 AM UTC

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

Translate
sachind88574992
Feb 9, 2019 1:24 AM UTC

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?

Translate
joerghoh
Adobe Employee
Adobe Employee
Feb 9, 2019 5:49 PM UTC

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.

Translate