Your achievements

Level 1

0% to

Level 2

Tip /
Sign in

Sign in to Community

to gain points, level up, and earn exciting badges like the new
Bedrock Mission!

Learn more

View all

Sign in to view all badges

[AEM Gems Webinar] Accelerating Experience Manager as a Cloud Service development with Rapid Development Environments
SOLVED

how to read OSGI configuration via JAVA

Avatar

Level 2

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; 

    } 

}

1 Accepted Solution

Avatar

Correct answer by
Employee

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

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

View solution in original post

6 Replies

Avatar

Community Advisor

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

@Inject

private ConfigurationAdmin configurationAdmin;

/Brijesh Yadav

Avatar

Correct answer by
Employee

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

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

Avatar

Community Advisor

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

Avatar

Level 10

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

Avatar

Level 1

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?

Avatar

Employee Advisor

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.