Expand my Community achievements bar.

Access OSGI ser­vice from Sightly WCMUsePojo | AEM 6.3

Avatar

Level 2

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.

18 Replies

Avatar

Employee

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

Avatar

Community Advisor

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 componen...



Arun Patidar

Avatar

Employee

You can just use data-sly-use for this, no need to create wrapper classes.

Avatar

Level 7

First scenario worked when I called my WCM use class in my sightly like below :

<p data-sly-use.search="amitsample.core.filters.Develope">

   ${search.details}

</p>

but this second scenario dint work when i tried calling my service directly in my sightly:

<p data-sly-use.servObj="amitsample.core.filters.HelloService">

   ${servObj.repositoryName}

</p>

For this i got error like:

Error during include of component '/apps/amitsample/components/content/servicecompo'

Error Message:

org.apache.sling.scripting.sightly.SightlyException: Identifier amitsample.core.filters.HelloService cannot be correctly instantiated by the Use API

Processing Info:

Page=/content/srevice
Resource Path=/content/srevice/jcr:content/par12/servicecompo
Cell=servicecompo
Cell Search Path=pagerendring|page/par12|parsys/servicecompo
Component Path=/apps/amitsample/components/content/servicecompo

Any Idea?

My other code :

Develope.java

================

And these are my code:

package amitsample.core.filters;

import com.adobe.cq.sightly.WCMUse;

import amitsample.core.filters.HelloService;

import org.slf4j.Logger;

import org.slf4j.LoggerFactory;

public class Develope extends WCMUse {

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

protected String details;

@Override

  public void activate() throws Exception {

  HelloService serv= getSlingScriptHelper().getService(HelloService.class);

details=serv.getRepositoryName();

logger.info("this is the first log values");

 

  }

   public String getDetails() {

  

    return details;

  }

}

HelloService.java

=============

package amitsample.core.filters;

/**

* A simple service interface

*/

public interface HelloService {

  

    /**

     * @return the name of the underlying JCR repository implementation

     */

    public String getRepositoryName();

}

HelloServiceImpl.java

===================

package amitsample.core.filters;

import javax.jcr.Repository;

import org.apache.felix.scr.annotations.Component;

import org.apache.felix.scr.annotations.Reference;

import org.apache.felix.scr.annotations.Service;

import org.apache.sling.jcr.api.SlingRepository;

@Service

@Component(metatype = false)

public class HelloServiceImpl implements HelloService {

  

    @Reference

    private SlingRepository repository;

    public String getRepositoryName() {

        return repository.getDescriptor(Repository.REP_NAME_DESC);

    }

}

Avatar

Level 3

Hi AdobeID24​,

I also faced similar issue.As mentioned do not use capital case in package name as It will not get instantiate

import amitsample.core.filters.HelloService;

You can use :amitsample.core.filters.helloService

Avatar

Level 3

Class name can have camel notation but package in which class exist should not have first letter in caps!

Avatar

Level 7

Hi Prabhat,

Thanks for replying

Helloservice.java is my class name as per standard I started my class name with capital letter ......so package would be

amitsample.core.filters.HelloService;

Avatar

Level 7

I tried also like giving my class name starting with capital but then it didnt work in aem...

below didnt work:

<sly data-sly-use.search="amitsample.core.filters.develope">

   ${search.details}

</p>

error:

Class can not be instantiated

this worked :

<sly data-sly-use.search="amitsample.core.filters.Develope">

   ${search.details}

</p>

Avatar

Level 3

Anyone knows why @Reference annotation won't work ?

Avatar

Employee

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