+
+ public static class AntTask extends org.apache.tools.ant.Task {
+ private org.apache.tools.ant.types.FileSet jars, code;
+ private File props, outfile;
+ private String appname;
+
+ private static File[] getfiles(org.apache.tools.ant.types.FileSet fs) {
+ org.apache.tools.ant.DirectoryScanner ds = fs.getDirectoryScanner();
+ ds.scan();
+ String[] nms = ds.getIncludedFiles();
+ File[] ret = new File[nms.length];
+ for(int i = 0; i < nms.length; i++)
+ ret[i] = new File(ds.getBasedir(), nms[i]);
+ return(ret);
+ }
+
+ private void rebuild(File[] jars, File[] code) throws IOException {
+ OutputStream out = new FileOutputStream(outfile);
+
+ System.out.println("Building " + outfile);
+
+ try {
+ Archive ar = new Archive(out);
+ if(appname != null)
+ ar.putprop("jsvc.j2ee.appname", appname);
+ if(props != null) {
+ InputStream in = new FileInputStream(props);
+ try {
+ ar.loadprops(in);
+ } finally {
+ in.close();
+ }
+ }
+
+ for(File f : code) {
+ InputStream in = new FileInputStream(f);
+ try {
+ ar.addcode(f.getName(), in);
+ } finally {
+ in.close();
+ }
+ }
+
+ ar.addjars(jars);
+ ar.writewebxml();
+
+ ar.finish();
+ } catch(MissingPropException e) {
+ throw(new org.apache.tools.ant.BuildException(e.getMessage(), e));
+ } finally {
+ out.close();
+ }
+ }
+
+ public void execute() {
+ File[] jars = (this.jars != null)?getfiles(this.jars):new File[0];
+ File[] code = (this.code != null)?getfiles(this.code):new File[0];
+ if(jars.length < 1)
+ throw(new org.apache.tools.ant.BuildException("Must have at least one JAR file", getLocation()));
+ if(outfile == null)
+ throw(new org.apache.tools.ant.BuildException("No output file specified", getLocation()));
+
+ Collection<File> deps = new LinkedList<File>();
+ deps.addAll(Arrays.asList(jars));
+ deps.addAll(Arrays.asList(code));
+ if(props != null)
+ deps.add(props);
+
+ boolean rebuild = false;
+ for(File dep : deps) {
+ if(!dep.exists())
+ throw(new org.apache.tools.ant.BuildException(dep + " does not exist", getLocation()));
+ if(dep.lastModified() > outfile.lastModified()) {
+ rebuild = true;
+ break;
+ }
+ }
+
+ if(rebuild) {
+ try {
+ rebuild(jars, code);
+ } catch(IOException e) {
+ throw(new org.apache.tools.ant.BuildException(e));
+ }
+ }
+ }
+
+ public void addJars(org.apache.tools.ant.types.FileSet path) {
+ this.jars = path;
+ }
+
+ public void setJars(org.apache.tools.ant.types.FileSet path) {
+ this.jars = path;
+ }
+
+ public void setCode(org.apache.tools.ant.types.FileSet path) {
+ this.code = path;
+ }
+
+ public void setPropfile(File propfile) {
+ this.props = propfile;
+ }
+
+ public void setDestfile(File outfile) {
+ this.outfile = outfile;
+ }
+
+ public void setAppname(String name) {
+ this.appname = name;
+ }
+ }