Commits

Anonymous committed 2e5fb66

Synching with Stripes, adding ability to search for named resource files

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

Comments (0)

Files changed (2)

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

          * is to be included in the results, false otherwise.
          */
         boolean matches(Class type);
+        
+        boolean matches(URL resource);
+
+        boolean doesMatchClass();
+        boolean doesMatchResource();
+    }
+    
+    public static abstract class ClassTest implements Test {
+        public boolean matches(URL resource) {
+            throw new UnsupportedOperationException();
+        }
+
+        public boolean doesMatchClass() {
+            return true;
+        }
+        public boolean doesMatchResource() {
+            return false;
+        }
+    }
+    
+    public static abstract class ResourceTest implements Test {
+        public boolean matches(Class cls) {
+            throw new UnsupportedOperationException();
+        }
+
+        public boolean doesMatchClass() {
+            return false;
+        }
+        public boolean doesMatchResource() {
+            return true;
+        }
     }
 
     /**
      * A Test that checks to see if each class is assignable to the provided class. Note
      * that this test will match the parent type itself if it is presented for matching.
      */
-    public static class IsA implements Test {
+    public static class IsA extends ClassTest {
         private Class parent;
 
         /** Constructs an IsA test using the supplied Class as the parent class/interface. */
     /**
      * A Test that checks to see if each class name ends with the provided suffix.
      */
-    public static class NameEndsWith implements Test {
+    public static class NameEndsWith extends ClassTest {
         private String suffix;
 
         /** Constructs a NameEndsWith test using the supplied suffix. */
      * A Test that checks to see if each class is annotated with a specific annotation. If it
      * is, then the test returns true, otherwise false.
      */
-    public static class AnnotatedWith implements Test {
+    public static class AnnotatedWith extends ClassTest {
         private Class<? extends Annotation> annotation;
 
         /** Construts an AnnotatedWith test for the specified annotation type. */
             return "annotated with @" + annotation.getSimpleName();
         }
     }
+    
+    public static class NameIs extends ResourceTest {
+        private String name;
+        
+        public NameIs(String name) { this.name = "/" + name; }
+        
+        public boolean matches(URL resource) {
+            return (resource.getPath().endsWith(name));
+        }
+        
+        @Override public String toString() {
+            return "named " + name;
+        }
+    }
 
     /** The set of matches being accumulated. */
-    private Set<Class<? extends T>> matches = new HashSet<Class<?extends T>>();
+    private Set<Class<? extends T>> classMatches = new HashSet<Class<?extends T>>();
+    
+    /** The set of matches being accumulated. */
+    private Set<URL> resourceMatches = new HashSet<URL>();
 
     /**
      * The ClassLoader to use when looking for classes. If null then the ClassLoader returned
      * @return the set of classes that have been discovered.
      */
     public Set<Class<? extends T>> getClasses() {
-        return matches;
+        return classMatches;
+    }
+    
+    public Set<URL> getResources() {
+        return resourceMatches;
     }
+    
 
     /**
      * Returns the classloader that will be used for scanning for classes. If no explicit
         }
     }
     
+    public void findNamedResource(String name, String... pathNames) {
+        if (pathNames == null) return;
+        
+        Test test = new NameIs(name);
+        for (String pkg : pathNames) {
+            findInPackage(test, pkg);
+        }
+    }
+    
     /**
      * Attempts to discover classes that pass the test. Accumulated
      * classes can be accessed by calling {@link #getClasses()}.
             if (file.isDirectory()) {
                 loadImplementationsInDirectory(test, packageOrClass, file);
             }
-            else if (file.getName().endsWith(".class")) {
+            else if (isTestApplicable(test, file.getName())) {
                 addIfMatching(test, packageOrClass);
             }
         }
     }
 
+    private boolean isTestApplicable(Test test, String path) {
+        return test.doesMatchResource() || path.endsWith(".class") && test.doesMatchClass();
+    }
+
     /**
      * Finds matching classes within a jar files that contains a folder structure
      * matching the package structure.  If the File is not a JarFile or does not exist a warning
 
             while ( (entry = jarStream.getNextJarEntry() ) != null) {
                 String name = entry.getName();
-                if (!entry.isDirectory() && name.startsWith(parent) && name.endsWith(".class")) {
+                if (!entry.isDirectory() && name.startsWith(parent) && isTestApplicable(test, name)) {
                     addIfMatching(test, name);
                 }
             }
      */
     protected void addIfMatching(Test test, String fqn) {
         try {
-            String externalName = fqn.substring(0, fqn.indexOf('.')).replace('/', '.');
             ClassLoader loader = getClassLoader();
-            if (log.isDebugEnabled()) {
-                log.debug("Checking to see if class " + externalName + " matches criteria [" + test + "]");
+            if (test.doesMatchClass()) {
+                String externalName = fqn.substring(0, fqn.indexOf('.')).replace('/', '.');
+                if (log.isDebugEnabled()) {
+                    log.debug("Checking to see if class " + externalName + " matches criteria [" + test + "]");
+                }
+    
+                Class type = loader.loadClass(externalName);
+                if (test.matches(type) ) {
+                    classMatches.add( (Class<T>) type);
+                }
             }
-
-            Class type = loader.loadClass(externalName);
-            if (test.matches(type) ) {
-                matches.add( (Class<T>) type);
+            if (test.doesMatchResource()) {
+                URL url = loader.getResource(fqn.substring(1));
+                if (url != null && test.matches(url)) {
+                    resourceMatches.add(url);
+                }
             }
         }
         catch (Throwable t) {

src/test/com/opensymphony/xwork2/util/ResolverUtilTest.java

+/*
+ * Copyright (c) 2002-2003 by OpenSymphony
+ * All rights reserved.
+ */
+package com.opensymphony.xwork2.util;
+
+import java.net.URL;
+import java.util.Set;
+
+import com.opensymphony.xwork2.ObjectFactory;
+import com.opensymphony.xwork2.spring.SpringObjectFactory;
+
+import junit.framework.TestCase;
+
+public class ResolverUtilTest extends TestCase {
+
+    public void testSimpleFind() throws Exception {
+        ResolverUtil<ObjectFactory> resolver = new ResolverUtil<ObjectFactory>();
+        resolver.findImplementations(ObjectFactory.class, "com");
+        Set<Class<? extends ObjectFactory>> impls = resolver.getClasses();
+        
+        assertTrue(impls.contains(ObjectFactory.class));
+        assertTrue(impls.contains(SpringObjectFactory.class));
+    }
+    
+    public void testMissingSomeFind() throws Exception {
+        ResolverUtil<ObjectFactory> resolver = new ResolverUtil<ObjectFactory>();
+        resolver.findImplementations(ObjectFactory.class, "com.opensymphony.xwork2.spring");
+        Set<Class<? extends ObjectFactory>> impls = resolver.getClasses();
+        
+        assertFalse(impls.contains(ObjectFactory.class));
+        assertTrue(impls.contains(SpringObjectFactory.class));
+    }
+    
+    public void testFindNamedResource() throws Exception {
+        ResolverUtil resolver = new ResolverUtil();
+        resolver.findNamedResource("xwork-default.xml", "");
+        Set<URL> impls = resolver.getResources();
+        
+        assertTrue(impls.size() > 0);
+    }
+
+}
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.