Highlighted

How can I get configuration values inside a utility class?

Avatar

Avatar

Matt-H-

Avatar

Matt-H-

Matt-H-

07-06-2018

I need help getting configurable values inside of a utility class.

I have these files, MattUtils.java and Overriders.java. MattUtils is essentially a set of utility functions. Overriders is a series of configurable setting (boolean checks, strings, etc.). In MattUtils, I would like to be able to get the Overriders settings and use them. But for some reason, when I substantiate Overriders, the configurable values come back as null.

MattUtils.java:

package com.matt.mattlib;

import javax.servlet.http.HttpSession;

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

import org.apache.sling.api.SlingHttpServletRequest;

import org.apache.sling.api.request.RequestParameter;

import java.io.IOException;

import java.io.UnsupportedEncodingException;

import java.util.*;

import javax.servlet.http.HttpServletRequest;

import javax.servlet.http.HttpSession;

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

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

import com.matt.mattlib.Overriders;

@Component(immediate = true)

@Service(value = MattUtils.class)

public class MattUtils {

    SlingHttpServletRequest slingRequest;

    @Reference

    Overriders ov;

    public static String getSessionValue(String name, SlingHttpServletRequest request) {

        HttpSession session = request.getSession(true);

        return session.getAttribute(name) != null ? session.getAttribute(name).toString().trim() : null;

    }

    public static String getSiteName() {

        String siteName = "Matt's Site";

        Overriders ov = new Overriders();

        if (ov != null && ov.getOverrideMySite() != null) {

            siteName = ov.getOverrideSiteName();

        }

        return siteName;

    }

   

    public static boolean isThisMySite() {

        boolean isMySite = false;

        boolean overrideMySite = false;

        Overriders ov = new Overriders();

        if (ov != null && ov.getOverrideMySite() != null) {

            //isMySite = ov.overrideMySite;

            isMySite = ov.getOverrideMySite();

        }

        return isMySite;

    }

}

Overriders.java:

package com.matt.mattlib;

import org.apache.sling.api.resource.*;

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

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

import org.apache.sling.jcr.api.SlingRepository;

import org.apache.sling.jcr.resource.JcrResourceResolverFactory;

import org.osgi.framework.BundleContext;

import org.osgi.framework.FrameworkUtil;

import org.osgi.framework.ServiceReference;

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

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

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

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

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

import org.osgi.service.cm.Configuration;

import org.osgi.service.cm.ConfigurationAdmin;

import org.osgi.service.component.ComponentContext;

import org.slf4j.Logger;

import org.slf4j.LoggerFactory;

import com.matt.mattlib.MattUtils;

@Component(immediate = true, name="Overriders", label="Overriders",

description="", metatype=true)

@Service(value = Overriders.class)

@Properties({

    @Property(label="Overriders master", description="Overriders isThisMySite",

        name="overrideMySite",

        boolValue=true

    ),

    @Property(label="Overriders", description="Overriders siteName",

        name="overrideSiteName",

        value="Matthew's Site"

    )

})

public class Overriders {

    Logger log = LoggerFactory.getLogger(this.getClass());

    public Boolean overrideMySite;

    public String overrideSiteName;

    @Reference

    private ConfigurationAdmin configAdmin;

   

    @Activate

    protected void activate(ComponentContext ctx) throws Exception {

        Configuration conf = configAdmin.getConfiguration("com.matt.mattlib.Overriders");

        this.overrideMySite = (Boolean)conf.getProperties().get("overrideMySite");

        this.overrideSiteName = (String)conf.getProperties().get("overrideSiteName");

    }

    public Overriders() {}

   

    public Boolean getOverrideMySite() {

        return overrideMySite;

    }

    public void setOverrideMySite(boolean overrideMySite) {

        this.overrideMySite = overrideMySite;

    }

    public String getOverrideSiteName() {

        return overrideSiteName;

    }

    public void setOverrideSiteName(String overrideSiteName) {

        this.overrideSiteName = overrideSiteName;

    }

}

com.matt.mattlib.Overriders.xml (the configuration file I reference)

<?xml version="1.0" encoding="UTF-8"?>

