RE: Re: Dependency injection in Restlet 2.0 with Guice
I've revised the RestletGuice incubator sources to deal with the change of return type of Finder.getTargetClass from Class? to Class? extends ServerResource. I've also reverted to the older name scheme. The main interface is called FinderFactory, and its methods are named finder, rather than inject. I added a lot of details to the incubator wiki page: http://wiki.restlet.org/developers/257-restlet/284-restlet.html -- http://restlet.tigris.org/ds/viewMessage.do?dsForumId=4447dsMessageId=2694645
RE: Re: Dependency injection in Restlet 2.0 with Guice
With the invaluable help of Bruno Harbulot (and Thierry and Jerome!), I checked in a preliminary version of org.restlet.ext.guice to the incubator sources. I confirmed that it works for one somewhat artificial example (see org.restlet.ext.guice.example), but it needs more eyes on it, it needs tests, and it needs some real use. The design is very much like what I reported blogging about at the beginning of this thread, except that FinderFactory is now called DependencyInjection because it adds a few extension methods for attach/attachDefault (on Routers and Virtual Hosts) and setNext (on Filters, Servers, and ServerLists). The idea is that you wrap regular attach/attachDefault/setNext calls with DependencyInjection calls to inject a target ServerResource. For example: DependencyInjection di = ...; TemplateRoute route = di.attach(router, /path, MyResource.class); // same as router.attach(/path, di.finderFor(MyResource.class)); // Secret is a Qualifier (JSR-330's version of BindingAnnotation) di.setNext(filter, AnotherResource.class, Secret.class); // same as filter.setNext(di.finderFor(AnotherResource.class, Secret.class)); I moved the FinderFactoryModule into the RestletGuice class, which was previously just a container for static methods, so it is now RestletGuice.Module. It implements DependencyInjection, and it still has the property that if it hasn't been used to create an Injector by the time one of its DependencyInjection methods is called, an Injector is created lazily from it. I removed all mention of com.google.inject from DependencyInjection, so this interface would be suitable for use with any JSR-330-compliant DI framework. All this meant was replacing methods that take a Key argument with variants taking a Class? extends ServerResource and a Class? extends Annotation, where the latter must be a javax.inject.Qualifier (or com.google.inject.BindingAnnotation). I removed support for Restlet 1.1 Handlers. I don't think I'm using any features of Guice introduced in 2.0, but I haven't checked to see if it still works with Guice 1.x. I haven't touched the code that I put in originally to deal with use of a single instance of a RestletGuice.Module by multiple Injectors; I suspect it isn't correct. I'll fix it eventually, but it seems like a pretty marginal use, so it's not a big priority. I haven't provided any custom Scopes because no one has told me of a Scope need that is so common and yet so tricky that it's worth complicating this extension to keep people from having to roll it themselves. If you think you have such a need, let me know. ResletGuice.Module binds providers for Application, Context, Request, and Response that by default use the getCurrent class method of the corresponding class. The getCurrent methods rely on a ThreadLocal value that is set properly for most purposes, including tasks executed via a TaskService; if you need to change the default behavior, you can override newApplicationProvider, newContextProvider, newRequestProvider, and newResponseProvider in a subclass of RestletGuice.Module. There should be no barriers to using RestletGuice.Module in conjunction with other frameworks, including guice-servlet. All you need to do is ensure that your resource bindings are made in Modules passed to the constructor of a RestletGuice.Module that is used to create the final Injector. I was going to extend Router to make the functionality a little more transparent, but that would prevent the extension of Router for other purposes. Also, for completeness I would have had to extend VirtualHost, Filter, Server, and ServerList, and you'd have to use those new subclass names to get access to the variant that takes a qualifier annotation type. It would have been a lot of API noise instead of just one interface. I considered another approach that I'd like to get feedback on, a mini-DSL, where the previous examples would look like this: TemplateRoute route = di.with(router).attach(/path, MyResource.class); di.with(filter).setNext(AnotherResource.class, Secret.class); That would add a few more interfaces for the intermediate types in the DSL, but they wouldn't be in your face. Would this be a better API? --tim -- http://restlet.tigris.org/ds/viewMessage.do?dsForumId=4447dsMessageId=2427214
Re: Re: Dependency injection in Restlet 2.0 with Guice
On Fri, Dec 4, 2009 at 3:34 PM, Tim Peierls t...@peierls.net wrote: There should be no barriers to using RestletGuice.Module in conjunction with other frameworks, including guice-servlet. All you need to do is ensure that your resource bindings are made in Modules passed to the constructor of a RestletGuice.Module that is used to create the final Injector. I'm mistaken -- it's not that strong a requirement. All you have to do is make sure that a RestletGuice.Module is used to create the final Injector. Your resource bindings, if any, can be made in any of the Modules used to create the final Injector, not necessarily in Modules used to construct the RestletGuice.Module. --tim -- http://restlet.tigris.org/ds/viewMessage.do?dsForumId=4447dsMessageId=2427220
RE: Dependency injection in Restlet 2.0 with Guice
Hi Tim and Harald, The goal and main value is see is to ensure the maximum portability of a Restlet Application from one deployment environment to another (JSE/standalone, JEE, GAE, etc.). GAE/J only provides HTTP listening through the Servlet API, so we have to use our Servlet/Restlet adaptation layer. The default way is to leverage our ServerServlet class from org.restlet.ext.servlet, but we also provide a ServletAdapter class to manual adaptation. It feels like both approaches should merge as soon as we configure the inner of the Restlet Application with Guice. Same GuiceRouter and GuiceFinder should be usable. Ive added a comment on the related RFE: Add support for Guice http://restlet.tigris.org/issues/show_bug.cgi?id=546 Harald, would you be willing to contribute your code to the Guice extension developed by Tim (with the help of Bruno Harbulot recently) and to collaborate on creating the best Guice extension possible? The current extension code is in Restlet Incubator but should find its official place in the next Restlet 2.1 version. Best regards, Jerome Louvel -- Restlet ~ Founder and Lead developer ~ http://www.restlet.org/ http://www.restlet.org Noelios Technologies ~ Co-founder ~ http://www.noelios.com/ http://www.noelios.com De : tpeie...@gmail.com [mailto:tpeie...@gmail.com] De la part de Tim Peierls Envoyé : dimanche 22 novembre 2009 22:36 À : discuss@restlet.tigris.org Objet : Re: Dependency injection in Restlet 2.0 with Guice I thought that the GAE Edition of Restlet hid the servlet-ness and made it possible to write standalone Restlet components that run in GAE. Is that not the case? --tim On Sun, Nov 22, 2009 at 4:09 PM, webp...@tigris.org wrote: Hi there, I was looking for dependency injection with guice, as I found the post from Tim. The approach described there, seems to best for a standalone setup. As I'm currently developing an application using the Google AppEngine, I was looking for an integration with a servlet based setup. I described my approach in my blog under a href=http://haraldpehl.blogspot.com/2009/11/google-appengine-restlet.html; http://haraldpehl.blogspot.com/2009/11/google-appengine-restlet.html/a Greetings Harald -- http://restlet.tigris.org/ds/viewMessage.do?dsForumId=4447dsMessageId=2423508
Re: Dependency injection in Restlet 2.0 with Guice
I like how Harald extends Router to be injector-aware in order to avoid requiring the explicit use of FinderFactory. In fact, I have been experimenting with a Router extension that adds two more methods: public TemplateRoute attach(String pathTemplate, Class? extends ServerResource targetClass, Class? extends Annotation targetAnnotation) { return attach(pathTemplate, finderFactory.finderOf(Key.get(targetClass, targetAnnotation))); } public TemplateRoute attachDefault(Class? extends ServerResource targetClass, Class? extends Annotation targetAnnotation) { return attachDefault(finderFactory.finderOf(Key.get(targetClass, targetAnnotation))); } This allows users to attach to resource classes qualified by an annotation, which was the only thing that I felt was missing from Harald's GuiceRouter. In order to smooth the way to JSR-330 integration, however, I think it would be better to call this class QualifiedRouter and make it abstract, with DI-framework-specific concrete subclasses. Ideally, construction could be left to a factory class that would produce the correct concrete subclass without having to mention it explicitly in the user code. I'd like to see if it's possible to avoid having any extra machinery to deal with servlets -- good support for servlets already comes with Guice and Spring (and others I'm sure), so I'm working on a way to get the injector-awareness passed in at the Application level so it doesn't leak into Restlet wiring code. I wouldn't want to require a particular structure for the linkage between Servlet and Restlet worlds. Another topic: What about scopes? Servlets need scopes like request, session, conversation, etc. Only the first of those makes any sense in Restlet, but I'm having trouble imagining a strong need for it in practice, given that Resources are fundamentally request-scoped. Does anyone have an example of a need for binding in request scope that isn't trivially satisfied by Restlet already? --tim On Mon, Nov 23, 2009 at 1:19 PM, Jerome Louvel jerome.lou...@noelios.comwrote: Hi Tim and Harald, The goal and main value is see is to ensure the maximum portability of a Restlet Application from one deployment environment to another (JSE/standalone, JEE, GAE, etc.). GAE/J only provides HTTP listening through the Servlet API, so we have to use our Servlet/Restlet adaptation layer. The default way is to leverage our ServerServlet class from org.restlet.ext.servlet, but we also provide a ServletAdapter class to manual adaptation. It feels like both approaches should merge as soon as we configure the inner of the Restlet Application with Guice. Same GuiceRouter and GuiceFinder should be usable. I’ve added a comment on the related RFE: “Add support for Guice” http://restlet.tigris.org/issues/show_bug.cgi?id=546 Harald, would you be willing to contribute your code to the Guice extension developed by Tim (with the help of Bruno Harbulot recently) and to collaborate on creating the best Guice extension possible? The current extension code is in Restlet Incubator but should find its official place in the next Restlet 2.1 version. Best regards, Jerome Louvel -- Restlet ~ Founder and Lead developer ~ http://www.restlet.org Noelios Technologies ~ Co-founder ~ http://www.noelios.com *De :* tpeie...@gmail.com [mailto:tpeie...@gmail.com] *De la part de* Tim Peierls *Envoyé :* dimanche 22 novembre 2009 22:36 *À :* discuss@restlet.tigris.org *Objet :* Re: Dependency injection in Restlet 2.0 with Guice I thought that the GAE Edition of Restlet hid the servlet-ness and made it possible to write standalone Restlet components that run in GAE. Is that not the case? --tim On Sun, Nov 22, 2009 at 4:09 PM, webp...@tigris.org wrote: Hi there, I was looking for dependency injection with guice, as I found the post from Tim. The approach described there, seems to best for a standalone setup. As I'm currently developing an application using the Google AppEngine, I was looking for an integration with a servlet based setup. I described my approach in my blog under a href= http://haraldpehl.blogspot.com/2009/11/google-appengine-restlet.html; http://haraldpehl.blogspot.com/2009/11/google-appengine-restlet.html/a Greetings Harald -- http://restlet.tigris.org/ds/viewMessage.do?dsForumId=4447dsMessageId=2423617
RE: Dependency injection in Restlet 2.0 with Guice
Sorry for joining in the middle and not paying attention. Do you mean a scope annotation on the Resource class itself? I have so far not seen a need for any scope other than the implicit request scope. We do make use of objects from other scopes from within Resources, but I believe you're not asking that question. Leigh. From: tpeie...@gmail.com [mailto:tpeie...@gmail.com] On Behalf Of Tim Peierls Sent: Monday, November 23, 2009 2:35 PM To: discuss@restlet.tigris.org Subject: Re: Dependency injection in Restlet 2.0 with Guice snip Another topic: What about scopes? Servlets need scopes like request, session, conversation, etc. Only the first of those makes any sense in Restlet, but I'm having trouble imagining a strong need for it in practice, given that Resources are fundamentally request-scoped. Does anyone have an example of a need for binding in request scope that isn't trivially satisfied by Restlet already? --tim -- http://restlet.tigris.org/ds/viewMessage.do?dsForumId=4447dsMessageId=2423620
Re: Dependency injection in Restlet 2.0 with Guice
I meant whether you would find it useful/necessary to be able to bind something in request scope and have it injected into your resources. An artificial example off the top of my head: // binding code bind(Scratchpad.class).in(RequestScoped.class); // Resource code public class MyResource extends ServerResource { @Inject MyResource(Scratchpad scratchPad) { ... } ... } It's not hard to accomplish if it doesn't have to be related to servlet request scope, but I wonder if there's any point to it. I have a feeling I'm forgetting something obvious. --tim On Mon, Nov 23, 2009 at 5:50 PM, Leigh L. Klotz, Jr. leigh.kl...@xerox.comwrote: Sorry for joining in the middle and not paying attention. Do you mean a scope annotation on the Resource class itself? I have so far not seen a need for any scope other than the implicit request scope. We do make use of objects from other scopes from within Resources, but I believe you're not asking that question. Leigh. -- *From:* tpeie...@gmail.com [mailto:tpeie...@gmail.com] *On Behalf Of *Tim Peierls *Sent:* Monday, November 23, 2009 2:35 PM *To:* discuss@restlet.tigris.org *Subject:* Re: Dependency injection in Restlet 2.0 with Guice snip Another topic: What about scopes? Servlets need scopes like request, session, conversation, etc. Only the first of those makes any sense in Restlet, but I'm having trouble imagining a strong need for it in practice, given that Resources are fundamentally request-scoped. Does anyone have an example of a need for binding in request scope that isn't trivially satisfied by Restlet already? --tim -- http://restlet.tigris.org/ds/viewMessage.do?dsForumId=4447dsMessageId=2423621
Re: Dependency injection in Restlet 2.0 with Guice
I thought that the GAE Edition of Restlet hid the servlet-ness and made it possible to write standalone Restlet components that run in GAE. Is that not the case? --tim On Sun, Nov 22, 2009 at 4:09 PM, webp...@tigris.org wrote: Hi there, I was looking for dependency injection with guice, as I found the post from Tim. The approach described there, seems to best for a standalone setup. As I'm currently developing an application using the Google AppEngine, I was looking for an integration with a servlet based setup. I described my approach in my blog under a href= http://haraldpehl.blogspot.com/2009/11/google-appengine-restlet.html; http://haraldpehl.blogspot.com/2009/11/google-appengine-restlet.html/a Greetings Harald -- http://restlet.tigris.org/ds/viewMessage.do?dsForumId=4447dsMessageId=2423119
RE: Dependency injection in Restlet 2.0 with Guice
Hi there, I was looking for dependency injection with guice, as I found the post from Tim. The approach described there, seems to best for a standalone setup. As I'm currently developing an application using the Google AppEngine, I was looking for an integration with a servlet based setup. I described my approach in my blog under a href=http://haraldpehl.blogspot.com/2009/11/google-appengine-restlet.html;http://haraldpehl.blogspot.com/2009/11/google-appengine-restlet.html/a Greetings Harald -- http://restlet.tigris.org/ds/viewMessage.do?dsForumId=4447dsMessageId=2423113
Re: Dependency injection in Restlet 2.0 with Guice
On Sun, May 31, 2009 at 12:17 PM, Jerome Louvel jerome.lou...@noelios.comwrote: So, I’ve just granted you SVN commit rights. I suggest that we first create a module “org.restlet.ext.guice” in the incubator part of SVN, see details here: I created some basic files and directories by copying the incubator/org.restlet.ext.jmx hierarchy and changing jmx to guice everywhere I could find it, then checking it in. No actual content. “Restlet Incubator” http://wiki.restlet.org/developers/257-restlet.html The wiki site was *very* slow when I tried (several times today), so I wasn't able to find a description of how the incubator modules are built. I'm not an Eclipse user (I don't use IDEs at all, in fact) -- if that's required, then I probably shouldn't be the one doing this extension work. I'd be happy to work with someone else to get this going, in that case. http://wiki.restlet.org/developers/257-restlet.html We have a few guidelines here as well that I recommend reading first: “Coding conventions” http://wiki.restlet.org/developers/179-restlet/51-restlet.html OK, I was able to read those. http://wiki.restlet.org/developers/179-restlet/51-restlet.html I’ve also created an empty entry in the developers wiki were you can write some documentation that will end up in the user guide: “Guice extension” http://wiki.restlet.org/developers/257-restlet/284-restlet.html I was able to get to this page -- took a long time -- but I was not able to edit it. I was able to log in at one point, but still couldn't edit. --tim -- http://restlet.tigris.org/ds/viewMessage.do?dsForumId=4447dsMessageId=2359231
RE: Dependency injection in Restlet 2.0 with Guice
Sounds good Tim. Would you be interested to contribute and maintain such an extension to Restlet? We could start by working in Restlet Incubator, using the “com.google.inject” package. Once “javax.inject” is available and the extension is stable, we could promote it as an official Restlet extension. Best regards, Jerome Louvel -- Restlet ~ Founder and Lead developer ~ http://www.restlet.org/ http://www.restlet.org Noelios Technologies ~ Co-founder ~ http://www.noelios.com/ http://www.noelios.com De : tpeie...@gmail.com [mailto:tpeie...@gmail.com] De la part de Tim Peierls Envoyé : jeudi 14 mai 2009 21:53 À : discuss@restlet.tigris.org Objet : Re: Dependency injection in Restlet 2.0 with Guice On Thu, May 14, 2009 at 3:10 PM, Jerome Louvel jerome.lou...@noelios.com wrote: By the way, do you think it would be technically possible to develop a similar integration that would leverage the recently announced javax.inject? http://crazybob.org/2009/05/announcing-javaxinjectinject.html http://crazybob.org/2009/05/announcing-javaxinjectinject.html If so, it could be an opportunity for a new Restlet extension. I'm sure it will be possible. I haven't worked out the details, but such an extension would provide an additional public interface, org.restlet.ext.inject.FinderFactory (better name needed?) and hooksfor obtaining a FinderFactory implementationthat would have to be specific to the injector implementation. In the mean time, the nice thing about javax.inject is that you should be able to use com.google.inject for now and later switch your imports to javax.inject when the support becomes available. --tim -- http://restlet.tigris.org/ds/viewMessage.do?dsForumId=4447dsMessageId=2292423
Re: Dependency injection in Restlet 2.0 with Guice
Yes, I'd be interested. I'd like to find out more about how Spring (and others) will support javax.inject before going any further in code, though. --tim On Sun, May 17, 2009 at 12:05 PM, Jerome Louvel jerome.lou...@noelios.comwrote: Sounds good Tim. Would you be interested to contribute and maintain such an extension to Restlet? We could start by working in Restlet Incubator, using the “com.google.inject” package. Once “javax.inject” is available and the extension is stable, we could promote it as an official Restlet extension. Best regards, Jerome Louvel -- Restlet ~ Founder and Lead developer ~ http://www.restlet.org Noelios Technologies ~ Co-founder ~ http://www.noelios.com *De :* tpeie...@gmail.com [mailto:tpeie...@gmail.com] *De la part de* Tim Peierls *Envoyé :* jeudi 14 mai 2009 21:53 *À :* discuss@restlet.tigris.org *Objet :* Re: Dependency injection in Restlet 2.0 with Guice On Thu, May 14, 2009 at 3:10 PM, Jerome Louvel jerome.lou...@noelios.com wrote: By the way, do you think it would be technically possible to develop a similar integration that would leverage the recently announced javax.inject? http://crazybob.org/2009/05/announcing-javaxinjectinject.html If so, it could be an opportunity for a new Restlet extension. I'm sure it will be possible. I haven't worked out the details, but such an extension would provide an additional public interface, org.restlet.ext.inject.FinderFactory (better name needed?) and hooksfor obtaining a FinderFactory implementationthat would have to be specific to the injector implementation. In the mean time, the nice thing about javax.inject is that you should be able to use com.google.inject for now and later switch your imports to javax.inject when the support becomes available. --tim -- http://restlet.tigris.org/ds/viewMessage.do?dsForumId=4447dsMessageId=2294374
RE: Dependency injection in Restlet 2.0 with Guice
Hi Tim, Thanks for updating your integration. Ive added an entry in the Community Wiki: Third-party integrations http://wiki.restlet.org/community/123-restlet.html By the way, do you think it would be technically possible to develop a similar integration that would leverage the recently announced javax.inject? http://crazybob.org/2009/05/announcing-javaxinjectinject.html If so, it could be an opportunity for a new Restlet extension. Best regards, Jerome Louvel -- Restlet ~ Founder and Lead developer ~ http://www.restlet.org/ http://www.restlet.org Noelios Technologies ~ Co-founder ~ http://www.noelios.com/ http://www.noelios.com De : Tim Peierls [mailto:tpeie...@gmail.com] Envoyé : mercredi 13 mai 2009 00:32 À : discuss@restlet.tigris.org Objet : Dependency injection in Restlet 2.0 with Guice I just posted a short article on my blog about using Guice with Restlet 2.0. http://tembrel.blogspot.com/2009/05/dependency-injection-in-restlet-20-with. html Not much new here, just updated to work with ServerResource. (Still supports Handler, but not for long if Handler is going away.) I had the impression that Leigh Klotz was working on something to do Guice dependency injection into Restlets, not just resources. Leigh, if you're listening, where are you with that? Anyone else working on Restlet-Guice integration? --tim -- http://restlet.tigris.org/ds/viewMessage.do?dsForumId=4447dsMessageId=2261819
Re: Dependency injection in Restlet 2.0 with Guice
On Thu, May 14, 2009 at 3:10 PM, Jerome Louvel jerome.lou...@noelios.comwrote: By the way, do you think it would be technically possible to develop a similar integration that would leverage the recently announced “javax.inject”? http://crazybob.org/2009/05/announcing-javaxinjectinject.html If so, it could be an opportunity for a new Restlet extension. I'm sure it will be possible. I haven't worked out the details, but such an extension would provide an additional public interface, org.restlet.ext.inject.FinderFactory (better name needed?) and hooks for obtaining a FinderFactory implementation that would have to be specific to the injector implementation. In the mean time, the nice thing about javax.inject is that you should be able to use com.google.inject for now and later switch your imports to javax.inject when the support becomes available. --tim -- http://restlet.tigris.org/ds/viewMessage.do?dsForumId=4447dsMessageId=2262105