From 093967db0e29ab675eef29fbe21d840f3467c5a7 Mon Sep 17 00:00:00 2001 From: Peter Donald Date: Sun, 30 Dec 2001 03:33:58 +0000 Subject: [PATCH] Removed AntClassLoader and replaced it with URLClassLoader. Now new ClassLoaders do not include the ant runtime by default git-svn-id: https://svn.apache.org/repos/asf/ant/core/trunk@270450 13f79535-47bb-0310-9956-ffa450edef68 --- .../apache/antlib/xml/XMLValidateTask.java | 13 +- .../org/apache/antlib/xml/XSLTProcess.java | 8 +- .../org/apache/tools/ant/AntClassLoader.java | 1118 ----------------- .../apache/tools/ant/taskdefs/Available.java | 162 ++- .../tools/ant/taskdefs/ExecuteJava.java | 51 +- .../apache/tools/ant/taskdefs/Property.java | 22 +- .../org/apache/tools/ant/taskdefs/Rmic.java | 4 +- .../apache/tools/ant/taskdefs/SQLExec.java | 38 +- .../compilers/DefaultCompilerAdapter.java | 3 +- .../ant/taskdefs/optional/depend/Depend.java | 7 +- .../optional/ejb/GenericDeploymentTool.java | 4 +- .../optional/ejb/WeblogicDeploymentTool.java | 4 +- .../optional/ejb/WebsphereDeploymentTool.java | 4 +- .../taskdefs/optional/junit/JUnitTask.java | 17 +- .../optional/junit/JUnitTestRunner.java | 4 - .../optional/junit/XMLResultAggregator.java | 10 +- .../org/apache/tools/ant/types/Mapper.java | 11 +- .../main/org/apache/tools/ant/types/Path.java | 30 +- .../org/apache/tools/ant/AntClassLoader.java | 1118 ----------------- .../apache/tools/ant/taskdefs/Available.java | 162 ++- .../tools/ant/taskdefs/ExecuteJava.java | 51 +- .../apache/tools/ant/taskdefs/Property.java | 22 +- .../org/apache/tools/ant/taskdefs/Rmic.java | 4 +- .../apache/tools/ant/taskdefs/SQLExec.java | 38 +- .../compilers/DefaultCompilerAdapter.java | 3 +- .../ant/taskdefs/optional/depend/Depend.java | 7 +- .../optional/ejb/GenericDeploymentTool.java | 4 +- .../optional/ejb/WeblogicDeploymentTool.java | 4 +- .../optional/ejb/WebsphereDeploymentTool.java | 4 +- .../taskdefs/optional/junit/JUnitTask.java | 17 +- .../optional/junit/JUnitTestRunner.java | 4 - .../optional/junit/XMLResultAggregator.java | 10 +- .../org/apache/tools/ant/types/Mapper.java | 11 +- .../todo/org/apache/tools/ant/types/Path.java | 30 +- 34 files changed, 356 insertions(+), 2643 deletions(-) delete mode 100644 proposal/myrmidon/src/main/org/apache/tools/ant/AntClassLoader.java delete mode 100644 proposal/myrmidon/src/todo/org/apache/tools/ant/AntClassLoader.java diff --git a/proposal/myrmidon/src/java/org/apache/antlib/xml/XMLValidateTask.java b/proposal/myrmidon/src/java/org/apache/antlib/xml/XMLValidateTask.java index 0407beede..eb3a8ee45 100644 --- a/proposal/myrmidon/src/java/org/apache/antlib/xml/XMLValidateTask.java +++ b/proposal/myrmidon/src/java/org/apache/antlib/xml/XMLValidateTask.java @@ -15,13 +15,12 @@ import java.io.IOException; import java.io.InputStream; import java.net.MalformedURLException; import java.net.URL; +import java.net.URLClassLoader; import java.util.ArrayList; import java.util.Enumeration; import java.util.Hashtable; import org.apache.myrmidon.api.TaskException; -import org.apache.tools.ant.AntClassLoader; import org.apache.tools.ant.DirectoryScanner; -import org.apache.tools.ant.Project; import org.apache.tools.ant.Task; import org.apache.tools.ant.types.FileSet; import org.apache.tools.ant.types.Path; @@ -369,17 +368,15 @@ public class XMLValidateTask // load the parser class // with JAXP, we would use a SAXParser factory Class readerClass = null; - //Class readerImpl = null; - //Class parserImpl = null; if( classpath != null ) { - AntClassLoader loader = new AntClassLoader( getProject(), classpath ); - // loader.addSystemPackageRoot("org.xml"); // needed to avoid conflict - readerClass = loader.loadClass( readerClassName ); - AntClassLoader.initializeClass( readerClass ); + final ClassLoader classLoader = new URLClassLoader( classpath.toURLs() ); + readerClass = classLoader.loadClass( readerClassName ); } else + { readerClass = Class.forName( readerClassName ); + } // then check it implements XMLReader if( XMLReader.class.isAssignableFrom( readerClass ) ) diff --git a/proposal/myrmidon/src/java/org/apache/antlib/xml/XSLTProcess.java b/proposal/myrmidon/src/java/org/apache/antlib/xml/XSLTProcess.java index cf48450a8..cf13d27b7 100644 --- a/proposal/myrmidon/src/java/org/apache/antlib/xml/XSLTProcess.java +++ b/proposal/myrmidon/src/java/org/apache/antlib/xml/XSLTProcess.java @@ -8,10 +8,10 @@ package org.apache.antlib.xml; import java.io.File; +import java.net.URLClassLoader; import java.util.ArrayList; import java.util.Iterator; import org.apache.myrmidon.api.TaskException; -import org.apache.tools.ant.AntClassLoader; import org.apache.tools.ant.DirectoryScanner; import org.apache.tools.ant.taskdefs.MatchingTask; import org.apache.tools.ant.types.Path; @@ -378,10 +378,8 @@ public class XSLTProcess } else { - AntClassLoader al = new AntClassLoader( getProject(), m_classpath ); - Class c = al.loadClass( classname ); - AntClassLoader.initializeClass( c ); - return c; + final ClassLoader classLoader = new URLClassLoader( m_classpath.toURLs() ); + return classLoader.loadClass( classname ); } } diff --git a/proposal/myrmidon/src/main/org/apache/tools/ant/AntClassLoader.java b/proposal/myrmidon/src/main/org/apache/tools/ant/AntClassLoader.java deleted file mode 100644 index 88d5d4983..000000000 --- a/proposal/myrmidon/src/main/org/apache/tools/ant/AntClassLoader.java +++ /dev/null @@ -1,1118 +0,0 @@ -/* - * Copyright (C) The Apache Software Foundation. All rights reserved. - * - * This software is published under the terms of the Apache Software License - * version 1.1, a copy of which has been included with this distribution in - * the LICENSE.txt file. - */ -package org.apache.tools.ant; - -import java.io.ByteArrayOutputStream; -import java.io.File; -import java.io.FileInputStream; -import java.io.IOException; -import java.io.InputStream; -import java.lang.reflect.InvocationTargetException; -import java.lang.reflect.Method; -import java.net.MalformedURLException; -import java.net.URL; -import java.util.ArrayList; -import java.util.Enumeration; -import java.util.Hashtable; -import java.util.Iterator; -import java.util.zip.ZipEntry; -import java.util.zip.ZipFile; -import org.apache.avalon.excalibur.io.FileUtil; -import org.apache.avalon.framework.logger.LogEnabled; -import org.apache.avalon.framework.logger.Logger; -import org.apache.myrmidon.api.TaskException; -import org.apache.tools.ant.types.Path; - -/** - * Used to load classes within ant with a different claspath from that used to - * start ant. Note that it is possible to force a class into this loader even - * when that class is on the system classpath by using the forceLoadClass - * method. Any subsequent classes loaded by that class will then use this loader - * rather than the system class loader. - * - * @author Conor MacNeill - * @author Jesse Glick - */ -public class AntClassLoader - extends ClassLoader - implements BuildListener, LogEnabled -{ - /** - * The size of buffers to be used in this classloader. - */ - private final static int BUFFER_SIZE = 8192; - - private static Method getProtectionDomain; - private static Method defineClassProtectionDomain; - private static Method getContextClassLoader; - private static Method setContextClassLoader; - - private Logger m_logger; - - /** - * Provide component with a logger. - * - * @param logger the logger - */ - public void enableLogging( Logger logger ) - { - m_logger = logger; - } - - protected final Logger getLogger() - { - return m_logger; - } - - /** - * The components of the classpath that the classloader searches for classes - */ - ArrayList pathComponents = new ArrayList(); - - /** - * Indicates whether the parent class loader should be consulted before - * trying to load with this class loader. - */ - private boolean parentFirst = true; - - /** - * These are the package roots that are to be loaded by the parent class - * loader regardless of whether the parent class loader is being searched - * first or not. - */ - private ArrayList systemPackages = new ArrayList(); - - /** - * These are the package roots that are to be loaded by this class loader - * regardless of whether the parent class loader is being searched first or - * not. - */ - private ArrayList loaderPackages = new ArrayList(); - - /** - * This flag indicates that the classloader will ignore the base classloader - * if it can't find a class. - */ - private boolean ignoreBase = false; - - /** - * The parent class loader, if one is given or can be determined - */ - private ClassLoader parent = null; - - /** - * A hashtable of zip files opened by the classloader - */ - private Hashtable zipFiles = new Hashtable(); - - /** - * The context loader saved when setting the thread's current context - * loader. - */ - private ClassLoader savedContextLoader = null; - private boolean isContextLoaderSaved = false; - - /** - * The project to which this class loader belongs. - */ - private Project project; - - static - { - try - { - getProtectionDomain = Class.class.getMethod( "getProtectionDomain", new Class[ 0 ] ); - Class protectionDomain = Class.forName( "java.security.ProtectionDomain" ); - Class[] args = new Class[]{String.class, byte[].class, Integer.TYPE, Integer.TYPE, protectionDomain}; - defineClassProtectionDomain = ClassLoader.class.getDeclaredMethod( "defineClass", args ); - - getContextClassLoader = Thread.class.getMethod( "getContextClassLoader", new Class[ 0 ] ); - args = new Class[]{ClassLoader.class}; - setContextClassLoader = Thread.class.getMethod( "setContextClassLoader", args ); - } - catch( Exception e ) - { - } - } - - /** - * Create a classloader for the given project using the classpath given. - * - * @param project the project to which this classloader is to belong. - * @param classpath the classpath to use to load the classes. This is - * combined with the system classpath in a manner determined by the - * value of ${build.sysclasspath} - */ - public AntClassLoader( Project project, Path classpath ) - throws TaskException - { - parent = AntClassLoader.class.getClassLoader(); - this.project = project; - project.addBuildListener( this ); - if( classpath != null ) - { - Path actualClasspath = classpath.concatSystemClasspath( "ignore" ); - String[] pathElements = actualClasspath.list(); - for( int i = 0; i < pathElements.length; ++i ) - { - try - { - addPathElement( (String)pathElements[ i ] ); - } - catch( TaskException e ) - { - // ignore path elements which are invalid relative to the project - } - } - } - } - - /** - * Create a classloader for the given project using the classpath given. - * - * @param parent the parent classloader to which unsatisfied loading - * attempts are delgated - * @param project the project to which this classloader is to belong. - * @param classpath the classpath to use to load the classes. - * @param parentFirst if true indicates that the parent classloader should - * be consulted before trying to load the a class through this loader. - */ - public AntClassLoader( ClassLoader parent, Project project, Path classpath, - boolean parentFirst ) - throws TaskException - { - this( project, classpath ); - if( parent != null ) - { - this.parent = parent; - } - this.parentFirst = parentFirst; - addSystemPackageRoot( "java" ); - addSystemPackageRoot( "javax" ); - } - - /** - * Create a classloader for the given project using the classpath given. - * - * @param project the project to which this classloader is to belong. - * @param classpath the classpath to use to load the classes. - * @param parentFirst if true indicates that the parent classloader should - * be consulted before trying to load the a class through this loader. - */ - public AntClassLoader( Project project, Path classpath, boolean parentFirst ) - throws TaskException - { - this( null, project, classpath, parentFirst ); - } - - /** - * Create an empty class loader. The classloader should be configured with - * path elements to specify where the loader is to look for classes. - * - * @param parent the parent classloader to which unsatisfied loading - * attempts are delgated - * @param parentFirst if true indicates that the parent classloader should - * be consulted before trying to load the a class through this loader. - */ - public AntClassLoader( ClassLoader parent, boolean parentFirst ) - { - if( parent != null ) - { - this.parent = parent; - } - else - { - parent = AntClassLoader.class.getClassLoader(); - } - project = null; - this.parentFirst = parentFirst; - } - - /** - * Force initialization of a class in a JDK 1.1 compatible, albeit hacky way - * - * @param theClass Description of Parameter - */ - public static void initializeClass( Class theClass ) - { - // ***HACK*** We try to create an instance to force the VM to run the - // class' static initializer. We don't care if the instance can't - // be created - we are just interested in the side effect. - try - { - theClass.newInstance(); - } - catch( Throwable t ) - { - //ignore - our work is done - } - } - - /** - * Set this classloader to run in isolated mode. In isolated mode, classes - * not found on the given classpath will not be referred to the base class - * loader but will cause a classNotFoundException. - * - * @param isolated The new Isolated value - */ - public void setIsolated( boolean isolated ) - { - ignoreBase = isolated; - } - - /** - * Set the current thread's context loader to this classloader, storing the - * current loader value for later resetting - */ - public void setThreadContextLoader() - throws TaskException - { - if( isContextLoaderSaved ) - { - throw new TaskException( "Context loader has not been reset" ); - } - if( getContextClassLoader != null && setContextClassLoader != null ) - { - try - { - savedContextLoader - = (ClassLoader)getContextClassLoader.invoke( Thread.currentThread(), new Object[ 0 ] ); - Object[] args = new Object[]{this}; - setContextClassLoader.invoke( Thread.currentThread(), args ); - isContextLoaderSaved = true; - } - catch( InvocationTargetException ite ) - { - Throwable t = ite.getTargetException(); - throw new TaskException( t.toString() ); - } - catch( Exception e ) - { - throw new TaskException( e.toString() ); - } - } - } - - /** - * 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( isParentFirst( name ) ) - { - url = ( parent == null ) ? super.getResource( name ) : parent.getResource( name ); - } - - if( url != null ) - { - log( "Resource " + name + " loaded from parent loader", - Project.MSG_DEBUG ); - - } - else - { - // try and load from this loader if the parent either didn't find - // it or wasn't consulted. - for( Iterator e = pathComponents.iterator(); e.hasNext() && url == null; ) - { - File pathComponent = (File)e.next(); - url = getResourceURL( pathComponent, name ); - if( url != null ) - { - log( "Resource " + name - + " loaded from ant loader", - Project.MSG_DEBUG ); - } - } - } - - if( url == null && !isParentFirst( name ) ) - { - // this loader was first but it didn't find it - try the parent - - url = ( parent == null ) ? super.getResource( name ) : parent.getResource( name ); - if( url != null ) - { - log( "Resource " + name + " loaded from parent loader", - Project.MSG_DEBUG ); - } - } - - if( url == null ) - { - getLogger().debug( "Couldn't load Resource " + name ); - } - - return url; - } - - /** - * Get a stream to read the requested resource name. - * - * @param name 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 on the loader's classpath. - */ - public InputStream getResourceAsStream( String name ) - { - - InputStream resourceStream = null; - if( isParentFirst( name ) ) - { - resourceStream = loadBaseResource( name ); - if( resourceStream != null ) - { - log( "ResourceStream for " + name - + " loaded from parent loader", Project.MSG_DEBUG ); - - } - else - { - resourceStream = loadResource( name ); - if( resourceStream != null ) - { - log( "ResourceStream for " + name - + " loaded from ant loader", Project.MSG_DEBUG ); - } - } - } - else - { - resourceStream = loadResource( name ); - if( resourceStream != null ) - { - log( "ResourceStream for " + name - + " loaded from ant loader", Project.MSG_DEBUG ); - - } - else - { - resourceStream = loadBaseResource( name ); - if( resourceStream != null ) - { - log( "ResourceStream for " + name - + " loaded from parent loader", Project.MSG_DEBUG ); - } - } - } - - if( resourceStream == null ) - { - log( "Couldn't load ResourceStream for " + name, - Project.MSG_DEBUG ); - } - - return resourceStream; - } - - /** - * Add a package root to the list of packages which must be loaded using - * this loader. All subpackages are also included. - * - * @param packageRoot the root of akll packages to be included. - */ - public void addLoaderPackageRoot( String packageRoot ) - { - loaderPackages.add( packageRoot + "." ); - } - - /** - * Add an element to the classpath to be searched - * - * @param pathElement The feature to be added to the PathElement attribute - * @exception TaskException Description of Exception - */ - public void addPathElement( String pathElement ) - throws TaskException - { - File pathComponent - = project != null ? FileUtil.resolveFile( project.getBaseDir(), pathElement ) - : new File( pathElement ); - pathComponents.add( pathComponent ); - } - - /** - * Add a package root to the list of packages which must be loaded on the - * parent loader. All subpackages are also included. - * - * @param packageRoot the root of all packages to be included. - */ - public void addSystemPackageRoot( String packageRoot ) - { - systemPackages.add( packageRoot + "." ); - } - - public void buildFinished( BuildEvent event ) - { - cleanup(); - } - - public void buildStarted( BuildEvent event ) - { - } - - public void cleanup() - { - pathComponents = null; - project = null; - for( Enumeration e = zipFiles.elements(); e.hasMoreElements(); ) - { - ZipFile zipFile = (ZipFile)e.nextElement(); - try - { - zipFile.close(); - } - catch( IOException ioe ) - { - // ignore - } - } - zipFiles = new Hashtable(); - } - - /** - * Search for and load a class on the classpath of this class loader. - * - * @param name the classname to be loaded. - * @return the required Class object - * @throws ClassNotFoundException if the requested class does not exist on - * this loader's classpath. - */ - public Class findClass( String name ) - throws ClassNotFoundException - { - getLogger().debug( "Finding class " + name ); - - return findClassInComponents( name ); - } - - /** - * Load a class through this class loader even if that class is available on - * the parent classpath. This ensures that any classes which are loaded by - * the returned class will use this classloader. - * - * @param classname the classname to be loaded. - * @return the required Class object - * @throws ClassNotFoundException if the requested class does not exist on - * this loader's classpath. - */ - public Class forceLoadClass( String classname ) - throws ClassNotFoundException - { - getLogger().debug( "force loading " + classname ); - - Class theClass = findLoadedClass( classname ); - - if( theClass == null ) - { - theClass = findClass( classname ); - } - - return theClass; - } - - /** - * Load a class through this class loader but defer to the parent class - * loader This ensures that instances of the returned class will be - * compatible with instances which which have already been loaded on the - * parent loader. - * - * @param classname the classname to be loaded. - * @return the required Class object - * @throws ClassNotFoundException if the requested class does not exist on - * this loader's classpath. - */ - public Class forceLoadSystemClass( String classname ) - throws ClassNotFoundException - { - getLogger().debug( "force system loading " + classname ); - - Class theClass = findLoadedClass( classname ); - - if( theClass == null ) - { - theClass = findBaseClass( classname ); - } - - return theClass; - } - - public void messageLogged( BuildEvent event ) - { - } - - /** - * Reset the current thread's context loader to its original value - */ - public void resetThreadContextLoader() - throws TaskException - { - if( isContextLoaderSaved && - getContextClassLoader != null && setContextClassLoader != null ) - { - try - { - Object[] args = new Object[]{savedContextLoader}; - setContextClassLoader.invoke( Thread.currentThread(), args ); - savedContextLoader = null; - isContextLoaderSaved = false; - } - catch( InvocationTargetException ite ) - { - Throwable t = ite.getTargetException(); - throw new TaskException( t.toString() ); - } - catch( Exception e ) - { - throw new TaskException( e.toString() ); - } - } - } - - public void targetFinished( BuildEvent event ) - { - } - - public void targetStarted( BuildEvent event ) - { - } - - public void taskFinished( BuildEvent event ) - { - } - - public void taskStarted( BuildEvent event ) - { - } - - /** - * Returns an enumeration of URLs representing all the resources with the - * given name by searching the class loader's classpath. - * - * @param name the resource name. - * @return an enumeration of URLs for the resources. - * @throws IOException if I/O errors occurs (can't happen) - */ - protected Enumeration findResources( String name ) - throws IOException - { - return new ResourceEnumeration( name ); - } - - /** - * Load a class with this class loader. This method will load a class. This - * class attempts to load the class firstly using the parent class loader. - * For JDK 1.1 compatability, this uses the findSystemClass method. - * - * @param classname the name of the class to be loaded. - * @param resolve true if all classes upon which this class depends are to - * be loaded. - * @return the required Class object - * @throws ClassNotFoundException if the requested class does not exist on - * the system classpath or this loader's classpath. - */ - protected Class loadClass( String classname, boolean resolve ) - throws ClassNotFoundException - { - - Class theClass = findLoadedClass( classname ); - if( theClass != null ) - { - return theClass; - } - - if( isParentFirst( classname ) ) - { - try - { - theClass = findBaseClass( classname ); - getLogger().debug( "Class " + classname + " loaded from parent loader" ); - } - catch( ClassNotFoundException cnfe ) - { - theClass = findClass( classname ); - getLogger().debug( "Class " + classname + " loaded from ant loader" ); - } - } - else - { - try - { - theClass = findClass( classname ); - getLogger().debug( "Class " + classname + " loaded from ant loader" ); - } - catch( ClassNotFoundException cnfe ) - { - if( ignoreBase ) - { - throw cnfe; - } - theClass = findBaseClass( classname ); - getLogger().debug( "Class " + classname + " loaded from parent loader" ); - } - } - - if( resolve ) - { - resolveClass( theClass ); - } - - return theClass; - } - - /** - * Log a message through the project object if one has been provided. - * - * @param message the message to log - * @param priority the logging priority of the message - */ - protected void log( String message, int priority ) - { - if( project != null ) - { - project.log( message, priority ); - } - // else { - // System.out.println(message); - // } - } - - /** - * Convert the class dot notation to a filesystem equivalent for searching - * purposes. - * - * @param classname the class name in dot format (ie java.lang.Integer) - * @return the classname in filesystem format (ie java/lang/Integer.class) - */ - private String getClassFilename( String classname ) - { - return classname.replace( '.', '/' ) + ".class"; - } - - /** - * Read a class definition from a stream. - * - * @param stream the stream from which the class is to be read. - * @param classname the class name of the class in the stream. - * @return the Class object read from the stream. - * @throws IOException if there is a problem reading the class from the - * stream. - */ - private Class getClassFromStream( InputStream stream, String classname ) - throws IOException - { - ByteArrayOutputStream baos = new ByteArrayOutputStream(); - int bytesRead = -1; - byte[] buffer = new byte[ BUFFER_SIZE ]; - - while( ( bytesRead = stream.read( buffer, 0, BUFFER_SIZE ) ) != -1 ) - { - baos.write( buffer, 0, bytesRead ); - } - - byte[] classData = baos.toByteArray(); - - // Simply put: - // defineClass(classname, classData, 0, classData.length, Project.class.getProtectionDomain()); - // Made more elaborate to be 1.1-safe. - if( defineClassProtectionDomain != null ) - { - try - { - Object domain = getProtectionDomain.invoke( Project.class, new Object[ 0 ] ); - Object[] args = new Object[]{classname, classData, new Integer( 0 ), new Integer( classData.length ), domain}; - return (Class)defineClassProtectionDomain.invoke( this, args ); - } - catch( InvocationTargetException ite ) - { - Throwable t = ite.getTargetException(); - if( t instanceof ClassFormatError ) - { - throw (ClassFormatError)t; - } - else if( t instanceof NoClassDefFoundError ) - { - throw (NoClassDefFoundError)t; - } - else - { - throw new IOException( t.toString() ); - } - } - catch( Exception e ) - { - throw new IOException( e.toString() ); - } - } - else - { - return defineClass( classname, classData, 0, classData.length ); - } - } - - /** - * 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 InputStream getResourceStream( File file, String resourceName ) - { - try - { - if( !file.exists() ) - { - return null; - } - - if( file.isDirectory() ) - { - File resource = new File( file, resourceName ); - - if( resource.exists() ) - { - return new FileInputStream( resource ); - } - } - else - { - // is the zip file in the cache - ZipFile zipFile = (ZipFile)zipFiles.get( file ); - if( zipFile == null ) - { - zipFile = new ZipFile( file ); - zipFiles.put( file, zipFile ); - } - ZipEntry entry = zipFile.getEntry( resourceName ); - if( entry != null ) - { - return zipFile.getInputStream( entry ); - } - } - } - catch( Exception e ) - { - getLogger().debug( "Ignoring Exception " + e.getClass().getName() + ": " + e.getMessage() + " reading resource " + resourceName + " from " + file ); - } - - return null; - } - - /** - * 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 = (ZipFile)zipFiles.get( file ); - if( zipFile == null ) - { - zipFile = new ZipFile( file ); - zipFiles.put( file, zipFile ); - } - - ZipEntry entry = zipFile.getEntry( resourceName ); - if( entry != null ) - { - try - { - return new URL( "jar:file:" + file.toString() + "!/" + entry ); - } - catch( MalformedURLException ex ) - { - return null; - } - } - } - } - catch( Exception e ) - { - e.printStackTrace(); - } - - return null; - } - - private boolean isParentFirst( String resourceName ) - { - // default to the global setting and then see - // if this class belongs to a package which has been - // designated to use a specific loader first (this one or the parent one) - boolean useParentFirst = parentFirst; - - for( Iterator e = systemPackages.iterator(); e.hasNext(); ) - { - String packageName = (String)e.next(); - if( resourceName.startsWith( packageName ) ) - { - useParentFirst = true; - break; - } - } - - for( Iterator e = loaderPackages.iterator(); e.hasNext(); ) - { - String packageName = (String)e.next(); - if( resourceName.startsWith( packageName ) ) - { - useParentFirst = false; - break; - } - } - - return useParentFirst; - } - - /** - * Find a system class (which should be loaded from the same classloader as - * the Ant core). - * - * @param name Description of Parameter - * @return Description of the Returned Value - * @exception ClassNotFoundException Description of Exception - */ - private Class findBaseClass( String name ) - throws ClassNotFoundException - { - if( parent == null ) - { - return findSystemClass( name ); - } - else - { - return parent.loadClass( name ); - } - } - - /** - * Find a class on the given classpath. - * - * @param name Description of Parameter - * @return Description of the Returned Value - * @exception ClassNotFoundException Description of Exception - */ - private Class findClassInComponents( String name ) - throws ClassNotFoundException - { - // we need to search the components of the path to see if we can find the - // class we want. - InputStream stream = null; - String classFilename = getClassFilename( name ); - try - { - for( Iterator e = pathComponents.iterator(); e.hasNext(); ) - { - File pathComponent = (File)e.next(); - try - { - stream = getResourceStream( pathComponent, classFilename ); - if( stream != null ) - { - return getClassFromStream( stream, name ); - } - } - catch( IOException ioe ) - { - // ioe.printStackTrace(); - getLogger().debug( "Exception reading component " + pathComponent ); - } - } - - throw new ClassNotFoundException( name ); - } - finally - { - try - { - if( stream != null ) - { - stream.close(); - } - } - catch( IOException e ) - { - } - } - } - - /** - * Find a system resource (which should be loaded from the parent - * classloader). - * - * @param name Description of Parameter - * @return Description of the Returned Value - */ - private InputStream loadBaseResource( String name ) - { - if( parent == null ) - { - return getSystemResourceAsStream( name ); - } - else - { - return parent.getResourceAsStream( name ); - } - } - - /** - * Get a stream to read the requested resource name from this loader. - * - * @param name 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 on the loader's classpath. - */ - private InputStream loadResource( String name ) - { - // we need to search the components of the path to see if we can find the - // class we want. - InputStream stream = null; - - for( Iterator e = pathComponents.iterator(); e.hasNext() && stream == null; ) - { - File pathComponent = (File)e.next(); - stream = getResourceStream( pathComponent, name ); - } - return stream; - } - - /** - * An enumeration of all resources of a given name found within the - * classpath of this class loader. This enumeration is used by the {@link - * #findResources(String) findResources} method, which is in turn used by - * the {@link ClassLoader#getResources ClassLoader.getResources} method. - * - * @author David A. Herman - * @see AntClassLoader#findResources(String) - * @see java.lang.ClassLoader#getResources(String) - */ - private class ResourceEnumeration - implements Enumeration - { - - /** - * The URL of the next resource to return in the enumeration. If this - * field is null then the enumeration has been completed, - * i.e., there are no more elements to return. - */ - private URL nextResource; - - /** - * The index of the next classpath element to search. - */ - private int pathElementsIndex; - - /** - * The name of the resource being searched for. - */ - private String resourceName; - - /** - * Construct a new enumeration of resources of the given name found - * within this class loader's classpath. - * - * @param name the name of the resource to search for. - */ - ResourceEnumeration( String name ) - { - this.resourceName = name; - this.pathElementsIndex = 0; - findNextResource(); - } - - /** - * Indicates whether there are more elements in the enumeration to - * return. - * - * @return true if there are more elements in the - * enumeration; false otherwise. - */ - public boolean hasMoreElements() - { - return ( this.nextResource != null ); - } - - /** - * Returns the next resource in the enumeration. - * - * @return the next resource in the enumeration. - */ - public Object nextElement() - { - URL ret = this.nextResource; - findNextResource(); - return ret; - } - - /** - * Locates the next resource of the correct name in the classpath and - * sets nextResource to the URL of that resource. If no - * more resources can be found, nextResource is set to - * null. - */ - private void findNextResource() - { - URL url = null; - while( ( pathElementsIndex < pathComponents.size() ) && - ( url == null ) ) - { - File pathComponent - = (File)pathComponents.get( pathElementsIndex ); - url = getResourceURL( pathComponent, this.resourceName ); - pathElementsIndex++; - } - this.nextResource = url; - } - } -} diff --git a/proposal/myrmidon/src/main/org/apache/tools/ant/taskdefs/Available.java b/proposal/myrmidon/src/main/org/apache/tools/ant/taskdefs/Available.java index b2ee761bb..4646dd95b 100644 --- a/proposal/myrmidon/src/main/org/apache/tools/ant/taskdefs/Available.java +++ b/proposal/myrmidon/src/main/org/apache/tools/ant/taskdefs/Available.java @@ -8,8 +8,9 @@ package org.apache.tools.ant.taskdefs; import java.io.File; +import java.net.URL; +import java.net.URLClassLoader; import org.apache.myrmidon.api.TaskException; -import org.apache.tools.ant.AntClassLoader; import org.apache.tools.ant.Task; import org.apache.tools.ant.taskdefs.condition.Condition; import org.apache.tools.ant.types.EnumeratedAttribute; @@ -29,22 +30,22 @@ public class Available extends Task implements Condition { - private String value = "true"; - private String classname; - private Path classpath; - private String file; - private Path filepath; - private AntClassLoader loader; + private String m_value = "true"; + private String m_classname; + private Path m_classpath; + private String m_file; + private Path m_filepath; + private ClassLoader m_classLoader; - private String property; - private String resource; - private FileDir type; + private String m_property; + private String m_resource; + private FileDir m_type; public void setClassname( String classname ) { if( !"".equals( classname ) ) { - this.classname = classname; + m_classname = classname; } } @@ -62,7 +63,7 @@ public class Available public void setFile( String file ) { - this.file = file; + m_file = file; } public void setFilepath( Path filepath ) @@ -73,102 +74,98 @@ public class Available public void setProperty( String property ) { - this.property = property; + m_property = property; } public void setResource( String resource ) { - this.resource = resource; + m_resource = resource; } public void setType( FileDir type ) { - this.type = type; + m_type = type; } public void setValue( String value ) { - this.value = value; + m_value = value; } public Path createClasspath() throws TaskException { - if( this.classpath == null ) + if( m_classpath == null ) { - this.classpath = new Path(); + m_classpath = new Path(); } - return this.classpath.createPath(); + return m_classpath.createPath(); } public Path createFilepath() throws TaskException { - if( this.filepath == null ) + if( m_filepath == null ) { - this.filepath = new Path(); + m_filepath = new Path(); } - return this.filepath.createPath(); + return m_filepath.createPath(); } public boolean eval() throws TaskException { - if( classname == null && file == null && resource == null ) + if( m_classname == null && m_file == null && m_resource == null ) { throw new TaskException( "At least one of (classname|file|resource) is required" ); } - if( type != null ) + if( m_type != null ) { - if( file == null ) + if( m_file == null ) { throw new TaskException( "The type attribute is only valid when specifying the file attribute." ); } } - if( classpath != null ) + if( m_classpath != null ) { - this.loader = new AntClassLoader( getProject(), classpath ); + final URL[] urls = m_classpath.toURLs(); + m_classLoader = new URLClassLoader( urls ); } - if( ( classname != null ) && !checkClass( classname ) ) + if( ( m_classname != null ) && !checkClass( m_classname ) ) { - getLogger().debug( "Unable to load class " + classname + " to set property " + property ); + getLogger().debug( "Unable to load class " + m_classname + " to set property " + m_property ); return false; } - if( ( file != null ) && !checkFile() ) + if( ( m_file != null ) && !checkFile() ) { - if( type != null ) + if( m_type != null ) { - getLogger().debug( "Unable to find " + type + " " + file + " to set property " + property ); + getLogger().debug( "Unable to find " + m_type + " " + m_file + " to set property " + m_property ); } else { - getLogger().debug( "Unable to find " + file + " to set property " + property ); + getLogger().debug( "Unable to find " + m_file + " to set property " + m_property ); } return false; } - if( ( resource != null ) && !checkResource( resource ) ) + if( ( m_resource != null ) && !checkResource( m_resource ) ) { - getLogger().debug( "Unable to load resource " + resource + " to set property " + property ); + getLogger().debug( "Unable to load resource " + m_resource + " to set property " + m_property ); return false; } - if( loader != null ) - { - loader.cleanup(); - } - return true; } public void execute() throws TaskException { - if( property == null ) + if( m_property == null ) { throw new TaskException( "property attribute is required" ); } @@ -176,9 +173,9 @@ public class Available if( eval() ) { String lSep = System.getProperty( "line.separator" ); - if( null == getProject().getProperty( property ) ) + if( null == getProject().getProperty( m_property ) ) { - setProperty( property, value ); + setProperty( m_property, m_value ); } //else ignore } @@ -188,24 +185,8 @@ public class Available { try { - if( loader != null ) - { - loader.loadClass( classname ); - } - else - { - ClassLoader l = this.getClass().getClassLoader(); - // Can return null to represent the bootstrap class loader. - // see API docs of Class.getClassLoader. - if( l != null ) - { - l.loadClass( classname ); - } - else - { - Class.forName( classname ); - } - } + final ClassLoader classLoader = getClassLoader(); + classLoader.loadClass( classname ); return true; } catch( ClassNotFoundException e ) @@ -221,13 +202,13 @@ public class Available private boolean checkFile() throws TaskException { - if( filepath == null ) + if( m_filepath == null ) { - return checkFile( resolveFile( file ), file ); + return checkFile( resolveFile( m_file ), m_file ); } else { - String[] paths = filepath.list(); + String[] paths = m_filepath.list(); for( int i = 0; i < paths.length; ++i ) { getLogger().debug( "Searching " + paths[ i ] ); @@ -248,20 +229,20 @@ public class Available // ** full-pathname specified == path in list // ** simple name specified == path in list - if( path.exists() && file.equals( paths[ i ] ) ) + if( path.exists() && m_file.equals( paths[ i ] ) ) { - if( type == null ) + if( m_type == null ) { getLogger().debug( "Found: " + path ); return true; } - else if( type.isDir() + else if( m_type.isDir() && path.isDirectory() ) { getLogger().debug( "Found directory: " + path ); return true; } - else if( type.isFile() + else if( m_type.isFile() && path.isFile() ) { getLogger().debug( "Found file: " + path ); @@ -274,14 +255,14 @@ public class Available File parent = path.getParentFile(); // ** full-pathname specified == parent dir of path in list if( parent != null && parent.exists() - && file.equals( parent.getAbsolutePath() ) ) + && m_file.equals( parent.getAbsolutePath() ) ) { - if( type == null ) + if( m_type == null ) { getLogger().debug( "Found: " + parent ); return true; } - else if( type.isDir() ) + else if( m_type.isDir() ) { getLogger().debug( "Found directory: " + parent ); return true; @@ -293,8 +274,8 @@ public class Available // ** simple name specified == path in list + name if( path.exists() && path.isDirectory() ) { - if( checkFile( new File( path, file ), - file + " in " + path ) ) + if( checkFile( new File( path, m_file ), + m_file + " in " + path ) ) { return true; } @@ -303,8 +284,8 @@ public class Available // ** simple name specified == parent dir + name if( parent != null && parent.exists() ) { - if( checkFile( new File( parent, file ), - file + " in " + parent ) ) + if( checkFile( new File( parent, m_file ), + m_file + " in " + parent ) ) { return true; } @@ -316,8 +297,8 @@ public class Available File grandParent = parent.getParentFile(); if( grandParent != null && grandParent.exists() ) { - if( checkFile( new File( grandParent, file ), - file + " in " + grandParent ) ) + if( checkFile( new File( grandParent, m_file ), + m_file + " in " + grandParent ) ) { return true; } @@ -330,9 +311,9 @@ public class Available private boolean checkFile( File f, String text ) { - if( type != null ) + if( m_type != null ) { - if( type.isDir() ) + if( m_type.isDir() ) { if( f.isDirectory() ) { @@ -340,7 +321,7 @@ public class Available } return f.isDirectory(); } - else if( type.isFile() ) + else if( m_type.isFile() ) { if( f.isFile() ) { @@ -358,22 +339,19 @@ public class Available private boolean checkResource( String resource ) { - if( loader != null ) + final ClassLoader classLoader = getClassLoader(); + return ( null != classLoader.getResourceAsStream( resource ) ); + } + + private ClassLoader getClassLoader() + { + if( null == m_classLoader ) { - return ( loader.getResourceAsStream( resource ) != null ); + return ClassLoader.getSystemClassLoader(); } else { - ClassLoader cL = this.getClass().getClassLoader(); - if( cL != null ) - { - return ( cL.getResourceAsStream( resource ) != null ); - } - else - { - return - ( ClassLoader.getSystemResourceAsStream( resource ) != null ); - } + return m_classLoader; } } diff --git a/proposal/myrmidon/src/main/org/apache/tools/ant/taskdefs/ExecuteJava.java b/proposal/myrmidon/src/main/org/apache/tools/ant/taskdefs/ExecuteJava.java index e1fec0c13..c74538b0d 100644 --- a/proposal/myrmidon/src/main/org/apache/tools/ant/taskdefs/ExecuteJava.java +++ b/proposal/myrmidon/src/main/org/apache/tools/ant/taskdefs/ExecuteJava.java @@ -9,8 +9,9 @@ package org.apache.tools.ant.taskdefs; import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Method; +import java.net.URL; +import java.net.URLClassLoader; import org.apache.myrmidon.api.TaskException; -import org.apache.tools.ant.AntClassLoader; import org.apache.tools.ant.Project; import org.apache.tools.ant.types.Commandline; import org.apache.tools.ant.types.CommandlineJava; @@ -20,56 +21,51 @@ import org.apache.tools.ant.types.Path; * @author thomas.haas@softwired-inc.com * @author Stefan Bodewig */ - public class ExecuteJava { + private Commandline m_javaCommand; + private Path m_classpath; + private CommandlineJava.SysProperties m_sysProperties; - private Commandline javaCommand = null; - private Path classpath = null; - private CommandlineJava.SysProperties sysProperties = null; - - public void setClasspath( Path p ) + public void setClasspath( final Path classpath ) { - classpath = p; + m_classpath = classpath; } - public void setJavaCommand( Commandline javaCommand ) + public void setJavaCommand( final Commandline javaCommand ) { - this.javaCommand = javaCommand; + m_javaCommand = javaCommand; } - public void setSystemProperties( CommandlineJava.SysProperties s ) + public void setSystemProperties( final CommandlineJava.SysProperties sysProperties ) { - sysProperties = s; + m_sysProperties = sysProperties; } public void execute( Project project ) throws TaskException { - final String classname = javaCommand.getExecutable(); - final Object[] argument = {javaCommand.getArguments()}; + final String classname = m_javaCommand.getExecutable(); + final Object[] argument = {m_javaCommand.getArguments()}; - AntClassLoader loader = null; try { - if( sysProperties != null ) + if( m_sysProperties != null ) { - sysProperties.setSystem(); + m_sysProperties.setSystem(); } final Class[] param = {Class.forName( "[Ljava.lang.String;" )}; Class target = null; - if( classpath == null ) + if( m_classpath == null ) { target = Class.forName( classname ); } else { - loader = new AntClassLoader( Project.class.getClassLoader(), project, classpath, false ); - loader.setIsolated( true ); - loader.setThreadContextLoader(); - target = loader.forceLoadClass( classname ); - AntClassLoader.initializeClass( target ); + final URL[] urls = m_classpath.toURLs(); + final URLClassLoader classLoader = new URLClassLoader( urls ); + target = classLoader.loadClass( classname ); } final Method main = target.getMethod( "main", param ); main.invoke( null, argument ); @@ -100,14 +96,9 @@ public class ExecuteJava } finally { - if( loader != null ) - { - loader.resetThreadContextLoader(); - loader.cleanup(); - } - if( sysProperties != null ) + if( m_sysProperties != null ) { - sysProperties.restoreSystem(); + m_sysProperties.restoreSystem(); } } } diff --git a/proposal/myrmidon/src/main/org/apache/tools/ant/taskdefs/Property.java b/proposal/myrmidon/src/main/org/apache/tools/ant/taskdefs/Property.java index a1cd1323f..51821b6ce 100644 --- a/proposal/myrmidon/src/main/org/apache/tools/ant/taskdefs/Property.java +++ b/proposal/myrmidon/src/main/org/apache/tools/ant/taskdefs/Property.java @@ -11,13 +11,13 @@ import java.io.File; import java.io.FileInputStream; import java.io.IOException; import java.io.InputStream; -import java.util.Enumeration; +import java.net.URL; +import java.net.URLClassLoader; import java.util.Iterator; import java.util.Properties; import org.apache.myrmidon.api.TaskException; import org.apache.myrmidon.framework.exec.Environment; import org.apache.myrmidon.framework.exec.ExecException; -import org.apache.tools.ant.AntClassLoader; import org.apache.tools.ant.Task; import org.apache.tools.ant.types.Path; import org.apache.tools.ant.types.Reference; @@ -269,26 +269,18 @@ public class Property getLogger().debug( "Resource Loading " + name ); try { - ClassLoader cL = null; - InputStream is = null; + ClassLoader classLoader = null; if( m_classpath != null ) { - cL = new AntClassLoader( getProject(), m_classpath ); + final URL[] urls = m_classpath.toURLs(); + classLoader = new URLClassLoader( urls ); } else { - cL = getClass().getClassLoader(); - } - - if( cL == null ) - { - is = ClassLoader.getSystemResourceAsStream( name ); - } - else - { - is = cL.getResourceAsStream( name ); + classLoader = ClassLoader.getSystemClassLoader(); } + final InputStream is = classLoader.getResourceAsStream( name ); if( is != null ) { diff --git a/proposal/myrmidon/src/main/org/apache/tools/ant/taskdefs/Rmic.java b/proposal/myrmidon/src/main/org/apache/tools/ant/taskdefs/Rmic.java index d37242ea2..20e39e822 100644 --- a/proposal/myrmidon/src/main/org/apache/tools/ant/taskdefs/Rmic.java +++ b/proposal/myrmidon/src/main/org/apache/tools/ant/taskdefs/Rmic.java @@ -9,11 +9,11 @@ package org.apache.tools.ant.taskdefs; import java.io.File; import java.io.IOException; +import java.net.URLClassLoader; import java.rmi.Remote; import java.util.ArrayList; import org.apache.avalon.excalibur.io.FileUtil; import org.apache.myrmidon.api.TaskException; -import org.apache.tools.ant.AntClassLoader; import org.apache.tools.ant.DirectoryScanner; import org.apache.tools.ant.taskdefs.rmic.RmicAdapter; import org.apache.tools.ant.taskdefs.rmic.RmicAdapterFactory; @@ -525,7 +525,7 @@ public class Rmic extends MatchingTask adapter.setRmic( this ); Path classpath = adapter.getClasspath(); - loader = new AntClassLoader( getProject(), classpath ); + loader = new URLClassLoader( classpath.toURLs() ); // scan base dirs to build up compile lists only if a // specific classname is not given diff --git a/proposal/myrmidon/src/main/org/apache/tools/ant/taskdefs/SQLExec.java b/proposal/myrmidon/src/main/org/apache/tools/ant/taskdefs/SQLExec.java index 7eff3717b..10091a85a 100644 --- a/proposal/myrmidon/src/main/org/apache/tools/ant/taskdefs/SQLExec.java +++ b/proposal/myrmidon/src/main/org/apache/tools/ant/taskdefs/SQLExec.java @@ -18,6 +18,7 @@ import java.io.InputStreamReader; import java.io.PrintStream; import java.io.Reader; import java.io.StringReader; +import java.net.URLClassLoader; import java.sql.Connection; import java.sql.DatabaseMetaData; import java.sql.Driver; @@ -31,7 +32,6 @@ import java.util.Iterator; import java.util.Properties; import java.util.StringTokenizer; import org.apache.myrmidon.api.TaskException; -import org.apache.tools.ant.AntClassLoader; import org.apache.tools.ant.DirectoryScanner; import org.apache.tools.ant.Task; import org.apache.tools.ant.types.EnumeratedAttribute; @@ -47,9 +47,9 @@ import org.apache.tools.ant.types.Reference; * @author Michael McCallum * @author Tim Stephenson */ -public class SQLExec extends Task +public class SQLExec + extends Task { - private int goodSql = 0, totalSql = 0; private ArrayList filesets = new ArrayList(); @@ -57,42 +57,42 @@ public class SQLExec extends Task /** * Database connection */ - private Connection conn = null; + private Connection conn; /** * Autocommit flag. Default value is false */ - private boolean autocommit = false; + private boolean autocommit; /** * SQL statement */ - private Statement statement = null; + private Statement statement; /** * DB driver. */ - private String driver = null; + private String driver; /** * DB url. */ - private String url = null; + private String url; /** * User name. */ - private String userId = null; + private String userId; /** * Password */ - private String password = null; + private String password; /** * SQL input file */ - private File srcFile = null; + private File srcFile; /** * SQL input command @@ -118,7 +118,7 @@ public class SQLExec extends Task /** * Print SQL results. */ - private boolean print = false; + private boolean print; /** * Print header columns. @@ -128,17 +128,17 @@ public class SQLExec extends Task /** * Results Output file. */ - private File output = null; + private File output; /** * RDBMS Product needed for this SQL. */ - private String rdbms = null; + private String rdbms; /** * RDBMS Version needed for this SQL. */ - private String version = null; + private String version; /** * Action to perform if an error is found @@ -148,12 +148,10 @@ public class SQLExec extends Task /** * Encoding to use when reading SQL statements from a file */ - private String encoding = null; + private String encoding; private Path classpath; - private AntClassLoader loader; - /** * Set the autocommit flag for the DB connection. * @@ -457,8 +455,8 @@ public class SQLExec extends Task { getLogger().debug( "Loading " + driver + " using AntClassLoader with classpath " + classpath ); - loader = new AntClassLoader( getProject(), classpath ); - dc = loader.loadClass( driver ); + final ClassLoader classLoader = new URLClassLoader( classpath.toURLs() ); + dc = classLoader.loadClass( driver ); } else { diff --git a/proposal/myrmidon/src/main/org/apache/tools/ant/taskdefs/compilers/DefaultCompilerAdapter.java b/proposal/myrmidon/src/main/org/apache/tools/ant/taskdefs/compilers/DefaultCompilerAdapter.java index 042adb3a9..485f203b9 100644 --- a/proposal/myrmidon/src/main/org/apache/tools/ant/taskdefs/compilers/DefaultCompilerAdapter.java +++ b/proposal/myrmidon/src/main/org/apache/tools/ant/taskdefs/compilers/DefaultCompilerAdapter.java @@ -401,8 +401,7 @@ public abstract class DefaultCompilerAdapter } catch( IOException e ) { - throw new TaskException( "Error running " + args[ 0 ] - + " compiler", e ); + throw new TaskException( "Error running " + args[ 0 ] + " compiler", e ); } } finally diff --git a/proposal/myrmidon/src/main/org/apache/tools/ant/taskdefs/optional/depend/Depend.java b/proposal/myrmidon/src/main/org/apache/tools/ant/taskdefs/optional/depend/Depend.java index aa77b8932..47e8679d6 100644 --- a/proposal/myrmidon/src/main/org/apache/tools/ant/taskdefs/optional/depend/Depend.java +++ b/proposal/myrmidon/src/main/org/apache/tools/ant/taskdefs/optional/depend/Depend.java @@ -15,12 +15,12 @@ import java.io.FileWriter; import java.io.IOException; import java.io.PrintWriter; import java.net.URL; +import java.net.URLClassLoader; import java.util.ArrayList; import java.util.Enumeration; import java.util.Hashtable; import java.util.Iterator; import org.apache.myrmidon.api.TaskException; -import org.apache.tools.ant.AntClassLoader; import org.apache.tools.ant.DirectoryScanner; import org.apache.tools.ant.taskdefs.MatchingTask; import org.apache.tools.ant.types.Path; @@ -579,7 +579,7 @@ public class Depend extends MatchingTask { // now determine which jars each class depends upon classpathDependencies = new Hashtable(); - AntClassLoader loader = new AntClassLoader( getProject(), dependClasspath ); + final ClassLoader classLoader = new URLClassLoader( dependClasspath.toURLs() ); Hashtable classpathFileCache = new Hashtable(); Object nullFileMarker = new Object(); @@ -599,7 +599,8 @@ public class Depend extends MatchingTask if( !dependency.startsWith( "java." ) && !dependency.startsWith( "javax." ) ) { - URL classURL = loader.getResource( dependency.replace( '.', '/' ) + ".class" ); + final String name = dependency.replace( '.', '/' ) + ".class"; + URL classURL = classLoader.getResource( name ); if( classURL != null ) { if( classURL.getProtocol().equals( "jar" ) ) diff --git a/proposal/myrmidon/src/main/org/apache/tools/ant/taskdefs/optional/ejb/GenericDeploymentTool.java b/proposal/myrmidon/src/main/org/apache/tools/ant/taskdefs/optional/ejb/GenericDeploymentTool.java index cdf0afba4..271014251 100644 --- a/proposal/myrmidon/src/main/org/apache/tools/ant/taskdefs/optional/ejb/GenericDeploymentTool.java +++ b/proposal/myrmidon/src/main/org/apache/tools/ant/taskdefs/optional/ejb/GenericDeploymentTool.java @@ -12,6 +12,7 @@ import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; +import java.net.URLClassLoader; import java.util.ArrayList; import java.util.HashSet; import java.util.Hashtable; @@ -27,7 +28,6 @@ import org.apache.avalon.framework.logger.AbstractLogEnabled; import org.apache.bcel.*; import org.apache.bcel.classfile.*; import org.apache.myrmidon.api.TaskException; -import org.apache.tools.ant.AntClassLoader; import org.apache.tools.ant.DirectoryScanner; import org.apache.tools.ant.Project; import org.apache.tools.ant.Task; @@ -329,7 +329,7 @@ public class GenericDeploymentTool } else { - classpathLoader = new AntClassLoader( getTask().getProject(), combinedClasspath ); + classpathLoader = new URLClassLoader( combinedClasspath.toURLs() ); } return classpathLoader; diff --git a/proposal/myrmidon/src/main/org/apache/tools/ant/taskdefs/optional/ejb/WeblogicDeploymentTool.java b/proposal/myrmidon/src/main/org/apache/tools/ant/taskdefs/optional/ejb/WeblogicDeploymentTool.java index 074d9ac69..f57d33106 100644 --- a/proposal/myrmidon/src/main/org/apache/tools/ant/taskdefs/optional/ejb/WeblogicDeploymentTool.java +++ b/proposal/myrmidon/src/main/org/apache/tools/ant/taskdefs/optional/ejb/WeblogicDeploymentTool.java @@ -12,6 +12,7 @@ import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; +import java.net.URLClassLoader; import java.util.Hashtable; import java.util.Iterator; import java.util.jar.JarEntry; @@ -21,7 +22,6 @@ import javax.xml.parsers.SAXParser; import javax.xml.parsers.SAXParserFactory; import org.apache.avalon.excalibur.io.FileUtil; import org.apache.myrmidon.api.TaskException; -import org.apache.tools.ant.AntClassLoader; import org.apache.tools.ant.taskdefs.Java; import org.apache.tools.ant.types.Path; import org.xml.sax.InputSource; @@ -332,7 +332,7 @@ public class WeblogicDeploymentTool extends GenericDeploymentTool lookupPath.append( classpath ); } - return new AntClassLoader( getTask().getProject(), lookupPath ); + return new URLClassLoader( lookupPath.toURLs() ); } protected DescriptorHandler getWeblogicDescriptorHandler( final File srcDir ) diff --git a/proposal/myrmidon/src/main/org/apache/tools/ant/taskdefs/optional/ejb/WebsphereDeploymentTool.java b/proposal/myrmidon/src/main/org/apache/tools/ant/taskdefs/optional/ejb/WebsphereDeploymentTool.java index 57959512c..9a41f559b 100644 --- a/proposal/myrmidon/src/main/org/apache/tools/ant/taskdefs/optional/ejb/WebsphereDeploymentTool.java +++ b/proposal/myrmidon/src/main/org/apache/tools/ant/taskdefs/optional/ejb/WebsphereDeploymentTool.java @@ -11,13 +11,13 @@ import java.io.File; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; +import java.net.URLClassLoader; import java.util.Hashtable; import java.util.Iterator; import java.util.jar.JarEntry; import java.util.jar.JarFile; import java.util.jar.JarOutputStream; import org.apache.myrmidon.api.TaskException; -import org.apache.tools.ant.AntClassLoader; import org.apache.tools.ant.taskdefs.Java; import org.apache.tools.ant.types.Argument; import org.apache.tools.ant.types.EnumeratedAttribute; @@ -410,7 +410,7 @@ public class WebsphereDeploymentTool { lookupPath.append( classpath ); } - return new AntClassLoader( getTask().getProject(), lookupPath ); + return new URLClassLoader( lookupPath.toURLs() ); } protected DescriptorHandler getDescriptorHandler( File srcDir ) diff --git a/proposal/myrmidon/src/main/org/apache/tools/ant/taskdefs/optional/junit/JUnitTask.java b/proposal/myrmidon/src/main/org/apache/tools/ant/taskdefs/optional/junit/JUnitTask.java index 323aba4a7..ad6bd19ed 100644 --- a/proposal/myrmidon/src/main/org/apache/tools/ant/taskdefs/optional/junit/JUnitTask.java +++ b/proposal/myrmidon/src/main/org/apache/tools/ant/taskdefs/optional/junit/JUnitTask.java @@ -12,6 +12,7 @@ import java.io.FileOutputStream; import java.io.IOException; import java.io.OutputStream; import java.net.URL; +import java.net.URLClassLoader; import java.util.ArrayList; import java.util.Enumeration; import java.util.Hashtable; @@ -19,7 +20,6 @@ import java.util.Iterator; import java.util.Properties; import java.util.Random; import org.apache.myrmidon.api.TaskException; -import org.apache.tools.ant.AntClassLoader; import org.apache.tools.ant.Task; import org.apache.tools.ant.taskdefs.exec.Execute; import org.apache.tools.ant.taskdefs.exec.LogOutputStream; @@ -694,19 +694,18 @@ public class JUnitTask extends Task try { getLogger().debug( "Using System properties " + System.getProperties() ); - AntClassLoader cl = null; + ClassLoader classLoader = null; Path classpath = commandline.getClasspath(); if( classpath != null ) { getLogger().debug( "Using CLASSPATH " + classpath ); - - cl = new AntClassLoader( null, getProject(), classpath, false ); - // make sure the test will be accepted as a TestCase - cl.addSystemPackageRoot( "junit" ); - // will cause trouble in JDK 1.1 if omitted - cl.addSystemPackageRoot( "org.apache.tools.ant" ); + classLoader = new URLClassLoader( classpath.toURLs() ); } - runner = new JUnitTestRunner( test, test.getHaltonerror(), test.getFiltertrace(), test.getHaltonfailure(), cl ); + runner = new JUnitTestRunner( test, + test.getHaltonerror(), + test.getFiltertrace(), + test.getHaltonfailure(), + classLoader ); if( summary ) { getLogger().info( "Running " + test.getName() ); diff --git a/proposal/myrmidon/src/main/org/apache/tools/ant/taskdefs/optional/junit/JUnitTestRunner.java b/proposal/myrmidon/src/main/org/apache/tools/ant/taskdefs/optional/junit/JUnitTestRunner.java index 4ad2d4bf1..4c2b31632 100644 --- a/proposal/myrmidon/src/main/org/apache/tools/ant/taskdefs/optional/junit/JUnitTestRunner.java +++ b/proposal/myrmidon/src/main/org/apache/tools/ant/taskdefs/optional/junit/JUnitTestRunner.java @@ -28,9 +28,6 @@ import junit.framework.TestResult; import junit.framework.TestSuite; import org.apache.avalon.framework.ExceptionUtil; import org.apache.myrmidon.api.TaskException; -import org.apache.tools.ant.AntClassLoader; -import org.apache.tools.ant.Project; -import org.apache.tools.ant.util.StringUtils; /** * Simple Testrunner for JUnit that runs all tests of a testsuite.

