Hey guys,
I'm lost while associating Ebean ORM server with a CurrentUser in Ninja framework that uses Guice everywhere.
Ebean server is initialized only once during the whole Ninja app start so it's a singleton.
So that I can use Ebean's model(entity) annotations @WhoCreated and @WhenCreated on my database models I have to give to ebean server an object that implements Ebean's simple interface CurrentUserProvider. Obviously in this implementation I need to get Ninja's request or session objec to get an actual current user ID. These are usually accesible in Ninja controllers since one can @Inject them there.
But now how can I "Inject" that session or request object into CurrentUser object if that object is initialized and created during a Ninja app start. So during that operation there is not yet any request, session. Since these are related with controllers (request cycle) only. Ebean server internally does not know anything about Guice (they do not use it).
Simplified example:
class NinjaAppModule extends AbstractModule {
configure() {
bind(ServerConfig.class).toProvider(ServerConfigProvider.class).asEagerSingleton();
bind(EbeanServer.class).toProvider(EbeanServerProvider.class).asEagerSingleton();
CurrentUserProviderImpl userProvider = new CurrentUserProviderImpl(properties, injector);
bind(CurrentUserProvider.class).toInstance(userProvider);
}
}
class CurrentUserProviderImpl implements CurrentUserProvider { // this is Ebean's interface that does not anything about Guice
Injector injector;
CurrentUserProviderImpl(Injector injector) {
this.injector = injector;
}
@Override
public Long currentUser() {
RequestSession sess = injector.getInstance(RequestSession.class);
return sess.get("userID");
}
}
Yes I know I could somehow use ThreadLocals in my request filter and set there userId but how to avoid using ThreadLocal ? Is anything like that possible with Guice ??
Thank you
Regards