Expand my Community achievements bar.

SOLVED

Calling OSGI service from sightly in maven project

Avatar

Level 7

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

1 Accepted Solution

Avatar

Correct answer by
Community Advisor

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

View solution in original post

9 Replies

Avatar

Correct answer by
Community Advisor

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

Avatar

Level 7

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

Avatar

Level 7

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()?

Avatar

Community Advisor

Hi,

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



Arun Patidar

Avatar

Level 7

<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?

Avatar

Community Advisor

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

Avatar

Level 7

Hi Arun,

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

Avatar

Community Advisor

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

Avatar

Level 2

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>