@@ -179,7 +176,6 @@ public class JUnitTestRunner implements TestListener else { testClass = loader.loadClass( test.getName() ); - AntClassLoader.initializeClass( testClass ); } Method suiteMethod = null; diff --git a/proposal/myrmidon/src/main/org/apache/tools/ant/taskdefs/optional/junit/XMLResultAggregator.java b/proposal/myrmidon/src/main/org/apache/tools/ant/taskdefs/optional/junit/XMLResultAggregator.java index 12d0f4191..39bd0657e 100644 --- a/proposal/myrmidon/src/main/org/apache/tools/ant/taskdefs/optional/junit/XMLResultAggregator.java +++ b/proposal/myrmidon/src/main/org/apache/tools/ant/taskdefs/optional/junit/XMLResultAggregator.java @@ -24,7 +24,6 @@ import org.apache.tools.ant.DirectoryScanner; import org.apache.tools.ant.Task; import org.apache.tools.ant.types.FileSet; import org.apache.tools.ant.util.DOMElementWriter; -import org.apache.tools.ant.util.StringUtils; import org.w3c.dom.Document; import org.w3c.dom.Element; import org.xml.sax.SAXException; @@ -184,7 +183,7 @@ public class XMLResultAggregator extends Task implements XMLConstants } if( toDir == null ) { - toDir = FileUtil.resolveFile( getProject().getBaseDir(), DEFAULT_DIR ); + toDir = FileUtil.resolveFile( getBaseDirectory(), DEFAULT_DIR ); } return new File( toDir, toFile ); } @@ -210,16 +209,13 @@ public class XMLResultAggregator extends Task implements XMLConstants if( pathname.endsWith( ".xml" ) ) { File file = new File( ds.getBasedir(), pathname ); - file = FileUtil. - resolveFile( getProject().getBaseDir(), file.getPath() ); + file = FileUtil.resolveFile( getBaseDirectory(), file.getPath() ); v.add( file ); } } } - File[] files = new File[ v.size() ]; - v.copyInto( files ); - return files; + return (File[])v.toArray( new File[ v.size() ] ); } /** diff --git a/proposal/myrmidon/src/main/org/apache/tools/ant/types/Mapper.java b/proposal/myrmidon/src/main/org/apache/tools/ant/types/Mapper.java index 6474fa756..4b35736b1 100644 --- a/proposal/myrmidon/src/main/org/apache/tools/ant/types/Mapper.java +++ b/proposal/myrmidon/src/main/org/apache/tools/ant/types/Mapper.java @@ -7,10 +7,10 @@ */ package org.apache.tools.ant.types; -import java.util.Properties; +import java.net.URL; +import java.net.URLClassLoader; import java.util.Stack; import org.apache.myrmidon.api.TaskException; -import org.apache.tools.ant.AntClassLoader; import org.apache.tools.ant.util.FileNameMapper; /** @@ -182,10 +182,9 @@ public class Mapper } else { - AntClassLoader al = new AntClassLoader( getProject(), - m_classpath ); - c = al.loadClass( m_classname ); - AntClassLoader.initializeClass( c ); + final URL[] urls = m_classpath.toURLs(); + final URLClassLoader classLoader = new URLClassLoader( urls ); + c = classLoader.loadClass( m_classname ); } FileNameMapper m = (FileNameMapper)c.newInstance(); diff --git a/proposal/myrmidon/src/main/org/apache/tools/ant/types/Path.java b/proposal/myrmidon/src/main/org/apache/tools/ant/types/Path.java index e0dc4992e..fac5ca02d 100644 --- a/proposal/myrmidon/src/main/org/apache/tools/ant/types/Path.java +++ b/proposal/myrmidon/src/main/org/apache/tools/ant/types/Path.java @@ -8,6 +8,8 @@ package org.apache.tools.ant.types; import java.io.File; +import java.io.IOException; +import java.net.URL; import java.util.ArrayList; import java.util.Iterator; import java.util.Locale; @@ -644,6 +646,33 @@ public class Path } } + /** + * Returns an array of URLs - useful for building a ClassLoader. + */ + public URL[] toURLs() + throws TaskException + { + try + { + final String[] list = list(); + + final URL[] result = new URL[ list.length ]; + + // path containing one or more elements + for( int i = 0; i < list.length; i++ ) + { + result[ i ] = new File( list[ i ] ).toURL(); + } + + return result; + } + catch( final IOException ioe ) + { + final String message = "Malformed path entry. Reason:" + ioe; + throw new TaskException( message, ioe ); + } + } + /** * Overrides the version of DataType to recurse on all DataType child * elements that may have been added. @@ -655,7 +684,6 @@ public class Path protected void dieOnCircularReference( Stack stk, Project p ) throws TaskException { - if( checked ) { return; diff --git a/proposal/myrmidon/src/todo/org/apache/tools/ant/AntClassLoader.java b/proposal/myrmidon/src/todo/org/apache/tools/ant/AntClassLoader.java deleted file mode 100644 index 88d5d4983..000000000 --- a/proposal/myrmidon/src/todo/org/apache/tools/ant/AntClassLoader.java +++ /dev/null @@ -1,1118 +0,0 @@ -/* - * Copyright (C) The Apache Software Foundation. All rights reserved. - * - * This software is published under the terms of the Apache Software License - * version 1.1, a copy of which has been included with this distribution in - * the LICENSE.txt file. - */ -package org.apache.tools.ant; - -import java.io.ByteArrayOutputStream; -import java.io.File; -import java.io.FileInputStream; -import java.io.IOException; -import java.io.InputStream; -import java.lang.reflect.InvocationTargetException; -import java.lang.reflect.Method; -import java.net.MalformedURLException; -import java.net.URL; -import java.util.ArrayList; -import java.util.Enumeration; -import java.util.Hashtable; -import java.util.Iterator; -import java.util.zip.ZipEntry; -import java.util.zip.ZipFile; -import org.apache.avalon.excalibur.io.FileUtil; -import org.apache.avalon.framework.logger.LogEnabled; -import org.apache.avalon.framework.logger.Logger; -import org.apache.myrmidon.api.TaskException; -import org.apache.tools.ant.types.Path; - -/** - * Used to load classes within ant with a different claspath from that used to - * start ant. Note that it is possible to force a class into this loader even - * when that class is on the system classpath by using the forceLoadClass - * method. Any subsequent classes loaded by that class will then use this loader - * rather than the system class loader. - * - * @author Conor MacNeill - * @author Jesse Glick - */ -public class AntClassLoader - extends ClassLoader - implements BuildListener, LogEnabled -{ - /** - * The size of buffers to be used in this classloader. - */ - private final static int BUFFER_SIZE = 8192; - - private static Method getProtectionDomain; - private static Method defineClassProtectionDomain; - private static Method getContextClassLoader; - private static Method setContextClassLoader; - - private Logger m_logger; - - /** - * Provide component with a logger. - * - * @param logger the logger - */ - public void enableLogging( Logger logger ) - { - m_logger = logger; - } - - protected final Logger getLogger() - { - return m_logger; - } - - /** - * The components of the classpath that the classloader searches for classes - */ - ArrayList pathComponents = new ArrayList(); - - /** - * Indicates whether the parent class loader should be consulted before - * trying to load with this class loader. - */ - private boolean parentFirst = true; - - /** - * These are the package roots that are to be loaded by the parent class - * loader regardless of whether the parent class loader is being searched - * first or not. - */ - private ArrayList systemPackages = new ArrayList(); - - /** - * These are the package roots that are to be loaded by this class loader - * regardless of whether the parent class loader is being searched first or - * not. - */ - private ArrayList loaderPackages = new ArrayList(); - - /** - * This flag indicates that the classloader will ignore the base classloader - * if it can't find a class. - */ - private boolean ignoreBase = false; - - /** - * The parent class loader, if one is given or can be determined - */ - private ClassLoader parent = null; - - /** - * A hashtable of zip files opened by the classloader - */ - private Hashtable zipFiles = new Hashtable(); - - /** - * The context loader saved when setting the thread's current context - * loader. - */ - private ClassLoader savedContextLoader = null; - private boolean isContextLoaderSaved = false; - - /** - * The project to which this class loader belongs. - */ - private Project project; - - static - { - try - { - getProtectionDomain = Class.class.getMethod( "getProtectionDomain", new Class[ 0 ] ); - Class protectionDomain = Class.forName( "java.security.ProtectionDomain" ); - Class[] args = new Class[]{String.class, byte[].class, Integer.TYPE, Integer.TYPE, protectionDomain}; - defineClassProtectionDomain = ClassLoader.class.getDeclaredMethod( "defineClass", args ); - - getContextClassLoader = Thread.class.getMethod( "getContextClassLoader", new Class[ 0 ] ); - args = new Class[]{ClassLoader.class}; - setContextClassLoader = Thread.class.getMethod( "setContextClassLoader", args ); - } - catch( Exception e ) - { - } - } - - /** - * Create a classloader for the given project using the classpath given. - * - * @param project the project to which this classloader is to belong. - * @param classpath the classpath to use to load the classes. This is - * combined with the system classpath in a manner determined by the - * value of ${build.sysclasspath} - */ - public AntClassLoader( Project project, Path classpath ) - throws TaskException - { - parent = AntClassLoader.class.getClassLoader(); - this.project = project; - project.addBuildListener( this ); - if( classpath != null ) - { - Path actualClasspath = classpath.concatSystemClasspath( "ignore" ); - String[] pathElements = actualClasspath.list(); - for( int i = 0; i < pathElements.length; ++i ) - { - try - { - addPathElement( (String)pathElements[ i ] ); - } - catch( TaskException e ) - { - // ignore path elements which are invalid relative to the project - } - } - } - } - - /** - * Create a classloader for the given project using the classpath given. - * - * @param parent the parent classloader to which unsatisfied loading - * attempts are delgated - * @param project the project to which this classloader is to belong. - * @param classpath the classpath to use to load the classes. - * @param parentFirst if true indicates that the parent classloader should - * be consulted before trying to load the a class through this loader. - */ - public AntClassLoader( ClassLoader parent, Project project, Path classpath, - boolean parentFirst ) - throws TaskException - { - this( project, classpath ); - if( parent != null ) - { - this.parent = parent; - } - this.parentFirst = parentFirst; - addSystemPackageRoot( "java" ); - addSystemPackageRoot( "javax" ); - } - - /** - * Create a classloader for the given project using the classpath given. - * - * @param project the project to which this classloader is to belong. - * @param classpath the classpath to use to load the classes. - * @param parentFirst if true indicates that the parent classloader should - * be consulted before trying to load the a class through this loader. - */ - public AntClassLoader( Project project, Path classpath, boolean parentFirst ) - throws TaskException - { - this( null, project, classpath, parentFirst ); - } - - /** - * Create an empty class loader. The classloader should be configured with - * path elements to specify where the loader is to look for classes. - * - * @param parent the parent classloader to which unsatisfied loading - * attempts are delgated - * @param parentFirst if true indicates that the parent classloader should - * be consulted before trying to load the a class through this loader. - */ - public AntClassLoader( ClassLoader parent, boolean parentFirst ) - { - if( parent != null ) - { - this.parent = parent; - } - else - { - parent = AntClassLoader.class.getClassLoader(); - } - project = null; - this.parentFirst = parentFirst; - } - - /** - * Force initialization of a class in a JDK 1.1 compatible, albeit hacky way - * - * @param theClass Description of Parameter - */ - public static void initializeClass( Class theClass ) - { - // ***HACK*** We try to create an instance to force the VM to run the - // class' static initializer. We don't care if the instance can't - // be created - we are just interested in the side effect. - try - { - theClass.newInstance(); - } - catch( Throwable t ) - { - //ignore - our work is done - } - } - - /** - * Set this classloader to run in isolated mode. In isolated mode, classes - * not found on the given classpath will not be referred to the base class - * loader but will cause a classNotFoundException. - * - * @param isolated The new Isolated value - */ - public void setIsolated( boolean isolated ) - { - ignoreBase = isolated; - } - - /** - * Set the current thread's context loader to this classloader, storing the - * current loader value for later resetting - */ - public void setThreadContextLoader() - throws TaskException - { - if( isContextLoaderSaved ) - { - throw new TaskException( "Context loader has not been reset" ); - } - if( getContextClassLoader != null && setContextClassLoader != null ) - { - try - { - savedContextLoader - = (ClassLoader)getContextClassLoader.invoke( Thread.currentThread(), new Object[ 0 ] ); - Object[] args = new Object[]{this}; - setContextClassLoader.invoke( Thread.currentThread(), args ); - isContextLoaderSaved = true; - } - catch( InvocationTargetException ite ) - { - Throwable t = ite.getTargetException(); - throw new TaskException( t.toString() ); - } - catch( Exception e ) - { - throw new TaskException( e.toString() ); - } - } - } - - /** - * 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( isParentFirst( name ) ) - { - url = ( parent == null ) ? super.getResource( name ) : parent.getResource( name ); - } - - if( url != null ) - { - log( "Resource " + name + " loaded from parent loader", - Project.MSG_DEBUG ); - - } - else - { - // try and load from this loader if the parent either didn't find - // it or wasn't consulted. - for( Iterator e = pathComponents.iterator(); e.hasNext() && url == null; ) - { - File pathComponent = (File)e.next(); - url = getResourceURL( pathComponent, name ); - if( url != null ) - { - log( "Resource " + name - + " loaded from ant loader", - Project.MSG_DEBUG ); - } - } - } - - if( url == null && !isParentFirst( name ) ) - { - // this loader was first but it didn't find it - try the parent - - url = ( parent == null ) ? super.getResource( name ) : parent.getResource( name ); - if( url != null ) - { - log( "Resource " + name + " loaded from parent loader", - Project.MSG_DEBUG ); - } - } - - if( url == null ) - { - getLogger().debug( "Couldn't load Resource " + name ); - } - - return url; - } - - /** - * Get a stream to read the requested resource name. - * - * @param name 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 on the loader's classpath. - */ - public InputStream getResourceAsStream( String name ) - { - - InputStream resourceStream = null; - if( isParentFirst( name ) ) - { - resourceStream = loadBaseResource( name ); - if( resourceStream != null ) - { - log( "ResourceStream for " + name - + " loaded from parent loader", Project.MSG_DEBUG ); - - } - else - { - resourceStream = loadResource( name ); - if( resourceStream != null ) - { - log( "ResourceStream for " + name - + " loaded from ant loader", Project.MSG_DEBUG ); - } - } - } - else - { - resourceStream = loadResource( name ); - if( resourceStream != null ) - { - log( "ResourceStream for " + name - + " loaded from ant loader", Project.MSG_DEBUG ); - - } - else - { - resourceStream = loadBaseResource( name ); - if( resourceStream != null ) - { - log( "ResourceStream for " + name - + " loaded from parent loader", Project.MSG_DEBUG ); - } - } - } - - if( resourceStream == null ) - { - log( "Couldn't load ResourceStream for " + name, - Project.MSG_DEBUG ); - } - - return resourceStream; - } - - /** - * Add a package root to the list of packages which must be loaded using - * this loader. All subpackages are also included. - * - * @param packageRoot the root of akll packages to be included. - */ - public void addLoaderPackageRoot( String packageRoot ) - { - loaderPackages.add( packageRoot + "." ); - } - - /** - * Add an element to the classpath to be searched - * - * @param pathElement The feature to be added to the PathElement attribute - * @exception TaskException Description of Exception - */ - public void addPathElement( String pathElement ) - throws TaskException - { - File pathComponent - = project != null ? FileUtil.resolveFile( project.getBaseDir(), pathElement ) - : new File( pathElement ); - pathComponents.add( pathComponent ); - } - - /** - * Add a package root to the list of packages which must be loaded on the - * parent loader. All subpackages are also included. - * - * @param packageRoot the root of all packages to be included. - */ - public void addSystemPackageRoot( String packageRoot ) - { - systemPackages.add( packageRoot + "." ); - } - - public void buildFinished( BuildEvent event ) - { - cleanup(); - } - - public void buildStarted( BuildEvent event ) - { - } - - public void cleanup() - { - pathComponents = null; - project = null; - for( Enumeration e = zipFiles.elements(); e.hasMoreElements(); ) - { - ZipFile zipFile = (ZipFile)e.nextElement(); - try - { - zipFile.close(); - } - catch( IOException ioe ) - { - // ignore - } - } - zipFiles = new Hashtable(); - } - - /** - * Search for and load a class on the classpath of this class loader. - * - * @param name the classname to be loaded. - * @return the required Class object - * @throws ClassNotFoundException if the requested class does not exist on - * this loader's classpath. - */ - public Class findClass( String name ) - throws ClassNotFoundException - { - getLogger().debug( "Finding class " + name ); - - return findClassInComponents( name ); - } - - /** - * Load a class through this class loader even if that class is available on - * the parent classpath. This ensures that any classes which are loaded by - * the returned class will use this classloader. - * - * @param classname the classname to be loaded. - * @return the required Class object - * @throws ClassNotFoundException if the requested class does not exist on - * this loader's classpath. - */ - public Class forceLoadClass( String classname ) - throws ClassNotFoundException - { - getLogger().debug( "force loading " + classname ); - - Class theClass = findLoadedClass( classname ); - - if( theClass == null ) - { - theClass = findClass( classname ); - } - - return theClass; - } - - /** - * Load a class through this class loader but defer to the parent class - * loader This ensures that instances of the returned class will be - * compatible with instances which which have already been loaded on the - * parent loader. - * - * @param classname the classname to be loaded. - * @return the required Class object - * @throws ClassNotFoundException if the requested class does not exist on - * this loader's classpath. - */ - public Class forceLoadSystemClass( String classname ) - throws ClassNotFoundException - { - getLogger().debug( "force system loading " + classname ); - - Class theClass = findLoadedClass( classname ); - - if( theClass == null ) - { - theClass = findBaseClass( classname ); - } - - return theClass; - } - - public void messageLogged( BuildEvent event ) - { - } - - /** - * Reset the current thread's context loader to its original value - */ - public void resetThreadContextLoader() - throws TaskException - { - if( isContextLoaderSaved && - getContextClassLoader != null && setContextClassLoader != null ) - { - try - { - Object[] args = new Object[]{savedContextLoader}; - setContextClassLoader.invoke( Thread.currentThread(), args ); - savedContextLoader = null; - isContextLoaderSaved = false; - } - catch( InvocationTargetException ite ) - { - Throwable t = ite.getTargetException(); - throw new TaskException( t.toString() ); - } - catch( Exception e ) - { - throw new TaskException( e.toString() ); - } - } - } - - public void targetFinished( BuildEvent event ) - { - } - - public void targetStarted( BuildEvent event ) - { - } - - public void taskFinished( BuildEvent event ) - { - } - - public void taskStarted( BuildEvent event ) - { - } - - /** - * Returns an enumeration of URLs representing all the resources with the - * given name by searching the class loader's classpath. - * - * @param name the resource name. - * @return an enumeration of URLs for the resources. - * @throws IOException if I/O errors occurs (can't happen) - */ - protected Enumeration findResources( String name ) - throws IOException - { - return new ResourceEnumeration( name ); - } - - /** - * Load a class with this class loader. This method will load a class. This - * class attempts to load the class firstly using the parent class loader. - * For JDK 1.1 compatability, this uses the findSystemClass method. - * - * @param classname the name of the class to be loaded. - * @param resolve true if all classes upon which this class depends are to - * be loaded. - * @return the required Class object - * @throws ClassNotFoundException if the requested class does not exist on - * the system classpath or this loader's classpath. - */ - protected Class loadClass( String classname, boolean resolve ) - throws ClassNotFoundException - { - - Class theClass = findLoadedClass( classname ); - if( theClass != null ) - { - return theClass; - } - - if( isParentFirst( classname ) ) - { - try - { - theClass = findBaseClass( classname ); - getLogger().debug( "Class " + classname + " loaded from parent loader" ); - } - catch( ClassNotFoundException cnfe ) - { - theClass = findClass( classname ); - getLogger().debug( "Class " + classname + " loaded from ant loader" ); - } - } - else - { - try - { - theClass = findClass( classname ); - getLogger().debug( "Class " + classname + " loaded from ant loader" ); - } - catch( ClassNotFoundException cnfe ) - { - if( ignoreBase ) - { - throw cnfe; - } - theClass = findBaseClass( classname ); - getLogger().debug( "Class " + classname + " loaded from parent loader" ); - } - } - - if( resolve ) - { - resolveClass( theClass ); - } - - return theClass; - } - - /** - * Log a message through the project object if one has been provided. - * - * @param message the message to log - * @param priority the logging priority of the message - */ - protected void log( String message, int priority ) - { - if( project != null ) - { - project.log( message, priority ); - } - // else { - // System.out.println(message); - // } - } - - /** - * Convert the class dot notation to a filesystem equivalent for searching - * purposes. - * - * @param classname the class name in dot format (ie java.lang.Integer) - * @return the classname in filesystem format (ie java/lang/Integer.class) - */ - private String getClassFilename( String classname ) - { - return classname.replace( '.', '/' ) + ".class"; - } - - /** - * Read a class definition from a stream. - * - * @param stream the stream from which the class is to be read. - * @param classname the class name of the class in the stream. - * @return the Class object read from the stream. - * @throws IOException if there is a problem reading the class from the - * stream. - */ - private Class getClassFromStream( InputStream stream, String classname ) - throws IOException - { - ByteArrayOutputStream baos = new ByteArrayOutputStream(); - int bytesRead = -1; - byte[] buffer = new byte[ BUFFER_SIZE ]; - - while( ( bytesRead = stream.read( buffer, 0, BUFFER_SIZE ) ) != -1 ) - { - baos.write( buffer, 0, bytesRead ); - } - - byte[] classData = baos.toByteArray(); - - // Simply put: - // defineClass(classname, classData, 0, classData.length, Project.class.getProtectionDomain()); - // Made more elaborate to be 1.1-safe. - if( defineClassProtectionDomain != null ) - { - try - { - Object domain = getProtectionDomain.invoke( Project.class, new Object[ 0 ] ); - Object[] args = new Object[]{classname, classData, new Integer( 0 ), new Integer( classData.length ), domain}; - return (Class)defineClassProtectionDomain.invoke( this, args ); - } - catch( InvocationTargetException ite ) - { - Throwable t = ite.getTargetException(); - if( t instanceof ClassFormatError ) - { - throw (ClassFormatError)t; - } - else if( t instanceof NoClassDefFoundError ) - { - throw (NoClassDefFoundError)t; - } - else - { - throw new IOException( t.toString() ); - } - } - catch( Exception e ) - { - throw new IOException( e.toString() ); - } - } - else - { - return defineClass( classname, classData, 0, classData.length ); - } - } - - /** - * 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 InputStream getResourceStream( File file, String resourceName ) - { - try - { - if( !file.exists() ) - { - return null; - } - - if( file.isDirectory() ) - { - File resource = new File( file, resourceName ); - - if( resource.exists() ) - { - return new FileInputStream( resource ); - } - } - else - { - // is the zip file in the cache - ZipFile zipFile = (ZipFile)zipFiles.get( file ); - if( zipFile == null ) - { - zipFile = new ZipFile( file ); - zipFiles.put( file, zipFile ); - } - ZipEntry entry = zipFile.getEntry( resourceName ); - if( entry != null ) - { - return zipFile.getInputStream( entry ); - } - } - } - catch( Exception e ) - { - getLogger().debug( "Ignoring Exception " + e.getClass().getName() + ": " + e.getMessage() + " reading resource " + resourceName + " from " + file ); - } - - return null; - } - - /** - * 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 = (ZipFile)zipFiles.get( file ); - if( zipFile == null ) - { - zipFile = new ZipFile( file ); - zipFiles.put( file, zipFile ); - } - - ZipEntry entry = zipFile.getEntry( resourceName ); - if( entry != null ) - { - try - { - return new URL( "jar:file:" + file.toString() + "!/" + entry ); - } - catch( MalformedURLException ex ) - { - return null; - } - } - } - } - catch( Exception e ) - { - e.printStackTrace(); - } - - return null; - } - - private boolean isParentFirst( String resourceName ) - { - // default to the global setting and then see - // if this class belongs to a package which has been - // designated to use a specific loader first (this one or the parent one) - boolean useParentFirst = parentFirst; - - for( Iterator e = systemPackages.iterator(); e.hasNext(); ) - { - String packageName = (String)e.next(); - if( resourceName.startsWith( packageName ) ) - { - useParentFirst = true; - break; - } - } - - for( Iterator e = loaderPackages.iterator(); e.hasNext(); ) - { - String packageName = (String)e.next(); - if( resourceName.startsWith( packageName ) ) - { - useParentFirst = false; - break; - } - } - - return useParentFirst; - } - - /** - * Find a system class (which should be loaded from the same classloader as - * the Ant core). - * - * @param name Description of Parameter - * @return Description of the Returned Value - * @exception ClassNotFoundException Description of Exception - */ - private Class findBaseClass( String name ) - throws ClassNotFoundException - { - if( parent == null ) - { - return findSystemClass( name ); - } - else - { - return parent.loadClass( name ); - } - } - - /** - * Find a class on the given classpath. - * - * @param name Description of Parameter - * @return Description of the Returned Value - * @exception ClassNotFoundException Description of Exception - */ - private Class findClassInComponents( String name ) - throws ClassNotFoundException - { - // we need to search the components of the path to see if we can find the - // class we want. - InputStream stream = null; - String classFilename = getClassFilename( name ); - try - { - for( Iterator e = pathComponents.iterator(); e.hasNext(); ) - { - File pathComponent = (File)e.next(); - try - { - stream = getResourceStream( pathComponent, classFilename ); - if( stream != null ) - { - return getClassFromStream( stream, name ); - } - } - catch( IOException ioe ) - { - // ioe.printStackTrace(); - getLogger().debug( "Exception reading component " + pathComponent ); - } - } - - throw new ClassNotFoundException( name ); - } - finally - { - try - { - if( stream != null ) - { - stream.close(); - } - } - catch( IOException e ) - { - } - } - } - - /** - * Find a system resource (which should be loaded from the parent - * classloader). - * - * @param name Description of Parameter - * @return Description of the Returned Value - */ - private InputStream loadBaseResource( String name ) - { - if( parent == null ) - { - return getSystemResourceAsStream( name ); - } - else - { - return parent.getResourceAsStream( name ); - } - } - - /** - * Get a stream to read the requested resource name from this loader. - * - * @param name 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 on the loader's classpath. - */ - private InputStream loadResource( String name ) - { - // we need to search the components of the path to see if we can find the - // class we want. - InputStream stream = null; - - for( Iterator e = pathComponents.iterator(); e.hasNext() && stream == null; ) - { - File pathComponent = (File)e.next(); - stream = getResourceStream( pathComponent, name ); - } - return stream; - } - - /** - * An enumeration of all resources of a given name found within the - * classpath of this class loader. This enumeration is used by the {@link - * #findResources(String) findResources} method, which is in turn used by - * the {@link ClassLoader#getResources ClassLoader.getResources} method. - * - * @author David A. Herman - * @see AntClassLoader#findResources(String) - * @see java.lang.ClassLoader#getResources(String) - */ - private class ResourceEnumeration - implements Enumeration - { - - /** - * The URL of the next resource to return in the enumeration. If this - * field is null then the enumeration has been completed, - * i.e., there are no more elements to return. - */ - private URL nextResource; - - /** - * The index of the next classpath element to search. - */ - private int pathElementsIndex; - - /** - * The name of the resource being searched for. - */ - private String resourceName; - - /** - * Construct a new enumeration of resources of the given name found - * within this class loader's classpath. - * - * @param name the name of the resource to search for. - */ - ResourceEnumeration( String name ) - { - this.resourceName = name; - this.pathElementsIndex = 0; - findNextResource(); - } - - /** - * Indicates whether there are more elements in the enumeration to - * return. - * - * @return true if there are more elements in the - * enumeration; false otherwise. - */ - public boolean hasMoreElements() - { - return ( this.nextResource != null ); - } - - /** - * Returns the next resource in the enumeration. - * - * @return the next resource in the enumeration. - */ - public Object nextElement() - { - URL ret = this.nextResource; - findNextResource(); - return ret; - } - - /** - * Locates the next resource of the correct name in the classpath and - * sets nextResource to the URL of that resource. If no - * more resources can be found, nextResource is set to - * null. - */ - private void findNextResource() - { - URL url = null; - while( ( pathElementsIndex < pathComponents.size() ) && - ( url == null ) ) - { - File pathComponent - = (File)pathComponents.get( pathElementsIndex ); - url = getResourceURL( pathComponent, this.resourceName ); - pathElementsIndex++; - } - this.nextResource = url; - } - } -} diff --git a/proposal/myrmidon/src/todo/org/apache/tools/ant/taskdefs/Available.java b/proposal/myrmidon/src/todo/org/apache/tools/ant/taskdefs/Available.java index b2ee761bb..4646dd95b 100644 --- a/proposal/myrmidon/src/todo/org/apache/tools/ant/taskdefs/Available.java +++ b/proposal/myrmidon/src/todo/org/apache/tools/ant/taskdefs/Available.java @@ -8,8 +8,9 @@ package org.apache.tools.ant.taskdefs; import java.io.File; +import java.net.URL; +import java.net.URLClassLoader; import org.apache.myrmidon.api.TaskException; -import org.apache.tools.ant.AntClassLoader; import org.apache.tools.ant.Task; import org.apache.tools.ant.taskdefs.condition.Condition; import org.apache.tools.ant.types.EnumeratedAttribute; @@ -29,22 +30,22 @@ public class Available extends Task implements Condition { - private String value = "true"; - private String classname; - private Path classpath; - private String file; - private Path filepath; - private AntClassLoader loader; + private String m_value = "true"; + private String m_classname; + private Path m_classpath; + private String m_file; + private Path m_filepath; + private ClassLoader m_classLoader; - private String property; - private String resource; - private FileDir type; + private String m_property; + private String m_resource; + private FileDir m_type; public void setClassname( String classname ) { if( !"".equals( classname ) ) { - this.classname = classname; + m_classname = classname; } } @@ -62,7 +63,7 @@ public class Available public void setFile( String file ) { - this.file = file; + m_file = file; } public void setFilepath( Path filepath ) @@ -73,102 +74,98 @@ public class Available public void setProperty( String property ) { - this.property = property; + m_property = property; } public void setResource( String resource ) { - this.resource = resource; + m_resource = resource; } public void setType( FileDir type ) { - this.type = type; + m_type = type; } public void setValue( String value ) { - this.value = value; + m_value = value; } public Path createClasspath() throws TaskException { - if( this.classpath == null ) + if( m_classpath == null ) { - this.classpath = new Path(); + m_classpath = new Path(); } - return this.classpath.createPath(); + return m_classpath.createPath(); } public Path createFilepath() throws TaskException { - if( this.filepath == null ) + if( m_filepath == null ) { - this.filepath = new Path(); + m_filepath = new Path(); } - return this.filepath.createPath(); + return m_filepath.createPath(); } public boolean eval() throws TaskException { - if( classname == null && file == null && resource == null ) + if( m_classname == null && m_file == null && m_resource == null ) { throw new TaskException( "At least one of (classname|file|resource) is required" ); } - if( type != null ) + if( m_type != null ) { - if( file == null ) + if( m_file == null ) { throw new TaskException( "The type attribute is only valid when specifying the file attribute." ); } } - if( classpath != null ) + if( m_classpath != null ) { - this.loader = new AntClassLoader( getProject(), classpath ); + final URL[] urls = m_classpath.toURLs(); + m_classLoader = new URLClassLoader( urls ); } - if( ( classname != null ) && !checkClass( classname ) ) + if( ( m_classname != null ) && !checkClass( m_classname ) ) { - getLogger().debug( "Unable to load class " + classname + " to set property " + property ); + getLogger().debug( "Unable to load class " + m_classname + " to set property " + m_property ); return false; } - if( ( file != null ) && !checkFile() ) + if( ( m_file != null ) && !checkFile() ) { - if( type != null ) + if( m_type != null ) { - getLogger().debug( "Unable to find " + type + " " + file + " to set property " + property ); + getLogger().debug( "Unable to find " + m_type + " " + m_file + " to set property " + m_property ); } else { - getLogger().debug( "Unable to find " + file + " to set property " + property ); + getLogger().debug( "Unable to find " + m_file + " to set property " + m_property ); } return false; } - if( ( resource != null ) && !checkResource( resource ) ) + if( ( m_resource != null ) && !checkResource( m_resource ) ) { - getLogger().debug( "Unable to load resource " + resource + " to set property " + property ); + getLogger().debug( "Unable to load resource " + m_resource + " to set property " + m_property ); return false; } - if( loader != null ) - { - loader.cleanup(); - } - return true; } public void execute() throws TaskException { - if( property == null ) + if( m_property == null ) { throw new TaskException( "property attribute is required" ); } @@ -176,9 +173,9 @@ public class Available if( eval() ) { String lSep = System.getProperty( "line.separator" ); - if( null == getProject().getProperty( property ) ) + if( null == getProject().getProperty( m_property ) ) { - setProperty( property, value ); + setProperty( m_property, m_value ); } //else ignore } @@ -188,24 +185,8 @@ public class Available { try { - if( loader != null ) - { - loader.loadClass( classname ); - } - else - { - ClassLoader l = this.getClass().getClassLoader(); - // Can return null to represent the bootstrap class loader. - // see API docs of Class.getClassLoader. - if( l != null ) - { - l.loadClass( classname ); - } - else - { - Class.forName( classname ); - } - } + final ClassLoader classLoader = getClassLoader(); + classLoader.loadClass( classname ); return true; } catch( ClassNotFoundException e ) @@ -221,13 +202,13 @@ public class Available private boolean checkFile() throws TaskException { - if( filepath == null ) + if( m_filepath == null ) { - return checkFile( resolveFile( file ), file ); + return checkFile( resolveFile( m_file ), m_file ); } else { - String[] paths = filepath.list(); + String[] paths = m_filepath.list(); for( int i = 0; i < paths.length; ++i ) { getLogger().debug( "Searching " + paths[ i ] ); @@ -248,20 +229,20 @@ public class Available // ** full-pathname specified == path in list // ** simple name specified == path in list - if( path.exists() && file.equals( paths[ i ] ) ) + if( path.exists() && m_file.equals( paths[ i ] ) ) { - if( type == null ) + if( m_type == null ) { getLogger().debug( "Found: " + path ); return true; } - else if( type.isDir() + else if( m_type.isDir() && path.isDirectory() ) { getLogger().debug( "Found directory: " + path ); return true; } - else if( type.isFile() + else if( m_type.isFile() && path.isFile() ) { getLogger().debug( "Found file: " + path ); @@ -274,14 +255,14 @@ public class Available File parent = path.getParentFile(); // ** full-pathname specified == parent dir of path in list if( parent != null && parent.exists() - && file.equals( parent.getAbsolutePath() ) ) + && m_file.equals( parent.getAbsolutePath() ) ) { - if( type == null ) + if( m_type == null ) { getLogger().debug( "Found: " + parent ); return true; } - else if( type.isDir() ) + else if( m_type.isDir() ) { getLogger().debug( "Found directory: " + parent ); return true; @@ -293,8 +274,8 @@ public class Available // ** simple name specified == path in list + name if( path.exists() && path.isDirectory() ) { - if( checkFile( new File( path, file ), - file + " in " + path ) ) + if( checkFile( new File( path, m_file ), + m_file + " in " + path ) ) { return true; } @@ -303,8 +284,8 @@ public class Available // ** simple name specified == parent dir + name if( parent != null && parent.exists() ) { - if( checkFile( new File( parent, file ), - file + " in " + parent ) ) + if( checkFile( new File( parent, m_file ), + m_file + " in " + parent ) ) { return true; } @@ -316,8 +297,8 @@ public class Available File grandParent = parent.getParentFile(); if( grandParent != null && grandParent.exists() ) { - if( checkFile( new File( grandParent, file ), - file + " in " + grandParent ) ) + if( checkFile( new File( grandParent, m_file ), + m_file + " in " + grandParent ) ) { return true; } @@ -330,9 +311,9 @@ public class Available private boolean checkFile( File f, String text ) { - if( type != null ) + if( m_type != null ) { - if( type.isDir() ) + if( m_type.isDir() ) { if( f.isDirectory() ) { @@ -340,7 +321,7 @@ public class Available } return f.isDirectory(); } - else if( type.isFile() ) + else if( m_type.isFile() ) { if( f.isFile() ) { @@ -358,22 +339,19 @@ public class Available private boolean checkResource( String resource ) { - if( loader != null ) + final ClassLoader classLoader = getClassLoader(); + return ( null != classLoader.getResourceAsStream( resource ) ); + } + + private ClassLoader getClassLoader() + { + if( null == m_classLoader ) { - return ( loader.getResourceAsStream( resource ) != null ); + return ClassLoader.getSystemClassLoader(); } else { - ClassLoader cL = this.getClass().getClassLoader(); - if( cL != null ) - { - return ( cL.getResourceAsStream( resource ) != null ); - } - else - { - return - ( ClassLoader.getSystemResourceAsStream( resource ) != null ); - } + return m_classLoader; } } diff --git a/proposal/myrmidon/src/todo/org/apache/tools/ant/taskdefs/ExecuteJava.java b/proposal/myrmidon/src/todo/org/apache/tools/ant/taskdefs/ExecuteJava.java index e1fec0c13..c74538b0d 100644 --- a/proposal/myrmidon/src/todo/org/apache/tools/ant/taskdefs/ExecuteJava.java +++ b/proposal/myrmidon/src/todo/org/apache/tools/ant/taskdefs/ExecuteJava.java @@ -9,8 +9,9 @@ package org.apache.tools.ant.taskdefs; import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Method; +import java.net.URL; +import java.net.URLClassLoader; import org.apache.myrmidon.api.TaskException; -import org.apache.tools.ant.AntClassLoader; import org.apache.tools.ant.Project; import org.apache.tools.ant.types.Commandline; import org.apache.tools.ant.types.CommandlineJava; @@ -20,56 +21,51 @@ import org.apache.tools.ant.types.Path; * @author thomas.haas@softwired-inc.com * @author Stefan Bodewig */ - public class ExecuteJava { + private Commandline m_javaCommand; + private Path m_classpath; + private CommandlineJava.SysProperties m_sysProperties; - private Commandline javaCommand = null; - private Path classpath = null; - private CommandlineJava.SysProperties sysProperties = null; - - public void setClasspath( Path p ) + public void setClasspath( final Path classpath ) { - classpath = p; + m_classpath = classpath; } - public void setJavaCommand( Commandline javaCommand ) + public void setJavaCommand( final Commandline javaCommand ) { - this.javaCommand = javaCommand; + m_javaCommand = javaCommand; } - public void setSystemProperties( CommandlineJava.SysProperties s ) + public void setSystemProperties( final CommandlineJava.SysProperties sysProperties ) { - sysProperties = s; + m_sysProperties = sysProperties; } public void execute( Project project ) throws TaskException { - final String classname = javaCommand.getExecutable(); - final Object[] argument = {javaCommand.getArguments()}; + final String classname = m_javaCommand.getExecutable(); + final Object[] argument = {m_javaCommand.getArguments()}; - AntClassLoader loader = null; try { - if( sysProperties != null ) + if( m_sysProperties != null ) { - sysProperties.setSystem(); + m_sysProperties.setSystem(); } final Class[] param = {Class.forName( "[Ljava.lang.String;" )}; Class target = null; - if( classpath == null ) + if( m_classpath == null ) { target = Class.forName( classname ); } else { - loader = new AntClassLoader( Project.class.getClassLoader(), project, classpath, false ); - loader.setIsolated( true ); - loader.setThreadContextLoader(); - target = loader.forceLoadClass( classname ); - AntClassLoader.initializeClass( target ); + final URL[] urls = m_classpath.toURLs(); + final URLClassLoader classLoader = new URLClassLoader( urls ); + target = classLoader.loadClass( classname ); } final Method main = target.getMethod( "main", param ); main.invoke( null, argument ); @@ -100,14 +96,9 @@ public class ExecuteJava } finally { - if( loader != null ) - { - loader.resetThreadContextLoader(); - loader.cleanup(); - } - if( sysProperties != null ) + if( m_sysProperties != null ) { - sysProperties.restoreSystem(); + m_sysProperties.restoreSystem(); } } } diff --git a/proposal/myrmidon/src/todo/org/apache/tools/ant/taskdefs/Property.java b/proposal/myrmidon/src/todo/org/apache/tools/ant/taskdefs/Property.java index a1cd1323f..51821b6ce 100644 --- a/proposal/myrmidon/src/todo/org/apache/tools/ant/taskdefs/Property.java +++ b/proposal/myrmidon/src/todo/org/apache/tools/ant/taskdefs/Property.java @@ -11,13 +11,13 @@ import java.io.File; import java.io.FileInputStream; import java.io.IOException; import java.io.InputStream; -import java.util.Enumeration; +import java.net.URL; +import java.net.URLClassLoader; import java.util.Iterator; import java.util.Properties; import org.apache.myrmidon.api.TaskException; import org.apache.myrmidon.framework.exec.Environment; import org.apache.myrmidon.framework.exec.ExecException; -import org.apache.tools.ant.AntClassLoader; import org.apache.tools.ant.Task; import org.apache.tools.ant.types.Path; import org.apache.tools.ant.types.Reference; @@ -269,26 +269,18 @@ public class Property getLogger().debug( "Resource Loading " + name ); try { - ClassLoader cL = null; - InputStream is = null; + ClassLoader classLoader = null; if( m_classpath != null ) { - cL = new AntClassLoader( getProject(), m_classpath ); + final URL[] urls = m_classpath.toURLs(); + classLoader = new URLClassLoader( urls ); } else { - cL = getClass().getClassLoader(); - } - - if( cL == null ) - { - is = ClassLoader.getSystemResourceAsStream( name ); - } - else - { - is = cL.getResourceAsStream( name ); + classLoader = ClassLoader.getSystemClassLoader(); } + final InputStream is = classLoader.getResourceAsStream( name ); if( is != null ) { diff --git a/proposal/myrmidon/src/todo/org/apache/tools/ant/taskdefs/Rmic.java b/proposal/myrmidon/src/todo/org/apache/tools/ant/taskdefs/Rmic.java index d37242ea2..20e39e822 100644 --- a/proposal/myrmidon/src/todo/org/apache/tools/ant/taskdefs/Rmic.java +++ b/proposal/myrmidon/src/todo/org/apache/tools/ant/taskdefs/Rmic.java @@ -9,11 +9,11 @@ package org.apache.tools.ant.taskdefs; import java.io.File; import java.io.IOException; +import java.net.URLClassLoader; import java.rmi.Remote; import java.util.ArrayList; import org.apache.avalon.excalibur.io.FileUtil; import org.apache.myrmidon.api.TaskException; -import org.apache.tools.ant.AntClassLoader; import org.apache.tools.ant.DirectoryScanner; import org.apache.tools.ant.taskdefs.rmic.RmicAdapter; import org.apache.tools.ant.taskdefs.rmic.RmicAdapterFactory; @@ -525,7 +525,7 @@ public class Rmic extends MatchingTask adapter.setRmic( this ); Path classpath = adapter.getClasspath(); - loader = new AntClassLoader( getProject(), classpath ); + loader = new URLClassLoader( classpath.toURLs() ); // scan base dirs to build up compile lists only if a // specific classname is not given diff --git a/proposal/myrmidon/src/todo/org/apache/tools/ant/taskdefs/SQLExec.java b/proposal/myrmidon/src/todo/org/apache/tools/ant/taskdefs/SQLExec.java index 7eff3717b..10091a85a 100644 --- a/proposal/myrmidon/src/todo/org/apache/tools/ant/taskdefs/SQLExec.java +++ b/proposal/myrmidon/src/todo/org/apache/tools/ant/taskdefs/SQLExec.java @@ -18,6 +18,7 @@ import java.io.InputStreamReader; import java.io.PrintStream; import java.io.Reader; import java.io.StringReader; +import java.net.URLClassLoader; import java.sql.Connection; import java.sql.DatabaseMetaData; import java.sql.Driver; @@ -31,7 +32,6 @@ import java.util.Iterator; import java.util.Properties; import java.util.StringTokenizer; import org.apache.myrmidon.api.TaskException; -import org.apache.tools.ant.AntClassLoader; import org.apache.tools.ant.DirectoryScanner; import org.apache.tools.ant.Task; import org.apache.tools.ant.types.EnumeratedAttribute; @@ -47,9 +47,9 @@ import org.apache.tools.ant.types.Reference; * @author Michael McCallum * @author Tim Stephenson */ -public class SQLExec extends Task +public class SQLExec + extends Task { - private int goodSql = 0, totalSql = 0; private ArrayList filesets = new ArrayList(); @@ -57,42 +57,42 @@ public class SQLExec extends Task /** * Database connection */ - private Connection conn = null; + private Connection conn; /** * Autocommit flag. Default value is false */ - private boolean autocommit = false; + private boolean autocommit; /** * SQL statement */ - private Statement statement = null; + private Statement statement; /** * DB driver. */ - private String driver = null; + private String driver; /** * DB url. */ - private String url = null; + private String url; /** * User name. */ - private String userId = null; + private String userId; /** * Password */ - private String password = null; + private String password; /** * SQL input file */ - private File srcFile = null; + private File srcFile; /** * SQL input command @@ -118,7 +118,7 @@ public class SQLExec extends Task /** * Print SQL results. */ - private boolean print = false; + private boolean print; /** * Print header columns. @@ -128,17 +128,17 @@ public class SQLExec extends Task /** * Results Output file. */ - private File output = null; + private File output; /** * RDBMS Product needed for this SQL. */ - private String rdbms = null; + private String rdbms; /** * RDBMS Version needed for this SQL. */ - private String version = null; + private String version; /** * Action to perform if an error is found @@ -148,12 +148,10 @@ public class SQLExec extends Task /** * Encoding to use when reading SQL statements from a file */ - private String encoding = null; + private String encoding; private Path classpath; - private AntClassLoader loader; - /** * Set the autocommit flag for the DB connection. * @@ -457,8 +455,8 @@ public class SQLExec extends Task { getLogger().debug( "Loading " + driver + " using AntClassLoader with classpath " + classpath ); - loader = new AntClassLoader( getProject(), classpath ); - dc = loader.loadClass( driver ); + final ClassLoader classLoader = new URLClassLoader( classpath.toURLs() ); + dc = classLoader.loadClass( driver ); } else { diff --git a/proposal/myrmidon/src/todo/org/apache/tools/ant/taskdefs/compilers/DefaultCompilerAdapter.java b/proposal/myrmidon/src/todo/org/apache/tools/ant/taskdefs/compilers/DefaultCompilerAdapter.java index 042adb3a9..485f203b9 100644 --- a/proposal/myrmidon/src/todo/org/apache/tools/ant/taskdefs/compilers/DefaultCompilerAdapter.java +++ b/proposal/myrmidon/src/todo/org/apache/tools/ant/taskdefs/compilers/DefaultCompilerAdapter.java @@ -401,8 +401,7 @@ public abstract class DefaultCompilerAdapter } catch( IOException e ) { - throw new TaskException( "Error running " + args[ 0 ] - + " compiler", e ); + throw new TaskException( "Error running " + args[ 0 ] + " compiler", e ); } } finally diff --git a/proposal/myrmidon/src/todo/org/apache/tools/ant/taskdefs/optional/depend/Depend.java b/proposal/myrmidon/src/todo/org/apache/tools/ant/taskdefs/optional/depend/Depend.java index aa77b8932..47e8679d6 100644 --- a/proposal/myrmidon/src/todo/org/apache/tools/ant/taskdefs/optional/depend/Depend.java +++ b/proposal/myrmidon/src/todo/org/apache/tools/ant/taskdefs/optional/depend/Depend.java @@ -15,12 +15,12 @@ import java.io.FileWriter; import java.io.IOException; import java.io.PrintWriter; import java.net.URL; +import java.net.URLClassLoader; import java.util.ArrayList; import java.util.Enumeration; import java.util.Hashtable; import java.util.Iterator; import org.apache.myrmidon.api.TaskException; -import org.apache.tools.ant.AntClassLoader; import org.apache.tools.ant.DirectoryScanner; import org.apache.tools.ant.taskdefs.MatchingTask; import org.apache.tools.ant.types.Path; @@ -579,7 +579,7 @@ public class Depend extends MatchingTask { // now determine which jars each class depends upon classpathDependencies = new Hashtable(); - AntClassLoader loader = new AntClassLoader( getProject(), dependClasspath ); + final ClassLoader classLoader = new URLClassLoader( dependClasspath.toURLs() ); Hashtable classpathFileCache = new Hashtable(); Object nullFileMarker = new Object(); @@ -599,7 +599,8 @@ public class Depend extends MatchingTask if( !dependency.startsWith( "java." ) && !dependency.startsWith( "javax." ) ) { - URL classURL = loader.getResource( dependency.replace( '.', '/' ) + ".class" ); + final String name = dependency.replace( '.', '/' ) + ".class"; + URL classURL = classLoader.getResource( name ); if( classURL != null ) { if( classURL.getProtocol().equals( "jar" ) ) diff --git a/proposal/myrmidon/src/todo/org/apache/tools/ant/taskdefs/optional/ejb/GenericDeploymentTool.java b/proposal/myrmidon/src/todo/org/apache/tools/ant/taskdefs/optional/ejb/GenericDeploymentTool.java index cdf0afba4..271014251 100644 --- a/proposal/myrmidon/src/todo/org/apache/tools/ant/taskdefs/optional/ejb/GenericDeploymentTool.java +++ b/proposal/myrmidon/src/todo/org/apache/tools/ant/taskdefs/optional/ejb/GenericDeploymentTool.java @@ -12,6 +12,7 @@ import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; +import java.net.URLClassLoader; import java.util.ArrayList; import java.util.HashSet; import java.util.Hashtable; @@ -27,7 +28,6 @@ import org.apache.avalon.framework.logger.AbstractLogEnabled; import org.apache.bcel.*; import org.apache.bcel.classfile.*; import org.apache.myrmidon.api.TaskException; -import org.apache.tools.ant.AntClassLoader; import org.apache.tools.ant.DirectoryScanner; import org.apache.tools.ant.Project; import org.apache.tools.ant.Task; @@ -329,7 +329,7 @@ public class GenericDeploymentTool } else { - classpathLoader = new AntClassLoader( getTask().getProject(), combinedClasspath ); + classpathLoader = new URLClassLoader( combinedClasspath.toURLs() ); } return classpathLoader; diff --git a/proposal/myrmidon/src/todo/org/apache/tools/ant/taskdefs/optional/ejb/WeblogicDeploymentTool.java b/proposal/myrmidon/src/todo/org/apache/tools/ant/taskdefs/optional/ejb/WeblogicDeploymentTool.java index 074d9ac69..f57d33106 100644 --- a/proposal/myrmidon/src/todo/org/apache/tools/ant/taskdefs/optional/ejb/WeblogicDeploymentTool.java +++ b/proposal/myrmidon/src/todo/org/apache/tools/ant/taskdefs/optional/ejb/WeblogicDeploymentTool.java @@ -12,6 +12,7 @@ import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; +import java.net.URLClassLoader; import java.util.Hashtable; import java.util.Iterator; import java.util.jar.JarEntry; @@ -21,7 +22,6 @@ import javax.xml.parsers.SAXParser; import javax.xml.parsers.SAXParserFactory; import org.apache.avalon.excalibur.io.FileUtil; import org.apache.myrmidon.api.TaskException; -import org.apache.tools.ant.AntClassLoader; import org.apache.tools.ant.taskdefs.Java; import org.apache.tools.ant.types.Path; import org.xml.sax.InputSource; @@ -332,7 +332,7 @@ public class WeblogicDeploymentTool extends GenericDeploymentTool lookupPath.append( classpath ); } - return new AntClassLoader( getTask().getProject(), lookupPath ); + return new URLClassLoader( lookupPath.toURLs() ); } protected DescriptorHandler getWeblogicDescriptorHandler( final File srcDir ) diff --git a/proposal/myrmidon/src/todo/org/apache/tools/ant/taskdefs/optional/ejb/WebsphereDeploymentTool.java b/proposal/myrmidon/src/todo/org/apache/tools/ant/taskdefs/optional/ejb/WebsphereDeploymentTool.java index 57959512c..9a41f559b 100644 --- a/proposal/myrmidon/src/todo/org/apache/tools/ant/taskdefs/optional/ejb/WebsphereDeploymentTool.java +++ b/proposal/myrmidon/src/todo/org/apache/tools/ant/taskdefs/optional/ejb/WebsphereDeploymentTool.java @@ -11,13 +11,13 @@ import java.io.File; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; +import java.net.URLClassLoader; import java.util.Hashtable; import java.util.Iterator; import java.util.jar.JarEntry; import java.util.jar.JarFile; import java.util.jar.JarOutputStream; import org.apache.myrmidon.api.TaskException; -import org.apache.tools.ant.AntClassLoader; import org.apache.tools.ant.taskdefs.Java; import org.apache.tools.ant.types.Argument; import org.apache.tools.ant.types.EnumeratedAttribute; @@ -410,7 +410,7 @@ public class WebsphereDeploymentTool { lookupPath.append( classpath ); } - return new AntClassLoader( getTask().getProject(), lookupPath ); + return new URLClassLoader( lookupPath.toURLs() ); } protected DescriptorHandler getDescriptorHandler( File srcDir ) diff --git a/proposal/myrmidon/src/todo/org/apache/tools/ant/taskdefs/optional/junit/JUnitTask.java b/proposal/myrmidon/src/todo/org/apache/tools/ant/taskdefs/optional/junit/JUnitTask.java index 323aba4a7..ad6bd19ed 100644 --- a/proposal/myrmidon/src/todo/org/apache/tools/ant/taskdefs/optional/junit/JUnitTask.java +++ b/proposal/myrmidon/src/todo/org/apache/tools/ant/taskdefs/optional/junit/JUnitTask.java @@ -12,6 +12,7 @@ import java.io.FileOutputStream; import java.io.IOException; import java.io.OutputStream; import java.net.URL; +import java.net.URLClassLoader; import java.util.ArrayList; import java.util.Enumeration; import java.util.Hashtable; @@ -19,7 +20,6 @@ import java.util.Iterator; import java.util.Properties; import java.util.Random; import org.apache.myrmidon.api.TaskException; -import org.apache.tools.ant.AntClassLoader; import org.apache.tools.ant.Task; import org.apache.tools.ant.taskdefs.exec.Execute; import org.apache.tools.ant.taskdefs.exec.LogOutputStream; @@ -694,19 +694,18 @@ public class JUnitTask extends Task try { getLogger().debug( "Using System properties " + System.getProperties() ); - AntClassLoader cl = null; + ClassLoader classLoader = null; Path classpath = commandline.getClasspath(); if( classpath != null ) { getLogger().debug( "Using CLASSPATH " + classpath ); - - cl = new AntClassLoader( null, getProject(), classpath, false ); - // make sure the test will be accepted as a TestCase - cl.addSystemPackageRoot( "junit" ); - // will cause trouble in JDK 1.1 if omitted - cl.addSystemPackageRoot( "org.apache.tools.ant" ); + classLoader = new URLClassLoader( classpath.toURLs() ); } - runner = new JUnitTestRunner( test, test.getHaltonerror(), test.getFiltertrace(), test.getHaltonfailure(), cl ); + runner = new JUnitTestRunner( test, + test.getHaltonerror(), + test.getFiltertrace(), + test.getHaltonfailure(), + classLoader ); if( summary ) { getLogger().info( "Running " + test.getName() ); diff --git a/proposal/myrmidon/src/todo/org/apache/tools/ant/taskdefs/optional/junit/JUnitTestRunner.java b/proposal/myrmidon/src/todo/org/apache/tools/ant/taskdefs/optional/junit/JUnitTestRunner.java index 4ad2d4bf1..4c2b31632 100644 --- a/proposal/myrmidon/src/todo/org/apache/tools/ant/taskdefs/optional/junit/JUnitTestRunner.java +++ b/proposal/myrmidon/src/todo/org/apache/tools/ant/taskdefs/optional/junit/JUnitTestRunner.java @@ -28,9 +28,6 @@ import junit.framework.TestResult; import junit.framework.TestSuite; import org.apache.avalon.framework.ExceptionUtil; import org.apache.myrmidon.api.TaskException; -import org.apache.tools.ant.AntClassLoader; -import org.apache.tools.ant.Project; -import org.apache.tools.ant.util.StringUtils; /** * Simple Testrunner for JUnit that runs all tests of a testsuite.

@@ -179,7 +176,6 @@ public class JUnitTestRunner implements TestListener else { testClass = loader.loadClass( test.getName() ); - AntClassLoader.initializeClass( testClass ); } Method suiteMethod = null; diff --git a/proposal/myrmidon/src/todo/org/apache/tools/ant/taskdefs/optional/junit/XMLResultAggregator.java b/proposal/myrmidon/src/todo/org/apache/tools/ant/taskdefs/optional/junit/XMLResultAggregator.java index 12d0f4191..39bd0657e 100644 --- a/proposal/myrmidon/src/todo/org/apache/tools/ant/taskdefs/optional/junit/XMLResultAggregator.java +++ b/proposal/myrmidon/src/todo/org/apache/tools/ant/taskdefs/optional/junit/XMLResultAggregator.java @@ -24,7 +24,6 @@ import org.apache.tools.ant.DirectoryScanner; import org.apache.tools.ant.Task; import org.apache.tools.ant.types.FileSet; import org.apache.tools.ant.util.DOMElementWriter; -import org.apache.tools.ant.util.StringUtils; import org.w3c.dom.Document; import org.w3c.dom.Element; import org.xml.sax.SAXException; @@ -184,7 +183,7 @@ public class XMLResultAggregator extends Task implements XMLConstants } if( toDir == null ) { - toDir = FileUtil.resolveFile( getProject().getBaseDir(), DEFAULT_DIR ); + toDir = FileUtil.resolveFile( getBaseDirectory(), DEFAULT_DIR ); } return new File( toDir, toFile ); } @@ -210,16 +209,13 @@ public class XMLResultAggregator extends Task implements XMLConstants if( pathname.endsWith( ".xml" ) ) { File file = new File( ds.getBasedir(), pathname ); - file = FileUtil. - resolveFile( getProject().getBaseDir(), file.getPath() ); + file = FileUtil.resolveFile( getBaseDirectory(), file.getPath() ); v.add( file ); } } } - File[] files = new File[ v.size() ]; - v.copyInto( files ); - return files; + return (File[])v.toArray( new File[ v.size() ] ); } /** diff --git a/proposal/myrmidon/src/todo/org/apache/tools/ant/types/Mapper.java b/proposal/myrmidon/src/todo/org/apache/tools/ant/types/Mapper.java index 6474fa756..4b35736b1 100644 --- a/proposal/myrmidon/src/todo/org/apache/tools/ant/types/Mapper.java +++ b/proposal/myrmidon/src/todo/org/apache/tools/ant/types/Mapper.java @@ -7,10 +7,10 @@ */ package org.apache.tools.ant.types; -import java.util.Properties; +import java.net.URL; +import java.net.URLClassLoader; import java.util.Stack; import org.apache.myrmidon.api.TaskException; -import org.apache.tools.ant.AntClassLoader; import org.apache.tools.ant.util.FileNameMapper; /** @@ -182,10 +182,9 @@ public class Mapper } else { - AntClassLoader al = new AntClassLoader( getProject(), - m_classpath ); - c = al.loadClass( m_classname ); - AntClassLoader.initializeClass( c ); + final URL[] urls = m_classpath.toURLs(); + final URLClassLoader classLoader = new URLClassLoader( urls ); + c = classLoader.loadClass( m_classname ); } FileNameMapper m = (FileNameMapper)c.newInstance(); diff --git a/proposal/myrmidon/src/todo/org/apache/tools/ant/types/Path.java b/proposal/myrmidon/src/todo/org/apache/tools/ant/types/Path.java index e0dc4992e..fac5ca02d 100644 --- a/proposal/myrmidon/src/todo/org/apache/tools/ant/types/Path.java +++ b/proposal/myrmidon/src/todo/org/apache/tools/ant/types/Path.java @@ -8,6 +8,8 @@ package org.apache.tools.ant.types; import java.io.File; +import java.io.IOException; +import java.net.URL; import java.util.ArrayList; import java.util.Iterator; import java.util.Locale; @@ -644,6 +646,33 @@ public class Path } } + /** + * Returns an array of URLs - useful for building a ClassLoader. + */ + public URL[] toURLs() + throws TaskException + { + try + { + final String[] list = list(); + + final URL[] result = new URL[ list.length ]; + + // path containing one or more elements + for( int i = 0; i < list.length; i++ ) + { + result[ i ] = new File( list[ i ] ).toURL(); + } + + return result; + } + catch( final IOException ioe ) + { + final String message = "Malformed path entry. Reason:" + ioe; + throw new TaskException( message, ioe ); + } + } + /** * Overrides the version of DataType to recurse on all DataType child * elements that may have been added. @@ -655,7 +684,6 @@ public class Path protected void dieOnCircularReference( Stack stk, Project p ) throws TaskException { - if( checked ) { return;