суббота, 4 июня 2011 г.

Providing of some "yum-yum" features for project

Hi, everybody!
Without any doubts, I already began to believe strongly, that someone, who said that time passes very fast, was devilishly right. And today, I'm going to tell you, my dear reader, about what interesting I've done this week. Admittedly, I had some unsolved tasks at beginning of week. They, preferably, were related to inner components of my project.
The first of those tasks was creation of some kind of "cache" for loaded messages properties files. In a nutshell, I've created a java map with languages, mapped to the loaded properties files. First of all, it is very convinient to have such map, because it works like a cache and provides an opportunity to access translations without reloading resources, when current language is changing. I've assigned the task of creation such map to custom resources loader component. In fact, this map creates when creates an instance of resource loader component. After creation (we have a single instance of custom resource loader), that component will contain the map therein and other components will have a read-only access to him. And since an access don't mean any modification with the resources within cache, we should not do him (access) synchronized.. Basically, in case of using a single instance of resource loader we also won't use a lot of memory for such cache.
And now, I'm about to show you, how I'm loading the resources into map. Generally, I'm doing it within a single private method of custom resource loader class. Notably, that this method is called once at constructor of resource loader class. Also it is important to note that constructor of that class receives as parameter an absolute path to directory where resources are located. Next, location of that directory is passed in as parameter to method for creation map for resources. This method is shown below:
   
    private void loadResources(String basedir) {
        File propertiesDir = new File(basedir);
        for (File possibleFile : propertiesDir.listFiles()) {
            log.warn("filename : " + possibleFile.getAbsolutePath());
            if (possibleFile.getName().startsWith(FilterUtil.PREFIX) && possibleFile.getName().endsWith(FilterUtil.SUFFIX)) {
                Locale locale = parseLocaleFrom(possibleFile.getName(), FilterUtil.PREFIX);
                log.warn("locale : " + locale.toString());
                getResource().put(locale, getFileSystemResource(possibleFile.getAbsolutePath(), FilterUtil.PREFIX, locale));
                getAvailablelocales().add(locale);
            }
        }
        if (log.isWarnEnabled() && (getResource().size() == 0)) {
            log.warn("No properties files found.");
        }
    }



As you can see, at first I'm creating a file object for directory with resources to be able to iterate over the list of nested files and directories. And then, I'm iterating over all these nested files and checking if current file is messages properties file. If it's true, I'm trying to derive the locale from the file name (e.g. if file is named messages_it.properties the locale "it" will be derived). Next, I'm loading resource bundle for that locale and putting it resource bundle into caching map with derived locale as key.
The second task for this week was creation of the feature, which allows us to have one velocity toolbox per one http client's session. Before this week we had single velocity toolbox for all client's sessions. And, as you can guess,  it is awful, because we have locales overlaping when multiple users are running wizards at the same time. This problem causes because, from one hand, we configure one instance of velocity toolbox per one filter and, from the other hand, web container instantiates only one filter instance for <filter ..> tag per JVM for application. 
Desicion was to have map, that contains http session's id as key and configured toolbox context as value. And since toolbox context object can be merged with velocity context, we will get appropriate context object for current client's preferred locale and use it within velocity context when will rendering tempaltes. 
Also, during this week I done tasks related to navigation components appearence, made changes within stroing/retrieving locale parameters methods and continued integration of existing localization tool into database update wizard.
So, I guess it is enought for today. If you have any quesions or suggestions leave them in comments below,


With best regards, Taras Chorny.

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

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