Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.
Comment: Migrated to Confluence 5.3

Excerpt
hiddentrue

How add new Annotations extending the base Annotations

Note: 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 the 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 by the application programmer to annotate his/her classes for use with Declarative Services.

It is suggested to maintain the Anntoations in their own package.

Define an AnnotationTagProvider

The AnnotationTagProvider 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 fed with the annotations and are expected to return a list of objects representing the actual information to be processed into the descriptor files. If a processor does not understand a particular annotations an empty list is just returned.

The objects returned by the AnnotationTagProvider must implement the JavaTag interface. To ease the implementation the AbstractTag may be used as a base class.

The org.apache.felix.scrplugin.tags.annotation.sling package contains the AnnotationTagProvider supporting Sling Annotations mentioned above as well as corresponding JavaTag implementations (mostly extending from AbstractTag).

Register the AnnotationsTagProvider

To finally make the AnnotationTagProvider 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.tags.annotation.AnnotationTagProvider file.

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.

...