@ravi_shankerj14
Add on to @Jörg_Hoh,
You can try inheriting the implementation class(ClassA or ClassB) along side implementing the interface. Check the below example.
ServiceOne
import org.osgi.service.component.annotations.Component;
import org.osgi.service.component.annotations.Reference;
@Component(service=ServiceOne.class)
public class ServiceOneImpl implements ServiceOne {
@Reference
ServiceTwo serviceTwo;
@Override
public String methodOne() {
return "{\"ServiceOne\": \"methodOne\"}";
}
@Override
public String reuseMethodTwo() {
return serviceTwo.methodTwo();
}
}
ServiceTwo
import org.osgi.service.component.annotations.Component;
@Component(service=ServiceTwo.class)
public class ServiceTwoImpl extends ServiceOneImpl implements ServiceTwo {
@Override
public String methodTwo() {
return "{\"ServiceTwo\": \"methodTwo\"}";
}
@Override
public String reuseMethodOne() {
return super.methodOne();
}
}
I am using a Servlet to invoke the ServiceTwo, which is internally calling ServiceOne method.
Servlet
import java.io.IOException;
import javax.servlet.Servlet;
import org.apache.sling.api.SlingHttpServletRequest;
import org.apache.sling.api.SlingHttpServletResponse;
import org.apache.sling.api.servlets.SlingAllMethodsServlet;
import org.osgi.service.component.annotations.Component;
import org.osgi.service.component.annotations.Reference;
@Component(service=Servlet.class, property={
"sling.servlet.methods=" + "GET",
"sling.servlet.paths="+ "/bin/test-servlet"
})
public class TestServlet extends SlingAllMethodsServlet{
private static final long serialVersionUID = 1L;
@Reference
ServiceOne serviceOne;
@Reference
ServiceTwo serviceTwo;
@Override
public void doGet(SlingHttpServletRequest request, SlingHttpServletResponse response) {
response.setContentType("application/json");
try {
response.getWriter().print(serviceOne.methodOne());
response.getWriter().print(serviceOne.reuseMethodTwo());
response.getWriter().print(serviceTwo.methodTwo());
response.getWriter().print(serviceTwo.reuseMethodOne());
} catch (IOException e) {
e.printStackTrace();
}
}
}
Output
{"ServiceOne": "methodOne"}{"ServiceTwo": "methodTwo"}{"ServiceTwo": "methodTwo"}{"ServiceOne": "methodOne"}