Hi All-

Well, I almost can't believe I'm saying this, but I'd like to weigh in
on the side of considering Annotation-based approaches to writing
resources in Restlet.  (Although I'm not so sure about Jerome's
current plan.)

I've mostly disliked, and sometimes despised, the use of Annotations
in most of the places I've encountered them, for many of the same
reasons some of you have mentioned.  I don't like the way they're used
in hibernate and JPA (predicting what will happen with those annotated
classes is difficult), I was initially excited by the idea of all-java
config in google's Guice framework until I realized what my classes
would look like with all the annotations.  And I don't even really
like the simple @Override convention for labeling methods (I figure
the tools could probably figure it out without the annotation).

But a few months ago I forced myself to read through the JX-RS spec in
a fit of due-diligence, while at the same time going through the
process of re-familiarizing myself with the state of the Restlet
project, which I hadn't looked at since before 1.1.  And it modified
my thoughts on annotations.

(It turns out the JAX-RS spec is unusually short and comprehensible,
which is unusual for these enterprisey specs.  The downside is that
it's short partly because they left out a little too much, and JAX-RS
is essentially not a stand-alone technology, you pretty much need some
sort of injection container around it, which the spec ignores, and
therefore there's effectively no way to write portable jax-rs code for
any application that does anything interesting, like accessing stored
data, for instance.)

Both Restlet and Jax-RS provide a framework for implementing
conceptual resources as sets of handler methods on a java class. From
the perspective of the resource class author, these handler methods
are the main public api of your class- routing happens somewhere else
in the container.  Additionally, both Restlet and JAX-RS recommend
that these Resource instances be per-request in scope, so they really
don't have much of a lifecycle, or many reciprocal interactions with
other parts of the application.

By the time any of the custom handler methods get called, all the
routing and much of the details of the HTTP protocol have been taken
care of by the container.  The main difference between JAX-RS and
Restlet is that JAX-RS puts as much of the code to handle the content
negotiation and other protocol issues into the container (and so
alllows its Resource classes to be superclass-less "POJOs"), while
Restlet puts this code into the mandated Resource (now ServerResource)
base-classes, where it is more accessible and allows greater
customization.

Anyhow, my point is that the main challenge seems to me to be in
sorting out which of the handler methods should get called.  And this
ends up being all about "metadata"- the metadata of the conceptual
Resource combining with the metadata attached to the HTTP requests.

And one problem for people trying to automate the mapping of requests
onto java methods is that, until recently, java methods didn't provide
much in the way of places to stick data about themselves.  I am
personally guilty of having written frameworks that resorted to method
naming patterns as a way around this, and it's, well, not elegant.

But if there's one appropriate use of Annotations it might be to add
data _about_ methods, specifically _public_ methods.  (The reason I
don't like seeing annotations used to decorate private member fields
is that these fields are supposed to be private, hidden implementation
details, not something for outside things to mess around with.)

There's really no way of getting around the need to associate some
data (like "text/html") with some handler method.  And if you don't
use annotations to do this, then you have to capture that association
in what becomes effectively routing code.

I did a number of experiments comparing how to implement the same
resources in Restlet and in Jax-RS, and for the most part the only
difference was the presence of this routing code in the Restlet
version, which got replaced by a couple of easy-to-understand
annotations.

So, to sum up what has been a much-too-long post, I'm convinced that
it's desireable to attempt to replace the unavoidable writing of
routing code with annotations on target methods, as long as the
annotations are clear, self-explanatory, and don't try to hide too
much magic.

-Dave Fogel

------------------------------------------------------
http://restlet.tigris.org/ds/viewMessage.do?dsForumId=4447&dsMessageId=1609318

Reply via email to