Discussion:
Get request context object outside request context space
Daniel Guryca
2016-12-01 08:37:41 UTC
Permalink
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
--
You received this message because you are subscribed to the Google Groups "google-guice" group.
To unsubscribe from this group and stop receiving emails from it, send an email to google-guice+***@googlegroups.com.
To post to this group, send email to google-***@googlegroups.com.
Visit this group at https://groups.google.com/group/google-guice.
To view this discussion on the web visit https://groups.google.com/d/msgid/google-guice/e1381e1e-9393-416a-bb79-241f3a58d91c%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Daniel Guryca
2016-12-01 08:54:52 UTC
Permalink
Sorry I made a small mistake in my code (caused by copy/paste) so below now
hopefully without bugs:


class NinjaAppModule extends AbstractModule {
@Inject
Injector injector;

configure() {

bind(ServerConfig.class).toProvider(ServerConfigProvider.class).asEagerSingleton();

bind(EbeanServer.class).toProvider(EbeanServerProvider.class).asEagerSingleton();
*CurrentUserProviderImpl userProvider = new
CurrentUserProviderImpl(injector);*
* bind(CurrentUserProvider.class).toInstance(userProvider);*
}
}


class CurrentUserProviderImpl implements *CurrentUserProvider* { //* CurrentUserProvide is
Ebean's interface that does not know anything about Guice*
Injector injector;

public CurrentUserProviderImpl(*Injector injector*) {
this.injector = injector;
}

@Override
public Long currentUser() {
RequestSession session =
*injector.getInstance(RequestSession.class);*
return session.get("userID");
}
}
--
You received this message because you are subscribed to the Google Groups "google-guice" group.
To unsubscribe from this group and stop receiving emails from it, send an email to google-guice+***@googlegroups.com.
To post to this group, send email to google-***@googlegroups.com.
Visit this group at https://groups.google.com/group/google-guice.
To view this discussion on the web visit https://groups.google.com/d/msgid/google-guice/07fc0b67-5db4-4c89-beb1-c00552e128f3%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Loading...