Commits

Anonymous committed 1d581c8

Added static method that returns URLs of a particular resource, need this for XW-510 (Back porting XW-466 from Struts 2 to Xwork 1.1.3 and above (allowing multiple validator definition to be loaded from classpath, overriding each others according to their precedence))

git-svn-id: http://svn.opensymphony.com/svn/xwork/branches/xwork_1-2@1689e221344d-f017-0410-9bd5-d282ab1896d7

  • Participants
  • Parent commits 369408d
  • Branches xwork_1-2

Comments (0)

Files changed (1)

File src/java/com/opensymphony/xwork/validator/ValidatorFactory.java

 import org.apache.commons.logging.LogFactory;
 
 import java.io.InputStream;
+import java.io.IOException;
 import java.util.HashMap;
 import java.util.Map;
+import java.util.Collections;
+import java.net.URL;
 
 
 /**
         return className;
     }
 
+    /**
+     * Returns an array of {@link java.net.URL} corresponding to <code>resourceName</code>.
+     * Search for {@link java.net.URL}s using {@link ClassLoader} in the following precedence :-
+     * <ul>
+     *      <li>ThreadLocal's context class loader</li>
+     *      <li>ValidatorFactory's class loader</li>
+     *      <li><code>callingClass</code>'s class loader</li>
+     * </ul>
+     *
+     * @param resourceName
+     * @param callingClass
+     * @return URL[]
+     * @throws java.io.IOException
+     */
+     public static URL[] getResources(String resourceName, Class callingClass) throws IOException {
+        // use ThreadLocal's class loader
+        URL[] urls =  (URL[]) Collections.list(
+                Thread.currentThread().getContextClassLoader().getResources(resourceName)
+        ).toArray(new URL[0]);
+
+        // use ClassLoaderUtil's classloader
+        if (urls.length == 0) {
+            urls = (URL[]) Collections.list(
+                   ValidatorFactory.class.getClassLoader().getResources(resourceName)
+            ).toArray(new URL[0]);
+        }
+
+        // use callingClass's classloader
+        if (urls.length == 0) {
+            urls = (URL[]) Collections.list(
+                    callingClass.getClassLoader().getResources(resourceName)
+            ).toArray(new URL[0]);
+        }
+
+        return urls;
+     }
+
+
     private static void parseValidators() {
         if (LOG.isDebugEnabled()) {
             LOG.debug("Loading validator definitions.");