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

📄 messagemanager.java

📁 intra_mart是日本NDD公司开发的著名企业应用解决方案中间件。集成了J2ee服务器
💻 JAVA
字号:
/*
 * MessageManager.java
 *
 * Created on 2003/08/05, 18:00
 */

package jp.co.intra_mart.framework.base.message;

import java.text.MessageFormat;
import java.util.Locale;
import java.util.MissingResourceException;
import java.util.ResourceBundle;

import jp.co.intra_mart.framework.system.log.LogConstant;
import jp.co.intra_mart.framework.system.log.LogManager;
import jp.co.intra_mart.framework.system.property.PropertyHandlerException;
import jp.co.intra_mart.framework.system.property.PropertyManager;
import jp.co.intra_mart.framework.system.property.PropertyManagerException;

/**
 * 儊僢僙乕僕儅僱乕僕儍偱偡丅
 *
 * @author INTRAMART
 * @since 4.2
 */
public class MessageManager {

    /**
     * 儊僢僙乕僕僼儗乕儉儚乕僋偺儘僌偺僾儗僼傿僢僋僗
     */
    static String LOG_HEAD = "[J2EE][Message]";

    /**
     * 儊僢僙乕僕僾儘僷僥傿僴儞僪儔偺僉乕
     */
    public static final String MESSAGE_PROPERTY_HANDLER_KEY = "i18n_message";

    /**
     * 儊僢僙乕僕儅僱乕僕儍庢摼僼儔僌
     */
    private static Boolean semaphore = new Boolean(true);

    /**
     * 儊僢僙乕僕儅僱乕僕儍
     */
    private static MessageManager manager;

    /**
     * 儊僢僙乕僕僾儘僷僥傿僴儞僪儔
     */
    private MessagePropertyHandler messagePropertyHandler;

    /**
     * MessageManager傪惗惉偡傞僐儞僗僩儔僋僞偱偡丅
     * 偙偺僐儞僗僩儔僋僞偼柧帵揑偵屇傃弌偡偙偲偼偱偒傑偣傫丅
     *
     * @throws MessageManagerException 儊僢僙乕僕儅僱乕僕儍偺惗惉偵幐攕偟偨
     */
    private MessageManager() throws MessageManagerException {
        PropertyManager propertyManager;

        // 僾儘僷僥傿儅僱乕僕儍偺庢摼
        try {
            propertyManager = PropertyManager.getPropertyManager();
        } catch (PropertyManagerException e) {
            String message = null;
            try {
                message =
                    ResourceBundle
                        .getBundle("jp.co.intra_mart.framework.base.message.i18n")
                        .getString("MessageManager.FailedToGetPropertyManager");
            } catch (MissingResourceException ex) {
            }
            throw new MessageManagerException(message, e);
        }

        // 儊僢僙乕僕僾儘僷僥傿僴儞僪儔偺庢摼
        try {
            this.messagePropertyHandler =
                (MessagePropertyHandler)propertyManager.getPropertyHandler(
                    MESSAGE_PROPERTY_HANDLER_KEY);
        } catch (PropertyHandlerException e) {
            String message = null;
            try {
                message =
                    ResourceBundle
                        .getBundle("jp.co.intra_mart.framework.base.message.i18n")
                        .getString("MessageManager.FailedToGetMessagePropertyHandler");
            } catch (MissingResourceException ex) {
            }
            throw new MessageManagerException(
                message + " : " + MESSAGE_PROPERTY_HANDLER_KEY,
                e);
        }
    }

    /**
     * 儊僢僙乕僕儅僱乕僕儍傪庢摼偟傑偡丅
     *
     * @return 儊僢僙乕僕儅僱乕僕儍
     * @throws MessageManagerException 儊僢僙乕僕儅僱乕僕儍偺惗惉偵幐攕偟偨
     */
    public static MessageManager getMessageManager()
        throws MessageManagerException {

        if (manager == null) {
            synchronized (semaphore) {
                if (manager == null) {
                    manager = new MessageManager();
                }
                String message = null;
                try {
                    message =
                        ResourceBundle
                            .getBundle("jp.co.intra_mart.framework.base.message.i18n")
                            .getString("MessageManager.SuccessedToCreateManager");
                } catch (MissingResourceException ex) {
                }
                LogManager.getLogManager().getLogAgent().sendMessage(
                    MessageManager.class.getName(),
                    LogConstant.LEVEL_INFO,
                    LOG_HEAD + message);
            }
        }

        return manager;
    }

