Access OSGI ser­vice from Sightly WCMUsePojo | AEM 6.3 | Community
Skip to main content
neerajg29767140
Level 2
Sep 28, 2017 6:51 PM UTC

Access OSGI ser­vice from Sightly WCMUsePojo | AEM 6.3

Create AEM 6.3 component making use of WCMUsePojo accessing OSGI Service.


Step 1: Create an interface name OSGITestInterface.java that will be implemented by service impl.


public interface OSGITestInterface {

String getOSGIName();

String getOSGIDesc();

String getOSGIuse();

String getOSGIData();

}


Step 2: Create a Interface Implementation class name OSGITestInterfaceImpl.java


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 OSGITestInterfaceImpl implements OSGITestInterface {


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


@Override

public String getOSGIName() {

  return "Test Service";

}


@Override

public String getOSGIDesc() {

  return "AEM OSGI Service";

}


@Override

public String getOSGIuse() {

  return "OSGI Service data";

}

        @Override

public String getOSGIData() { 

  String name = this.getOSGIName();

  String desc = this.getOSGIDesc();

  String use = this.getOSGIuse();

  return name + desc + use;

}


}


Step 3: Create Test Component and use below sightly code in component html.


Fetch values from OSGI Service using WCMSUepojo.

    <sly data-sly-use.info="com.test.utils.TestPojoComp"></sly>

    ${info.details}

</div>


Step 4: Write helper class extending WCmUsePojo.


import org.slf4j.Logger;

import org.slf4j.LoggerFactory;


import com.adobe.cq.sightly.WCMUsePojo;


public class TestPojoComp extends WCMUsePojo {

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

protected String detail;


@Override

  public void activate() {  


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

    detail = service.getOSGIData();

  }


  public String getDetails() {

    return this.detail;

  }

}


Step 5: Create webpage using any of existing template. Drag and drop newly created component into the page.


Validate output.

This post is no longer active and is closed to new replies. Need help? Start a new post to ask your question.

18 replies

Jitendra_S_Toma
Level 10
Sep 19, 2018 4:12 PM UTC

You can get service reference using request object as well.

How to get OSGI Service reference in AEM?

Translate
AdobeID24
Level 5
Sep 19, 2018 3:34 PM UTC

wow ..well explained

Translate
Prabhat_Jain
Level 2
Sep 10, 2018 11:41 AM UTC

Anyone knows why @Reference annotation won't work ?

Translate
Feike_Visser1
Employee
Sep 10, 2018 11:43 AM UTC

this only works in OSGi components, WCMUsePojo is not an OSGi-component

Translate
Prabhat_Jain
Level 2
Sep 10, 2018 12:04 PM UTC

Thanks 4 the answer

Translate
Feike_Visser1
Employee
Sep 29, 2017 3:51 PM UTC

You can also use the OSGi-service directly in HTL, so in this case no need for a wrapper-class.

Translate
AdobeID24
Level 5
Sep 19, 2018 3:35 PM UTC

How we can use services directly in Sightly?

Translate
arunpatidar
New Participant
Sep 19, 2018 4:34 PM UTC

Hi,

Services can be injected through sling model, in below example SlingSettingsService service is injecting and used in HTL

Adobe Experience Manager Help | Creating an Adobe Experience Manager HTML Template Language component that uses JavaScri…

Translate
Arun Patidar
Ratna_Kumar
Level 10
Sep 29, 2017 6:55 AM UTC

Good Community work!!

~Ratna

Translate
smacdonald2008
Level 10
Sep 29, 2017 1:50 PM UTC

Nice summary - we show this pattern here - where the service we hook into from WCMUsePojo queries the JCR -- Scott's Digital Community: Creating an AEM HTL component that queries the JCR

Translate