@@ -57,17 +57,22 @@ package org.apache.tools.ant.taskdefs;
import org.apache.tools.ant.*;
import org.apache.tools.ant.*;
import org.apache.tools.ant.types.*;
import org.apache.tools.ant.types.*;
import java.util.*;
import java.io.*;
/**
/**
* Base class for Taskdef and Typedef - does all the classpath
* Base class for Taskdef and Typedef - does all the classpath
* handling and and class loading.
* handling and and class loading.
*
*
* @author costin@dnt.ro
* @author Costin Manolache
* @author <a href="stefan.bodewig@epost.de">Stefan Bodewig</a>
* @author <a href="stefan.bodewig@epost.de">Stefan Bodewig</a>
*/
*/
public abstract class Definer extends Task {
public abstract class Definer extends Task {
private String name;
private String name;
private String value;
private String value;
private Path classpath;
private Path classpath;
private File file;
private String resource;
public void setClasspath(Path classpath) {
public void setClasspath(Path classpath) {
if (this.classpath == null) {
if (this.classpath == null) {
@@ -89,24 +94,73 @@ public abstract class Definer extends Task {
}
}
public void execute() throws BuildException {
public void execute() throws BuildException {
if (name==null || value==null ) {
String msg = "name or classname attributes of "
+ getTaskName() + " element "
+ "are undefined";
throw new BuildException(msg);
}
try {
AntClassLoader al = null;
if (classpath != null) {
al = new AntClassLoader(project, classpath);
} else {
al = new AntClassLoader(project, Path.systemClasspath);
AntClassLoader al=createLoader();
if (file==null && resource==null ) {
// simple case - one definition
if ( name==null || value==null ) {
String msg = "name or classname attributes of "
+ getTaskName() + " element "
+ "are undefined";
throw new BuildException(msg);
}
}
// need to load Task via system classloader or the new
// task we want to define will never be a Task but always
// be wrapped into a TaskAdapter.
al.addSystemPackageRoot("org.apache.tools.ant");
addDefinition( al, name, value );
} else {
try {
if (name != null || value != null) {
String msg = "You must not specify name or value "
+ "together with file or resource.";
throw new BuildException(msg, location);
}
if (file != null && resource != null) {
String msg = "You must not specify both, file and resource.";
throw new BuildException(msg, location);
}
Properties props=new Properties();
InputStream is=null;
if( file != null ) {
log("Loading definitions from file " + file,
Project.MSG_VERBOSE);
is=new FileInputStream( file );
if (is == null) {
log("Could not load definitions from file " + file
+ ". It doesn\'t exist.", Project.MSG_WARN);
}
}
if( resource!=null ) {
log("Loading definitions from resource " + resource,
Project.MSG_VERBOSE);
is=al.getResourceAsStream( resource );
if (is == null) {
log("Could not load definitions from resource "
+ resource + ". It could not be found.",
Project.MSG_WARN);
}
}
if( is!=null ) {
props.load( is );
Enumeration keys=props.keys();
while( keys.hasMoreElements() ) {
String n=(String)keys.nextElement();
String v=props.getProperty( n );
addDefinition( al, n, v );
}
}
} catch( IOException ex ) {
throw new BuildException(ex, location);
}
}
}
private void addDefinition( ClassLoader al, String name, String value ) {
try {
Class c = al.loadClass(value);
Class c = al.loadClass(value);
AntClassLoader.initializeClass(c);
AntClassLoader.initializeClass(c);
addDefinition(name, c);
addDefinition(name, c);
@@ -120,7 +174,30 @@ public abstract class Definer extends Task {
throw new BuildException(msg, ncdfe, location);
throw new BuildException(msg, ncdfe, location);
}
}
}
}
private AntClassLoader createLoader() {
AntClassLoader al = null;
if (classpath != null) {
al = new AntClassLoader(project, classpath);
} else {
al = new AntClassLoader(project, Path.systemClasspath);
}
// need to load Task via system classloader or the new
// task we want to define will never be a Task but always
// be wrapped into a TaskAdapter.
al.addSystemPackageRoot("org.apache.tools.ant");
return al;
}
public void setFile( File file ) {
this.file=file;
}
public void setResource( String res ) {
this.resource=res;
}
public void setName( String name) {
public void setName( String name) {
this.name = name;
this.name = name;
}
}