net.kamhon.ieagle.util.MessageFactory.java Source code

Java tutorial

Introduction

Here is the source code for net.kamhon.ieagle.util.MessageFactory.java

Source

/*
 * Copyright 2012 Eng Kam Hon (kamhon@gmail.com)
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *      http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package net.kamhon.ieagle.util;

import java.lang.reflect.Field;
import java.util.Locale;
import java.util.Map;
import java.util.Properties;
import java.util.PropertyResourceBundle;
import java.util.ResourceBundle;

import net.kamhon.ieagle.exception.SystemErrorException;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

public class MessageFactory {
    private static final Log log = LogFactory.getLog(MessageFactory.class);

    private static MessageFactory singleton;

    private Properties MESSAGE_RESOURCES = new Properties();
    private boolean isAlwaysReload;

    private MessageFactory() {
    }

    public static MessageFactory getMessageFactory() throws SystemErrorException {
        if (singleton == null) {
            try {
                singleton = new MessageFactory();
            } catch (SystemErrorException ex) {
                singleton = null;
                throw ex;
            }
        }
        return singleton;
    }

    public PropertyResourceBundleMorpher getResourceBundle(String key, Locale locale) throws SystemErrorException {
        processIsAlwaysReload();

        setResources(key, locale);
        return (PropertyResourceBundleMorpher) MESSAGE_RESOURCES.get(key + locale.toString());
    }

    private void processIsAlwaysReload() {
        if (isAlwaysReload()) {
            // remove all object
            MESSAGE_RESOURCES.clear();

            Class<?> type = ResourceBundle.class;
            try {
                Field cacheList = type.getDeclaredField("cacheList");
                cacheList.setAccessible(true);

                Map<?, ?> cache = (Map<?, ?>) cacheList.get(ResourceBundle.class);
                cache.clear();

                log.debug("cache.size() = " + cache.size());
            } catch (Exception ex) {
                log.fatal(ex, ex.fillInStackTrace());
            }
        }
    }

    private void setResources(String key, Locale locale) throws SystemErrorException {
        if (!MESSAGE_RESOURCES.containsKey(key + locale.toString())) {
            try {
                PropertyResourceBundle bundle = (PropertyResourceBundle) PropertyResourceBundle.getBundle(key,
                        locale);
                PropertyResourceBundleMorpher morph = new PropertyResourceBundleMorpher(bundle);
                MESSAGE_RESOURCES.put(key + locale.toString(), morph);
            } catch (Exception ex) {
                log.fatal("ERROR SETUP MESSAGE FACTORY!!. APPLICATION FAILURE!!.", ex);
                throw new SystemErrorException(ex);
            }
        }
    }

    public boolean isAlwaysReload() {
        return isAlwaysReload;
    }

    /**
     * <p>
     * This set true during development for fasten development time. But this is not recommend set to true for
     * production because will drop the performance.
     * <p>
     * 
     * @param isAlwaysReload
     */
    public void setAlwaysReload(boolean isAlwaysReload) {
        this.isAlwaysReload = isAlwaysReload;
    }
}