Discussion:
Jersey-Guice chicken and egg problem
Christopher Piggott
2011-12-17 17:56:19 UTC
Permalink
Hi,

This question seems to fall between jersey and guice, so I wasn't sure
where to ask. Ultimately, though, the end goal is "Make
GrizzlyWebServer instance available to resources" so I lean slightly
toward asking jersey folks.

My server startup looks like this (some class names changed to protect
the guilty)

ServletAdapter sa = new ServletAdapter();
sa.addServletListener( MyContextListener.class.getName() );
sa.addFilter( new GuiceFilter(), "guiceFilter", null );
GrizzlyWebServer ws = new GrizzlyWebServer( port, ".", use_ssl );
ws.addGrizzlyAdapter( sa, new String[] { "/" } );
ws.start();


start() causes grizzly to create the listener which calls my context
listener which creates an injector (using my modules).

The problem is, now I want to be able to get at certain things from
that GrizzlyWebServer instance (for example, statistics) so that I can
include them in a jersey resource. I would like to solve this by
creating the GWS with guice, but that's a chicken and egg problem - in
the above setup, it's the GWS that (indirectly, through the context
listener) creates the injector.

I can think of several awful ways to solve this, involving singletons
and "Providers" that operate outside of guice - yuck.

Is there a cleaner way? Like for example, is there some way to get at
the ws instance through the ContextListener?

--Chris
Marek Potociar
2011-12-20 09:36:48 UTC
Permalink
IMHO the problem is more suitable for users-***@public.gmane.org mailing
list as it seems to me that what you need is an integration between
Guice and Grizzly. Or am I missing it?

Marek
Post by Christopher Piggott
Hi,
This question seems to fall between jersey and guice, so I wasn't sure
where to ask. Ultimately, though, the end goal is "Make
GrizzlyWebServer instance available to resources" so I lean slightly
toward asking jersey folks.
My server startup looks like this (some class names changed to protect
the guilty)
ServletAdapter sa = new ServletAdapter();
sa.addServletListener( MyContextListener.class.getName() );
sa.addFilter( new GuiceFilter(), "guiceFilter", null );
GrizzlyWebServer ws = new GrizzlyWebServer( port, ".", use_ssl );
ws.addGrizzlyAdapter( sa, new String[] { "/" } );
ws.start();
start() causes grizzly to create the listener which calls my context
listener which creates an injector (using my modules).
The problem is, now I want to be able to get at certain things from
that GrizzlyWebServer instance (for example, statistics) so that I can
include them in a jersey resource. I would like to solve this by
creating the GWS with guice, but that's a chicken and egg problem - in
the above setup, it's the GWS that (indirectly, through the context
listener) creates the injector.
I can think of several awful ways to solve this, involving singletons
and "Providers" that operate outside of guice - yuck.
Is there a cleaner way? Like for example, is there some way to get at
the ws instance through the ContextListener?
--Chris
Loading...