Extending SCR Annotations Excerpt: How add new Annotations extending the base Annotations

The Apache Felix SCR Tooling is not supported anymore. Please use the official OSGi annotations and bnd based tooling instead.
This page documents functionality available with the SCR Generator 1.0.0 release providing the declaration file generation mechanism for the Maven SCR Plugin 1.6.0 (and newer) and the SCR Ant Task 1.0.0 (and newer).

The SCR Annotations library has been updated in version 1.4.0 to comply with the rules described here. As such this library may be used as basis to learn more about providing pluggbale Java 5 annotations for the Maven SCR Plugin and SCR Ant Task. The source code for the SCR Annotations library is available from the Apache Felix SVN repository at annotations.

This page outlines the required steps to implement your own extended Java 5 annotations:

  1. Define the Annotations

  2. Define an AnnotationTagProvider

  3. Register the AnnotationsTagProvider

It is interesting to note, that the SCR Annotations 1.4.0 library providing the default and Sling Java 5 tags for the SCR Generator by itself is already implemented according to the rules outlined herein. This means, that actually the SCR Generator does not have any built-in annotations but only provides the framework to allow for the definition of Java 5 Annotations.

Define the Annotations

To start with you will define Java 5 Annotations to suit your application needs. The org.apache.felix.scr.annotations.sling package provides three sample annotations:

  • SlingServlet — used to declare a javax.servlet.Servlet service component with the appropriate service registration properties to configure the service as an Apache Sling Servlet.

  • SlingFilter — used to declare a javax.servlet.Filter service component with the appropriate service registration properties to configure the service as a Filter used by the Apache Sling Main Servlet.

  • SlingFilterScope — helper annotation to define the filter.scope service registration property for Filters defined with the SlingFilter annotation.

These annotations will be used to annotate classes for use with Declarative Services.

It is suggested to maintain the annotations in a separate package or even in a separate module that your projects may depend upon.

Define an AnnotationProcessor

The AnnotationProcessor interface defines the interface of a helper class which is used to convert Java 5 Annotation data into internal data structures to be then used as the basis for the generation of the descriptor files.

Implementations of this interface are provided with the ScannedClass instances and are expected to augment the provided ClassDescription.

The SlingAnnotationProcessor class implements the AnnotationProcessorto handle the Sling annotations described above.

Register the AnnotationProcessor

To finally make the AnnotationProcessor implementations available to the SCR Generator (and thus the Maven SCR Plugin and/or SCR Ant Task) the fully qualified class names of these implementations must be listed in an META-INF/services/org.apache.felix.scrplugin.annotations.AnnotationProcessor file in the library providing the annotations.

The classes listed in this file will automatically be picked up by the SCR Generator from the library placed on the build class path and thus enable support for the respective annotations.

Again, refer to the actual implementation in the SCR Annotations library referred to above.