1 Reply Latest reply on Dec 20, 2012 5:47 AM by kasq

    SCR Reference

    Shashi Aachar

      Hi,

         I would like to know how can i make my class object as reference?

         For ex:

          /** @scr.reference */

          private SlingRepository slingRepository;

         

         In the java i have to get slingRepository through reference only. So i want to make my customized class objects accessible through reference only. Provide pointers if u know, Thanks for helping.

       

       

      Regards,

      Shashi

        • 1. Re: SCR Reference
          kasq Level 3

          Hi,

           

          Try to read following links: 

           

          http://dev.day.com/content/ddc/blog/2008/05/slingosgi.html

          http://blogs.adobe.com/kmossman/2012/04/osgi-sling-service-example.html

           

           

          and bellow some example code which maybe hepls you somehow:

           

          ISampleService.java

          package com.project.intf;

           

          public interface ISampleService {

          }

             

          SampleService.java

          package com.project;

           

          import org.apache.felix.scr.annotations.Component;

          import org.apache.felix.scr.annotations.Properties;

          import org.apache.felix.scr.annotations.Property;

          import org.apache.felix.scr.annotations.Service;

          import org.osgi.service.component.ComponentContext;

          import com.project.intf.ISampleService;

           

           

          @Component(immediate=true, metatype=false, label="COMPONENT NAME")

          @Service

          @Properties(value = {

                    @Property(name="service.description", value="Sample Service")

          })

          public class SampleService implements ISampleService {

           

                    protected synchronized void activate(ComponentContext componentContext) {

                              //some code here

                     }

                    

                      protected void deactivate(ComponentContext componentContext) {

                              //some code here

                     }

          }

           

          and then if you want access to your sample service using reference (for example in your custom  Sling Post Servlet):

           

          ExampleServlet.java

          package com.project;

           

          import org.apache.felix.scr.annotations.Component;

          import org.apache.felix.scr.annotations.Properties;

          import org.apache.felix.scr.annotations.Property;

          import org.apache.felix.scr.annotations.Reference;

          import org.apache.felix.scr.annotations.ReferenceCardinality;

          import org.apache.felix.scr.annotations.ReferencePolicy;

          import org.apache.felix.scr.annotations.Service;

          import org.apache.sling.api.SlingHttpServletRequest;

          import org.apache.sling.api.SlingHttpServletResponse;

          import org.apache.sling.api.servlets.SlingAllMethodsServlet;

          import com.project.intf.ISampleService;

           

          @Component(immediate=true, metatype=false, label="........")

          @Service

          @Properties(value = {

                    @Property(name="sling.servlet.methods", value="GET"),

               @Property(name="sling.servlet.resourceTypes", value="sling/servlet/default"),

               @Property(name="sling.servlet.selectors", value="ex"),

               @Property(name="sling.servlet.extensions", value="html")

          })

           

          public class ExampleServlet extends SlingAllMethodsServlet {

           

             @Reference(policy=ReferencePolicy.DYNAMIC, cardinality=ReferenceCardinality.MANDATORY_UNARY)

             private ISampleService yourservice;

           

                    protected void doGet(SlingHttpServletRequest request, SlingHttpServletResponse response) throws ServletException {

               // some code

            }

           

          }

           

          Of course this is a example code , but maybe it hepls you to understand a litte bit OSGI Sling Services

           

          Regards,

          kasq