Login | Register
My pages Projects Community openCollabNet

Discussions > discuss > WadlApplication and Guice in Restlet 1.1.1 problem with Finder.createTarget(Class<? extends Handler>...)

restlet
Discussion topic

Back to topic list

WadlApplication and Guice in Restlet 1.1.1 problem with Finder.createTarget(Class&lt;? extends Handler&gt;...)

Author leighklotz
Full name Leigh L. Klotz, Jr.
Date 2009-02-03 10:44:16 PST
Message I am using Restlet Resource Dependency Injection in Guice/Restlet as
described in
 
http://tembrel.blogs​pot.com/2008/07/reso​urce-dependency-inje​ction-in.htm
l
<http://tembrel.blogs​pot.com/2008/07/reso​urce-dependency-inje​ction-in.ht
ml>

Much like the Spring integration org.restlet.ext.spri​ng.SpringFinder,
this package provides its own Finder, via a FinderFactory.

WadlApplication does not work when using the Finder from the Guice
integration.

Below I explain the issue, make a suggestion to change Finder and
WadlApplication to eliminate the issue, and propose a workaround to Tim
Peierls's suggested code.

Explanation
Specifically, the following line in WadlApplication fails for classes
found with the Guice Finder:
  class WadlApplication { ...
    private ResourceInfo getResourceInfo(Finder finder, String path,
Request request, Response response) {
        // The handler instance targeted by this finder.
        final Handler handler =
finder.createTarget(​finder.getTargetClas​s(), request, response);

As near as I can tell, in Restlet 1.1.1, that line in WadlApplication is
the only place that uses finder.createTarget(class, request, response).

Suggestions to change Finder and WadlApplication:

1. Change WadlApplication to use finder.findTarget(request, response)
instead of finder.createTarget(class, request, response)

2. If there is no need for finder.createTarget(Class<? extends Handler>,
Request, Response) to be public, I would suggest that it be demoted, and
that WadlApplication be changed simple to use finder.findTarget(request,
response).

Here is further analysis of why this fails with the Guice version, and a
workaround for Tim Peierls code suggestions.

Workaround for Guice Finder:

The Guice Finder does not implement the method createTarget(Class<?
extends Handler>, Request, Response), defaulting it to the Finder
implementation. Additionally, it does not implement getTargetClassname,
because it allows targets to be found by Guice Keys, which are more
generic than classnames.

When used from WadlApplication, finder.getTargetClass returns null, and
finder.createTarget then fails.

I implemented a deponent version createTarget(Class<? extends Handler>,
Request, Response) which ignores Handler if null, and checks it against
the class if one is specified.

   @Override public Handler createTarget(Class<? extends Handler>
targetClass, Request request, Response response) {
      if (targetClass != null && Key.get(targetClass) != key)
        throw new
RuntimeException(Str​ing.format("RestletF​inderFactory.createT​arget
targetClass is %s but Key %s as not equal.",
                                                 targetClass,
                                                 key));
      return getInjector().getInstance(key);
    }

Conclusion:
I think this solution is not optimal. I believe the main issues are
raised above and would prefer to see the createTarget(class, request,
response) method deprecated and WadlApplication changed to use
findTarget instead.

Leigh.
Attachments

« Previous message in topic | 1 of 3 | Next message in topic »

Messages

Show all messages in topic

WadlApplication and Guice in Restlet 1.1.1 problem with Finder.createTarget(Class&lt;? extends Handler&gt;...) leighklotz Leigh L. Klotz, Jr. 2009-02-03 10:44:16 PST
     Re: WadlApplication and Guice in Restlet 1.1.1 problem with Finder.createTarget(Class&lt;? extends Handler&gt;...) tpeierls Tim Peierls 2009-02-03 10:52:30 PST
         RE: WadlApplication and Guice in Restlet 1.1.1 problem with Finder.createTarget(Class&lt;? extends Handler&gt;...) jlouvel Jerome Louvel 2009-02-07 04:55:29 PST
Messages per page: