On Sep 16, 4:09 pm, Leigh Klotz <[EMAIL PROTECTED]> wrote:
> Unfortunately, I have to report I get the same NullPointerException
> going the ContextScope route, if the ContextScope<Locale> is injected
> with a Provider<Locale>.  If I use a LocaleFactory.get static method,
> it works. (I noticed your DWR integration invokes static methods on
> existing DWR classes and none are injected.)

Ah, yes. I wrote that before I figured out the module injection hack
for method interceptors. I didn't need it for DWR originally.

So the real problem is getting scopes injected. The ContextScope stuff
is a red herring -- sorry! -- although if it makes your code look
nicer you could still use it once the real problem is solved. See
below.


> Do you think the method interceptor injection would work for me?  I
> admit I read it a couple of times and am not sure that it would.

I think it might. The important thing is to get Modules themselves
injected -- that's the tricky part of the code that my blog entry
refers to.

Once you have that, you can have one Module responsible for creating
and exporting the LocaleScope for bind time use, passing it as a
constructor argument, say, to other Modules that need it. (If you only
ever have one Injector, you can skip that complication and keep it a
singleton static field, LOCALE, but I think the flexibility is worth
the extra effort.) The Module that creates the LocaleScope also would
register it for injection (member injection only, not constructor
injection, since it has to exist for bind time use).

Improvising a bit:

    public class LocaleScope
            extends AbstractSimpleContextScope<Locale> { // let's say
        @Inject private volatile Provider<Locale> localeProvider;
        // use localeProvider as you did in your example
    }

    public class LocaleScopeProducerModule
            extends AbstractSelfInjectingModule { // for want of a
better name
        private final LocaleScope localeScope = new LocaleScope();
        protected void configure() {
            // This method is provided by AbstractSelfInjectingModule.
            // It will cause member injection on localeScope *and* on
            // this Module (vacuously, in this case):
            registerForInjection(localeScope);
        }
        public LocaleScope getLocaleScope() { return localeScope; }
    }

    public class LocaleScopeClientModule1 extends AbstractModule {
        private final LocaleScope localeScope;
        LocaleScopeClientModule1(LocaleScope localeScope) {
            this.localeScope = localeScope;
        protected void configure() {
            bind(something).to(somethingElse).in(localeScope);
            // ...
        }
    }

    public class RootModule extends AbstractModule {
        protected void configure() {
            LocaleScopeProducerModule lspm = new
LocaleScopeProducerModule();
            install(lspm);
            LocaleScope localeScope = lspm.getLocaleScope();
            install(new LocaleScopeClientModule1(localeScope));
            install(new LocaleScopeClientModule2(localeScope));
        }
    }

You don't need a RootModule -- that's just for presentation.

AbstractSelfInjectingModule is named
org.directwebremoting.guice.util.AbstractModule in the DWR repository.
Reusing AbstractModule might not be a good idea (though I kind of like
it), but it's too late for DWR -- however, you can use
AbstractSelfInjectingModule or invent your own name.

To be safe, I made Provider<Locale> in LocaleScope a volatile field,
since I'm never entirely sure whether there is a happens-before edge
between the static injection phase of Injector creation and any use of
that Injector. If you're confident that there is, feel free to remove
the volatile keyword.

A side note: Consider using a ConcurrentHashMap for your Locale-
specific cache, even if you don't expect multiple threads to be
accessing your LocaleScope.

--tim
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"google-guice" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/google-guice?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to