Java tutorial
/* * Copyright (c) 2008 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.util; import java.text.MessageFormat; import java.util.HashMap; import java.util.Locale; import jp.terasoluna.fw.exception.SystemException; import jp.terasoluna.fw.web.struts.MessageFormatCacheMapFactory; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.apache.struts.util.MessageResources; import org.apache.struts.util.MessageResourcesFactory; import org.springframework.beans.BeansException; import org.springframework.context.MessageSource; import org.springframework.context.NoSuchMessageException; import org.springframework.web.context.ContextLoader; import org.springframework.web.context.WebApplicationContext; /** * <p> * Spring?bZ?[W\?[XStrutspMessageResourcesNX?B<br> * </p> * <p> * SpringMessageResourcesFactorystruts-config.xmlmessage-resourcesvf * factory???B<br> * <ul> * <li>parameter?lMessageSourceBeanIDw???A * wMessageSourceg?B<br> * <strong>?wBean???ABean * MessageSourceCX^X??APT?[oNO??B</strong> * </li> * <li>parameter?l???ftHg "messageSource" g?B * </li> * </ul> * </p> * * <p> * <fieldset style="border:1pt solid black;padding:10px;width:100%;"> * <legend>struts-config.xml??i?parameter??j</legend> * <pre><code> * <message-resources parameter="" * factory="jp.terasoluna.fw.web.struts.util.SpringMessageResourcesFactory"/> * </code></pre> * </fieldset> * </p> * * <p> * <fieldset style="border:1pt solid black;padding:10px;width:100%;"> * <legend>struts-config.xml??i?parameterw?j</legend> * <pre><code> * <message-resources parameter="hogeMessageSource" * factory="jp.terasoluna.fw.web.struts.util.SpringMessageResourcesFactory"/> * </code></pre> * </fieldset> * </p> * * <p> * <fieldset style="border:1pt solid black;padding:10px;width:100%;"> * <legend>Bean`t@C?</legend> * ResourceBundleMessageSourcep???Abasenamesv?peB * ?bZ?[Wv?peBt@Cw?B * <pre><code> * <bean id="messageSource" * class="org.springframework.context.support.ResourceBundleMessageSource"> * <property name="basenames" value="MessageResources"/> * </bean> * </code></pre> * * DataSourceMessageSourcepDB?bZ?[W?B * <pre><code> * <bean id="messageSource" * class="jp.terasoluna.fw.message.DataSourceMessageSource"> * <property name="dbMessageResourceDAO" ref="dbMessageResourceDAO"/> * </bean> * </code></pre> * </fieldset> * </p> * * <p> * <fieldset style="border:1pt solid black;padding:10px;width:100%;"> * <legend>Q?bZ?[W\?[X`??Bean`t@C?</legend> * <pre><code> * <bean id="messageSource" * class="jp.terasoluna.fw.message.DataSourceMessageSource"> * <property name="dbMessageResourceDAO" ref="dbMessageResourceDAO"/> * <property name="parentMessageSource" ref="parentSource"/> * </bean> * * <bean id="parentSource" * class="org.springframework.context.support.ResourceBundleMessageSource"> * <property name="basenames" value="MessageResources"/> * </bean> * </code></pre> * </fieldset> * </p> * * <p> * NX?A * Won't FixStrutsoO STR-2172(https://issues.apache.org/jira/browse/STR-2172) * iL?B<br> * ???A{@link MessageFormatCacheMapFactory} Q??B * </p> * * @see jp.terasoluna.fw.web.struts.MessageFormatCacheMapFactory * @see jp.terasoluna.fw.web.struts.util.SpringMessageResourcesFactory * @see org.springframework.context.support.ResourceBundleMessageSource * @see jp.terasoluna.fw.message.DataSourceMessageSource * */ public class SpringMessageResources extends MessageResources { /** * VAo?[WID */ private static final long serialVersionUID = 8870342287587564386L; /** * ?OCX^X */ private static Log log = LogFactory.getLog(SpringMessageResources.class); /** * G?[?bZ?[WL?[ */ private static final String ERR_BEAN_EXCEPTION = "errors.message.bean.exception"; /** * AvP?[VReLXg */ private WebApplicationContext context = null; /** * Spring?bZ?[W\?[X */ private MessageSource messageSource = null; /** * wp??[^SpringMessageResources???B * * @param factory ?bZ?[W\?[Xt@Ng * @param config ReiMessageSourceBean * ?i?ftHg"messageSource"?j */ public SpringMessageResources(MessageResourcesFactory factory, String config) { super(factory, config); replaceMessageFormatCache(); this.context = ContextLoader.getCurrentWebApplicationContext(); initMessageSource(); } /** * wp??[^SpringMessageResources???B * * @param factory ?bZ?[W\?[Xt@Ng * @param config ReiMessageSourceBean * ?i?ftHg"messageSource"?j * @param returnNull <code>org.apache.struts.util.MessageResources</code> * NX <code>returnNull</code>?B * <code>false</code> w?AL?[Y?bZ?[W * ??????Locale.key???`?bZ?[W * p?B */ public SpringMessageResources(MessageResourcesFactory factory, String config, boolean returnNull) { super(factory, config, returnNull); replaceMessageFormatCache(); this.context = ContextLoader.getCurrentWebApplicationContext(); initMessageSource(); } /** * MessageFormatLbV(formats)CX^X??s?B * <p> * StrutsoO STR-2172pLbVIuWFNg??B * </p> * @see MessageFormatCacheMapFactory */ private void replaceMessageFormatCache() { HashMap<String, MessageFormat> map = MessageFormatCacheMapFactory.getInstance(); if (map != null) { formats = map; } } /** * MessageSource??s?B */ private void initMessageSource() { if (this.context != null) { if (config != null && !("".equals(config))) { // p??[^wBeanID try { this.messageSource = (MessageSource) this.context.getBean(config, MessageSource.class); } catch (BeansException e) { if (log.isErrorEnabled()) { StringBuilder mes = new StringBuilder(); mes.append(config); mes.append(" is not found"); mes.append(" or it is not MessageSource instance."); log.error(mes.toString()); } throw new SystemException(e, ERR_BEAN_EXCEPTION); } if (log.isDebugEnabled()) { StringBuilder mes = new StringBuilder(); mes.append(config); mes.append(" MessageSource is used."); log.debug(mes.toString()); } } // ftHgMessageSource?p?iApplicationContext?g?j if (this.messageSource == null) { this.messageSource = this.context; if (log.isDebugEnabled()) { log.debug("Default MessageSource is used."); } } } else { if (log.isWarnEnabled()) { log.warn("ApplicationContext is not found."); } } } /** * wL?[?P?[?bZ?[W?B * * @see org.apache.struts.util.MessageResources#getMessage(java.util.Locale, java.lang.String) */ @Override public String getMessage(Locale locale, String key) { if (this.messageSource != null) { String retMessage = null; try { retMessage = this.messageSource.getMessage(key, null, locale); } catch (NoSuchMessageException e) { // } if (retMessage != null) { if (log.isDebugEnabled()) { StringBuilder mes = new StringBuilder(); mes.append("key:["); mes.append(key); mes.append("] locale:["); mes.append(locale); mes.append("] message:["); mes.append(retMessage); mes.append("]"); log.debug(mes.toString()); } return retMessage; } } // ?bZ?[W????AreturnNull?L?]p if (!returnNull) { StringBuilder mes = new StringBuilder(); mes.append("???"); mes.append(messageKey(locale, key)); mes.append("???"); return mes.toString(); } return null; } }