You are viewing a plain text version of this content. The canonical link for it is here.
Posted to cvs@avalon.apache.org by bl...@apache.org on 2002/09/16 23:16:11 UTC

cvs commit: jakarta-avalon-excalibur/container/src/java/org/apache/excalibur/container/classloader JarScanner.java

bloritsch    2002/09/16 14:16:11

  Modified:    container/src/java/org/apache/excalibur/container/classloader
                        JarScanner.java
  Log:
  add some more scanning facilities to JarScanner.
  
  Revision  Changes    Path
  1.2       +104 -9    jakarta-avalon-excalibur/container/src/java/org/apache/excalibur/container/classloader/JarScanner.java
  
  Index: JarScanner.java
  ===================================================================
  RCS file: /home/cvs/jakarta-avalon-excalibur/container/src/java/org/apache/excalibur/container/classloader/JarScanner.java,v
  retrieving revision 1.1
  retrieving revision 1.2
  diff -u -r1.1 -r1.2
  --- JarScanner.java	16 Sep 2002 20:05:39 -0000	1.1
  +++ JarScanner.java	16 Sep 2002 21:16:11 -0000	1.2
  @@ -12,8 +12,10 @@
   import java.util.Iterator;
   import java.util.Map;
   import java.util.Set;
  +import java.util.StringTokenizer;
   import java.util.jar.Attributes;
   
  +import java.io.File;
   import java.io.IOException;
   
   import java.net.JarURLConnection;
  @@ -22,20 +24,20 @@
   import java.net.URLStreamHandlerFactory;
   
   /**
  - * A ClassLoader that lists all the entries of a particular type.  Using
  - * this classloader we can resolve all the Avalon types and services,
  - * and iterate through the list.  In all other ways, this
  - * <code>ClassLoader</code> behaves identically to the
  - * @link{java.net.URLClassLoader}
  + * The JarScanner will scan any URL pointing to a jar, and check for any Avalon
  + * component declaration entries.  We have a convenience method to grab all the
  + * entries in the System class path and extension directories.
    *
    * @author <a href="mailto:bloritsch@apache.org">Berin Loritsch</a>
    */
   public final class JarScanner
   {
  -    protected static final String AVALON = "Avalon";
  -    protected static final String BLOCK = AVALON + "-Block";
  -    protected static final String TYPE = "Type";
  -    protected static final String SERVICE = "Service";
  +    private static final String AVALON = "Avalon";
  +    private static final String BLOCK = AVALON + "-Block";
  +    private static final String TYPE = "Type";
  +    private static final String SERVICE = "Service";
  +
  +    private static JarEntries m_systemJarEntries = null;
   
       private JarScanner()
       {}
  @@ -94,6 +96,99 @@
           catch ( IOException ioe )
           {
               // TODO: Handle error condition
  +        }
  +
  +        return entries;
  +    }
  +
  +    /**
  +     * Scan all the jar files in <code>System.getProperty("java.class.path")</code>
  +     * and <code>System.getProperty("java.ext.dirs")</code>
  +     */
  +    public JarEntries getSystemJarEntries()
  +    {
  +        if ( m_systemJarEntries == null )
  +        {
  +            m_systemJarEntries = loadSystemJarEntries();
  +        }
  +
  +        return m_systemJarEntries;
  +    }
  +
  +    /**
  +     * Scan all the jar files in <code>System.getProperty("java.class.path")</code>
  +     * and <code>System.getProperty("java.ext.dirs")</code>
  +     */
  +    private JarEntries loadSystemJarEntries()
  +    {
  +        JarEntries entries = new JarEntries();
  +        String pathSeparator = System.getProperty("path.separator");
  +
  +        StringTokenizer classPath = new StringTokenizer( System.getProperty("java.class.path"),
  +                                               pathSeparator, false);
  +
  +        while( classPath.hasMoreTokens() )
  +        {
  +            String path = classPath.nextToken();
  +
  +            try
  +            {
  +                entries.merge( scan( new URL( path ) ) );
  +            }
  +            catch ( Exception e )
  +            {
  +                // skip and move on
  +            }
  +        }
  +
  +        classPath  = new StringTokenizer( System.getProperty("java.ext.dirs"),
  +                                               pathSeparator, false);
  +
  +        while( classPath.hasMoreTokens() )
  +        {
  +            String dir = classPath.nextToken();
  +
  +            entries.merge( scanDirectory( new File( dir ) ) );
  +        }
  +
  +        return entries;
  +    }
  +
  +    /**
  +     * Scans for all jars in a directory, and also treats the directory itself
  +     * as a possible classpath location.
  +     */
  +    public JarEntries scanDirectory( File path )
  +    {
  +        JarEntries entries = new JarEntries();
  +
  +        if ( path.isDirectory() )
  +        {
  +            File[] pathEntries = path.listFiles();
  +
  +            for ( int i = 0; i < pathEntries.length; i++ )
  +            {
  +                if ( pathEntries[i].isFile() )
  +                {
  +                    try
  +                    {
  +                        entries.merge( scan( pathEntries[i].toURL() ) );
  +                    }
  +                    catch ( Exception e )
  +                    {
  +                        // ignore and move on to the next entry
  +                    }
  +                }
  +            }
  +        }
  +
  +        try
  +        {
  +            entries.merge( scan( path.toURL() ) );
  +        }
  +        catch ( Exception e )
  +        {
  +            // ignore and move on
           }
   
           return entries;
  
  
  

--
To unsubscribe, e-mail:   <ma...@jakarta.apache.org>
For additional commands, e-mail: <ma...@jakarta.apache.org>