Calling OSGI service from sightly in maven project

AdobeID24 22-09-2018

This is my sightly code :

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

   ${search.details}

</p>

this is my bundle which I uploaded in felix console :

1577187_pastedImage_0.png

This is my path where i set my maven project:

1577188_pastedImage_1.png

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 detail;

@Override

  public void activate() throws Exception {

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

detail=serv.getRepositoryName();

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

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

 

  }

   public String getDetails() {

   return "Amitgggg";

  }

}

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);

    }

}

Now I am setting logs to see whether it is working or not but I am not getting anything in my logs and in which page I am dragging and dropping the  component that uses   develop.java (WCMuse class) file thru sightly, There  also no value I am expecting  return "Amitgggg"; this in my page but not getting any idea what is the problem .

1577207_pastedImage_3.png

1577208_pastedImage_8.png

Accepted Solutions (1)

Accepted Solutions (1)

Arun_Patidar
MVP
22-09-2018

Hi,

try below

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

   ${search.details}

</p>

You can directly call services in HTL like above

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

   ${servObj.repositoryName}

</p>

Answers (7)

Answers (7)

Arun_Patidar
MVP
23-09-2018

Hi,

Could be issue with your service implementation.

I tried below code, working fine -

package com.aem.community.core.forum;

import javax.jcr.Repository;

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

import org.osgi.service.component.annotations.Component;

import org.osgi.service.component.annotations.Reference;

@Component(service = HelloService.class, immediate = true)

public class HelloServiceImpl implements HelloService {

  @Reference

  private SlingRepository repository;

  public String getRepositoryName() {

  return repository.getDescriptor(Repository.REP_NAME_DESC);

  }

}

AdobeID24 22-09-2018

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

   ${search.details}

</p>

above one is working fine and I am getting result on my page where i dragged the component .

Now i tried the second option:

You can directly call services in HTL like above

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

   ${servObj.repositoryName}

</p>

this is not working ..I have not changed any code as pasted in above in my question just using services directly in Sightly as you suggested but I am getting error like below:

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?

AdobeID24 22-09-2018

One doubt :

here   ${search.details}

details is what?

It  came from method

public String getDetails form this part?

Similarly 

  ${servObj.repositoryName}?

It  came from method

   public String getRepositoryName()?