conor 2003/07/09 06:11:16 Modified: src/main/org/apache/tools/ant/launch Launcher.java Locator.java src/main/org/apache/tools/ant/util FileUtils.java LoaderUtils.java src/script ant Log: Refactor some code into the Launcher. Update ant script to use new launcher. Revision Changes Path 1.3 +3 -4 ant/src/main/org/apache/tools/ant/launch/Launcher.java Index: Launcher.java =================================================================== RCS file: /home/cvs/ant/src/main/org/apache/tools/ant/launch/Launcher.java,v retrieving revision 1.2 retrieving revision 1.3 diff -u -w -u -r1.2 -r1.3 --- Launcher.java 4 Jul 2003 14:04:53 -0000 1.2 +++ Launcher.java 9 Jul 2003 13:11:15 -0000 1.3 @@ -101,16 +101,15 @@ String antHomeProperty = System.getProperty(ANTHOME_PROPERTY); File antHome = null; - URL launchJarURL = Locator.getClassLocationURL(getClass()); - File jarDir = new File(launchJarURL.getFile()).getParentFile(); + File sourceJar = Locator.getClassSource(getClass()); + File jarDir = sourceJar.getParentFile(); if (antHomeProperty != null) { antHome = new File(antHomeProperty); } if (antHome == null || !antHome.exists()) { - URL antHomeURL = new URL(launchJarURL, ".."); - antHome = new File(antHomeURL.getFile()); + antHome = jarDir.getParentFile(); System.setProperty(ANTHOME_PROPERTY, antHome.getAbsolutePath()); } 1.3 +88 -33 ant/src/main/org/apache/tools/ant/launch/Locator.java Index: Locator.java =================================================================== RCS file: /home/cvs/ant/src/main/org/apache/tools/ant/launch/Locator.java,v retrieving revision 1.2 retrieving revision 1.3 diff -u -w -u -r1.2 -r1.3 --- Locator.java 4 Jul 2003 14:04:53 -0000 1.2 +++ Locator.java 9 Jul 2003 13:11:15 -0000 1.3 @@ -58,6 +58,8 @@ import java.net.URL; import java.io.File; import java.io.FilenameFilter; +import java.text.CharacterIterator; +import java.text.StringCharacterIterator; /** * The Locator is a utility class which is used to find certain items @@ -67,48 +69,101 @@ * @since Ant 1.6 */ public class Locator { + /** - * Get the URL for the given class's load location. + * Find the directory or jar file the class has been loaded from. + * + * @return null if we cannot determine the location. * - * @param theClass the class whose load URL is desired. - * @return a URL which identifies the component from which this class - * was loaded. - * @throws MalformedURLException if the class' URL cannot be - * constructed. + * @since Ant 1.6 */ - public static URL getClassLocationURL(Class theClass) - throws MalformedURLException { - String className = theClass.getName().replace('.', '/') + ".class"; - URL classRawURL = theClass.getClassLoader().getResource(className); + public static File getClassSource(Class c) { + String classResource = c.getName().replace('.', '/') + ".class"; + return getResourceSource(c.getClassLoader(), classResource); + } + + /** + * Find the directory or a give resource has been loaded from. + * + * @return null if we cannot determine the location. + * + * @since Ant 1.6 + */ + public static File getResourceSource(ClassLoader c, String resource) { + if (c == null) { + c = Locator.class.getClassLoader(); + } + + URL url = c.getResource(resource); + if (url != null) { + String u = url.toString(); + if (u.startsWith("jar:file:")) { + int pling = u.indexOf("!"); + String jarName = u.substring(4, pling); + return new File(fromURI(jarName)); + } else if (u.startsWith("file:")) { + int tail = u.indexOf(resource); + String dirName = u.substring(0, tail); + return new File(fromURI(dirName)); + } + } + return null; + } - String fileComponent = classRawURL.getFile(); - if (classRawURL.getProtocol().equals("file")) { - // Class comes from a directory of class files rather than - // from a jar. - int classFileIndex = fileComponent.lastIndexOf(className); - if (classFileIndex != -1) { - fileComponent = fileComponent.substring(0, classFileIndex); - } - - return new URL("file:" + fileComponent); - } else if (classRawURL.getProtocol().equals("jar")) { - // Class is coming from a jar. The file component of the URL - // is actually the URL of the jar file - int classSeparatorIndex = fileComponent.lastIndexOf("!"); - if (classSeparatorIndex != -1) { - fileComponent = fileComponent.substring(0, classSeparatorIndex); + /** + * Constructs a file path from a <code>file:</code> URI. + * + * <p>Will be an absolute path if the given URI is absolute.</p> + * + * <p>Swallows '%' that are not followed by two characters, + * doesn't deal with non-ASCII characters.</p> + * + * @param uri the URI designating a file in the local filesystem. + * @return the local file system path for the file. + * @since Ant 1.6 + */ + public static String fromURI(String uri) { + if (!uri.startsWith("file:")) { + throw new IllegalArgumentException("Can only handle file: URIs"); + } + if (uri.startsWith("file://")) { + uri = uri.substring(7); + } else { + uri = uri.substring(5); } - return new URL(fileComponent); + uri = uri.replace('/', File.separatorChar); + if (File.pathSeparatorChar == ';' && uri.startsWith("\\") && uri.length() > 2 + && Character.isLetter(uri.charAt(1)) && uri.charAt(2) == ':') { + uri = uri.substring(1); + } + + StringBuffer sb = new StringBuffer(); + CharacterIterator iter = new StringCharacterIterator(uri); + for (char c = iter.first(); c != CharacterIterator.DONE; + c = iter.next()) { + if (c == '%') { + char c1 = iter.next(); + if (c1 != CharacterIterator.DONE) { + int i1 = Character.digit(c1, 16); + char c2 = iter.next(); + if (c2 != CharacterIterator.DONE) { + int i2 = Character.digit(c2, 16); + sb.append((char) ((i1 << 4) + i2)); + } + } } else { - // its running out of something besides a jar. - // We just return the Raw URL as a best guess - return classRawURL; + sb.append(c); } } + String path = sb.toString(); + return path; + } + + /** - * Get the URL necessary to load the Sun compiler tools. If the classes + * Get the File necessary to load the Sun compiler tools. If the classes * are available to this class, then no additional URL is required and * null is returned. This may be because the classes are explcitly in the * class path or provided by the JVM directly @@ -196,7 +251,7 @@ urls = new URL[1]; String path = location.getPath(); for (int i = 0; i < extensions.length; ++i) { - if (path.endsWith(extensions[i])) { + if (path.toLowerCase().endsWith(extensions[i])) { urls[0] = location.toURL(); break; } @@ -208,7 +263,7 @@ new FilenameFilter() { public boolean accept(File dir, String name) { for (int i = 0; i < extensions.length; ++i) { - if (name.endsWith(extensions[i])) { + if (name.toLowerCase().endsWith(extensions[i])) { return true; } } 1.48 +3 -35 ant/src/main/org/apache/tools/ant/util/FileUtils.java Index: FileUtils.java =================================================================== RCS file: /home/cvs/ant/src/main/org/apache/tools/ant/util/FileUtils.java,v retrieving revision 1.47 retrieving revision 1.48 diff -u -w -u -r1.47 -r1.48 --- FileUtils.java 5 Jul 2003 14:34:13 -0000 1.47 +++ FileUtils.java 9 Jul 2003 13:11:16 -0000 1.48 @@ -83,6 +83,7 @@ import org.apache.tools.ant.filters.TokenFilter; import org.apache.tools.ant.taskdefs.condition.Os; import org.apache.tools.ant.types.FilterSetCollection; +import org.apache.tools.ant.launch.Locator; /** * This class also encapsulates methods which allow Files to be @@ -1258,41 +1259,8 @@ * @since Ant 1.6 */ public String fromURI(String uri) { - if (!uri.startsWith("file:")) { - throw new IllegalArgumentException("Can only handle file: URIs"); - } - if (uri.startsWith("file://")) { - uri = uri.substring(7); - } else { - uri = uri.substring(5); - } - - uri = uri.replace('/', File.separatorChar); - if (Os.isFamily("dos") && uri.startsWith("\\") && uri.length() > 2 - && Character.isLetter(uri.charAt(1)) && uri.charAt(2) == ':') { - uri = uri.substring(1); - } - - StringBuffer sb = new StringBuffer(); - CharacterIterator iter = new StringCharacterIterator(uri); - for (char c = iter.first(); c != CharacterIterator.DONE; - c = iter.next()) { - if (c == '%') { - char c1 = iter.next(); - if (c1 != CharacterIterator.DONE) { - int i1 = Character.digit(c1, 16); - char c2 = iter.next(); - if (c2 != CharacterIterator.DONE) { - int i2 = Character.digit(c2, 16); - sb.append((char) ((i1 << 4) + i2)); - } - } - } else { - sb.append(c); - } - } + String path = Locator.fromURI(uri); - String path = sb.toString(); // catch exception if normalize thinks this is not an absolute path try { path = normalize(path).getAbsolutePath(); 1.8 +23 -40 ant/src/main/org/apache/tools/ant/util/LoaderUtils.java Index: LoaderUtils.java =================================================================== RCS file: /home/cvs/ant/src/main/org/apache/tools/ant/util/LoaderUtils.java,v retrieving revision 1.7 retrieving revision 1.8 diff -u -w -u -r1.7 -r1.8 --- LoaderUtils.java 31 Mar 2003 13:46:18 -0000 1.7 +++ LoaderUtils.java 9 Jul 2003 13:11:16 -0000 1.8 @@ -58,6 +58,7 @@ import java.lang.reflect.Method; import java.net.URL; import org.apache.tools.ant.BuildException; +import org.apache.tools.ant.launch.Locator; /** * ClassLoader utility methods @@ -148,6 +149,26 @@ } /** + * Normalize a source location + * + * @param source the source location to be normalized. + * + * @return the normalized source location. + */ + private static File normalizeSource(File source) { + if (source != null) { + FileUtils fileUtils = FileUtils.newFileUtils(); + try { + source = fileUtils.normalize(source.getAbsolutePath()); + } catch (BuildException e) { + // relative path + } + } + + return source; + } + + /** * Find the directory or jar file the class has been loaded from. * * @return null if we cannot determine the location. @@ -155,8 +176,7 @@ * @since Ant 1.6 */ public static File getClassSource(Class c) { - String classFile = c.getName().replace('.', '/') + ".class"; - return getResourceSource(c.getClassLoader(), classFile); + return normalizeSource(Locator.getClassSource(c)); } /** @@ -167,47 +187,10 @@ * @since Ant 1.6 */ public static File getResourceSource(ClassLoader c, String resource) { - FileUtils fileUtils = FileUtils.newFileUtils(); if (c == null) { c = LoaderUtils.class.getClassLoader(); } - - URL url = c.getResource(resource); - if (url != null) { - String u = url.toString(); - if (u.startsWith("jar:file:")) { - int pling = u.indexOf("!"); - String jarName = u.substring(4, pling); - return new File(fileUtils.fromURI(jarName)); - } else if (u.startsWith("file:")) { - int tail = u.indexOf(resource); - String dirName = u.substring(0, tail); - return new File(fileUtils.fromURI(dirName)); - } + return normalizeSource(Locator.getResourceSource(c, resource)); } - return null; - } - - // if we want to drop JDK 1.1, here is code that does something similar - // - stolen from Diagnostics, stolen from Axis, stolen from somewhere else - // - // try { - // java.net.URL url = clazz.getProtectionDomain().getCodeSource().getLocation(); - // String location = url.toString(); - // if (location.startsWith("jar")) { - // url = ((java.net.JarURLConnection) url.openConnection()).getJarFileURL(); - // location = url.toString(); - // } - // - // if (location.startsWith("file")) { - // java.io.File file = new java.io.File(url.getFile()); - // return file.getAbsolutePath(); - // } else { - // return url.toString(); - // } - // } catch (Throwable t) { - // } - // return null; - } 1.37 +25 -41 ant/src/script/ant Index: ant =================================================================== RCS file: /home/cvs/ant/src/script/ant,v retrieving revision 1.36 retrieving revision 1.37 diff -u -w -u -r1.36 -r1.37 --- ant 31 Mar 2003 09:23:30 -0000 1.36 +++ ant 9 Jul 2003 13:11:16 -0000 1.37 @@ -12,6 +12,7 @@ if [ -z "$rpm_mode" ] ; then rpm_mode=false; fi + if [ -z "$usejikes" ] ; then usejikes=false; fi @@ -62,7 +63,6 @@ # make it fully qualified ANT_HOME=`cd "$ANT_HOME" && pwd` - fi # For Cygwin, ensure paths are in UNIX format before anything is touched @@ -120,29 +120,14 @@ ANT_LIB="${JAVALIBDIR}/ant" fi -# add in the dependency .jar files in non-RPM mode (the default) -for i in "${ANT_LIB}"/*.jar -do - # if the directory is empty, then it will return the input string - # this is stupid, so case for it - if [ -f "$i" ] ; then if [ -z "$LOCALCLASSPATH" ] ; then - LOCALCLASSPATH="$i" + LOCALCLASSPATH=$ANT_LIB/ant-launcher.jar else - LOCALCLASSPATH="$i:$LOCALCLASSPATH" - fi - fi -done - -if [ -n "$JAVA_HOME" ] ; then - if [ -f "$JAVA_HOME/lib/tools.jar" ] ; then - LOCALCLASSPATH="$LOCALCLASSPATH:$JAVA_HOME/lib/tools.jar" + LOCALCLASSPATH=$ANT_LIB/ant-launcher.jar:$LOCALCLASSPATH fi - if [ -f "$JAVA_HOME/lib/classes.zip" ] ; then - LOCALCLASSPATH="$LOCALCLASSPATH:$JAVA_HOME/lib/classes.zip" - fi +if [ -n "$JAVA_HOME" ] ; then # OSX hack to make Ant work with jikes if $darwin ; then OSXHACK="${JAVA_HOME}/../Classes" @@ -176,15 +161,14 @@ if [ -n "$CYGHOME" ]; then if [ -n "$JIKESPATH" ]; then - JIKESPATH=`cygpath --path --windows "$JIKESPATH"` - exec "$JAVACMD" $ANT_OPTS -classpath "$LOCALCLASSPATH" -Dant.home="${ANT_HOME}" -Djikes.class.path="$JIKESPATH" -Dcygwin.user.home="$CYGHOME" org.apache.tools.ant.Main $ANT_ARGS "$@" + exec "$JAVACMD" $ANT_OPTS -classpath "$LOCALCLASSPATH" -Dant.home="${ANT_HOME}" -Djikes.class.path="$JIKESPATH" -Dcygwin.user.home="$CYGHOME" org.apache.tools.ant.launch.Launcher $ANT_ARGS "$@" else - exec "$JAVACMD" $ANT_OPTS -classpath "$LOCALCLASSPATH" -Dant.home="${ANT_HOME}" -Dcygwin.user.home="$CYGHOME" org.apache.tools.ant.Main $ANT_ARGS "$@" + exec "$JAVACMD" $ANT_OPTS -classpath "$LOCALCLASSPATH" -Dant.home="${ANT_HOME}" -Dcygwin.user.home="$CYGHOME" org.apache.tools.ant.launch.Launcher $ANT_ARGS "$@" fi else if [ -n "$JIKESPATH" ]; then - exec "$JAVACMD" $ANT_OPTS -classpath "$LOCALCLASSPATH" -Dant.home="${ANT_HOME}" -Djikes.class.path="$JIKESPATH" org.apache.tools.ant.Main $ANT_ARGS "$@" + exec "$JAVACMD" $ANT_OPTS -classpath "$LOCALCLASSPATH" -Dant.home="${ANT_HOME}" -Djikes.class.path="$JIKESPATH" org.apache.tools.ant.launch.Launcher $ANT_ARGS "$@" else - exec "$JAVACMD" $ANT_OPTS -classpath "$LOCALCLASSPATH" -Dant.home="${ANT_HOME}" org.apache.tools.ant.Main $ANT_ARGS "$@" + exec "$JAVACMD" $ANT_OPTS -classpath "$LOCALCLASSPATH" -Dant.home="${ANT_HOME}" org.apache.tools.ant.launch.Launcher $ANT_ARGS "$@" fi fi
--------------------------------------------------------------------- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]