Thanks Tim! I've enjoyed your Restlet-Guice pages and appreciate
this. Your ContextScope abstraction makes the implementation much
cleaner and easier to read and I hope it gets incorporated into a
Guice extension.
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.)
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.
Here's what I did, in slightly more detail:
- I extracted ContextScope and its supporting classes from the latest
release of DWR and modeled a LocaleScope after your DWRScopes class
(see snippets below).
- When I used a static LocaleFactory.get() call inside LocaleScope to
obtain the current Locale, everything worked.
- However, when I used a Provider<Locale> injected into
LocaleProvider, I get the same NullPointerException in
com.google.inject.InjectorImpl$8.get(InjectorImpl.java:1037) that I
get with my open-coded version of LocaleScope which I posted
previously in this thread.
Snippets:
This works:
public class LocaleScopes {
public static final ContextScope<Locale> LOCALE =
new AbstractSimpleContextScope<Locale>(Locale.class,
"LocaleScopes.LOCALE") {
private Map<Locale, Map<String, Object>> cache = new
HashMap<Locale, Map<String, Object>>();
public Locale get() { return LocaleFactory.get(); }
...
}
}
This fails:
public void configure() {
...
bind(Locale.class).toProvider(LocaleProvider.class);
requestInjection(ProviderLocaleScopes.LOCALE);
}
public class LocaleScopes {
public static final ContextScope<Locale> LOCALE =
new AbstractSimpleContextScope<Locale>(Locale.class,
"LocaleScopes.LOCALE") {
private Map<Locale, Map<String, Object>> cache = new
HashMap<Locale, Map<String, Object>>();
@Inject private Provider<Locale> localeProvider;
public Locale get() { return localeProvider.get(); }
...
}
}
Thank you,
Leigh.
On Sep 16, 6:06 am, Tim Peierls <[EMAIL PROTECTED]> wrote:
> If you get too frustrated waiting for this, the hack I described for
> injecting method interceptors in Guice 1.0 can be applied to scopes as
> well:
>
> http://tembrel.blogspot.com/2007/09/injecting-method-interceptors-in-...
>
> For a more comprehensive solution, if you're willing to do a little
> legwork, the DWR-Guice integration package I described contains a mini-
> framework for writing Guice scopes that depend on the notion of a
> "current" context, where in your case the context would be the Locale.
> The link in this blog entry
>
> http://tembrel.blogspot.com/2007/04/guice-support-in-dwr.html
>
> is to older code, in which the DWR and Guice concerns are entangled in
> the same package. The most recent code on the DWR site contains all
> the non-DWR Guice support in an separate independent package,
> org.directwebremoting.guice.util, but I haven't had time to bundle it
> up for nicely for general consumption. (Several folks have taken a
> look at it and said the lack of complete documentation was a
> showstopper for them.)
>
> --tim
>
> On Sep 15, 7:58 pm, Leigh Klotz <[EMAIL PROTECTED]> wrote:
>
> > On Sep 13, 1:21 am, "[EMAIL PROTECTED]" <[EMAIL PROTECTED]> wrote:
>
> > > On Sep 12, 5:00 pm, Leigh Klotz <[EMAIL PROTECTED]> wrote:
>
> > > > Does anybody have suggestions about how I can let my LOCALE Scope
> > > > object use the Guice modules to obtain a Locale Provider?
>
> > > Your scopes shouldn't be static.
> > > There's a new method in recent snapshots called requestInjection()
> > > on AbstractModule. Use this method to get your
> > > scope instance injected in the same place that
> > > you call bindScope().
>
> > Thank you! This looks like just what I want. Unfortunately, I can't
> > get it to work.
>
> > First Issue (minor):
>
> > I looked in SVN head for examples on how to make the scope not be
> > static.
> > SVN head still seems to use static scopes:
> > Inhttp://code.google.com/p/google-guice/source/browse/trunk/servlet/src...
> > public static final Scope REQUEST = new Scope()
>
> > I found I could use
> > bindScope(LocaleScoped.class, LocaleScope.LOCALE);
> > requestInjection(LocaleScope.LOCALE);
> > and this appeared to work, until I ran into another bug (see bellw).
> > I'm not clear on what I would do with a non-static scope (i.e., how to
> > refer to it elsewhere, but I suppose this will become clear when the
> > Guice ServletScopes get re-written to be non-static.)
>
> > Second issue (killer):
> > Once I try to use the Provider<Locale> that I inject into my Locale
> > Scope, in snapshot20080818 but I run into a NullPointerException. I
> > think it's the same issue
> > ashttp://code.google.com/p/google-guice/issues/detail?id=222
> > which was from snapshot20080713.
>
> > In 20080818 the NullPointerException is in
> > com.google.inject.InjectorImpl$8.get(InjectorImpl.java:1037)
> > instead of line 1015.
>
> > To work around the Scope issue with NullPointerException, I tried
> > using a Provider<MyClass> instead of a Scope for @Locale MyClass, but
> > ran into the problem that I can't force MyClassProvider to use Guice
> > to create the MyClass object I need to populate its cache; i.e., I see
> > no equivalent of the creator.get() call from Scope.scope, except of
> > course provider.get(), which I obviously can't use because it would
> > just call MyClassProvider.get() infinitely. (I suppose I could do
> > some hack with two Providers for MyClass, and use an annotation to
> > mark the real one. Ugh.)
>
> > So, I think the right thing is to wait for Issue 222 to be resolved.
>
> > Thank you,
> > Leigh.
--~--~---------~--~----~------------~-------~--~----~
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
-~----------~----~----~----~------~----~------~--~---