tomcat-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Conor MacNeill" <co...@m64.com>
Subject [PATCH] eliminate duplicated code in ant Jar and Zip taskdefs
Date Wed, 29 Dec 1999 05:00:47 GMT
Hi,

I submitted this patch before but there was no response. I think its
worthwhile so I'd like to try it again (in the right format :-).

This patch eliminates the duplicated code in Jar.java and Zip.java taskdefs
by making Jar.java extend Zip.java. A desirable result of this is the
ability to specify an "items" value of "*" for Jar tasks indicating
inclusion into the jar file of all files/dirs in the basedir directory. I
think this will be important for building WAR files.

Cheers
Conor

Index: Jar.java
===================================================================
RCS file:
/home/cvspublic/jakarta-tools/ant/src/main/org/apache/tools/ant/taskdefs/Jar
.java,v
retrieving revision 1.5
diff -u -r1.5 Jar.java
--- Jar.java	1999/11/24 03:02:17	1.5
+++ Jar.java	1999/12/29 04:54:29
@@ -57,9 +57,6 @@
 import org.apache.tools.ant.*;

 import java.io.*;
-import java.util.Enumeration;
-import java.util.StringTokenizer;
-import java.util.Vector;
 import java.util.zip.*;

 /**
@@ -68,155 +65,49 @@
  * @author duncan@x180.com
  */

