Using existing or exposing Ratpack's Guice injector
I'm adding ratpack to a pre-existing app that uses a single instance of a class for access to a data store (multiple instances cannot be created due to locks on the data store).
The problem I'm having is that I can't make Ratpack use the same Guice injector as the rest of the app, so that there is only one instance of my @Singleton class. Ratpack creates its own injector, and I can't get access to it so that I can use the same instances in the rest of the app.
I was thinking that I could give Ratpack my injector to use, but the only means I have of putting things into Ratpack are: the handler factory classname, and the "others" property of the LaunchConfig (Strings only). As for retrieving Ratpack's injector so I can use it, the only means I have of getting things from Ratpack is: RatpackServer#getLaunchConfig()... which is fairly useless for finding arbitrary data.
I am thinking that perhaps I can create a custom implementation of the GuiceBackedHandlerFactory, so that it can use a pre-existing Guice injector. However, I'm not sure how to pass the injector into it aside from using a static singleton method (which I don't want to do). Also, in the default impelmentation of GuiceBackedHandlerFactory, there's some things going on with module registries and so forth and I'm not sure what to do about that...
Does anyone have an idea what the easiest/best way to accomplish my goal would be?
We could add methods to the Ratpack Guice class that use a parent injector (https://github.com/ratpack/ratpack/issues/142). Then you would have to do your own bootstrapping, as the RatpackMain entry points won't work for you.
You'd have to use the LaunchConfigBuilder to build a launch config (with your own HandlerFactory implemenation), then build a server using RatpackServerBuilder. All pretty easy.
What kind of app is this? Groovy or Java? Getting this working with the Groovy script app mode (i.e. reads ratpack.groovy) is going to be more work.
Re: Using existing or exposing Ratpack's Guice injector
@Marcin yeah that's pretty much what I was using. I had no problem getting ratpack to use my module, the problem was getting it to use the same injector.
@Luke sorry for the delayed response: that looks perfect, thanks for the help! I am using ratpack via Java because I didn't want to add Groovy to this particular application, so that's no problem. I am looking forward to your first releast of 0.9 so that I don't have to lock my app to snapshot versions any more!