You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@uima.apache.org by sc...@apache.org on 2019/07/02 14:36:22 UTC
svn commit: r1862430 -
/uima/uv3/uimaj-v3/trunk/uimaj-core/src/main/java/org/apache/uima/internal/util/I18nUtil.java
Author: schor
Date: Tue Jul 2 14:36:21 2019
New Revision: 1862430
URL: http://svn.apache.org/viewvc?rev=1862430&view=rev
Log:
[UIMA-6088][UIMA-5961]
Modified:
uima/uv3/uimaj-v3/trunk/uimaj-core/src/main/java/org/apache/uima/internal/util/I18nUtil.java
Modified: uima/uv3/uimaj-v3/trunk/uimaj-core/src/main/java/org/apache/uima/internal/util/I18nUtil.java
URL: http://svn.apache.org/viewvc/uima/uv3/uimaj-v3/trunk/uimaj-core/src/main/java/org/apache/uima/internal/util/I18nUtil.java?rev=1862430&r1=1862429&r2=1862430&view=diff
==============================================================================
--- uima/uv3/uimaj-v3/trunk/uimaj-core/src/main/java/org/apache/uima/internal/util/I18nUtil.java (original)
+++ uima/uv3/uimaj-v3/trunk/uimaj-core/src/main/java/org/apache/uima/internal/util/I18nUtil.java Tue Jul 2 14:36:21 2019
@@ -20,86 +20,17 @@
package org.apache.uima.internal.util;
import java.text.MessageFormat;
-import java.util.Arrays;
-import java.util.HashMap;
-import java.util.IdentityHashMap;
import java.util.Locale;
-import java.util.Map;
import java.util.ResourceBundle;
/**
* Internationaliation utilities.
*
+ * Static methods only
+ *
*/
public class I18nUtil {
-
- /**
- * Cache for bundle lookup
- * otherwise, there are multiple lookups in a call-stack-climbing class loader
- *
- */
-
- static class Bid {
- final String bundleName;
- final Locale locale;
- final ClassLoader loader;
- final ClassLoader [] loaders;
- public Bid(String bundleName, Locale locale, ClassLoader loader, ClassLoader[] loaders) {
- super();
- this.bundleName = bundleName;
- this.locale = locale;
- this.loader = (loaders != null) ? null : loader;
- this.loaders = loaders;
- }
- @Override
- public int hashCode() {
- final int prime = 31;
- int result = 1;
- result = prime * result + ((bundleName == null) ? 0 : bundleName.hashCode());
- result = prime * result + ((loader == null) ? 0 : loader.hashCode());
- result = prime * result + ((loaders == null) ? 0 : Arrays.hashCode(loaders));
- result = prime * result + ((locale == null) ? 0 : locale.hashCode());
- return result;
- }
- @Override
- public boolean equals(Object obj) {
- if (this == obj)
- return true;
- if (obj == null)
- return false;
- if (getClass() != obj.getClass())
- return false;
- Bid other = (Bid) obj;
- if (bundleName == null) {
- if (other.bundleName != null)
- return false;
- } else if (!bundleName.equals(other.bundleName))
- return false;
- if (loader == null) {
- if (other.loader != null)
- return false;
- } else if (!loader.equals(other.loader))
- return false;
- if (locale == null) {
- if (other.locale != null)
- return false;
- } else if ( locale != other.locale)
- return false;
- if (loaders == null) {
- if (other.loaders != null)
- return false;
- } else if (!Arrays.equals(loaders, other.loaders))
- return false;
- return true;
- }
-
- }
-
- private static final ThreadLocal<Map<Bid, ResourceBundle>> b_cache =
- ThreadLocal.withInitial(() -> new HashMap<>());
-
-
/**
* Localize a message to the default Locale.
*
@@ -172,7 +103,7 @@ public class I18nUtil {
* @param aArguments
* arguments to message (may be null if none)
* @param aLoader
- * ClassLoader to use to load the resource bundle. If null, the ClassLoader that loased
+ * ClassLoader to use to load the resource bundle. If null, the ClassLoader that loaded
* <code>I18nUtil</code> is used.
*
* @return localized message. If an exception occurs, returns "MESSAGE LOCALIZATION FAILED:"
@@ -181,28 +112,35 @@ public class I18nUtil {
public static String localizeMessage(String aResourceBundleName, Locale aLocale,
String aMessageKey, Object[] aArguments, ClassLoader aLoader) {
try {
- if (aLoader == null) {
- // get the constant, thread-safe, stack-climbing class loader
- aLoader = MsgLocalizationClassLoader.getMsgLocalizationClassLoader();
- }
-
- final boolean is_stack_climbing_loader = aLoader == MsgLocalizationClassLoader.getMsgLocalizationClassLoader();
-
- // locate the resource bundle for this exception's messages
- String message;
- if (aResourceBundleName == null) {
- message = "Null ResourceBundle, key = \"" + aMessageKey + "\"";
- } else {
- ClassLoader[] cls = is_stack_climbing_loader ? Misc.getCallingClass_classLoaders() : null;
- final ClassLoader final_aLoader = aLoader;
- Bid cache_key = new Bid(aResourceBundleName, aLocale, aLoader, cls);
- ResourceBundle bundle = b_cache.get().computeIfAbsent(cache_key,
- (bid) ->
- ResourceBundle.getBundle(aResourceBundleName, aLocale, final_aLoader));
- message = bundle.getString(aMessageKey);
- }
+ ResourceBundle bundle = resolveResourceBundle(aResourceBundleName, aLocale, aLoader);
+ return localizeMessage(bundle, aLocale, aMessageKey, aArguments);
+ } catch (Exception e) {
+ return "MESSAGE LOCALIZATION FAILED: " + e.getMessage();
+ }
+ }
+
+ /**
+ * Localize a message to a specified Locale.
+ *
+ * @param aResourceBundle
+ * the resource bundle to use to resolve message keys
+ * @param aLocale
+ * locale to which to localize
+ * @param aMessageKey
+ * key of message to localize
+ * @param aArguments
+ * arguments to message (may be null if none)
+ *
+ * @return localized message. If an exception occurs, returns "MESSAGE LOCALIZATION FAILED:"
+ * followed by the exception message.
+ */
+ public static String localizeMessage(ResourceBundle bundle, Locale aLocale, String aMessageKey, Object[] aArguments) {
+ try {
+ String message = (bundle == null)
+ ? ("Null ResourceBundle, key = \"" + aMessageKey + "\"")
+ : bundle.getString(aMessageKey);
// if arguments exist, use MessageFormat to include them
- if (aArguments != null && aArguments.length > 0) {
+ if (bundle != null && aArguments != null && aArguments.length > 0) {
MessageFormat fmt = new MessageFormat(message);
fmt.setLocale(aLocale);
return fmt.format(aArguments);
@@ -210,7 +148,15 @@ public class I18nUtil {
return message;
} catch (Exception e) {
return "MESSAGE LOCALIZATION FAILED: The key " + aMessageKey + " may be missing in the properties file " + e.getMessage();
+ }
+ }
+
+ public static ResourceBundle resolveResourceBundle(String aResourceBundleName, Locale aLocale, ClassLoader aLoader) {
+ if (aLoader == null) {
+ aLoader = MsgLocalizationClassLoader.getMsgLocalizationClassLoader();
}
+ // locate the resource bundle for this exception's messages
+ return ResourceBundle.getBundle(aResourceBundleName, aLocale, aLoader);
}
public static void setTccl(ClassLoader tccl) {