-public class Jar extends Task {
+public class Jar extends Zip {

-    private File jarFile;
-    private File baseDir;
-    private Vector items = new Vector();
     private File manifest;
-    private Vector ignoreList = new Vector();

     public void setJarfile(String jarFilename) {
-	jarFile = project.resolveFile(jarFilename);
+	setZipfile(jarFilename);
     }

-    public void setBasedir(String baseDirname) {
-	baseDir = project.resolveFile(baseDirname);
-    }
-
-    public void setItems(String itemString) {
-	StringTokenizer tok = new StringTokenizer(itemString, ",", false);
-	while (tok.hasMoreTokens()) {
-	    items.addElement(tok.nextToken().trim());
-	}
-    }
-    /**
-        List of filenames and directory names to not
-        include in the final .jar file. They should be either
-        , or " " (space) separated.
-        <p>
-        For example:
-        <p>
-        ignore="package.html, foo.class"
-        <p>
-        The ignored files will be logged.
-
-        @author Jon S. Stevens <a
href="mailto:jon@clearink.com">jon@clearink.com</a>
-    */
-    public void setIgnore(String ignoreString) {
-        ignoreString = ignoreString;
-        if (ignoreString != null && ignoreString.length() > 0) {
-            StringTokenizer tok =
-            new StringTokenizer(ignoreString, ", ", false);
-            while (tok.hasMoreTokens()) {
-                ignoreList.addElement ( tok.nextToken().trim() );
-            }
-        }
-    }
-
     public void setManifest(String manifestFilename) {
 	manifest = project.resolveFile(manifestFilename);
     }

-    public void execute() throws BuildException {
-	project.log("Building jar: " + jarFile.getAbsolutePath());
+    protected void initZipOutputStream(ZipOutputStream zOut)
+	throws IOException, BuildException
+    {
+	zOut.setMethod(ZipOutputStream.DEFLATED);

-	try {
-	    FileOutputStream out = new FileOutputStream(jarFile);
-	    ZipOutputStream zOut = new ZipOutputStream(out);
-	    zOut.setMethod(ZipOutputStream.DEFLATED);
-
-	    // add manifest first
-	    if (manifest != null) {
-		ZipEntry ze = new ZipEntry("META-INF/");
-		zOut.putNextEntry(ze);
-		jarFile(manifest, zOut, "META-INF/MANIFEST.MF");
-	    } else {
-		ZipEntry ze = new ZipEntry("META-INF/");
-		zOut.putNextEntry(ze);
-		String s = "/org/apache/tools/ant/defaultManifest.mf";
-		InputStream in = this.getClass().getResourceAsStream(s);
-        if ( in == null )
-            throw new BuildException ( "Could not find: " + s );
-		jarFile(in, zOut, "META-INF/MANIFEST.MF");
-	    }
-
-	    // add items
-
-	    Enumeration e = items.elements();
-	    while (e.hasMoreElements()) {
-            String s = (String)e.nextElement();
-            // check to make sure item is not in ignore list
-            // shouldn't be ignored here, but just want to make sure
-            if (! ignoreList.contains(s)) {
-                File f = new File(baseDir, s);
-                if (f.isDirectory()) {
-                    jarDir(f, zOut, s + "/");
-                } else {
-                    jarFile(f, zOut, s);
-                }
-            } else {
-                project.log("Ignored: " + s);
-            }
-	    }
-
-	    // close up
-
-	    zOut.close();
-	    out.close();
-	} catch (IOException ioe) {
-	    String msg = "Problem creating jar " + ioe.getMessage();
-	    throw new BuildException(msg);
+	// add manifest first
+	if (manifest != null) {
+	    ZipEntry ze = new ZipEntry("META-INF/");
+	    zOut.putNextEntry(ze);
+	    zipFile(manifest, zOut, "META-INF/MANIFEST.MF");
+	} else {
+	    ZipEntry ze = new ZipEntry("META-INF/");
+	    zOut.putNextEntry(ze);
+	    String s = "/org/apache/tools/ant/defaultManifest.mf";
+	    InputStream in = this.getClass().getResourceAsStream(s);
+            if ( in == null )
+		throw new BuildException ( "Could not find: " + s );
+	    zipFile(in, zOut, "META-INF/MANIFEST.MF");
 	}
     }

-    private void jarDir(File dir, ZipOutputStream zOut, String vPath)
-	throws IOException
+    protected void zipDir(File dir, ZipOutputStream zOut, String vPath)
+        throws IOException
     {
 	// First add directory to zip entry
 	if( ! "META-INF/".equals(vPath) ) {
 	    // we already added a META-INF
 	    ZipEntry ze = new ZipEntry(vPath);
 	    zOut.putNextEntry(ze);
-	}
-
-	String[] list = dir.list();
-	for (int i = 0; i < list.length; i++) {
-	    String f = list[i];
-        // check to make sure item is not in ignore list
-        if (! ignoreList.contains(f)) {
-            File file = new File(dir, f);
-            if (file.isDirectory()) {
-                jarDir(file, zOut, vPath + f + "/");
-            } else {
-                jarFile(file, zOut, vPath + f);
-            }
-        } else {
-            project.log("Ignored: " + f);
-        }
 	}
-    }
-
-    private void jarFile(InputStream in, ZipOutputStream zOut, String
vPath)
-	throws IOException
-    {
-	ZipEntry ze = new ZipEntry(vPath);
-	zOut.putNextEntry(ze);
-
-	byte[] buffer = new byte[8 * 1024];
-	int count = 0;
-	do {
-	    zOut.write(buffer, 0, count);
-	    count = in.read(buffer, 0, buffer.length);
-	} while (count != -1);
-    }
-
-    private void jarFile(File file, ZipOutputStream zOut, String vPath)
-        throws IOException
-    {
-	FileInputStream fIn = new FileInputStream(file);
-	jarFile(fIn, zOut, vPath);
-	fIn.close();
+	super.zipDir(dir, zOut, vPath);
     }
 }

Index: Zip.java
===================================================================
RCS file:
/home/cvspublic/jakarta-tools/ant/src/main/org/apache/tools/ant/taskdefs/Zip
.java,v
retrieving revision 1.3
diff -u -r1.3 Zip.java
--- Zip.java	1999/11/30 22:26:50	1.3
+++ Zip.java	1999/12/29 04:54:30
@@ -144,7 +144,7 @@

         try {
             ZipOutputStream zOut = new ZipOutputStream(new
FileOutputStream(zipFile));
-            zOut.setMethod(ZipOutputStream.DEFLATED);
+	    initZipOutputStream(zOut);

             if ( allItems ) {
                 String[] lst = baseDir.list();
@@ -179,7 +179,13 @@
         }
     }

-    private void zipDir(File dir, ZipOutputStream zOut, String vPath)
+    protected void initZipOutputStream(ZipOutputStream zOut)
+	throws IOException, BuildException
+    {
+	zOut.setMethod(ZipOutputStream.DEFLATED);
+    }
+
+    protected void zipDir(File dir, ZipOutputStream zOut, String vPath)
         throws IOException
     {
         String[] list = dir.list();
@@ -199,7 +205,7 @@
         }
     }

-    private void zipFile(InputStream in, ZipOutputStream zOut, String
vPath)
+    protected void zipFile(InputStream in, ZipOutputStream zOut, String
vPath)
         throws IOException
     {
         ZipEntry ze = new ZipEntry(vPath);
@@ -213,7 +219,7 @@
         } while (count != -1);
     }

-    private void zipFile(File file, ZipOutputStream zOut, String vPath)
+    protected void zipFile(File file, ZipOutputStream zOut, String vPath)
         throws IOException
     {
         FileInputStream fIn = new FileInputStream(file);


Mime
View raw message