⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 multimessageresources.java

📁 一个关于tlms的一个小程序 看看能否帮助到别人
💻 JAVA
字号:
/*
 * =========================================================================
 *
 *  This software is confidential and proprietary to szmx Pte. Ltd. You shall
 *  use this software only in accordance with the terms of the license
 *  agreement you entered into with szmx.  No aspect or part or all of this
 *  software may be reproduced, modified or disclosed without full and
 *  direct written authorisation from szmx.
 *
 *  szmx SUPPLIES THIS SOFTWARE ON AN 揂S IS?BASIS. szmx MAKES NO
 *  REPRESENTATIONS OR WARRANTIES, EITHER EXPRESSLY OR IMPLIEDLY, ABOUT THE
 *  SUITABILITY OR NON-INFRINGEMENT OF THE SOFTWARE. szmx SHALL NOT BE LIABLE
 *  FOR ANY LOSSES OR DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING,
 *  MODIFYING OR DISTRIBUTING THIS SOFTWARE OR ITS DERIVATIVES.
 *
 *  Change Revision
 * -------------------------------------------------------------------------
 *  Version    Remarks
 *  v1.0       - Initial Release
 *
 * =========================================================================
 */
package com.szmx.framework.util;

import org.apache.struts.util.MessageResourcesFactory;
import org.apache.struts.util.PropertyMessageResources;

import java.io.IOException;
import java.io.InputStream;
import java.util.Iterator;
import java.util.Properties;
import java.util.StringTokenizer;

/**
 * Concrete subclass of <code>MessageResources</code> that reads message keys
 * and corresponding strings from named property resources in the same manner
 * that <code>java.util.PropertyResourceBundle</code> does.  The
 * <code>base</code> property defines the base property resource name, and
 * must be specified.
 * <p>
 * <strong>IMPLEMENTATION NOTE</strong> - This class trades memory for
 * speed by caching all msgutil located via generalizing the Locale under
 * the original locale as well.
 * This results in specific msgutil being stored in the message cache
 * more than once, but improves response time on subsequent requests for
 * the same locale + key combination.
 *
 * @version 1.0
 *
 * @since 15/12/2005
 */

public class MultiMessageResources extends PropertyMessageResources {

    /**
     * Construct a new MultiPropertyMessageResources according to the
     * specified parameters.
     *
     * @param factory The MessageResourcesFactory that created us
     * @param config The configuration parameter for this MessageResources
     */
    public MultiMessageResources(MessageResourcesFactory factory,
                                    String config)
    {
        super(factory, config);
    }

    /**
     * Construct a new MultiPropertyMessageResources according to the
     * specified parameters.
     *
     * @param factory The MessageResourcesFactory that created us
     * @param config The configuration parameter for this MessageResources
     * @param returnNull The returnNull property we should initialize with
     */
    public MultiMessageResources(MessageResourcesFactory factory,
                                    String config, boolean returnNull)
    {
        super(factory, config, returnNull);
    }

    /**
     * @see org.apache.struts.util.PropertyMessageResources#loadLocale(java.lang.String)
     */
    protected synchronized void loadLocale(String localeKey)
    {
        if( config!= null && config.indexOf(",")<0)
        {
            super.loadLocale(localeKey);
            return;
        }
        if (log.isTraceEnabled())
        {
            log.trace("loadLocale(" + localeKey + ")");
        }
        // Have we already attempted to load msgutil for this locale?
        if (locales.get(localeKey) != null)
        {
            return;
        }
        locales.put(localeKey, localeKey);

        // Set up to load the property resource for this locale key, if we can
        StringTokenizer t = new StringTokenizer(config, ",");
        InputStream is = null;
        String name = null;
        Properties props = new Properties();
        ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
        if (classLoader == null)
        {
            classLoader = this.getClass().getClassLoader();
        }
        while (t.hasMoreTokens())
        {
            name = t.nextToken().replace('.', '/');
            if (localeKey.length() > 0)
            {
                name += "_" + localeKey;
            }
            name += ".properties";
            name = name.trim();

            // Load the specified property resource
            if (log.isTraceEnabled())
            {
                log.trace("  Loading resource '" + name + "'");
            }

            is = classLoader.getResourceAsStream(name);
            if (is != null)
            {
                try
                {
                    props.load(is);
                }
                catch (IOException e)
                {
                    log.error("loadLocale()", e);
                }
                finally
                {
                    try
                    {
                        is.close();
                    }
                    catch (IOException e)
                    {
                        log.error("loadLocale()", e);
                    }
                }
            }
        }

        if (log.isTraceEnabled())
        {
            log.trace("  Loading resource completed");
        }

        // Copy the corresponding values into our cache
        if (props.size() < 1)
        {
            return;
        }

        synchronized (messages)
        {
            Iterator names = props.keySet().iterator();
            while (names.hasNext())
            {
                String key = (String) names.next();
                if (log.isTraceEnabled())
                {
                    log.trace("  Saving message key '" + messageKey(localeKey, key));
                }
                messages.put(messageKey(localeKey, key), props.getProperty(key));
            }
        }
    }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -