You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@sling.apache.org by Tobias Bocanegra <to...@day.com> on 2008/08/12 22:11:37 UTC

Using java annotations for servlet resolution mapping

hi,
currently sling uses @scr javadoc comments to specify the bindings
from the request infos to servlets. for example:

/**
 * @scr.service
 * @scr.property name="sling.servlet.resourceTypes" values="bin/test"
 * @scr.property name="sling.servlet.extensions" values="txt, html"
 */
public class TestServlet extends SlingAllMethodsServlet {
...
}

this has the following drawbacks:
- the possible @scr properties are hard to guess if not documented
- the @scr properties are not "type safe"
- no intellisense support on IDEs
- only works if servlets are compiled using the maven scr plugin

i suggest to introduce a couple of java annotations that provide the
same mechanism.
they provide all missing features mentioned above.

example:
public interface SlingServletBinding {

    @Retention(RetentionPolicy.RUNTIME)
    @Target(ElementType.TYPE)
    public @interface RESOURCE_TYPE {
        String[] value();
    }

    @Retention(RetentionPolicy.RUNTIME)
    @Target(ElementType.TYPE)
    public @interface EXTENSION {
        String[] value();
    }

   ....
}


the servlet can then be annotated like this:

@SlingServletBinding.RESOURCE_TYPE("bin/test")
@SlingServletBinding.EXTENSION({"txt", "html"})
public class TestServlet extends SlingAllMethodsServlet {
}

and the annotations can be determined like this:

        TestServlet servlet = new TestServlet();
        for (Annotation a: servlet.getClass().getAnnotations()) {
            if (a.annotationType() == SlingServletBinding.EXTENSION.class) {
                for (String e: ((SlingServletBinding.EXTENSION) a).value()) {
                    System.out.printf("%s extension %s%n",
servlet.getClass().getName(), e);
                }
            }
            if (a.annotationType() == SlingServletBinding.RESOURCE_TYPE.class) {
                for (String e: ((SlingServletBinding.RESOURCE_TYPE)
a).value()) {
                    System.out.printf("%s restype %s%n",
servlet.getClass().getName(), e);
                }
            }
        }


i think this would provide more stable code. unfortunately it does not
free it from using
the scr plugin, since the servlet needs to be registered as osgi
component nevertheless.

WDYT?

regards, toby

Re: Using java annotations for servlet resolution mapping

Posted by Carsten Ziegeler <cz...@apache.org>.
Tobias Bocanegra wrote:
> this has the following drawbacks:
> - the possible @scr properties are hard to guess if not documented
Hmm, I think they are documented, but perhaps not as prominent as this 
should be.

> - the @scr properties are not "type safe"
This depends. You can use @scr.property nameRef="SOME_CONSTANT" 
valueRef="ANOTHER_CONSTANT"
where the constants can come from the same class or any class which is 
in the imports of the current class. Even the property type is deferred 
from the constants. The refs are checked during build time by the plugin.

Carsten

-- 
Carsten Ziegeler
cziegeler@apache.org