From 8e9693d55af30637faeeaf4c5ddb6feaa1d1874d Mon Sep 17 00:00:00 2001 From: Gavin Cornwell Date: Thu, 11 May 2006 12:42:53 +0000 Subject: [PATCH] Added lookup of custom properties files, this means a webclient.properties can be added to alfresco.extension git-svn-id: https://svn.alfresco.com/repos/alfresco-enterprise/alfresco/HEAD/root@2841 c4b6b30b-aa2e-2d43-bbcb-ca4b014f7261 --- .../web/app/ResourceBundleWrapper.java | 107 ++++++++++++++++-- 1 file changed, 99 insertions(+), 8 deletions(-) diff --git a/source/java/org/alfresco/web/app/ResourceBundleWrapper.java b/source/java/org/alfresco/web/app/ResourceBundleWrapper.java index 5b5f69c23e..a76f851b19 100644 --- a/source/java/org/alfresco/web/app/ResourceBundleWrapper.java +++ b/source/java/org/alfresco/web/app/ResourceBundleWrapper.java @@ -20,6 +20,7 @@ import java.util.Enumeration; import java.util.Locale; import java.util.MissingResourceException; import java.util.ResourceBundle; +import java.util.Vector; import org.alfresco.error.AlfrescoRuntimeException; import org.apache.log4j.Logger; @@ -28,6 +29,7 @@ import org.apache.log4j.Priority; /** * Wrapper around Alfresco Resource Bundle objects. Used to catch and handle missing * resource exception to help identify missing I18N strings in client apps. + * Also used to look for the requested string in a custom resource bundle. * * @author Kevin Roast */ @@ -36,15 +38,19 @@ public final class ResourceBundleWrapper extends ResourceBundle private static Logger logger = Logger.getLogger(ResourceBundleWrapper.class); private ResourceBundle delegate; + private ResourceBundle delegateCustom; /** * Constructor * - * @param bundle The ResourceBundle to route calls too + * @param bundle The ResourceBundle to route calls too + * @param customBundle A custom version of bundle to look in if the string is + * not found in bundle */ - private ResourceBundleWrapper(ResourceBundle bundle) + private ResourceBundleWrapper(ResourceBundle bundle, ResourceBundle customBundle) { this.delegate = bundle; + this.delegateCustom = customBundle; } /** @@ -52,7 +58,29 @@ public final class ResourceBundleWrapper extends ResourceBundle */ public Enumeration getKeys() { - return this.delegate.getKeys(); + if (this.delegateCustom == null) + { + return this.delegate.getKeys(); + } + else + { + // get existing keys + Enumeration keys = this.delegate.getKeys(); + Enumeration customKeys = this.delegateCustom.getKeys(); + + // combine keys into one list + Vector allKeys = new Vector(100, 2); + while (keys.hasMoreElements()) + { + allKeys.add(keys.nextElement()); + } + while (customKeys.hasMoreElements()) + { + allKeys.add(customKeys.nextElement()); + } + + return allKeys.elements(); + } } /** @@ -60,17 +88,39 @@ public final class ResourceBundleWrapper extends ResourceBundle */ protected Object handleGetObject(String key) { + Object result = null; + try { - return this.delegate.getObject(key); + result = this.delegate.getObject(key); } catch (MissingResourceException err) { - if (logger.isEnabledFor(Priority.WARN)) - logger.warn("Failed to find I18N message string key: " + key); + // if the string wasn't found in the normal bundle + // try the custom bundle if there is one + try + { + if (this.delegateCustom != null) + { + result = this.delegateCustom.getObject(key); + } + } + catch (MissingResourceException mre) + { + // don't do anything here, dealt with below + } - return "$$" + key + "$$"; + // if the key was not found return a default string + if (result == null) + { + if (logger.isEnabledFor(Priority.WARN)) + logger.warn("Failed to find I18N message string key: " + key); + + result = "$$" + key + "$$"; + } } + + return result; } /** @@ -89,7 +139,48 @@ public final class ResourceBundleWrapper extends ResourceBundle throw new AlfrescoRuntimeException("Unable to load Alfresco messages bundle: " + name); } + // also look up the custom version of the bundle in the extension package + String customName = determineCustomBundleName(name); + ResourceBundle customBundle = null; + try + { + customBundle = ResourceBundle.getBundle(customName, locale); + + if (logger.isDebugEnabled()) + logger.debug("Located and loaded custom bundle: " + customName); + } + catch (MissingResourceException mre) + { + // ignore the error, just leave custom bundle as null + } + // apply our wrapper to catch MissingResourceException - return new ResourceBundleWrapper(bundle); + return new ResourceBundleWrapper(bundle, customBundle); + } + + /** + * Determines the name for the custom bundle to lookup based on the given + * bundle name + * + * @param bundleName The standard bundle + * @return The name of the custom bundle (in the alfresco.extension package) + */ + protected static String determineCustomBundleName(String bundleName) + { + String extensionPackage = "alfresco.extension."; + String customBundleName = null; + + int idx = bundleName.lastIndexOf("."); + if (idx == -1) + { + customBundleName = extensionPackage + bundleName; + } + else + { + customBundleName = extensionPackage + + bundleName.substring(idx+1, bundleName.length()); + } + + return customBundleName; } }