2013-12-18 28 views
2

根據我的要求,我需要外部化message.properties文件(保留在war文件之外),同時它應該在更新時自動重新加載。外部化message.properties文件在Tomcat中沒有拾取

因此,我通過以下代碼實現了這兩個功能,並且它與Jetty Server一起工作良好。但是在我使用Tomcat Server的時候,外部化的屬性文件並沒有被系統接收,而是它只使用戰爭中的文件。

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 + '!'; 
     } 
    } 

} 

(這裏的文件路徑我傳遞一個VM參數使用「resources.messages.file.path」鍵)

首先,我認爲這是與訪問文件系統的問題,試了很多辦法。然後,我聽到的catalina.policy文件,我添加了一些線這樣的..

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上測試)

+0

如果創建「PropertyResourceBundle」的調用引發異常,它可能會使用WAR中的文件 - 因爲catch處理程序回退到使用標準類加載方法ResourceBundle.getBundle()創建資源包。你能檢查應用程序日誌,'catalina.out'和'localhost.log'文件是否有錯誤?根據代碼,當無法加載「PropertyResourceBundle」時,將會記錄一條錯誤消息。 –

+0

感謝您的快速回復。是的,我檢查了日誌文件。但是我看不到任何與此相關的具體錯誤。只有我們可以得出結論是messageSource對象爲null。偶記錄靜態塊,不打印。 – ssdehero

回答

3

最後我發現我弄錯了。

這的messageSource setter方法不應該是messageSource

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

現在代碼工作正常靜態去除靜態訪問。和不需要在catalina.policy文件中輸入該權限。感謝所有幫助過我的人。

相關問題