jp.terasoluna.fw.web.struts.action.GlobalMessageResources.java Source code

Java tutorial

Introduction

Here is the source code for jp.terasoluna.fw.web.struts.action.GlobalMessageResources.java

Source

/*
 * Copyright (c) 2007 NTT DATA Corporation
 *
 * 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 jp.terasoluna.fw.web.struts.action;

import java.util.ArrayList;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.Properties;

import jp.terasoluna.fw.util.PropertyUtil;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.struts.util.MessageResources;
import org.apache.struts.util.MessageResourcesFactory;

/**
 * ?AVXe?bZ?[W\?[X??NX?B
 *
 * <p>
 * Strutsdl?A?bZ?[W\?[X`t@Cp???A
 * `W?[?AW?[
 * ?bZ?[W\?[XI`?B<br>
 * TERASOLUNAW?[?A
 * ?bZ?[W?AVXe?bZ?[Wp@?B<br>
 * NX?AVXe?it?[??[N?j?bZ?[W\?[X?A
 * ?bZ?[W\?[X??ATERASOLUNAt?[??[N?A
 * ?bZ?[W\?[XNXpQ??B<br>
 * </p>
 * <h5>??</h5>
 * <ol>
 *  <li>TERASOLUNAt?[??[NMessageResources?A
 *      SNXVXe?A?bZ?[W\?[X
 *      Kv?B</li>
 *  <li>?bZ?[W\?[X???B</li>
 * </ol>
 * <h5>?bZ?[W\?[X?</h5>
 * <p>
 * ?bZ?[W\?[X?AftHg?bZ?[W\?[X`
 * t@C?B
 * ?bZ?[W\?[X`t@CftHgt@C
 * application-messages.properties??B
 * t@C?X???A
 * VXe?v?peBt@C?isystem.properties?j
 * L?[??s?B<br>
 * <pre><code>
 * application.messages=sample1-messages
 * </code></pre>
 * application.messages?A.properties?t@C?B
 * <b>K.properties?L?q?B</b><br>
 * </p>
 * <h5>?bZ?[W\?[X`t@Cp</h5>
 * ?bZ?[W?bZ?[W\?[X`t@C
 * ?iapplication-messages.properties?j
 * ?AVXe?v?peBt@C?isystem.properties?j
 * `t@Ct@C?A
 * LL?[?iadd.message.file.x?j
 * gpOt@Cw?A
 * ?bZ?[W\?[X?B<br>
 * <code><pre>
 * add.message.file.1=app1-message
 * add.message.file.2=app2-message
 * </pre></code>
 * v?peBL?[1n?Arr????A
 * Ot@C??I?B<br>
 * <h5>VXe?bZ?[W\?[X?</h5>
 * GlobalMessageResources?A
 * ftHgsystem-message.properties??[h?A
 * VXe?bZ?[W?B
 * VXe?bZ?[W\?[X`t@C?A?X?B
 *
 * @see jp.terasoluna.fw.web.struts.action.DBMessageResourcesFactory
 * @see jp.terasoluna.fw.web.struts.action.DBMessageResources
 * @see jp.terasoluna.fw.web.struts.action.PropertyMessageResourcesExFactory
 * @see jp.terasoluna.fw.web.struts.action.PropertyMessageResourcesEx
 *
 */
public final class GlobalMessageResources extends MessageResources {

    /**
     * VAo?[WID
     */
    private static final long serialVersionUID = -1224092114113256826L;

    /**
     * ?ONX?B
     */
    @SuppressWarnings("hiding")
    private static Log log = LogFactory.getLog(GlobalMessageResources.class);

    /**
     * t?[??[N?bZ?[W??B
     */
    private Map<String, String> fwMessages = new HashMap<String, String>();

    /**
     * VXe?bZ?[W??B
     */
    private Map<String, String> globalMessages = new HashMap<String, String>();

    /**
     * t?[??[N?bZ?[W\?[X?B
     */
    private static final String SYSTEM_MESSAGE = "system-messages";

    /**
     * ftHgAvP?[V?bZ?[W\?[X?B
     */
    private static final String DEFAULT_APPLICATION_MESSAGE = "application-messages";

    /**
     * <code>system.properties</code>L?q
     * ?bZ?[W\?[X?B
     */
    private static final String APPLICATION_CONFIG_KEY = "application.messages";

    /**
     * ?[g?bZ?[Wt@CL?q?ApO?bZ?[Wt@C?B
     */
    private static final String ADD_MESSAGES_FILE = "add.message.file.";

    /**
     * VOgIuWFNg?B
     */
    private static GlobalMessageResources globalMessageResources = null;

    /**
     * NXVOgCX^Xp?B
     *
     * @return GlobalMessageResources NXVOgCX^X
     */
    public static GlobalMessageResources getInstance() {
        if (globalMessageResources == null) {
            synchronized (GlobalMessageResources.class) {
                GlobalMessageResources createdResources = new GlobalMessageResources(null, SYSTEM_MESSAGE);
                globalMessageResources = createdResources;
            }
        }
        return globalMessageResources;
    }

    /**
     *  wp??[^
     *  <code>GlobalMessageResources</code> ???B
     *
     * @param factory ?bZ?[W\?[Xt@Ng
     * @param config  <code>MessageResource</code> ?p??[^
     */
    private GlobalMessageResources(MessageResourcesFactory factory, String config) {
        super(factory, config);
        globalInit();
        applicationInit();
    }

