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