<jcr:root xmlns:sling="http://sling.apache.org/jcr/sling/1.0" xmlns:jcr="http://www.jcp.org/jcr/1.0"

    jcr:primaryType="sling:OsgiConfig"

    overrideMySite="{Boolean}true"

    overrideName="Matthew's Site"

    />

So, for example, in my JSP, I have

<b><%= MattUtils.getSiteName() %> <%= MattUtils.isThisMySite() %></b>

I would expect that with Overriders in place, It would be Matthew's Site true, but it remains Matt's Site false - same as if there was no Overriders.

<%

Overriders ovr = new Overriders();

%>

<b><%= ovr.getOverrideSiteName() %>|<%= ovr.getOverrideMySite() %></b>

I would expect the output to be Matthew's Site|true, but instead it is null|null. (Same with <b><%= ovr.overrideSiteName %>|<%= ovr.overrideMySite %></b>)

If I define ovr using sling.getService, it acts a little differently.

<%

Overriders ovr = sling.getService(Overriders.class);

%>

<b><%= ovr.getOverrideSiteName() %>|<%= ovr.getOverrideMySite() %></b>

I would expect the output to be Matthew's Site|true, but instead it is null|true. So - the boolean came out right, but the string did not.

I've also tried taking out the @Service(value = Overriders.class) in Overriders and just use Overriders ovr = new Overriders();, but that didn't change anything.

I would like help in understanding what I'm doing wrong and what I need to do to make it work the way I think it should.

Thank you.

Replies

Highlighted

Avatar

Avatar

smacdonald2008

Total Posts

12.7K

Likes

1.4K

Correct Answer

2.3K

Avatar

smacdonald2008

Total Posts

12.7K

Likes

1.4K

Correct Answer

2.3K
smacdonald2008

08-06-2018

What AEM Version are you using. You should also look at replacing use of JSP with HTL.

Highlighted

Avatar

Avatar

smacdonald2008

Total Posts

12.7K

Likes

1.4K

Correct Answer

2.3K

Avatar

smacdonald2008

Total Posts

12.7K

Likes

1.4K

Correct Answer

2.3K
smacdonald2008

08-06-2018

Also - if you are referring to OSGi config values - then see this community article that shows you how to get config values -- Scott's Digital Community: Reading OSGi Configuration Values for Adobe Experience Manager 6.3

Highlighted

Avatar

Avatar

bsloki

MVP

Total Posts

1.9K

Likes

170

Correct Answer

544

Avatar

bsloki

MVP

Total Posts

1.9K

Likes

170

Correct Answer

544
bsloki
MVP

08-06-2018

Overriders ov = new Overriders();

        if (ov != null && ov.getOverrideMySite() != null) {

            //isMySite = ov.overrideMySite;

            isMySite = ov.getOverrideMySite();

        }

Hi,

you dont have to do new Overriders(). As you already have @Reference,

try directly using that variable.

Remove this line - //Overriders ov = new Overriders();

Highlighted

Avatar

Avatar

Matt-H-

Avatar

Matt-H-

Matt-H-

08-06-2018

Thank you for looking at it. When I remove that line, I get the error:

non-static variable ov cannot be referenced from a static context

I did bypass the problem with

    @Reference

    static Overriders ov;

But I still was getting null|null.

I'll try adding a bunch of logging and seeing where things are falling apart.

Oh, and smacdonald2008, I am using AEM 6.3. Thank you for the article, I'll take a look.

Highlighted

Avatar

Avatar

bsloki

MVP

Total Posts

1.9K

Likes

170

Correct Answer

544

Avatar

bsloki

MVP

Total Posts

1.9K

Likes

170

Correct Answer

544
bsloki
MVP

08-06-2018

Dont make the reference 'static' rather remove 'static' from the function definition

Highlighted

Avatar

Avatar

bsloki

MVP

Total Posts

1.9K

Likes

170

Correct Answer

544

Avatar

bsloki

MVP

Total Posts

1.9K

Likes

170

Correct Answer

544
bsloki
MVP

08-06-2018

Also, I just noticed that in JSP you are directly using Overriders service call and not MattUitls.

what is the objective of MattUtils class?