users@jersey.java.net

Re: [Jersey] Guice / Reference to WebApplication

From: Paul Sandoz <Paul.Sandoz_at_Sun.COM>
Date: Fri, 04 Jun 2010 11:14:22 +0200

Hi Johannes,

I think we can define a JerseyGuiceModule, from which you extend from,
that defines those bindings.

If you don't define a scope for:

    @Provides
    public WebApplication webApp( @NotNull GuiceContainer
guiceContainer ) {
      WebComponent component = Reflection.field( "webComponent"
).ofType( WebComponent.class ).in( guiceContainer ).get();
      WebApplication webApplication = Reflection.field( "application"
).ofType( WebApplication.class ).in( component ).get();
      return webApplication;
    }

it should work yes? Since Jersey is managing the scope of stuff to be
provided it is not necessary to declare a scope for any of the
@Provides methods you have defined.

There are some issues supporting injection of @*Param because of
restrictions in the Guice binding model. Modification of Guice will be
required.

Paul.

On Jun 3, 2010, at 8:17 PM, Johannes Schneider wrote:

> -----BEGIN PGP SIGNED MESSAGE-----
> Hash: SHA1
>
> On 06/02/2010 08:59 PM, Christopher Piggott wrote:
>> I'm not so sure about the second part, but I'm using Guice and what
>> you are asking about worked for me:
>>
>> public class RootResource {
>> @Context private UriInfo uriInfo; // injected by jersey/servlet/
>> etc
>> @Inject @Named("DataDir") private File dataDir; // Injected by
>> Guice
>> ...
>> }
>>
>> and that works fine for me.
>
> Yes, this works for me, too. But I tried if it is possible to just
> use a
> constructor (and depend on one injection framework). So I could make
> the
> vars final..
>
> It is not a big issue. Maybe not worth the work... But at least I
> wanted
> to try...
>
>
> Johannes
>
>>
>> When I created my injector, I gave it bindings for this class:
>>
>> public class MyServletModule extends ServletModule {
>> ...
>> @Override
>> protected void configureServlets() {
>> ...
>> bind(RootResource.class);
>> }
>> }
>>
>> but I didn't do anything else.
>>
>>
>>
>> On Wed, Jun 2, 2010 at 2:07 PM, Johannes Schneider
>> <mailings_at_cedarsoft.com> wrote:
>> I used a profiler to find all references to WebApplication. Just
>> found
>> one... And with some dirty hacks it resulted in that module
>> configuration:
>>
>>
>> @Singleton
>> @Provides
>> public WebApplication webApp( @NotNull GuiceContainer
>> guiceContainer ) {
>> WebComponent component = Reflection.field( "webComponent"
>> ).ofType( WebComponent.class ).in( guiceContainer ).get();
>> WebApplication webApplication = Reflection.field( "application"
>> ).ofType( WebApplication.class ).in( component ).get();
>> return webApplication;
>> }
>>
>> @RequestScoped
>> @Provides
>> public HttpContext httpContext( @NotNull WebApplication
>> webApplication ) {
>> return webApplication.getThreadLocalHttpContext();
>> }
>>
>> @RequestScoped
>> @Provides
>> public UriInfo uriInfo( @NotNull HttpContext httpContext ) {
>> return httpContext.getUriInfo();
>> }
>> @RequestScoped
>> @Provides
>> public HttpRequestContext requestContext( HttpContext
>> httpContext ) {
>> return httpContext.getRequest();
>> }
>> @RequestScoped
>> @Provides
>> public HttpHeaders httpHeaders( HttpContext httpContext ) {
>> return httpContext.getRequest();
>> }
>> @RequestScoped
>> @Provides
>> public Request request( HttpContext httpContext ) {
>> return httpContext.getRequest();
>> }
>> @RequestScoped
>> @Provides
>> public SecurityContext securityContext( HttpContext httpContext ) {
>> return httpContext.getRequest();
>> }
>>
>>
>> On 06/02/2010 07:39 PM, Johannes Schneider wrote:
>>>>> At the moment I am struggling with the Guice integration.
>>>>>
>>>>> It is working based on that blog entry:
>>>>> http://zcox.wordpress.com/2009/09/22/jersey-guice-scala/
>>>>>
>>>>>
>>>>> When I mark an constructor of a resources with
>>>>> @com.google.inject.Inject
>>>>> it is injected using Guice.
>>>>> But I also want to add some context stuff (UriInfo) etc.
>>>>>
>>>>> Therefore I tried to add some bindings to Guice. But I don't
>>>>> know how I
>>>>> can get the current UriInfo. I have found that code:
>>>>> http://pastie.org/521175
>>>>> But I think that is wrong, since there an additional instance of
>>>>> WebApplicationImpl is created (or am I missing something?).
>>>>>
>>>>> So is there any possibility to get the reference to the
>>>>> WebApplication?
>>>>> Any magic/static method?
>>>>>
>>>>>
>>>>>
>>>>> I could extend
>>>>> com.sun.jersey.guice.spi.container.servlet.GuiceContainer
>>>>> and use the instance passed to #initiate. But that looks a
>>>>> little bit
>>>>> dirty because it is too late. Guice has still been created (see
>>>>> the
>>>>> injected Injector to the constructor).
>>>>>
>>>>> Any hints?
>>>>>
>>>>>
>>>>>
>>>>> Thanks,
>>>>>
>>>>> Johannes
>>
>> ---------------------------------------------------------------------
>> To unsubscribe, e-mail: users-unsubscribe_at_jersey.dev.java.net
>> For additional commands, e-mail: users-help_at_jersey.dev.java.net
>>
>>
>>>
> -
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe_at_jersey.dev.java.net
> For additional commands, e-mail: users-help_at_jersey.dev.java.net
>>>
>>>
>
>> ---------------------------------------------------------------------
>> To unsubscribe, e-mail: users-unsubscribe_at_jersey.dev.java.net
>> For additional commands, e-mail: users-help_at_jersey.dev.java.net
>
>
> - --
> Johannes Schneider - blog.cedarsoft.com
> -----BEGIN PGP SIGNATURE-----
> Version: GnuPG v1.4.10 (GNU/Linux)
>
> iQEcBAEBAgAGBQJMB/HIAAoJEAytD9R7Qv6dudYH/3Akgl4ARdg7kLxMwr+VPtoL
> 9/zAR33oSeh5C9pdttQZ9KgCzpZAqcTeMW5VaWGD2kfOJVNWjJ/dLmS/MJIm2nWG
> mWPK5k7OX+P48eqAo8aOlajk+NFpJGllmkXoNZL6WqRZXHGvgNOf5JRqcIrn8Gv8
> auiuG88PcHrZNH7OBTNzvHACFoFO40XvTqwnThpYxpmxZWQe1RHcPCghJmG5fXTl
> zJrpQyY+dTHEE5qTaC33HubRP+8CBtwsxx/7FEyBUzLPRvh6dN3sO0/3NVgeuT5U
> JK95bJ1C/G5CsLJX0aCF7sH0n7drJ26Kw3qUb/2CF7mmOhdBLUHYCqNl+tWWzls=
> =wCKz
> -----END PGP SIGNATURE-----
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe_at_jersey.dev.java.net
> For additional commands, e-mail: users-help_at_jersey.dev.java.net
>