ant-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From co...@apache.org
Subject cvs commit: jakarta-ant/src/main/org/apache/tools/ant AntClassLoader.java
Date Wed, 09 May 2001 15:14:59 GMT
conor       01/05/09 08:14:57

  Modified:    src/main/org/apache/tools/ant AntClassLoader.java
  Log:
  Add getResource() implementation to AntClassLoader
  
  PR:	749
  Submitted by:	mlauer-ssci@lauer-edv.com <mlauer-ssci@lauer-edv.com>
  
  Revision  Changes    Path
  1.17      +100 -0    jakarta-ant/src/main/org/apache/tools/ant/AntClassLoader.java
  
  Index: AntClassLoader.java
  ===================================================================
  RCS file: /home/cvs/jakarta-ant/src/main/org/apache/tools/ant/AntClassLoader.java,v
  retrieving revision 1.16
  retrieving revision 1.17
  diff -u -r1.16 -r1.17
  --- AntClassLoader.java	2001/03/15 13:53:29	1.16
  +++ AntClassLoader.java	2001/05/09 15:14:46	1.17
  @@ -58,6 +58,7 @@
   import java.util.*;
   import java.util.zip.*;
   import java.io.*;
  +import java.net.*;
   import org.apache.tools.ant.types.Path;
   
   /**
  @@ -378,6 +379,105 @@
           }
           
           return useSystemFirst;
  +    }
  +
  +
  +
  +
  +
  +    /**
  +     * Finds the resource with the given name. A resource is 
  +     * some data (images, audio, text, etc)
  +     * that can be accessed by class
  +     * code in a way that is independent of the location of the code.
  +     *
  +     * @param name the name of the resource for which a stream is required.
  +     *
  +     * @return a URL for reading the resource, or null if the resource 
  +     *         could not be found or the caller
  +     * doesn't have adequate privileges to get the resource.
  +     */
  +    public URL getResource(String name) {
  +        // we need to search the components of the path to see if we can find the
  +        // class we want.
  +        URL url = null;
  +        if (isSystemFirst(name)) {
  +            url = super.getResource(name);
  +        }
  +
  +        // try and load from this loader if the parent eitehr didn't find it or
  +        // wasn;t consulted.
  +        if (url == null) {
  +            String[] pathElements = classpath.list();
  +            for (int i = 0; i < pathElements.length && url == null; ++i) {
  +                File pathComponent = project.resolveFile((String)pathElements[i]);
  +                url = getResourceURL(pathComponent, name);
  +            }
  +        }
  +        
  +        if (url == null && !isSystemFirst(name)) {
  +            // this loader was first but it didn't find it - try the parent
  +            url = super.getResource(name);
  +        }
  +
  +        return url;
  +    }
  +
  +    /**
  +     * Get an inputstream to a given resource in the given file which may
  +     * either be a directory or a zip file.
  +     *
  +     * @param file the file (directory or jar) in which to search for 
  +     *             the resource.
  +     * @param resourceName the name of the resource for which a stream 
  +     *                     is required.
  +     *
  +     * @return a stream to the required resource or null if the 
  +     *         resource cannot be found in the given file object
  +     */
  +    private URL getResourceURL(File file, String resourceName) {
  +        try {
  +            if (!file.exists()) {
  +                return null;
  +            }
  +
  +            if (file.isDirectory()) {
  +                File resource = new File(file, resourceName);
  +
  +                if (resource.exists()) {
  +                    try {
  +                        return new URL("file:"+resource.toString());
  +                    } catch (MalformedURLException ex) {
  +                        return null;
  +                    }
  +                }
  +            }
  +            else {
  +                ZipFile zipFile = null;
  +                try {
  +                    zipFile = new ZipFile(file);
  +
  +                    ZipEntry entry = zipFile.getEntry(resourceName);
  +                    if (entry != null) {
  +                        try {
  +                            return new URL("jar:file:"+file.toString()+"!/"+entry);
  +                        } catch (MalformedURLException ex) {
  +                            return null;
  +                        }
  +                    }
  +                }
  +                finally {
  +                    if (zipFile != null) {
  +                        zipFile.close();
  +                    }
  +                }
  +            }
  +        }
  +        catch (Exception e) {
  +            e.printStackTrace();
  +        }
  +
  +        return null;
       }
   
   
  
  
  

Mime
View raw message