avalon-cvs mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From leosu...@apache.org
Subject cvs commit: avalon-sandbox/attributes/api/src/java/org/apache/avalon/attributes URLClassLoaderScanner.java ClassLoaderUtil.java ClassLoaderScanner.java Attributes.java
Date Tue, 12 Aug 2003 00:09:18 GMT
leosutic    2003/08/11 17:09:18

  Modified:    attributes/api/src/java/org/apache/avalon/attributes
                        Attributes.java
  Added:       attributes/api/src/java/org/apache/avalon/attributes
                        URLClassLoaderScanner.java ClassLoaderUtil.java
                        ClassLoaderScanner.java
  Log:
  Added code for scanning ClassLoaders and finding out what classes can be
  loaded from them. Also added a convenience function that will select from
  a Collection of Classes only those Classes that have an attribute with a
  certain class.
  
  Revision  Changes    Path
  1.6       +13 -0     avalon-sandbox/attributes/api/src/java/org/apache/avalon/attributes/Attributes.java
  
  Index: Attributes.java
  ===================================================================
  RCS file: /home/cvs/avalon-sandbox/attributes/api/src/java/org/apache/avalon/attributes/Attributes.java,v
  retrieving revision 1.5
  retrieving revision 1.6
  diff -u -r1.5 -r1.6
  --- Attributes.java	11 Aug 2003 00:20:05 -0000	1.5
  +++ Attributes.java	12 Aug 2003 00:09:18 -0000	1.6
  @@ -151,6 +151,19 @@
           return getAttributes (getAttributes (method), attributeClass);
       }
       
  +    public static Collection getClassesWithAttributeType (Collection classes, Class attributeClass)
{
  +        ArrayList result = new ArrayList ();
  +        Iterator iter = classes.iterator ();
  +        while (iter.hasNext ()) {
  +            Class clazz = (Class) iter.next ();
  +            if (hasAttributeType (clazz, attributeClass)) {
  +                result.add (clazz);
  +            }
  +        }
  +        
  +        return result;
  +    }
  +    
       /**
        * Convenience function to test whether a collection of attributes contain
        * an attribute of a given class.
  
  
  
  1.1                  avalon-sandbox/attributes/api/src/java/org/apache/avalon/attributes/URLClassLoaderScanner.java
  
  Index: URLClassLoaderScanner.java
  ===================================================================
  package org.apache.avalon.attributes;
  
  import java.io.File;
  import java.net.URL;
  import java.net.URLClassLoader;
  import java.util.ArrayList;
  import java.util.Enumeration;
  import java.util.HashSet;
  import java.util.Collection;
  import java.util.jar.JarFile;
  import java.util.jar.JarEntry;
  
  public class URLClassLoaderScanner implements ClassLoaderScanner {
      
      public Class getClassLoaderClass () {
          return URLClassLoader.class;
      }
      
      private Collection getAllClassesFromJarFile (ClassLoader cl, JarFile jar) throws Exception
{
          HashSet result = new HashSet ();
          Enumeration enum = jar.entries ();
          while (enum.hasMoreElements ()) {
              JarEntry entry = (JarEntry) enum.nextElement ();
              if (!entry.isDirectory ()) {
                  String className = entry.getName ();
                  if (className.endsWith (".class")) {
                      className = className.substring (0, className.length () - 6);
                      className.replace ('/', '.').replace ('\\', '.').replace (':', '.');
                      try {
                          Class clazz = cl.loadClass (className);
                          result.add (clazz);
                      } catch (Exception e) {
                      }
                  }
              }
          }
          return result;
      }
      
      private Collection getAllClassesFromDirectory (ClassLoader cl, File dir, String prefix)
throws Exception {
          HashSet result = new HashSet ();
          File[] files = dir.listFiles ();
          for (int i = 0; i < files.length; i++) {
              if (files[i].isDirectory ()) {
                  String newPrefix = prefix.equals ("") ? (files[i].getName () + ".") : (prefix
+ files[i].getName () + ".");
                  result.addAll (getAllClassesFromDirectory (cl, files[i], newPrefix));
              } else if (files[i].getName ().endsWith (".class")) {
                  String className = prefix + files[i].getName ().substring (0, files[i].getName
().length () - 6);
                  try {
                      Class clazz = cl.loadClass (className);
                      result.add (clazz);
                  } catch (Exception e) {
                  }
              }
          }
          return result;
      }
      
      private Collection getAllClassesFromDirectory (ClassLoader cl, File dir) throws Exception
{
          return getAllClassesFromDirectory (cl, dir, "");
      }
      
      private Collection getAllClasses (ClassLoader cl, URL url) throws Exception {
          if ("file".equals (url.getProtocol ())) {
              File f = new File (url.getPath ());
              if (f.exists ()) {
                  if (f.isDirectory ()) {
                      return getAllClassesFromDirectory (cl, f);
                  } else {
                      return getAllClassesFromJarFile (cl, new JarFile (f));
                  }                
              } else {
                  return new ArrayList (0);
              }
          } else {
              return new ArrayList (0);
          }
      }
      
      public Collection getClasses (ClassLoader cl) {
          URLClassLoader classLoader = (URLClassLoader) cl;
          
          URL[] sourceURLs = classLoader.getURLs ();
          HashSet result = new HashSet ();
          
          try {
              for (int i = 0; i < sourceURLs.length; i++) {
                  result.addAll (getAllClasses (classLoader, sourceURLs[i]));
              }
          } catch (Exception e) {
          }
          
          return result;
      }
  }
  
  
  1.1                  avalon-sandbox/attributes/api/src/java/org/apache/avalon/attributes/ClassLoaderUtil.java
  
  Index: ClassLoaderUtil.java
  ===================================================================
  package org.apache.avalon.attributes;
  
  import java.util.Collection;
  import java.util.HashMap;
  import java.util.Map;
  
  public class ClassLoaderUtil {
      
      private final static ClassLoaderUtil classLoaderUtil = new ClassLoaderUtil ();
      static {
          classLoaderUtil.addClassLoaderScanner (new URLClassLoaderScanner ());
      }
      
      private Map classLoaderScanners = new HashMap ();
      
      public static ClassLoaderUtil getClassLoaderUtil () {
          return classLoaderUtil;
      }
      
      public void addClassLoaderScanner (ClassLoaderScanner scanner) {
          classLoaderScanners.put (scanner.getClassLoaderClass (), scanner);
      }
      
      public Collection getClasses (ClassLoader cl) {
          ClassLoaderScanner scanner = (ClassLoaderScanner) classLoaderScanners.get (cl.getClass
());
          if (scanner != null) {
              return scanner.getClasses (cl);
          } else {
              throw new UnsupportedOperationException ("No ClassLoaderScanner for ClassLoader
of type " + 
                  cl.getClass ().getName ());
          }
      }
  }
  
  
  1.1                  avalon-sandbox/attributes/api/src/java/org/apache/avalon/attributes/ClassLoaderScanner.java
  
  Index: ClassLoaderScanner.java
  ===================================================================
  package org.apache.avalon.attributes;
  
  import java.util.Collection;
  
  public interface ClassLoaderScanner {
      public Class getClassLoaderClass ();
      public Collection getClasses (ClassLoader cl);
  }
  
  

---------------------------------------------------------------------
To unsubscribe, e-mail: cvs-unsubscribe@avalon.apache.org
For additional commands, e-mail: cvs-help@avalon.apache.org


Mime
View raw message