    /**
     * 儘働乕儖傪巜掕偣偢丄僉乕偵奩摉偡傞儊僢僙乕僕傪庢摼偟傑偡丅
     * 偙偺儊僜僢僪偼{@link #getMessage(java.lang.String, java.lang.String, java.lang.Object[], java.util.Locale) getMessage(application, key, args, java.util.Locale.getDefault())}傪屇傫偩偲偒偲摨偠摦嶌傪偟傑偡丅
     *
     * @param application 傾僾儕働乕僔儑儞ID
     * @param key 僉乕
     * @param args 儊僢僙乕僕偵愝掕偡傞僷儔儊乕僞
     * @return 儊僢僙乕僕乮懚嵼偟側偄応崌<code>???<I>key</I>???</code>乯
     */
    public String getMessage(String application, String key, Object[] args) {
        return getMessage(application, key, args, Locale.getDefault());
    }

    /**
     * 儘働乕儖傪巜掕偟丄僉乕偵奩摉偡傞儊僢僙乕僕傪庢摼偟傑偡丅
     * 巜掕偝傟偨傾僾儕働乕僔儑儞ID丄僉乕丄儘働乕儖偵堦抳偡傞儊僢僙乕僕傪僾儘僷僥傿偐傜庢摼偟丄
     * 僷儔儊乕僞傪儊僢僙乕僕撪晹偵杽傔崬傫偩寢壥傪曉偟傑偡丅<BR><BR>
     * 偙偺儊僜僢僪偱偼撪晹偱埲壓偺傛偆側摦嶌傪偟傑偡丅
     * <OL>
     * <LI>{@link #getMessagePropertyHandler()}傪屇傃丄MessagePropertyHandler傪庢摼偡傞丅
     * <LI>{@link MessagePropertyHandler#getMessage(java.lang.String, java.lang.String, java.util.Locale) MessagePropertyHandler.getMessage(application, key, locale)}傪屇傃丄儊僢僙乕僕傪庢摼偡傞丅
     * <LI><code>new java.text.MessageFormat(<I>忋偱庢摼偟偨儊僢僙乕僕</I>).format(args)</code>傪屇傃丄僷儔儊乕僞傪杽傔崬傫偩儊僢僙乕僕傪曉偡丅
     * </OL>
     *
     * @param application 傾僾儕働乕僔儑儞ID
     * @param key 僉乕
     * @param args 儊僢僙乕僕偵愝掕偡傞僷儔儊乕僞
     * @param locale 儘働乕儖
     * @return 儊僢僙乕僕乮懚嵼偟側偄応崌<code>???<I>key</I>???</code>乯
     */
    public String getMessage(
        String application,
        String key,
        Object[] args,
        Locale locale) {

        String errorMessage = "???" + key + "???";
        String original = null;

        // 僾儘僷僥傿僴儞僪儔偺庢摼
        MessagePropertyHandler handler = getMessagePropertyHandler();
        if (handler == null) {
            String warning = null;
            try {
                warning =
                    ResourceBundle
                        .getBundle("jp.co.intra_mart.framework.base.message.i18n")
                        .getString("MessageManager.FailedToGetMessagePropertyHandler");
            } catch (MissingResourceException ex) {
            }
            LogManager.getLogManager().getLogAgent().sendMessage(
                MessageManager.class.getName(),
                LogConstant.LEVEL_WARNNING,
                LOG_HEAD
                    + warning
                    + " : application = "
                    + application
                    + ", key = "
                    + key
                    + ", locale = "
                    + locale
                    + " : getMessage(String, String, Object[], Locale)");
            return errorMessage;
        }

        // 儊僢僙乕僕偺庢摼
        try {
            original = handler.getMessage(application, key, locale);
        } catch (MessagePropertyException e) {
            String warning = null;
            try {
                warning =
                    ResourceBundle
                        .getBundle("jp.co.intra_mart.framework.base.message.i18n")
                        .getString("MessageManager.FailedToGetMessage");
            } catch (MissingResourceException ex) {
            }
            LogManager.getLogManager().getLogAgent().sendMessage(
                MessageManager.class.getName(),
                LogConstant.LEVEL_WARNNING,
                LOG_HEAD
                    + warning
                    + " : application = "
                    + application
                    + ", key = "
                    + key
                    + ", locale = "
                    + locale
                    + " : getMessage(String, String, Object[], Locale)");
            return errorMessage;
        }

        // 庢摼偟偨儊僢僙乕僕傪傕偲偵僼僅乕儅僢僩傪惗惉
        MessageFormat format = new MessageFormat(original);

        // 僼僅乕儅僢僩偵偟偨偑偭偰堷悢傪揧偊偰廔椆
        return format.format(args);
    }

    /**
     * 儊僢僙乕僕僾儘僷僥傿僴儞僪儔傪庢摼偟傑偡丅
     *
     * @return 儊僢僙乕僕僾儘僷僥傿僴儞僪儔
     */
    public MessagePropertyHandler getMessagePropertyHandler() {
        return this.messagePropertyHandler;
    }
}

⌨️ 快捷键说明

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