Calling OSGI service from sightly in maven project | Community
Skip to main content
AdobeID24
Level 5
September 22, 2018
Solved

Calling OSGI service from sightly in maven project

  • September 22, 2018
  • 8 replies
  • 7394 views

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 :

This is my path where i set my maven project:

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 .

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

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>

8 replies

arunpatidar
Community Advisor
arunpatidarCommunity AdvisorAccepted solution
Community Advisor
September 22, 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>

Arun Patidar
AdobeID24
AdobeID24Author
Level 5
September 22, 2018

silly mistake ...Arun Thnaks a lot ...I was hitting my brain on this for so long ..Really helpful.....

AdobeID24
AdobeID24Author
Level 5
September 22, 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()?

arunpatidar
Community Advisor
Community Advisor
September 22, 2018

Hi,

Yes, you need to name method in java with getMethodName and HTL you use methodName to invoke same method.

Arun Patidar
AdobeID24
AdobeID24Author
Level 5
September 23, 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?

arunpatidar
Community Advisor
Community Advisor
September 23, 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);

  }

}

Arun Patidar
AdobeID24
AdobeID24Author
Level 5
September 23, 2018

Hi Arun,

You have not used @Service annotation so does it mean that is not service ?It is a component.

arunpatidar
Community Advisor
Community Advisor
September 23, 2018

Hi,

I used osgi annotation, in osgi annotations service are created like above. more info at below:

Using OSGi annotations (>= AEM6.2) - Experience Delivers

Arun Patidar
Level 2
October 9, 2023

Hello, 

I am trying to consume a method in OSGi service to read its OSGi config value, from a simple HTL tag. But I always see that the "<service class> cannot be resolved to a type" error always, though my OSGI bundle is up and the service is shown to be REGISTERED and active. Any pointers on why am I facing the same issue while using like <p data-sly-use=fully_qualified_servicename>