Saturday 25 March 2017

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,
  1. You have to create a OSGI service as usual by creating a interface and implementing it.
  2. Create a class extending WCMUse and get the instance of your OSGI service. 
  3. 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.aemquickstart.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.aemquickstart.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 class which extends WCMUse name Developer.java
package com.aemquickstart.core.services;
 
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.apache.felix.scr.annotations.Component;
import org.apache.felix.scr.annotations.Service;
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 component
OSGI service value:-
<div data-sly-use.info="com.aemquickstart.core.services.Developer">
    ${info.details}
</div>

No comments :

Post a Comment