外部化的message.properties文件未在Tomcat中获取 [英] Externalized message.properties file is not picking up in Tomcat

查看:184
本文介绍了外部化的message.properties文件未在Tomcat中获取的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

根据我的要求,我需要外化message.properties文件(保留在war文件之外),同时它应该在更新时自动重新加载。

According to my requirement, I need to externalize the message.properties file (Keep outside from war file) and in same time it should be automatically re loadable on update.

所以我通过遵循代码并使用Jetty Server正常工作来实现这两个目标。但是当我使用Tomcat Server时,外部化属性文件没有被系统拾取,而是仅使用战争中的文件。

So I achieved both by following code and its working fine with Jetty Server. But in when I use Tomcat Server that externalized property file is not picking up by the system, instead its uses only the file inside the war.

public final class Messages
{
    public static final String BUNDLE_NAME = "com.sample.project.core.ui.resources.messages";
    //    private static ResourceBundle resourceBundle = ResourceBundle.getBundle(BUNDLE_NAME);
    private static ResourceBundle resourceBundle;
    private static final Logger LOGGER = Logger.getLogger(Messages.class);

    private static ReloadableResourceBundleMessageSource messageSource;

    static
    {
        try
        {
            FileInputStream fis =
                new FileInputStream(System.getProperty("resources.messages.file.path"));
            resourceBundle = new PropertyResourceBundle(fis);
        }
        catch (FileNotFoundException e)
        {
            LOGGER.error("messages.properties file not found: " + e);
            resourceBundle = ResourceBundle.getBundle(BUNDLE_NAME);
        }
        catch (Exception e)
        {
            LOGGER.error("messages.properties file reading failed: " + e);
            resourceBundle = ResourceBundle.getBundle(BUNDLE_NAME);
        }

    }

    private Messages()
    {
    }

    /**
     * <p>
     * setter methos to ReloadableResourceBundleMessageSource object.
     * </p>
     * 
     * 
     * @param inMessageSource
     *            set reloadable resources bundle
     **/
    public static void setMessageSource(final ReloadableResourceBundleMessageSource inMessageSource)
    {
        Messages.messageSource = inMessageSource;
        Messages.messageSource.setBasename(System.getProperty("resources.messages.file.path"));
    }

    /**
     * <p>
     * Resolve a message by a key and argument replacements.
     * </p>
     * 
     * @see MessageFormat#format(String, Object...)
     * @param key
     *            the message to look up
     * @param arguments
     *            optional message arguments
     * @return the resolved message
     **/
    public static String getMessage(final String key, final Object... arguments)
    {
        try
        {
            if (messageSource != null)
            {
                return messageSource.getMessage(key, arguments, Locale.getDefault());
            }
            else
            {
                if (arguments != null)
                    return MessageFormat.format(resourceBundle.getString(key), arguments);
                return resourceBundle.getString(key);
            }
        }
        catch (NoSuchMessageException e)
        {
            LOGGER.error("Message key not found: " + key);
            return '!' + key + '!';
        }
        catch (MissingResourceException e)
        {
            LOGGER.error("Message key not found: " + key);
            return '!' + key + '!';
        }
    }

}

(这里我使用 resources.messages.file.path 键作为VM参数传递的文件路径

(Here file path I'm passing as a VM argument using "resources.messages.file.path" key)

首先,我认为访问文件系统是一个问题,并尝试了很多方法。然后我听说catalina.policy文件,我添加了一些像这样的行..

First I thought it was a problem with accessing the file system and tried many ways. Then I heard about catalina.policy file and I added some lines like this..

grant codeBase "file:${catalina.base}/webapps/sample.war/-" {
    permission java.security.AllPermission;
    permission java.io.FilePermission "file:${catalina.base}${file.separator}webapps${file.separator}messages.properties", "read, write";

    permission java.util.PropertyPermission "resources.messages.file.path", "read";
}

但他们没有给我带来好运。我很绝望。知道这个问题是什么吗?请帮我。先感谢您。 (在Tomcat6上测试过)

But non of them gave me luck. I'm desperate. Any idea what this issue is? Please help me. Thank you in advance. (Tested on Tomcat6)

推荐答案

最后我找到了搞砸的地方。

Finally I found where I screwed up.

messageSource 的这个setter方法不应该是静态而我已删除静态访问 messageSource

This setter method of messageSource should not be static and I removed static access of messageSource

    messageSource = inMessageSource;
    messageSource.setBasename(System.getProperty("resources.messages.file.path"));

现在代码工作正常。并且在catalina.policy文件中不需要该权限条目。感谢所有帮助过我的人。

Now code is working fine. And no need of that permission entry in catalina.policy file. Thanks to everyone who helped me.

这篇关于外部化的message.properties文件未在Tomcat中获取的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