Browse Source

Refactor some code into the Launcher.

Update ant script to use new launcher.


git-svn-id: https://svn.apache.org/repos/asf/ant/core/trunk@274793 13f79535-47bb-0310-9956-ffa450edef68
master
Conor MacNeill 22 years ago
parent
commit
f3939727d9
5 changed files with 141 additions and 152 deletions
  1. +3
    -4
      src/main/org/apache/tools/ant/launch/Launcher.java
  2. +88
    -33
      src/main/org/apache/tools/ant/launch/Locator.java
  3. +2
    -34
      src/main/org/apache/tools/ant/util/FileUtils.java
  4. +23
    -40
      src/main/org/apache/tools/ant/util/LoaderUtils.java
  5. +25
    -41
      src/script/ant

+ 3
- 4
src/main/org/apache/tools/ant/launch/Launcher.java View File

@@ -101,16 +101,15 @@ public class Launcher {
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());
}



+ 88
- 33
src/main/org/apache/tools/ant/launch/Locator.java View File

@@ -58,6 +58,8 @@ import java.net.MalformedURLException;
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 @@ import java.io.FilenameFilter;
* @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);

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);
}
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();
}

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);
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;
}

return new URL(fileComponent);
/**
* 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 {
// its running out of something besides a jar.
// We just return the Raw URL as a best guess
return classRawURL;
uri = uri.substring(5);
}

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 {
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 @@ public class Locator {
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 @@ public class Locator {
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;
}
}


+ 2
- 34
src/main/org/apache/tools/ant/util/FileUtils.java View File

@@ -83,6 +83,7 @@ import org.apache.tools.ant.filters.util.ChainReaderHelper;
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 @@ public class FileUtils {
* @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();


+ 23
- 40
src/main/org/apache/tools/ant/util/LoaderUtils.java View File

@@ -58,6 +58,7 @@ import java.lang.reflect.InvocationTargetException;
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
@@ -147,6 +148,26 @@ public class LoaderUtils {
setContextClassLoader != null;
}

/**
* 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.
*
@@ -155,8 +176,7 @@ public class LoaderUtils {
* @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 @@ public class LoaderUtils {
* @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 null;
return normalizeSource(Locator.getResourceSource(c, resource));
}

// 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;

}


+ 25
- 41
src/script/ant View File

@@ -4,7 +4,7 @@
# reserved.

# load system-wide ant configuration
if [ -f "/etc/ant.conf" ] ; then
if [ -f "/etc/ant.conf" ] ; then
. /etc/ant.conf
fi

@@ -12,12 +12,13 @@ fi
if [ -z "$rpm_mode" ] ; then
rpm_mode=false;
fi

if [ -z "$usejikes" ] ; then
usejikes=false;
fi

# load user ant configuration
if [ -f "$HOME/.antrc" ] ; then
if [ -f "$HOME/.antrc" ] ; then
. "$HOME/.antrc"
fi

@@ -28,18 +29,18 @@ case "`uname`" in
CYGWIN*) cygwin=true ;;
Darwin*) darwin=true
if [ -z "$JAVA_HOME" ] ; then
JAVA_HOME=/System/Library/Frameworks/JavaVM.framework/Home
JAVA_HOME=/System/Library/Frameworks/JavaVM.framework/Home
fi
;;
esac

if [ -z "$ANT_HOME" ] ; then
# try to find ANT
if [ -d /opt/ant ] ; then
if [ -d /opt/ant ] ; then
ANT_HOME=/opt/ant
fi

if [ -d "${HOME}/opt/ant" ] ; then
if [ -d "${HOME}/opt/ant" ] ; then
ANT_HOME="${HOME}/opt/ant"
fi

@@ -52,17 +53,16 @@ if [ -z "$ANT_HOME" ] ; then
ls=`ls -ld "$PRG"`
link=`expr "$ls" : '.*-> \(.*\)$'`
if expr "$link" : '/.*' > /dev/null; then
PRG="$link"
PRG="$link"
else
PRG=`dirname "$PRG"`"/$link"
PRG=`dirname "$PRG"`"/$link"
fi
done
ANT_HOME=`dirname "$PRG"`/..

# make it fully qualified
ANT_HOME=`cd "$ANT_HOME" && pwd`

fi

# For Cygwin, ensure paths are in UNIX format before anything is touched
@@ -78,9 +78,9 @@ fi
# set ANT_LIB location
ANT_LIB="${ANT_HOME}/lib"

if [ -z "$JAVACMD" ] ; then
if [ -z "$JAVACMD" ] ; then
if [ -n "$JAVA_HOME" ] ; then
if [ -x "$JAVA_HOME/jre/sh/java" ] ; then
if [ -x "$JAVA_HOME/jre/sh/java" ] ; then
# IBM's JDK on AIX uses strange locations for the executables
JAVACMD="$JAVA_HOME/jre/sh/java"
else
@@ -88,12 +88,12 @@ if [ -z "$JAVACMD" ] ; then
fi
else
JAVACMD=`which java 2> /dev/null `
if [ -z "$JAVACMD" ] ; then
if [ -z "$JAVACMD" ] ; then
JAVACMD=java
fi
fi
fi
if [ ! -x "$JAVACMD" ] ; then
echo "Error: JAVA_HOME is not defined correctly."
echo " We cannot execute $JAVACMD"
@@ -104,10 +104,10 @@ if [ -n "$CLASSPATH" ] ; then
LOCALCLASSPATH="$CLASSPATH"
fi

# in rpm_mode get ant/optional/xml parser&api from JAVALIBDIR
# in rpm_mode get ant/optional/xml parser&api from JAVALIBDIR
if $rpm_mode; then
JAVALIBDIR=/usr/share/java
for i in ant ant-optional jaxp_parser xml_apis
for i in ant ant-optional jaxp_parser xml_apis
do
if [ -z "$LOCALCLASSPATH" ] ; then
LOCALCLASSPATH="$JAVALIBDIR/$i.jar"
@@ -120,29 +120,14 @@ if $rpm_mode; then
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"
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"
fi
if [ -z "$LOCALCLASSPATH" ] ; then
LOCALCLASSPATH=$ANT_LIB/ant-launcher.jar
else
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 @@ fi

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

Loading…
Cancel
Save