0
votes

I am trying to pass SlingRequest object from a component jsp to a osgi service as follows. But the service is not getting registered. (not sure why?)

<c:set var="inverterData" value='<%=sling.getService(InverterFactory.class).getOverview(slingRequest, resourceResolver)%>' scope="request" />

Question : Is it allowed to pass a SlingRequest object from a component jsp to a osgi service ? If it is allowed, why my service not getting registered. If I take out SlingRequest object, the service is getting registered and I am able to call the service from jsp.

1

1 Answers

0
votes

including your OOTB /libs/foundation/global.jsp makes you to get the default tag libs, sling objects and CQ objects defined.

when you pass the fully qualified name of the service to sling.getService method, SlingRepository gets injected into service by the OSGi bundle managed component.

In your code snippet sling.getService(InverterFactory.class) the service is obtained from the sling SlingScriptHelper object to retrieve OSGi services available in Sling, and this generally will be called on the interface. After your creation of a Service object by using sling.getService, you can invoke any methods exposed by that service.

Have a look at the ScriptHelper api documentation and sling scripting variables

try code snippet below

<% your.qualified.path.InverterFactory Invfac = sling.getService(your.qualified.path.InverterFactory.class); %> <c:set var="inverterData" value='<%=Invfac.getOverview(slingRequest, resourceResolver)%>' scope="request" />

also i don't think SlingRequest object should cause any problem, i have tried your code snippet syntax and just returned some string from OSGI service, it works fine. Below is my example

HelloService Interface

public interface HelloService { public String getRepositoryName(); public String getRepositoryName(SlingHttpServletRequest slingRequest, ResourceResolver resourceResolver); }

HelloServiceImpl

@Service
@Component(metatype = false)
public class HelloServiceImpl implements HelloService {
 protected final Logger log = LoggerFactory.getLogger(this.getClass());    
    @Reference
    private SlingRepository repository;
    public String getRepositoryName() {
        return repository.getDescriptor(Repository.REP_NAME_DESC);
    }   
    public String getRepositoryName(SlingHttpServletRequest slingRequest, ResourceResolver resourceResolver) {
    log.info("*** Inside my interface impl OSGI ***");
    String returnstr = repository.getDescriptor(Repository.REP_NAME_DESC)+" Data from OSGI";
       //return repository.getDescriptor(Repository.REP_NAME_DESC);
       return returnstr;
    }
}

OSGICalling.jsp

<%@ page import="com.mycompany.myrestservice.HelloService" %>
<%
com.mycompany.myrestservice.HelloService hs = sling.getService(com.mycompany.myrestservice.HelloService.class);
String repo = hs.getRepositoryName() ; 
out.println("myrepo::" + repo);
%>   
<c:set var="inverteraa" value='<%=sling.getService(com.mycompany.myrestservice.HelloService.class).getRepositoryName(slingRequest,resourceResolver)%>' />
<c:out value="${inverteraa}"/>

OutPut

outputosgicall