Quantcast
Channel: AEM Guide
Viewing all articles
Browse latest Browse all 162

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

$
0
0
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;
publicinterface 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
publicclassSightlySerivceimplements SightlySerivceInterface {

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

@Override
publicString getDeveloperName() {
return"John";
}

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

@Override
publicString getDeveloperSkills() {
return"JAVA, OSGI, HTML, JS";
}
@Override
publicString getDeveloperData() {
Stringname=this.getDeveloperName();
String profile =this.getDeveloperProfile();
String skills =this.getDeveloperSkills();
returnname+" 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;

publicclassDeveloperextendsWCMUse {
Logger logger = LoggerFactory.getLogger(Developer.class);
protected String detail;

@Override
publicvoid activate() {

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

publicString getDetails() {
returnthis.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>


Viewing all articles
Browse latest Browse all 162

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>