Removed unused code, comments and variables. Also, added delegatedClassLoaderMap to handle delegatedClassLoader as per Tomcat.
Project: http://git-wip-us.apache.org/repos/asf/struts/repo Commit: http://git-wip-us.apache.org/repos/asf/struts/commit/c826d97c Tree: http://git-wip-us.apache.org/repos/asf/struts/tree/c826d97c Diff: http://git-wip-us.apache.org/repos/asf/struts/diff/c826d97c Branch: refs/heads/develop Commit: c826d97cafafc9ce54e15745b23cd211e233c2fa Parents: 4586d1f Author: Luigi Fugaro <l.fug...@gmail.com> Authored: Tue Jul 29 18:44:50 2014 +0200 Committer: Luigi Fugaro <l.fug...@gmail.com> Committed: Tue Jul 29 18:44:50 2014 +0200 ---------------------------------------------------------------------- .../xwork2/util/LocalizedTextUtil.java | 45 +++++++++----------- 1 file changed, 21 insertions(+), 24 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/struts/blob/c826d97c/xwork-core/src/main/java/com/opensymphony/xwork2/util/LocalizedTextUtil.java ---------------------------------------------------------------------- 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 dae9925..dbaef27 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<Integer, List<String>> classLoaderMap = new ConcurrentHashMap<Integer, List<String>>(); -// private static final List<String> DEFAULT_RESOURCE_BUNDLES = new CopyOnWriteArrayList<String>(); 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<String, ResourceBundle> bundlesMap = new ConcurrentHashMap<String, ResourceBundle>(); private static final ConcurrentMap<MessageFormatKey, MessageFormat> messageFormats = new ConcurrentHashMap<MessageFormatKey, MessageFormat>(); + private static final ConcurrentMap<Integer, ClassLoader> delegatedClassLoaderMap = new ConcurrentHashMap<Integer, ClassLoader>(); - 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<String> bundles = new ArrayList<String>(); 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<String> bundles = classLoaderMap.get(ccl.hashCode()); if (bundles == null) { bundles = new ArrayList<String>(); @@ -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<String> localList = classLoaderMap.get(Thread.currentThread().getContextClassLoader().hashCode()); + List<String> 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. * <p/> * Will use <code>Thread.currentThread().getContextClassLoader()</code> 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;