dev@glassfish.java.net

Re: Local Strings in GlassFish

From: Jerome Dochez <jerome.dochez_at_oracle.com>
Date: Tue, 20 Jul 2010 23:38:52 -0700

On 7/20/10 2:55 PM, Byron Nevins wrote:
> I have a proposal for a new convention for getting localized Strings
> at runtime. We've discussed this in Admin meetings and it was agreed
> that it is useful.
>
> Now I'm opening it up to the entire GlassFish community. Please take
> a look at this SHORT simple proposal [1]. All comments are welcome.
>
> [1] http://blogs.sun.com/foo/entry/local_strings_in_glassfish_a
>
>
I have a counter proposal :)

Let's leverage injection to figure out which instance of StringManager
should be injected...

For instance :

@Service
@PerLookup
public class StringManager implements Injectable {

     StringManagerImpl delegate;

     public void injectedInto(Object target) {
         delegate = StringManager.getManager(target.getClass());
     }

     public String get(String label, Object... args) {
         return delegate.get(label, args);
     }
}

Now in the client code ...

@Service
public RandomCode implements RandomContract {

     @Inject
     StringManager strMgr;

     public void someMethod(....) {

         logger.log(strMgr.get("mylabe", foo, bar));

     }
}

The key to understand the code above is Injectable, check hk2 javadocs,
it means that the StringManager has a chance to know where it is being
injected into...

Advantages : no extra code needed per package, no static field whasoever
(YEAH !), every gets garbaged collected in the right time.
Disavantages : we have to change all StringManager pattern and it
requires DI (habitat lookup will not work here since Injectable
interface only works when injected).

We should do the same thing with Logger.

Jerome