Expand my Community achievements bar.

Learn about Edge Delivery Services in upcoming GEM session

osgi service giving nullpointer exception

Avatar

Level 2

i am trying with osgi service in cq5.5 but throwing nullpointerexception

 Please find below code

interface

package com.day.samples;

import javax.jcr.Node;
import javax.jcr.RepositoryException;

public interface HelloService {
    public String sayHello();
    public String getReversedNodePath(Node node)  throws RepositoryException;
    
    public String getRepository();
    public void log(String text);
}

 

 

implementation

package com.day.samples.impl;

import javax.jcr.Node;
import javax.jcr.RepositoryException;

import org.apache.sling.jcr.api.SlingRepository;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.day.samples.HelloService;

public class HelloServiceImpl implements HelloService{

    private SlingRepository repository;
    
    private static final Logger log = LoggerFactory.getLogger(HelloServiceImpl.class);
    
    public String sayHello() {
        return ("Hello World!!");
    }    
    
    public String getReversedNodePath(Node node) throws RepositoryException {
        return new StringBuffer(node.getPath()).reverse().toString();
    }

    public String getRepository() {        
        return repository.getDescriptor(SlingRepository.REP_NAME_DESC);
    }    
    
    protected void bindRepository(SlingRepository repository) {
        this.repository = repository;
    }
    
    protected void unbindRepository(SlingRepository repository) {
        this.repository = null;
    }

    public void log(String text) {
        log.error(text);
    }

    
}

servicecomponents.xml

 

<?xml version="1.0" encoding="UTF-8"?>
<components xmlns:scr="http://www.osgi.org/xmlns/scr/v1.0.0">
    <scr:component enabled="true" immediate="true" name="com.day.samples.impl.HelloServiceImpl">
        <scr:implementation class="com.day.samples.impl.HelloServiceImpl"/>
        <scr:service servicefactory="false">
            <scr:provide interface="com.day.samples.HelloService"/>
        </scr:service>
        <scr:property name="service.description" value="Say hello sample service"/>
        <scr:property name="service.vendor" value="Day"/>
        <scr:property name="service.pid" value="com.day.samples.impl.HelloServiceImpl"/>
        <scr:reference name="repository" interface="org.apache.sling.jcr.api.SlingRepository" cardinality="1..1" policy="static" bind="bindRepository" unbind="unbindRepository"/>
    </scr:component>
</components>

 

 

jsp

<%@include file="/libs/foundation/global.jsp" %>
<div class="container_16">
    <div class="grid_16">
        <div> breadcrumb </div>
        <div> title </div>
    </div>
    <div class="grid_12 body_container">
        <cq:include path="par" resourceType="foundation/components/parsys" />
    </div>
    <div class="grid_4 right_container">
        <div> newslist </div>
        <div> rightpar </div>
    </div>
    <div class="clear"></div>


    <% com.day.samples.HelloService service =   sling.getService(com.day.samples.HelloService.class);%>

    <% if(service !=  null ){ %>

    <%= service.sayHello() %>
    <%}%>

</div>

 

but sling.getService(com.day.samples.HelloService.class); (service)returning null

 

please let me know what could be wrong here

 

please find jar as an attachment

0 Replies