Can not use OSGi Service in WCMUsePojo class | Community
Skip to main content
New Participant
April 12, 2018
Solved

Can not use OSGi Service in WCMUsePojo class

  • April 12, 2018
  • 14 replies
  • 7294 views

I want to read OSGI run mode configurations with OSGI R6 annotation (the code above).

I found the error in the log file and the applicationConfiguration variable in ComponentApi class always have null value.

Any wrong in my code?

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 Vijayalakshmi_S

Hi,

I tried to execute your code in my local (in your original post) and I was able to reproduce your issue of service Object being null.

When I changed the service name in my Impl Class, it works. Adding the complete snippet for your reference.

(in Pojo class, I added a getter for addressApi for testing the value entered via OSGI config)

AppConfig.Java

@ObjectClassDefinition(name = "Sprint2AEM - Application Configuration", description = "This contains all application configuration")

public @interface AppConfig {

@AttributeDefinition(name = "address.api",description = "Domain",type = AttributeType.STRING)

String getAddressApi() default "";

}

ApplicationConfiguration.java:

public interface ApplicationConfiguration {

     public String getAddressApi();

}

ApplicationConfigurationImpl.java:

import org.osgi.service.component.annotations.Activate;

import org.osgi.service.component.annotations.Component;

import org.osgi.service.component.annotations.Modified;

import org.osgi.service.metatype.annotations.Designate;

import org.slf4j.Logger;

import org.slf4j.LoggerFactory;

@Component(service = ApplicationConfiguration.class,immediate = true)

@Designate(ocd = AppConfig.class)

public class ApplicationConfigurationImpl implements ApplicationConfiguration{

     private static final Logger LOG = LoggerFactory.getLogger(ApplicationConfigurationImpl.class);

     private String addressApi;

  @Activate

  @Modified

   private void activate(AppConfig appConfig){

      this.addressApi = appConfig.getAddressApi();

   }

     @Override

   public String getAddressApi() {

      return addressApi;

   }

}

ComponentApi.java(Extending WCMUsePojo)

import org.slf4j.Logger;

import org.slf4j.LoggerFactory;

import com.adobe.cq.sightly.WCMUsePojo;

public class ComponentApi extends WCMUsePojo{

     private static final Logger LOG = LoggerFactory.getLogger(ComponentApi.class);

     private String apiUrl="";

     private String addressApi;

@Override

public void activate() throws Exception {

         ApplicationConfiguration applicationConfiguration = getSlingScriptHelper().getService(ApplicationConfiguration.class);

         LOG.info("App configuration Service Object Test=="+applicationConfiguration);

         addressApi = applicationConfiguration.getAddressApi();

        //apiUrl = addressApi + get("apiUrl", String.class);

}

public String getAddressApi() { return addressApi; }

public String getApiUrl() { return apiUrl; }

}

HTL Code:

<h3>Hello World component - Testing OSGI config and Service reference from WCMUsePojo</h3>

<div data-sly-use.obj="com.aem.learnings.core.ComponentApi">

${obj.addressApi ? obj.addressApi : "Not from pojo/OSGI Config"}      

</div>

Screenshots:

14 replies

Vijayalakshmi_S
New Participant
April 12, 2018

HI,

Service property in @Component annotation has impl class. Can you change it to "ApplicationConfiguration.class"

@Component(service = ApplicationConfigurationImpl.class,immediate = true)

@Designate(ocd = AppConfig.class)

public class ApplicationConfigurationImpl implements ApplicationConfiguration{

....

smacdonald2008
New Participant
April 12, 2018

Here is the WCMUsePojo syntax to get a refernece to an AEM Service:

public void activate() {

        this.numberOfMovies = Integer.valueOf(getProperties().get("maxMovies", ""));

        this.path = getProperties().get("moviesPath", "");

        

        service = getSlingScriptHelper().getService(MovieInterface.class);

    }

You can see this in this HELPX article -- Adobe Experience Manager Help | Creating an AEM 6.3 HTML Template Language movie component

This Article uses DS annotations. Official OSGi Declarative Services Annotations in AEM - Adobe Experience Manager | AEM/CQ | Apache Sling

New Participant
April 12, 2018

Both are in active

Feike_Visser1
Employee
April 12, 2018

Code seems ok, please have a look in the bundle is started, and the service is active in the OSGi console (/system/console)