Search code examples
springannotationsosgiaemapache-felix

AEM 6.3 - Migrate Felix to OSGi annotations: How to deal with propertyPrivate?


I'm migrating an AEM 6.1 application to AEM 6.3. Since Felix annotations (org.apache.felix.scr.annotations.*) are deprecated, I decided to migrate my components to the OSGi annotations (org.osgi.service.component.annotations.*).

Once I figured out how it works, it is pretty easy. But there is one case I don't know how to handle: Properties with propertyPriavte = true.

The old implementation looks like this:

@Component(metatype = true)
@Service(Servlet.class)
@Properties({
        @Property(name = "sling.servlet.selectors", value = "overlay", propertyPrivate = true),
})
public class OverlayServletImpl extends OverlayServlet {
...
}

The property sling.servlet.selectors would not be configurable in the Configuration Manager at the AEM console, but it would be configurable due to a config file, right? So, I still need to define this property.

For other properties I changed my implementation like this:

// OverlayServletImpl
@Component(
        service = Servlet.class,
        configurationPid = "my.package.path.OverlayServletImpl"
)
@Designate(
        ocd = OverlayServletImplConfiguration.class
)
public class OverlayServletImpl extends OverlayServlet {
...
}

// Configuration
@ObjectClassDefinition(name = "Overlay Servlet")
public @interface OverlayServletImplConfiguration {

    String sling_servlet_selectors() default "overlay";
...
}

Now, I have the property sling.servlet.selectors, but it is also available in Configuration Manager and it'S value can be changed there. But I don't want that.

How can I do that? Is this possible with the OSGi annotations?

Thank you and best regards!


Solution

  • It looks like this might be possible if you use the @Component annotation to specify your private properties.

    @Component(service = Servlet.class,
      property = 
      { SLING_SERVLET_RESOURCE_TYPES + "=aemhtlexamples/structure/page",
        SLING_SERVLET_METHODS + "=GET", 
        SLING_SERVLET_EXTENSIONS + "=html", 
        SLING_SERVLET_SELECTORS + "=hello" })
    public class SimpleServlet extends SlingSafeMethodsServlet {
    
      @Override
      protected void doGet(final SlingHttpServletRequest req, final SlingHttpServletResponse resp)
            throws ServletException, IOException {
        final Resource resource = req.getResource();
        resp.getOutputStream().println(resource.toString());
        resp.getOutputStream().println("This content is generated by the SimpleServlet");
      }
    }
    

    Source: https://github.com/heervisscher/htl-examples/blob/master/core/src/main/java/com/adobe/examples/htl/core/servlets/SimpleServlet.java