diff --git a/xwork-core/src/main/java/com/opensymphony/xwork2/util/LocalizedTextUtil.java b/xwork-core/src/main/java/com/opensymphony/xwork2/util/LocalizedTextUtil.java index dae992582..dbaef2743 100644 --- a/xwork-core/src/main/java/com/opensymphony/xwork2/util/LocalizedTextUtil.java +++ b/xwork-core/src/main/java/com/opensymphony/xwork2/util/LocalizedTextUtil.java @@ -87,14 +87,12 @@ import java.util.concurrent.ConcurrentMap; public class LocalizedTextUtil { private static final ConcurrentMap> classLoaderMap = new ConcurrentHashMap>(); -// private static final List DEFAULT_RESOURCE_BUNDLES = new CopyOnWriteArrayList(); private static final Logger LOG = LoggerFactory.getLogger(LocalizedTextUtil.class); private static boolean reloadBundles = false; - private static final ResourceBundle EMPTY_BUNDLE = new EmptyResourceBundle(); private static final ConcurrentMap bundlesMap = new ConcurrentHashMap(); private static final ConcurrentMap messageFormats = new ConcurrentHashMap(); + private static final ConcurrentMap delegatedClassLoaderMap = new ConcurrentHashMap(); - private static ClassLoader delegatedClassLoader; private static final String RELOADED = "com.opensymphony.xwork2.util.LocalizedTextUtil.reloaded"; private static final String XWORK_MESSAGES_BUNDLE = "com/opensymphony/xwork2/xwork-messages"; @@ -107,9 +105,7 @@ public class LocalizedTextUtil { * Clears the internal list of resource bundles. */ public static void clearDefaultResourceBundles() { -// DEFAULT_RESOURCE_BUNDLES.clear(); -// DEFAULT_RESOURCE_BUNDLES.add(XWORK_MESSAGES_BUNDLE); - ClassLoader ccl = Thread.currentThread().getContextClassLoader(); + ClassLoader ccl = getThreadCurrentThreadGetContextClassLoader(); List bundles = new ArrayList(); classLoaderMap.put(ccl.hashCode(), bundles); bundles.add(0, XWORK_MESSAGES_BUNDLE); @@ -133,8 +129,9 @@ public class LocalizedTextUtil { */ public static void addDefaultResourceBundle(String resourceBundleName) { //make sure this doesn't get added more than once + ClassLoader ccl = null; synchronized (classLoaderMap) { - ClassLoader ccl = Thread.currentThread().getContextClassLoader(); + ccl = getThreadCurrentThreadGetContextClassLoader(); List bundles = classLoaderMap.get(ccl.hashCode()); if (bundles == null) { bundles = new ArrayList(); @@ -146,7 +143,7 @@ public class LocalizedTextUtil { } if (LOG.isDebugEnabled()) { - //LOG.debug("Added default resource bundle '" + resourceBundleName + "' to default resource bundles = " + DEFAULT_RESOURCE_BUNDLES); + LOG.debug("Added default resource bundle '" + resourceBundleName + "' to default resource bundles for the following classloader " + ccl.toString()); } } @@ -201,7 +198,7 @@ public class LocalizedTextUtil { * @return a localized message based on the specified key, or null if no localized message can be found for it */ public static String findDefaultText(String aTextName, Locale locale) { - List localList = classLoaderMap.get(Thread.currentThread().getContextClassLoader().hashCode()); + List localList = classLoaderMap.get(getThreadCurrentThreadGetContextClassLoader().hashCode()); for (String bundleName : localList) { ResourceBundle bundle = findResourceBundle(bundleName, locale); @@ -240,8 +237,6 @@ public class LocalizedTextUtil { * Finds the given resorce bundle by it's name. *

* Will use Thread.currentThread().getContextClassLoader() as the classloader. - * If {@link #delegatedClassLoader} is defined and the bundle cannot be found the current - * classloader it will delegate to that. * * @param aBundleName the name of the bundle (usually it's FQN classname). * @param locale the locale. @@ -250,34 +245,32 @@ public class LocalizedTextUtil { public static ResourceBundle findResourceBundle(String aBundleName, Locale locale) { String key = createMissesKey(aBundleName, locale); - ResourceBundle bundle; + ResourceBundle bundle = null; + ClassLoader classLoader = getThreadCurrentThreadGetContextClassLoader(); try { + key = classLoader.hashCode()+key; + if (!bundlesMap.containsKey(key)) { - bundle = ResourceBundle.getBundle(aBundleName, locale, Thread.currentThread().getContextClassLoader()); + bundle = ResourceBundle.getBundle(aBundleName, locale, classLoader); bundlesMap.putIfAbsent(key, bundle); } else { bundle = bundlesMap.get(key); } } catch (MissingResourceException ex) { - if (delegatedClassLoader != null) { + if (delegatedClassLoaderMap.containsKey(classLoader.hashCode())) { try { if (!bundlesMap.containsKey(key)) { - bundle = ResourceBundle.getBundle(aBundleName, locale, delegatedClassLoader); + bundle = ResourceBundle.getBundle(aBundleName, locale, delegatedClassLoaderMap.get(classLoader.hashCode())); bundlesMap.putIfAbsent(key, bundle); } else { bundle = bundlesMap.get(key); } } catch (MissingResourceException e) { - bundle = EMPTY_BUNDLE; -// bundlesMap.putIfAbsent(key, bundle); } - } else { - bundle = EMPTY_BUNDLE; -// bundlesMap.putIfAbsent(key, bundle); } } - return (bundle == EMPTY_BUNDLE) ? null : bundle; + return bundle; } /** @@ -287,7 +280,7 @@ public class LocalizedTextUtil { */ public static void setDelegatedClassLoader(final ClassLoader classLoader) { synchronized (bundlesMap) { - delegatedClassLoader = classLoader; + delegatedClassLoaderMap.put(getThreadCurrentThreadGetContextClassLoader().hashCode(), classLoader); } } @@ -297,7 +290,7 @@ public class LocalizedTextUtil { * @param bundleName */ public static void clearBundle(final String bundleName) { - bundlesMap.remove(bundleName); + bundlesMap.remove(getThreadCurrentThreadGetContextClassLoader().hashCode() + bundleName); } @@ -819,7 +812,7 @@ public class LocalizedTextUtil { private static void clearTomcatCache() { - ClassLoader loader = Thread.currentThread().getContextClassLoader(); + ClassLoader loader = getThreadCurrentThreadGetContextClassLoader(); // no need for compilation here. Class cl = loader.getClass(); @@ -896,6 +889,10 @@ public class LocalizedTextUtil { } } + private static ClassLoader getThreadCurrentThreadGetContextClassLoader() { + return Thread.currentThread().getContextClassLoader(); + } + static class GetDefaultMessageReturnArg { String message; boolean foundInBundle;