users@jersey.java.net

Re: [Jersey] Guice / Reference to WebApplication

From: Johannes Schneider <mailings_at_cedarsoft.com>
Date: Fri, 04 Jun 2010 13:50:09 +0200

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

On 06/04/2010 11:14 AM, Paul Sandoz wrote:
> 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?

Good point! Unfortunately the reflection code is executed every time
then...
There are discussions at Guice about lazy singletons.

But the problem is that really ugly double reflection approach...

> 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.

Do you have sample code for this? Don't want to run into any problems....


Thanks,

Johannes

>
> Paul.
>
> On Jun 3, 2010, at 8:17 PM, Johannes Schneider wrote:
>
> 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
>
>
>>
- ---------------------------------------------------------------------
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)

iQEcBAEBAgAGBQJMCOhwAAoJEAytD9R7Qv6d7XgIAN1ULvxlMBCA7BeHps3M/Qb1
K8LWNU6RaFgtdRMMxQw1A/yCNPJw3AJB86pxWqSXRKDZrSh93tHuwG5NfoiZn+dq
6mToDMJtOFA6SDIqYcC8BuW2LoVC7cVaFWSlU4pRcNddo32y83ES90HGE1XgFYuf
IWWngpm033A27aJWrCKADPGx1zvo8Lp77U/D/6xpvwjkDIcuXyKBkqZvOygWZ3HM
6U6zv0YWfME6SEU7dpyEif4gS4L648Qdg2BPs7TT3wLIVPLQx7Ec/+VadjDVt9iI
bn1w0vg4k9lJsX+BmsefvxSoVccPLmCDDvdLXlKGp9Gz4Fbf2QuaT08sXgdU1Hg=
=TiUu
-----END PGP SIGNATURE-----