Get request context object outside request context space

103 views
Skip to first unread message

Daniel Guryca

unread,
Dec 1, 2016, 3:37:41 AM12/1/16
to google-guice
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

Daniel Guryca

unread,
Dec 1, 2016, 3:54:53 AM12/1/16
to google-guice
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");
    }
}
Reply all
Reply to author
Forward
0 new messages