title: AEM:OSGI sling service activate method not being executed
body: <p>I am trying to create a very basic sling service in AEM:</p>
  
  <pre><code>package com.mypackage;
  
  /**
   * A simple service interface
   */
  public interface TestService {
  
      /**
       * @return the name of the underlying JCR repository implementation
       */
      public String getPropertyName();
  
  }
  </code></pre>
  
  <p>The implementation class:</p>
  
  <pre><code>package com.mymypackage.impl;
  
  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;
  import org.slf4j.Logger;
  import org.slf4j.LoggerFactory;
  
  import com.mypackage.TestService;
  
  
  @Component(label = "Title", description = "Description.", immediate = true, metatype = true, policy = ConfigurationPolicy.REQUIRE)
  @Service(value = {TestService.class})
  @Properties({
      @Property(name = "propertyPath", label = "Property Label", description = "Property Desc.")
  })
  public class TestServiceImpl implements TestService {
  
      private static final Logger log = LoggerFactory.getLogger(TestServiceImpl.class);
  
      String propertyPath = null;
  
      @Activate
      public void activate(ComponentContext ctx) {
          Dictionary properties = ctx.getProperties();
          String propertyPath =(String)properties.get("propertyPath");
          log.info("====================getPropertyName activate========================"+propertyPath);
          this.propertyPath = propertyPath;
      }
  
  
      @Override
      public String getPropertyName() {
          // TODO Auto-generated method stub
          log.info("====================getPropertyName========================"+propertyPath);
          return propertyPath;
      }
  }
  </code></pre>
  
  <p>and I have created a node of type sling:OsgiConfig inside the config folder. The name of this node is com.mypackage.impl.TestServiceImpl.xml and are the content of it:</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt;
  &lt;jcr:root xmlns:sling="http://sling.apache.org/jcr/sling/1.0" xmlns:jcr="http://www.jcp.org/jcr/1.0"
      jcr:primaryType="sling:OsgiConfig"
      propertyPath="http://www.myuhc.com"/&gt;
  </code></pre>
  
  <p>and this is how I am trying to use it inside a Java class:</p>
  
  <pre><code>public static String getTestService() {
          TestService testService = new TestServiceImpl();
          String prop = testService.getPropertyName();
          return prop;
  }
  </code></pre>
  
  <p>This method is being called from a JSP using customtaglib (method being mapped through a .tld file)</p>
  
  <p>When I use this approach the activate method inside the HelloServiceImpl class is not called and hence the property is not set. But when I use the service inside a component JSP like this:</p>
  
  <pre><code>&lt;%@page import="org.osgi.service.cm.ConfigurationAdmin"%&gt;
  &lt;%@page import="org.osgi.service.cm.Configuration"%&gt;
  
  &lt;%
  Configuration conf = sling.getService(org.osgi.service.cm.ConfigurationAdmin.class).getConfiguration("Name of the config");
  String myProp = (String) conf.getProperties().get("property key");
  %&gt;
  </code></pre>
  
  <p>everything works fine. There is something really wrong I must be doing while trying to call the service from a Java class. How can I use that approach. I don't want to use scriptlet inside JSP. Also, any best practices would be highly appreciated.</p>
  
  <p>Thanks in advance</p>