users@jersey.java.net

[Jersey] Jersey-Guice chicken and egg problem

From: Christopher Piggott <cpiggott_at_gmail.com>
Date: Sat, 17 Dec 2011 12:56:19 -0500

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