April 7, 2020
Estimated Post Reading Time ~

Access OSGI ser­vice from the WCMUse-class in Sightly



OSGI service are very helpful once its comes to the development of a module. A Service can be used to perform small task like string operations to big like processing shopping cart. For developers who are shifting to Sightly for better development practices and taking advantage of AEM 6.x features, it might be a troublesome that how a OSGI Service can be accessed in Sightly module.

Zoom out a bit and you will be able to see things more clear. Here’s all that needs to be done,
You have to create a OSGI service as usual by creating a interface and implementing it.
Create a class extending WCMUse and get the instance of your OSGI service.
Class created in step #2, use this in Sightly component to get the values / output

Let’s get started,

1. Create an interface name SightlyServiceInterface.java that will be implemented by our service

package com.adobeaemclub.adobeaemclub.core.services;

public interface SightlySerivceInterface {
String getDeveloperName();
String getDeveloperProfile();
String getDeveloperSkills();
String getDeveloperData(); 
}

2. Create a service class name SightlyService.java, define the method’s implementations 

package com.adobeaemclub.adobeaemclub.core.services;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.apache.felix.scr.annotations.Component;
import org.apache.felix.scr.annotations.Service;

@Component
@Service
public class SightlySerivce implements SightlySerivceInterface {

Logger logger = LoggerFactory.getLogger(SightlySerivce.class);

@Override
public String getDeveloperName() {
return "John";
}

@Override
public String getDeveloperProfile() {
return "AEM Developer";
}

@Override
public String getDeveloperSkills() {
return "JAVA, OSGI, HTML, JS";
}
        @Override
public String getDeveloperData() {
String name = this.getDeveloperName();
String profile = this.getDeveloperProfile();
String skills = this.getDeveloperSkills();
return name + " is a " + profile + ", He is expert in skills like " + skills;
}

}

3. Write a class which extends WCMUse name Developer.java
package com.adobeaemclub.adobeaemclub.core.services;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.adobe.cq.sightly.WCMUse;

public class Developer extends WCMUse {
Logger logger = LoggerFactory.getLogger(Developer.class);
protected String detail;

@Override
  public void activate() {   

    SightlySerivceInterface service = getSlingScriptHelper().getService(SightlySerivceInterface.class);
    detail = service.getDeveloperData();
  }

  public String getDetails() {
    return this.detail;
  }
}

Line 15 :- Getting our service instance

4. Access values in Sightly componentOur service value:- 
<div data-sly-use.info="com.adobeaemclub.adobeaemclub.core.services.Developer"> ${info.details} 
</div>

5. Once the component is used, the output can be seen



By aem4beginner

No comments:

Post a Comment

If you have any doubts or questions, please let us know.