    /**
     * v?peBt@C?A}bvl?s?B
     */
    private synchronized void globalInit() {

        // ?bZ?[WNA
        fwMessages.clear();

        // ?bZ?[W\?[Xt@C??[h?B
        Properties prop = PropertyUtil.loadProperties(this.config);
        if (prop == null) {
            // ???s?I?B
            return;
        }
        Enumeration keyEnum = prop.propertyNames();
        while (keyEnum.hasMoreElements()) {
            Object keyObj = keyEnum.nextElement();
            Object value = prop.get(keyObj);
            if (log.isDebugEnabled()) {
                log.debug("Saving framework message key [" + keyObj + "]" + "value [" + value + "]");
            }
            fwMessages.put((String) keyObj, (String) value);
        }
    }

    /**
     * ?bZ?[W\?[Xt@C??[h
     * ?bZ?[W\?[X?s?B
     */
    private synchronized void applicationInit() {
        String appKey = PropertyUtil.getProperty(APPLICATION_CONFIG_KEY);
        if (appKey == null) {
            // L?[???AftHg
            // ?bZ?[W\?[X??B
            appKey = DEFAULT_APPLICATION_MESSAGE;
        }

        // ?[gv?peB
        Properties prop = PropertyUtil.loadProperties(appKey);
        if (prop == null) {
            // ???s?I?B
            return;
        }
        // ?[g?bZ?[W??bZ?[W\?[X
        // Mapo^?B
        Map<String, String> rootAppricationMap = getRootApplicationMap(prop);
        // ??s?bZ?[W\?[X?B
        Map<String, String> addApplicationMap = getAddApplicationMap(prop, appKey);
        // }bv}?[W
        addApplicationMap.putAll(rootAppricationMap);
        globalMessages = addApplicationMap;
    }

    /**
     * ?[g?bZ?[Wt@CL?q?bZ?[W}bv
     * p?B???}bvp?B
     *
     * @param prop ?[gv?peBt@C
     * @return ?[gv?peBt@CL?q?bZ?[W
     */
    private Map<String, String> getRootApplicationMap(Properties prop) {
        Map<String, String> rootApplicationMap = new HashMap<String, String>();
        // L?[
        Iterator it = prop.keySet().iterator();
        while (it.hasNext()) {
            String key = (String) it.next();
            // L?[?Aadd.message.file.n?O?B
            if (key.startsWith(ADD_MESSAGES_FILE)) {
                continue;
            }
            String value = prop.getProperty(key);
            if (log.isDebugEnabled()) {
                log.debug("Saving root-application message key [" + key + "]" + "value [" + value + "]");
            }
            rootApplicationMap.put(key, value);
        }
        return rootApplicationMap;
    }

    /**
     * ?[g?bZ?[W?AO?bZ?[W\?[Xt@C
     * ?A?bZ?[W?B
     * Ot@C???}bvp?B
     * ?[gt@C??[ht@Cv???A
     * iv?[v?A?B
     *
     * @param prop ?[gv?peBt@C
     * @param rootProperty ?[gv?peBt@C
     * @return O?bZ?[W}bv
     */
    private Map<String, String> getAddApplicationMap(Properties prop, String rootProperty) {
        Map<String, String> addApplicationMap = new HashMap<String, String>();
        List<String> fileNameList = new ArrayList<String>();
        for (int i = 1;; i++) {
            // Ot@C
            String fileName = prop.getProperty(ADD_MESSAGES_FILE + i);
            if (fileName == null) {
                // t@C???A?I
                break;
            } else if (fileName.equals(rootProperty)) {
                // ?[gt@Cv?A?I
                // (iv?[v)
                break;
            }
            fileNameList.add(fileName);
        }

        // t@C?A?Ot@C??[h?A
        // }bv?B
        Iterator fileNameIt = fileNameList.iterator();
        while (fileNameIt.hasNext()) {
            String outerFileName = (String) fileNameIt.next();
            Properties outerProp = PropertyUtil.loadProperties(outerFileName);
            // Ot@CK?
            if (outerProp == null) {
                if (log.isWarnEnabled()) {
                    log.warn("\"" + outerFileName + "\" is illegal.");
                }
                continue;
            }
            // Ot@CL?[
            Iterator outerFileKeyIt = outerProp.keySet().iterator();
            while (outerFileKeyIt.hasNext()) {
                String outerMessageKey = (String) outerFileKeyIt.next();
                String outerValue = outerProp.getProperty(outerMessageKey);
                if (log.isDebugEnabled()) {
                    log.debug("Saving outer-file-application message key [" + outerMessageKey + "]" + "value ["
                            + outerValue + "]");
                }
                addApplicationMap.put(outerMessageKey, outerValue);
            }
        }
        return addApplicationMap;
    }

    /**
     * ?bZ?[Wp?B
     * ?bZ?[WD???A
     * <ol>
     *   <li>?bZ?[W\?[X</li>
     *   <li>VXe?bZ?[W\?[X</li>
     * </ol>
     * ??B
     *
     * @param locale ?P?[IuWFNg
     * @param key ?bZ?[W\?[XL?[
     * @return ?bZ?[W
     */
    @Override
    public String getMessage(Locale locale, String key) {
        if (key == null) {
            return null;
        }
        // ?bZ?[W??A^?[?B
        String globalMsg = globalMessages.get(key);
        if (globalMsg != null) {
            return globalMsg;
        }
        // t?[??[N?bZ?[Wp?B
        return fwMessages.get(key);
    }
}