Commits

Anonymous committed 98a64a4

XW-682 Add a delegatedClassLoader to LocalizedTextUtil

git-svn-id: http://svn.opensymphony.com/svn/xwork/trunk@1977e221344d-f017-0410-9bd5-d282ab1896d7

  • Participants
  • Parent commits dc7bfaf

Comments (0)

Files changed (1)

src/java/com/opensymphony/xwork2/util/LocalizedTextUtil.java

     private static final ConcurrentMap<String, ResourceBundle> bundlesMap = new ConcurrentHashMap<String, ResourceBundle>();
     private static final Map<MessageFormatKey, MessageFormat> messageFormats = new HashMap<MessageFormatKey, MessageFormat>();
 
+    private static ClassLoader delegatedClassLoader;
+    
     static {
         clearDefaultResourceBundles();
     }
      * 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.
 
             bundle = bundlesMap.get(key);
         } catch (MissingResourceException ex) {
-            bundle = EMPTY_BUNDLE;
-            bundlesMap.put(key, bundle);
+            if ( delegatedClassLoader != null) {
+                try {
+                    if (!bundlesMap.containsKey(key)) {
+                        bundle = ResourceBundle.getBundle(
+                                aBundleName,
+                                locale,
+                                delegatedClassLoader);
+                        bundlesMap.put(key, bundle);
+                    }
+
+                    bundle = bundlesMap.get(key);
+                    
+                } catch (MissingResourceException e) {
+                    bundle = EMPTY_BUNDLE;
+                    bundlesMap.put(key, bundle);
+                }
+            } else {
+                bundle = EMPTY_BUNDLE;
+                bundlesMap.put(key, bundle);
+            }
         }
         return (bundle == EMPTY_BUNDLE) ? null : bundle;
     }
 
     /**
+     * Sets a {@link ClassLoader} to look up the bundle from if none can be found on the current thread's classloader
+     *
+     * @param classLoader
+     */
+    public static void setDelegatedClassLoader(final ClassLoader classLoader) {
+        synchronized (bundlesMap) {
+            delegatedClassLoader = classLoader;
+        }
+    }
+
+    /**
+     * Removes the bundle from any cached "misses"
+     *
+     * @param bundleName
+     */
+    public static void clearBundle(final String bundleName) {
+        synchronized (bundlesMap) {
+            bundlesMap.remove(bundleName);
+        }
+    }
+
+
+    /**
      * Creates a key to used for lookup/storing in the bundle misses cache.
      *
      * @param aBundleName the name of the bundle (usually it's FQN classname).