воскресенье, 31 июля 2011 г.

Improving custom LocalizationTool class

Hi there,

During whole this week, I'm being worried with the best solution about using ResourceTool class (exactly our subclass of this - LocalizationTool). At first, let me tell you pre-history of this. At the beggining of this week me and my mentor were continuing review of code and were making testing of project. Notably, that we noticed one serious bug related to extending ResourceTool class. Even if this it worked, we weren't satisfied of such solution because it was not quite reusable. So, we were targeted on receiving more simple and more usable subclass of velocity resource tool at the same time.

As you can found out from the source code of ResourceTool (see the latest version on grepcode), guys, who has implemented ResourceTool class, has screwed up with design of that tool. But nevertheless, I did not believe, that they were so bleary-eyed  and, suspecting that something wrong with that "grepcode", I have downloaded source code of ResourceTool directly from repository. And, surprisingly, I found out that it quite differs from the "grepcode" version. Looking at that source code I noticed, that desired solution can be simply achieved by changing only one method that loads resource bundles. The only one method, it's great - I was thinking to myself!! And I've already done it and have tested. It works. See result version of code for localiztion tool class below:


/**
 * This class is intended for accessing {@link ResourceBundle} and formatting messages therein.
 */
@DefaultKey("l10n")
public class LocalizationTool extends ResourceTool {

/**
* Its need to override base class method to be able to change its locale property outside the
* class hierarchy
*/
@Override
public void setLocale(Locale locale) {
super.setLocale(locale);
}

/**
* To be able to load resource bundles outside the class path we need to override this method
*/
@Override
protected ResourceBundle getBundle(String baseName, Object loc) {
Locale locale = (loc == null) ? getLocale() : LocaleUtility.fromSpecification(String.valueOf(loc));
if (baseName == null || locale == null) {
return null;
}
return CustomResourseLoader.getInstance(null).getResourceBundle(locale);
}
}


This is truly very simple and much reusable then the one that was before. I think you'll like that solution because of its grace and simplicity. I've committed the appropriate changeset. Also I've merged trunk into my branch to keep  my project in accordance to current trunk code. I think, that received solution is much better that the one, that was before.

That is all for today, you can leave your opinion in the commants section below.

With best wishes, Taras Chorny!

Комментариев нет:

Отправить комментарий