Definir los ficheros de localización fuera del classpath

3072 vistas

Para poder usar un ResourceBundle definido fuera del classpath, tendremos que declarar un módulo de carga de recursos asÃ:

Nota: esta clase es una versión mejorada de la propuesta en la FAQ [iurl=90&all=0&fs=1196#1200]Cargar varios ficheros de recursos (bundles) sin necesidad de usar las llaves de los bundles[/iurl].

  • Soporte de ficheros de recursos múltiples
  • Acepta los recursos en el classpath
  • Acepta los recursos de tipo url



java
  1. package com.vvic.struts.utils;
  2.  
  3. import java.io.InputStream;
  4. import java.net.URL;
  5. import java.util.Enumeration;
  6. import java.util.Properties;
  7.  
  8. import org.apache.commons.logging.Log;
  9. import org.apache.commons.logging.LogFactory;
  10. import org.apache.struts.util.MessageResourcesFactory;
  11. import org.apache.struts.util.PropertyMessageResources;
  12.  
  13. public class MultiPropertyMessageResources extends PropertyMessageResources {
  14.  
  15. protected static final Log log;
  16.  
  17. public MultiPropertyMessageResources(MessageResourcesFactory factory,
  18. String config) {
  19. super(factory, config);
  20. log.info("Initializing, config='" + config + "'");
  21. }
  22.  
  23. public MultiPropertyMessageResources(MessageResourcesFactory factory,
  24. String config, boolean returnNull) {
  25. super(factory, config, returnNull);
  26. log.info("Initializing, config='" + config + "', returnNull="
  27. + returnNull);
  28. }
  29.  
  30. protected void loadLocale(String localeKey) {
  31. label0: {
  32. if (log.isTraceEnabled())
  33. log.trace("loadLocale(" + localeKey + ")");
  34. synchronized (locales) {
  35. if (locales.get(localeKey) == null)
  36. break label0;
  37. }
  38. return;
  39. }
  40. locales.put(localeKey, localeKey);
  41.  
  42. String configFiles[] = config.split(",");
  43. for (int i = 0; i < configFiles.length; i++) {
  44. String name = configFiles[i].trim().replace('.', '/');
  45. if (localeKey.length() > 0)
  46. name = name + "_" + localeKey;
  47. name = name + ".properties";
  48. InputStream is = null;
  49. Properties props = new Properties();
  50. try {
  51. if (log.isTraceEnabled())
  52. log.trace("  Loading resource '" + name + "'");
  53. ClassLoader classLoader = Thread.currentThread()
  54. .getContextClassLoader();
  55. if (classLoader == null)
  56. classLoader = getClass().getClassLoader();
  57. is = classLoader.getResourceAsStream(name);
  58. if (is != null) {
  59. props.load(is);
  60. is.close();
  61. }
  62. if (log.isTraceEnabled())
  63. log.trace("  Loading resource completed");
  64. } catch (Throwable t) {
  65. log.error("loadLocale()", t);
  66. if (is != null)
  67. try {
  68. is.close();
  69. } catch (Throwable u) {
  70. }
  71. }
  72.  
  73. if (props.size() < 1) {
  74. try {
  75. is = new URL(name).openStream();
  76. props.load(is);
  77. is.close();
  78. } catch (Throwable t) {
  79. log.error("loadLocale()", t);
  80. }
  81. }
  82. if (props.size() < 1) {
  83. log.warn("Unable to load resource " + name);
  84. return;
  85. }
  86. synchronized (messages) {
  87. String key;
  88. for (Enumeration names = props.keys(); names.hasMoreElements(); messages
  89. .put(messageKey(localeKey, key), props.getProperty(key))) {
  90. key = (String) names.nextElement();
  91. if (log.isTraceEnabled())
  92. log.trace("  Saving message key '"
  93. + messageKey(localeKey, key));
  94. }
  95.  
  96. }
  97. }
  98.  
  99. return;
  100. }
  101.  
  102. static {
  103. log = LogFactory.getLog(MultiPropertyMessageResources.class);
  104. }
  105. }



Ejemplo de configuración en el fichero struts-config.xml:



xml
  1. <message-resources
  2. parameter="resources.application,file:///workspace/globalResources/erreurs"
  3. factory="com.vvic.struts.utils.MultiPropertyMessageResourcesFactory"
  4. />