I'm using Guice-Servlet, and I have a Localizer object that is request 
scoped: this Localizer is built using the locale from the request.

But I also need a default localizer at some places *where I'm not in a 
request*. This default Localizer would be a singleton created using the 
default Locale of the application.

I currently have a solution, but it involves catching an exception 
(ProvisionException) to know if the current scope is "request". I'd like to 
know if there is better way to determine the current scope, for the 
provider to return the correct Localizer?

My current code :

---------------------------------------------------

protected void bindRequestScopedLocalizer()
{
    bind(Key.get(ILocalizer.class, 
RequestScopedLocalizer.class)).to(MyLocalizer.class).in(ServletScopes.REQUEST);
}

@Provides
@Singleton
@DefaultLocalizer
public ILocalizer providesDefaultLocalizer()
{
    Locale locale = Locale.getDefault();
    TimeZone timeZone = TimeZone.getTimeZone("UTC");
    
    return new MyLocalizer(locale, timeZone);  
}

@Provides
protected ILocalizer providesLocalizer(Provider<HttpServletRequest> 
requestProvider, 
                                       @RequestScopedLocalizer 
Provider<ILocalizer> requestScopedLocalizerProvider,
                                       @DefaultLocalizer ILocalizer 
defaultLocalizer)
{
    try
    {
        @SuppressWarnings("unused")
        HttpServletRequest request = requestProvider.get();
    }
    // If we're not in a Request scope, we use the default Localizer.
    catch(ProvisionException ex)
    {
        return defaultLocalizer;
    }
    
    return requestScopedLocalizerProvider.get();
}

---------------------------------------------------

Thanks in advance!


-- 
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 [email protected].
To post to this group, send email to [email protected].
Visit this group at http://groups.google.com/group/google-guice?hl=en.
For more options, visit https://groups.google.com/groups/opt_out.


Reply via email to