git-svn-id: https://svn.apache.org/repos/asf/ant/core/trunk@270740 13f79535-47bb-0310-9956-ffa450edef68master
@@ -21,30 +21,27 @@ import org.apache.tools.zip.ZipOutputStream; | |||||
*/ | */ | ||||
public class Ear extends Jar | public class Ear extends Jar | ||||
{ | { | ||||
private File deploymentDescriptor; | |||||
private boolean descriptorAdded; | |||||
private File m_appxml; | |||||
private boolean m_descriptorAdded; | |||||
public Ear() | public Ear() | ||||
{ | { | ||||
super(); | |||||
archiveType = "ear"; | |||||
emptyBehavior = "create"; | |||||
m_archiveType = "ear"; | |||||
m_emptyBehavior = "create"; | |||||
} | } | ||||
public void setAppxml( File descr ) | |||||
public void setAppxml( final File appxml ) | |||||
throws TaskException | throws TaskException | ||||
{ | { | ||||
deploymentDescriptor = descr; | |||||
if( !deploymentDescriptor.exists() ) | |||||
throw new TaskException( "Deployment descriptor: " + deploymentDescriptor + " does not exist." ); | |||||
m_appxml = appxml; | |||||
if( !m_appxml.exists() ) | |||||
{ | |||||
final String message = "Deployment descriptor: " + | |||||
m_appxml + " does not exist."; | |||||
throw new TaskException( message ); | |||||
} | |||||
// Create a ZipFileSet for this file, and pass it up. | |||||
ZipFileSet fs = new ZipFileSet(); | |||||
fs.setDir( new File( deploymentDescriptor.getParent() ) ); | |||||
fs.setIncludes( deploymentDescriptor.getName() ); | |||||
fs.setFullpath( "META-INF/application.xml" ); | |||||
super.addFileset( fs ); | |||||
addFileAs( m_appxml, "META-INF/application.xml" ); | |||||
} | } | ||||
public void addArchives( ZipFileSet fs ) | public void addArchives( ZipFileSet fs ) | ||||
@@ -56,23 +53,13 @@ public class Ear extends Jar | |||||
super.addFileset( fs ); | super.addFileset( fs ); | ||||
} | } | ||||
/** | |||||
* Make sure we don't think we already have a web.xml next time this task | |||||
* gets executed. | |||||
*/ | |||||
protected void cleanUp() | |||||
{ | |||||
descriptorAdded = false; | |||||
super.cleanUp(); | |||||
} | |||||
protected void initZipOutputStream( ZipOutputStream zOut ) | |||||
protected void initZipOutputStream( final ZipOutputStream zOut ) | |||||
throws IOException, TaskException | throws IOException, TaskException | ||||
{ | { | ||||
// If no webxml file is specified, it's an error. | |||||
if( deploymentDescriptor == null && !isInUpdateMode() ) | |||||
if( m_appxml == null && !isInUpdateMode() ) | |||||
{ | { | ||||
throw new TaskException( "appxml attribute is required" ); | |||||
final String message = "appxml attribute is required"; | |||||
throw new TaskException( message ); | |||||
} | } | ||||
super.initZipOutputStream( zOut ); | super.initZipOutputStream( zOut ); | ||||
@@ -87,17 +74,19 @@ public class Ear extends Jar | |||||
// a <fileset> element. | // a <fileset> element. | ||||
if( vPath.equalsIgnoreCase( "META-INF/aplication.xml" ) ) | if( vPath.equalsIgnoreCase( "META-INF/aplication.xml" ) ) | ||||
{ | { | ||||
if( deploymentDescriptor == null || !deploymentDescriptor.equals( file ) || descriptorAdded ) | |||||
if( m_appxml == null || | |||||
!m_appxml.equals( file ) || | |||||
m_descriptorAdded ) | |||||
{ | { | ||||
final String message = "Warning: selected " + archiveType + | |||||
final String message = "Warning: selected " + m_archiveType + | |||||
" files include a META-INF/application.xml which will be ignored " + | " files include a META-INF/application.xml which will be ignored " + | ||||
"(please use appxml attribute to " + archiveType + " task)"; | |||||
"(please use appxml attribute to " + m_archiveType + " task)"; | |||||
getLogger().warn( message ); | getLogger().warn( message ); | ||||
} | } | ||||
else | else | ||||
{ | { | ||||
super.zipFile( file, zOut, vPath ); | super.zipFile( file, zOut, vPath ); | ||||
descriptorAdded = true; | |||||
m_descriptorAdded = true; | |||||
} | } | ||||
} | } | ||||
else | else | ||||
@@ -29,7 +29,8 @@ import org.apache.tools.zip.ZipOutputStream; | |||||
* | * | ||||
* @author James Davidson <a href="mailto:duncan@x180.com">duncan@x180.com</a> | * @author James Davidson <a href="mailto:duncan@x180.com">duncan@x180.com</a> | ||||
*/ | */ | ||||
public class Jar extends Zip | |||||
public class Jar | |||||
extends Zip | |||||
{ | { | ||||
/** | /** | ||||
* The index file name. | * The index file name. | ||||
@@ -39,16 +40,15 @@ public class Jar extends Zip | |||||
/** | /** | ||||
* true if a manifest has been specified in the task | * true if a manifest has been specified in the task | ||||
*/ | */ | ||||
private boolean buildFileManifest = false; | |||||
private boolean buildFileManifest; | |||||
/** | /** | ||||
* jar index is JDK 1.3+ only | * jar index is JDK 1.3+ only | ||||
*/ | */ | ||||
private boolean index = false; | |||||
private Manifest execManifest; | |||||
private Manifest manifest; | |||||
private File manifestFile; | |||||
private boolean m_index; | |||||
private Manifest m_execManifest; | |||||
private Manifest m_manifest; | |||||
private File m_manifestFile; | |||||
/** | /** | ||||
* constructor | * constructor | ||||
@@ -56,8 +56,8 @@ public class Jar extends Zip | |||||
public Jar() | public Jar() | ||||
{ | { | ||||
super(); | super(); | ||||
archiveType = "jar"; | |||||
emptyBehavior = "create"; | |||||
m_archiveType = "jar"; | |||||
m_emptyBehavior = "create"; | |||||
setEncoding( "UTF8" ); | setEncoding( "UTF8" ); | ||||
} | } | ||||
@@ -69,7 +69,7 @@ public class Jar extends Zip | |||||
*/ | */ | ||||
public void setIndex( boolean flag ) | public void setIndex( boolean flag ) | ||||
{ | { | ||||
index = flag; | |||||
m_index = flag; | |||||
} | } | ||||
public void setManifest( File manifestFile ) | public void setManifest( File manifestFile ) | ||||
@@ -77,30 +77,33 @@ public class Jar extends Zip | |||||
{ | { | ||||
if( !manifestFile.exists() ) | if( !manifestFile.exists() ) | ||||
{ | { | ||||
throw new TaskException( "Manifest file: " + manifestFile + " does not exist." ); | |||||
final String message = "Manifest file: " + manifestFile + " does not exist."; | |||||
throw new TaskException( message ); | |||||
} | } | ||||
this.manifestFile = manifestFile; | |||||
this.m_manifestFile = manifestFile; | |||||
Reader r = null; | Reader r = null; | ||||
try | try | ||||
{ | { | ||||
r = new FileReader( manifestFile ); | r = new FileReader( manifestFile ); | ||||
Manifest newManifest = new Manifest( r ); | Manifest newManifest = new Manifest( r ); | ||||
if( manifest == null ) | |||||
if( m_manifest == null ) | |||||
{ | { | ||||
manifest = Manifest.getDefaultManifest(); | |||||
m_manifest = Manifest.getDefaultManifest(); | |||||
} | } | ||||
manifest.merge( newManifest ); | |||||
m_manifest.merge( newManifest ); | |||||
} | } | ||||
catch( ManifestException e ) | catch( ManifestException e ) | ||||
{ | { | ||||
getLogger().error( "Manifest is invalid: " + e.getMessage() ); | |||||
throw new TaskException( "Invalid Manifest: " + manifestFile, e ); | |||||
final String message = "Manifest " + manifestFile + " is invalid: " + e.getMessage(); | |||||
getLogger().error( message ); | |||||
throw new TaskException( message, e ); | |||||
} | } | ||||
catch( IOException e ) | catch( IOException e ) | ||||
{ | { | ||||
throw new TaskException( "Unable to read manifest file: " + manifestFile, e ); | |||||
final String message = "Unable to read manifest file: " + manifestFile; | |||||
throw new TaskException( message, e ); | |||||
} | } | ||||
finally | finally | ||||
{ | { | ||||
@@ -127,11 +130,11 @@ public class Jar extends Zip | |||||
public void addConfiguredManifest( Manifest newManifest ) | public void addConfiguredManifest( Manifest newManifest ) | ||||
throws ManifestException, TaskException | throws ManifestException, TaskException | ||||
{ | { | ||||
if( manifest == null ) | |||||
if( m_manifest == null ) | |||||
{ | { | ||||
manifest = Manifest.getDefaultManifest(); | |||||
m_manifest = Manifest.getDefaultManifest(); | |||||
} | } | ||||
manifest.merge( newManifest ); | |||||
m_manifest.merge( newManifest ); | |||||
buildFileManifest = true; | buildFileManifest = true; | ||||
} | } | ||||
@@ -155,7 +158,7 @@ public class Jar extends Zip | |||||
throws TaskException | throws TaskException | ||||
{ | { | ||||
// need to handle manifest as a special check | // need to handle manifest as a special check | ||||
if( buildFileManifest || manifestFile == null ) | |||||
if( buildFileManifest || m_manifestFile == null ) | |||||
{ | { | ||||
java.util.zip.ZipFile theZipFile = null; | java.util.zip.ZipFile theZipFile = null; | ||||
try | try | ||||
@@ -168,11 +171,11 @@ public class Jar extends Zip | |||||
return false; | return false; | ||||
} | } | ||||
Manifest currentManifest = new Manifest( new InputStreamReader( theZipFile.getInputStream( entry ) ) ); | Manifest currentManifest = new Manifest( new InputStreamReader( theZipFile.getInputStream( entry ) ) ); | ||||
if( manifest == null ) | |||||
if( m_manifest == null ) | |||||
{ | { | ||||
manifest = Manifest.getDefaultManifest(); | |||||
m_manifest = Manifest.getDefaultManifest(); | |||||
} | } | ||||
if( !currentManifest.equals( manifest ) ) | |||||
if( !currentManifest.equals( m_manifest ) ) | |||||
{ | { | ||||
getLogger().debug( "Updating jar since jar manifest has changed" ); | getLogger().debug( "Updating jar since jar manifest has changed" ); | ||||
return false; | return false; | ||||
@@ -199,22 +202,13 @@ public class Jar extends Zip | |||||
} | } | ||||
} | } | ||||
} | } | ||||
else if( manifestFile.lastModified() > zipFile.lastModified() ) | |||||
else if( m_manifestFile.lastModified() > zipFile.lastModified() ) | |||||
{ | { | ||||
return false; | return false; | ||||
} | } | ||||
return super.isUpToDate( scanners, zipFile ); | return super.isUpToDate( scanners, zipFile ); | ||||
} | } | ||||
/** | |||||
* Make sure we don't think we already have a MANIFEST next time this task | |||||
* gets executed. | |||||
*/ | |||||
protected void cleanUp() | |||||
{ | |||||
super.cleanUp(); | |||||
} | |||||
protected boolean createEmptyZip( File zipFile ) | protected boolean createEmptyZip( File zipFile ) | ||||
{ | { | ||||
// Jar files always contain a manifest and can never be empty | // Jar files always contain a manifest and can never be empty | ||||
@@ -224,7 +218,7 @@ public class Jar extends Zip | |||||
protected void finalizeZipOutputStream( ZipOutputStream zOut ) | protected void finalizeZipOutputStream( ZipOutputStream zOut ) | ||||
throws IOException, TaskException | throws IOException, TaskException | ||||
{ | { | ||||
if( index ) | |||||
if( m_index ) | |||||
{ | { | ||||
createIndexList( zOut ); | createIndexList( zOut ); | ||||
} | } | ||||
@@ -235,13 +229,13 @@ public class Jar extends Zip | |||||
{ | { | ||||
try | try | ||||
{ | { | ||||
execManifest = Manifest.getDefaultManifest(); | |||||
m_execManifest = Manifest.getDefaultManifest(); | |||||
if( manifest != null ) | |||||
if( m_manifest != null ) | |||||
{ | { | ||||
execManifest.merge( manifest ); | |||||
m_execManifest.merge( m_manifest ); | |||||
} | } | ||||
for( Iterator e = execManifest.getWarnings(); e.hasNext(); ) | |||||
for( Iterator e = m_execManifest.getWarnings(); e.hasNext(); ) | |||||
{ | { | ||||
getLogger().warn( "Manifest warning: " + (String)e.next() ); | getLogger().warn( "Manifest warning: " + (String)e.next() ); | ||||
} | } | ||||
@@ -250,7 +244,7 @@ public class Jar extends Zip | |||||
// time to write the manifest | // time to write the manifest | ||||
ByteArrayOutputStream baos = new ByteArrayOutputStream(); | ByteArrayOutputStream baos = new ByteArrayOutputStream(); | ||||
PrintWriter writer = new PrintWriter( baos ); | PrintWriter writer = new PrintWriter( baos ); | ||||
execManifest.write( writer ); | |||||
m_execManifest.write( writer ); | |||||
writer.flush(); | writer.flush(); | ||||
ByteArrayInputStream bais = new ByteArrayInputStream( baos.toByteArray() ); | ByteArrayInputStream bais = new ByteArrayInputStream( baos.toByteArray() ); | ||||
@@ -273,9 +267,9 @@ public class Jar extends Zip | |||||
// a <fileset> element. | // a <fileset> element. | ||||
if( vPath.equalsIgnoreCase( "META-INF/MANIFEST.MF" ) ) | if( vPath.equalsIgnoreCase( "META-INF/MANIFEST.MF" ) ) | ||||
{ | { | ||||
final String message = "Warning: selected " + archiveType + | |||||
final String message = "Warning: selected " + m_archiveType + | |||||
" files include a META-INF/MANIFEST.MF which will be ignored " + | " files include a META-INF/MANIFEST.MF which will be ignored " + | ||||
"(please use manifest attribute to " + archiveType + " task)"; | |||||
"(please use manifest attribute to " + m_archiveType + " task)"; | |||||
getLogger().warn( message ); | getLogger().warn( message ); | ||||
} | } | ||||
else | else | ||||
@@ -328,12 +322,12 @@ public class Jar extends Zip | |||||
writer.println(); | writer.println(); | ||||
// header newline | // header newline | ||||
writer.println( zipFile.getName() ); | |||||
writer.println( m_file.getName() ); | |||||
// JarIndex is sorting the directories by ascending order. | // JarIndex is sorting the directories by ascending order. | ||||
// it's painful to do in JDK 1.1 and it has no value but cosmetic | // it's painful to do in JDK 1.1 and it has no value but cosmetic | ||||
// since it will be read into a hashtable by the classloader. | // since it will be read into a hashtable by the classloader. | ||||
Enumeration enum = addedDirs.keys(); | |||||
Enumeration enum = m_addedDirs.keys(); | |||||
while( enum.hasMoreElements() ) | while( enum.hasMoreElements() ) | ||||
{ | { | ||||
String dir = (String)enum.nextElement(); | String dir = (String)enum.nextElement(); | ||||
@@ -377,13 +371,13 @@ public class Jar extends Zip | |||||
{ | { | ||||
try | try | ||||
{ | { | ||||
if( execManifest == null ) | |||||
if( m_execManifest == null ) | |||||
{ | { | ||||
execManifest = new Manifest( new InputStreamReader( is ) ); | |||||
m_execManifest = new Manifest( new InputStreamReader( is ) ); | |||||
} | } | ||||
else if( isAddingNewFiles() ) | else if( isAddingNewFiles() ) | ||||
{ | { | ||||
execManifest.merge( new Manifest( new InputStreamReader( is ) ) ); | |||||
m_execManifest.merge( new Manifest( new InputStreamReader( is ) ) ); | |||||
} | } | ||||
} | } | ||||
catch( ManifestException e ) | catch( ManifestException e ) | ||||
@@ -18,70 +18,59 @@ import org.apache.tools.zip.ZipOutputStream; | |||||
* | * | ||||
* @author <a href="mailto:stefan.bodewig@epost.de">Stefan Bodewig</a> | * @author <a href="mailto:stefan.bodewig@epost.de">Stefan Bodewig</a> | ||||
*/ | */ | ||||
public class War extends Jar | |||||
public class War | |||||
extends Jar | |||||
{ | { | ||||
private File deploymentDescriptor; | |||||
private boolean descriptorAdded; | |||||
private File m_webxml; | |||||
private boolean m_descriptorAdded; | |||||
public War() | public War() | ||||
{ | { | ||||
super(); | super(); | ||||
archiveType = "war"; | |||||
emptyBehavior = "create"; | |||||
m_archiveType = "war"; | |||||
m_emptyBehavior = "create"; | |||||
} | } | ||||
public void setWebxml( File descr ) | |||||
public void setWebxml( final File descr ) | |||||
throws TaskException | throws TaskException | ||||
{ | { | ||||
deploymentDescriptor = descr; | |||||
if( !deploymentDescriptor.exists() ) | |||||
throw new TaskException( "Deployment descriptor: " + deploymentDescriptor + " does not exist." ); | |||||
m_webxml = descr; | |||||
if( !m_webxml.exists() ) | |||||
{ | |||||
final String message = "Deployment descriptor: " + | |||||
m_webxml + " does not exist."; | |||||
throw new TaskException( message ); | |||||
} | |||||
// Create a ZipFileSet for this file, and pass it up. | |||||
ZipFileSet fs = new ZipFileSet(); | |||||
fs.setDir( new File( deploymentDescriptor.getParent() ) ); | |||||
fs.setIncludes( deploymentDescriptor.getName() ); | |||||
fs.setFullpath( "WEB-INF/web.xml" ); | |||||
super.addFileset( fs ); | |||||
addFileAs(descr, "WEB-INF/web.xml" ); | |||||
} | } | ||||
public void addClasses( ZipFileSet fs ) | |||||
public void addClasses( final ZipFileSet fs ) | |||||
{ | { | ||||
// We just set the prefix for this fileset, and pass it up. | // We just set the prefix for this fileset, and pass it up. | ||||
fs.setPrefix( "WEB-INF/classes/" ); | fs.setPrefix( "WEB-INF/classes/" ); | ||||
super.addFileset( fs ); | super.addFileset( fs ); | ||||
} | } | ||||
public void addLib( ZipFileSet fs ) | |||||
public void addLib( final ZipFileSet fs ) | |||||
{ | { | ||||
// We just set the prefix for this fileset, and pass it up. | // We just set the prefix for this fileset, and pass it up. | ||||
fs.setPrefix( "WEB-INF/lib/" ); | fs.setPrefix( "WEB-INF/lib/" ); | ||||
super.addFileset( fs ); | super.addFileset( fs ); | ||||
} | } | ||||
public void addWebinf( ZipFileSet fs ) | |||||
public void addWebinf( final ZipFileSet fs ) | |||||
{ | { | ||||
// We just set the prefix for this fileset, and pass it up. | // We just set the prefix for this fileset, and pass it up. | ||||
fs.setPrefix( "WEB-INF/" ); | fs.setPrefix( "WEB-INF/" ); | ||||
super.addFileset( fs ); | super.addFileset( fs ); | ||||
} | } | ||||
/** | |||||
* Make sure we don't think we already have a web.xml next time this task | |||||
* gets executed. | |||||
*/ | |||||
protected void cleanUp() | |||||
{ | |||||
descriptorAdded = false; | |||||
super.cleanUp(); | |||||
} | |||||
protected void initZipOutputStream( ZipOutputStream zOut ) | |||||
protected void initZipOutputStream( final ZipOutputStream zOut ) | |||||
throws IOException, TaskException | throws IOException, TaskException | ||||
{ | { | ||||
// If no webxml file is specified, it's an error. | // If no webxml file is specified, it's an error. | ||||
if( deploymentDescriptor == null && !isInUpdateMode() ) | |||||
if( m_webxml == null && !isInUpdateMode() ) | |||||
{ | { | ||||
throw new TaskException( "webxml attribute is required" ); | throw new TaskException( "webxml attribute is required" ); | ||||
} | } | ||||
@@ -89,7 +78,9 @@ public class War extends Jar | |||||
super.initZipOutputStream( zOut ); | super.initZipOutputStream( zOut ); | ||||
} | } | ||||
protected void zipFile( File file, ZipOutputStream zOut, String vPath ) | |||||
protected void zipFile( final File file, | |||||
final ZipOutputStream zOut, | |||||
final String vPath ) | |||||
throws IOException, TaskException | throws IOException, TaskException | ||||
{ | { | ||||
// If the file being added is WEB-INF/web.xml, we warn if it's not the | // If the file being added is WEB-INF/web.xml, we warn if it's not the | ||||
@@ -98,17 +89,17 @@ public class War extends Jar | |||||
// a <fileset> element. | // a <fileset> element. | ||||
if( vPath.equalsIgnoreCase( "WEB-INF/web.xml" ) ) | if( vPath.equalsIgnoreCase( "WEB-INF/web.xml" ) ) | ||||
{ | { | ||||
if( deploymentDescriptor == null || !deploymentDescriptor.equals( file ) || descriptorAdded ) | |||||
if( m_webxml == null || !m_webxml.equals( file ) || m_descriptorAdded ) | |||||
{ | { | ||||
final String message = "Warning: selected " + archiveType + | |||||
final String message = "Warning: selected " + m_archiveType + | |||||
" files include a WEB-INF/web.xml which will be ignored " + | " files include a WEB-INF/web.xml which will be ignored " + | ||||
"(please use webxml attribute to " + archiveType + " task)"; | |||||
"(please use webxml attribute to " + m_archiveType + " task)"; | |||||
getLogger().warn( message ); | getLogger().warn( message ); | ||||
} | } | ||||
else | else | ||||
{ | { | ||||
super.zipFile( file, zOut, vPath ); | super.zipFile( file, zOut, vPath ); | ||||
descriptorAdded = true; | |||||
m_descriptorAdded = true; | |||||
} | } | ||||
} | } | ||||
else | else | ||||
@@ -0,0 +1,22 @@ | |||||
/* | |||||
* Copyright (C) The Apache Software Foundation. All rights reserved. | |||||
* | |||||
* This software is published under the terms of the Apache Software License | |||||
* version 1.1, a copy of which has been included with this distribution in | |||||
* the LICENSE.txt file. | |||||
*/ | |||||
package org.apache.tools.ant.taskdefs; | |||||
import org.apache.tools.ant.types.EnumeratedAttribute; | |||||
/** | |||||
* Possible behaviors when there are no matching files for the task. | |||||
*/ | |||||
public class WhenEmpty | |||||
extends EnumeratedAttribute | |||||
{ | |||||
public String[] getValues() | |||||
{ | |||||
return new String[]{"fail", "skip", "create"}; | |||||
} | |||||
} |
@@ -23,12 +23,11 @@ import java.util.zip.ZipInputStream; | |||||
import org.apache.myrmidon.api.TaskException; | import org.apache.myrmidon.api.TaskException; | ||||
import org.apache.tools.ant.types.DirectoryScanner; | import org.apache.tools.ant.types.DirectoryScanner; | ||||
import org.apache.tools.ant.types.FileScanner; | import org.apache.tools.ant.types.FileScanner; | ||||
import org.apache.tools.ant.types.EnumeratedAttribute; | |||||
import org.apache.tools.ant.types.FileSet; | import org.apache.tools.ant.types.FileSet; | ||||
import org.apache.tools.ant.types.SourceFileScanner; | |||||
import org.apache.tools.ant.types.ZipFileSet; | import org.apache.tools.ant.types.ZipFileSet; | ||||
import org.apache.tools.ant.types.ZipScanner; | import org.apache.tools.ant.types.ZipScanner; | ||||
import org.apache.tools.ant.util.mappers.MergingMapper; | import org.apache.tools.ant.util.mappers.MergingMapper; | ||||
import org.apache.tools.ant.types.SourceFileScanner; | |||||
import org.apache.tools.zip.ZipEntry; | import org.apache.tools.zip.ZipEntry; | ||||
import org.apache.tools.zip.ZipOutputStream; | import org.apache.tools.zip.ZipOutputStream; | ||||
@@ -39,36 +38,35 @@ import org.apache.tools.zip.ZipOutputStream; | |||||
* @author Jon S. Stevens <a href="mailto:jon@clearink.com">jon@clearink.com</a> | * @author Jon S. Stevens <a href="mailto:jon@clearink.com">jon@clearink.com</a> | ||||
* @author <a href="mailto:stefan.bodewig@epost.de">Stefan Bodewig</a> | * @author <a href="mailto:stefan.bodewig@epost.de">Stefan Bodewig</a> | ||||
*/ | */ | ||||
public class Zip extends MatchingTask | |||||
public class Zip | |||||
extends MatchingTask | |||||
{ | { | ||||
// For directories: | // For directories: | ||||
private final static long EMPTY_CRC = new CRC32().getValue(); | private final static long EMPTY_CRC = new CRC32().getValue(); | ||||
private boolean doCompress = true; | |||||
private boolean doUpdate = false; | |||||
private boolean doFilesonly = false; | |||||
protected String archiveType = "zip"; | |||||
protected String emptyBehavior = "skip"; | |||||
private ArrayList filesets = new ArrayList(); | |||||
protected Hashtable addedDirs = new Hashtable(); | |||||
private ArrayList addedFiles = new ArrayList(); | |||||
protected File zipFile; | |||||
private boolean m_compress = true; | |||||
private boolean m_update; | |||||
private boolean m_filesonly; | |||||
protected String m_archiveType = "zip"; | |||||
protected String m_emptyBehavior = "skip"; | |||||
private ArrayList m_filesets = new ArrayList(); | |||||
protected Hashtable m_addedDirs = new Hashtable(); | |||||
private ArrayList m_addedFiles = new ArrayList(); | |||||
protected File m_file; | |||||
/** | /** | ||||
* true when we are adding new files into the Zip file, as opposed to adding | * true when we are adding new files into the Zip file, as opposed to adding | ||||
* back the unchanged files | * back the unchanged files | ||||
*/ | */ | ||||
private boolean addingNewFiles; | |||||
private File baseDir; | |||||
private boolean m_addingNewFiles; | |||||
private File m_baseDir; | |||||
/** | /** | ||||
* Encoding to use for filenames, defaults to the platform's default | * Encoding to use for filenames, defaults to the platform's default | ||||
* encoding. | * encoding. | ||||
*/ | */ | ||||
private String encoding; | |||||
private String m_encoding; | |||||
protected static String[][] grabFileNames( FileScanner[] scanners ) | |||||
private static String[][] grabFileNames( final FileScanner[] scanners ) | |||||
throws TaskException | throws TaskException | ||||
{ | { | ||||
String[][] result = new String[ scanners.length ][]; | String[][] result = new String[ scanners.length ][]; | ||||
@@ -83,15 +81,17 @@ public class Zip extends MatchingTask | |||||
return result; | return result; | ||||
} | } | ||||
protected static File[] grabFiles( FileScanner[] scanners, | |||||
String[][] fileNames ) | |||||
private static File[] grabFiles( final FileScanner[] scanners, | |||||
final String[][] filenames ) | |||||
{ | { | ||||
ArrayList files = new ArrayList(); | |||||
for( int i = 0; i < fileNames.length; i++ ) | |||||
final ArrayList files = new ArrayList(); | |||||
for( int i = 0; i < filenames.length; i++ ) | |||||
{ | { | ||||
File thisBaseDir = scanners[ i ].getBasedir(); | |||||
for( int j = 0; j < fileNames[ i ].length; j++ ) | |||||
files.add( new File( thisBaseDir, fileNames[ i ][ j ] ) ); | |||||
final File baseDir = scanners[ i ].getBasedir(); | |||||
for( int j = 0; j < filenames[ i ].length; j++ ) | |||||
{ | |||||
files.add( new File( baseDir, filenames[ i ][ j ] ) ); | |||||
} | |||||
} | } | ||||
final File[] toret = new File[ files.size() ]; | final File[] toret = new File[ files.size() ]; | ||||
return (File[])files.toArray( toret ); | return (File[])files.toArray( toret ); | ||||
@@ -102,9 +102,9 @@ public class Zip extends MatchingTask | |||||
* | * | ||||
* @param baseDir The new Basedir value | * @param baseDir The new Basedir value | ||||
*/ | */ | ||||
public void setBasedir( File baseDir ) | |||||
public void setBasedir( final File baseDir ) | |||||
{ | { | ||||
this.baseDir = baseDir; | |||||
m_baseDir = baseDir; | |||||
} | } | ||||
/** | /** | ||||
@@ -112,9 +112,9 @@ public class Zip extends MatchingTask | |||||
* | * | ||||
* @param c The new Compress value | * @param c The new Compress value | ||||
*/ | */ | ||||
public void setCompress( boolean c ) | |||||
public void setCompress( final boolean compress ) | |||||
{ | { | ||||
doCompress = c; | |||||
m_compress = compress; | |||||
} | } | ||||
/** | /** | ||||
@@ -128,9 +128,9 @@ public class Zip extends MatchingTask | |||||
* | * | ||||
* @param encoding The new Encoding value | * @param encoding The new Encoding value | ||||
*/ | */ | ||||
public void setEncoding( String encoding ) | |||||
public void setEncoding( final String encoding ) | |||||
{ | { | ||||
this.encoding = encoding; | |||||
m_encoding = encoding; | |||||
} | } | ||||
/** | /** | ||||
@@ -138,9 +138,9 @@ public class Zip extends MatchingTask | |||||
* | * | ||||
* @param file The new File value | * @param file The new File value | ||||
*/ | */ | ||||
public void setFile( File file ) | |||||
public void setFile( final File file ) | |||||
{ | { | ||||
this.zipFile = file; | |||||
m_file = file; | |||||
} | } | ||||
/** | /** | ||||
@@ -148,9 +148,9 @@ public class Zip extends MatchingTask | |||||
* | * | ||||
* @param f The new Filesonly value | * @param f The new Filesonly value | ||||
*/ | */ | ||||
public void setFilesonly( boolean f ) | |||||
public void setFilesonly( final boolean filesonly ) | |||||
{ | { | ||||
doFilesonly = f; | |||||
m_filesonly = filesonly; | |||||
} | } | ||||
/** | /** | ||||
@@ -159,9 +159,9 @@ public class Zip extends MatchingTask | |||||
* | * | ||||
* @param c The new Update value | * @param c The new Update value | ||||
*/ | */ | ||||
public void setUpdate( boolean c ) | |||||
public void setUpdate( final boolean update ) | |||||
{ | { | ||||
doUpdate = c; | |||||
m_update = update; | |||||
} | } | ||||
/** | /** | ||||
@@ -173,9 +173,9 @@ public class Zip extends MatchingTask | |||||
* | * | ||||
* @param we The new Whenempty value | * @param we The new Whenempty value | ||||
*/ | */ | ||||
public void setWhenempty( WhenEmpty we ) | |||||
public void setWhenempty( final WhenEmpty we ) | |||||
{ | { | ||||
emptyBehavior = we.getValue(); | |||||
m_emptyBehavior = we.getValue(); | |||||
} | } | ||||
/** | /** | ||||
@@ -183,19 +183,17 @@ public class Zip extends MatchingTask | |||||
* | * | ||||
* @return The InUpdateMode value | * @return The InUpdateMode value | ||||
*/ | */ | ||||
public boolean isInUpdateMode() | |||||
protected final boolean isInUpdateMode() | |||||
{ | { | ||||
return doUpdate; | |||||
return m_update; | |||||
} | } | ||||
/** | /** | ||||
* Adds a set of files (nested fileset attribute). | * Adds a set of files (nested fileset attribute). | ||||
* | |||||
* @param set The feature to be added to the Fileset attribute | |||||
*/ | */ | ||||
public void addFileset( FileSet set ) | |||||
public void addFileset( final FileSet set ) | |||||
{ | { | ||||
filesets.add( set ); | |||||
m_filesets.add( set ); | |||||
} | } | ||||
/** | /** | ||||
@@ -204,23 +202,27 @@ public class Zip extends MatchingTask | |||||
* | * | ||||
* @param set The feature to be added to the Zipfileset attribute | * @param set The feature to be added to the Zipfileset attribute | ||||
*/ | */ | ||||
public void addZipfileset( ZipFileSet set ) | |||||
public void addZipfileset( final ZipFileSet set ) | |||||
{ | { | ||||
filesets.add( set ); | |||||
m_filesets.add( set ); | |||||
} | } | ||||
public void execute() | public void execute() | ||||
throws TaskException | throws TaskException | ||||
{ | { | ||||
if( baseDir == null && filesets.size() == 0 && "zip".equals( archiveType ) ) | |||||
if( m_baseDir == null && m_filesets.size() == 0 && | |||||
"zip".equals( m_archiveType ) ) | |||||
{ | { | ||||
throw new TaskException( "basedir attribute must be set, or at least " + | |||||
"one fileset must be given!" ); | |||||
final String message = "basedir attribute must be set, or at least " + | |||||
"one fileset must be given!"; | |||||
throw new TaskException( message ); | |||||
} | } | ||||
if( zipFile == null ) | |||||
if( m_file == null ) | |||||
{ | { | ||||
throw new TaskException( "You must specify the " + archiveType + " file to create!" ); | |||||
final String message = "You must specify the " + | |||||
m_archiveType + " file to create!"; | |||||
throw new TaskException( message ); | |||||
} | } | ||||
// Renamed version of original file, if it exists | // Renamed version of original file, if it exists | ||||
@@ -228,14 +230,14 @@ public class Zip extends MatchingTask | |||||
// Whether or not an actual update is required - | // Whether or not an actual update is required - | ||||
// we don't need to update if the original file doesn't exist | // we don't need to update if the original file doesn't exist | ||||
addingNewFiles = true; | |||||
doUpdate = doUpdate && zipFile.exists(); | |||||
if( doUpdate ) | |||||
m_addingNewFiles = true; | |||||
m_update = m_update && m_file.exists(); | |||||
if( m_update ) | |||||
{ | { | ||||
try | try | ||||
{ | { | ||||
renamedFile = File.createTempFile( "zip", ".tmp", | renamedFile = File.createTempFile( "zip", ".tmp", | ||||
zipFile.getParentFile() ); | |||||
m_file.getParentFile() ); | |||||
} | } | ||||
catch( final IOException ioe ) | catch( final IOException ioe ) | ||||
{ | { | ||||
@@ -244,7 +246,7 @@ public class Zip extends MatchingTask | |||||
try | try | ||||
{ | { | ||||
if( !zipFile.renameTo( renamedFile ) ) | |||||
if( !m_file.renameTo( renamedFile ) ) | |||||
{ | { | ||||
throw new TaskException( "Unable to rename old file to temporary file" ); | throw new TaskException( "Unable to rename old file to temporary file" ); | ||||
} | } | ||||
@@ -257,13 +259,13 @@ public class Zip extends MatchingTask | |||||
// Create the scanners to pass to isUpToDate(). | // Create the scanners to pass to isUpToDate(). | ||||
ArrayList dss = new ArrayList(); | ArrayList dss = new ArrayList(); | ||||
if( baseDir != null ) | |||||
if( m_baseDir != null ) | |||||
{ | { | ||||
dss.add( getDirectoryScanner( baseDir ) ); | |||||
dss.add( getDirectoryScanner( m_baseDir ) ); | |||||
} | } | ||||
for( int i = 0; i < filesets.size(); i++ ) | |||||
for( int i = 0; i < m_filesets.size(); i++ ) | |||||
{ | { | ||||
FileSet fs = (FileSet)filesets.get( i ); | |||||
FileSet fs = (FileSet)m_filesets.get( i ); | |||||
dss.add( fs.getDirectoryScanner() ); | dss.add( fs.getDirectoryScanner() ); | ||||
} | } | ||||
int dssSize = dss.size(); | int dssSize = dss.size(); | ||||
@@ -272,24 +274,24 @@ public class Zip extends MatchingTask | |||||
// quick exit if the target is up to date | // quick exit if the target is up to date | ||||
// can also handle empty archives | // can also handle empty archives | ||||
if( isUpToDate( scanners, zipFile ) ) | |||||
if( isUpToDate( scanners, m_file ) ) | |||||
{ | { | ||||
return; | return; | ||||
} | } | ||||
String action = doUpdate ? "Updating " : "Building "; | |||||
String action = m_update ? "Updating " : "Building "; | |||||
getLogger().info( action + archiveType + ": " + zipFile.getAbsolutePath() ); | |||||
getLogger().info( action + m_archiveType + ": " + m_file.getAbsolutePath() ); | |||||
boolean success = false; | boolean success = false; | ||||
try | try | ||||
{ | { | ||||
ZipOutputStream zOut = | ZipOutputStream zOut = | ||||
new ZipOutputStream( new FileOutputStream( zipFile ) ); | |||||
zOut.setEncoding( encoding ); | |||||
new ZipOutputStream( new FileOutputStream( m_file ) ); | |||||
zOut.setEncoding( m_encoding ); | |||||
try | try | ||||
{ | { | ||||
if( doCompress ) | |||||
if( m_compress ) | |||||
{ | { | ||||
zOut.setMethod( ZipOutputStream.DEFLATED ); | zOut.setMethod( ZipOutputStream.DEFLATED ); | ||||
} | } | ||||
@@ -300,26 +302,26 @@ public class Zip extends MatchingTask | |||||
initZipOutputStream( zOut ); | initZipOutputStream( zOut ); | ||||
// Add the implicit fileset to the archive. | // Add the implicit fileset to the archive. | ||||
if( baseDir != null ) | |||||
if( m_baseDir != null ) | |||||
{ | { | ||||
addFiles( getDirectoryScanner( baseDir ), zOut, "", "" ); | |||||
addFiles( getDirectoryScanner( m_baseDir ), zOut, "", "" ); | |||||
} | } | ||||
// Add the explicit filesets to the archive. | // Add the explicit filesets to the archive. | ||||
addFiles( filesets, zOut ); | |||||
if( doUpdate ) | |||||
addFiles( m_filesets, zOut ); | |||||
if( m_update ) | |||||
{ | { | ||||
addingNewFiles = false; | |||||
m_addingNewFiles = false; | |||||
ZipFileSet oldFiles = new ZipFileSet(); | ZipFileSet oldFiles = new ZipFileSet(); | ||||
oldFiles.setSrc( renamedFile ); | oldFiles.setSrc( renamedFile ); | ||||
StringBuffer exclusionPattern = new StringBuffer(); | StringBuffer exclusionPattern = new StringBuffer(); | ||||
for( int i = 0; i < addedFiles.size(); i++ ) | |||||
for( int i = 0; i < m_addedFiles.size(); i++ ) | |||||
{ | { | ||||
if( i != 0 ) | if( i != 0 ) | ||||
{ | { | ||||
exclusionPattern.append( "," ); | exclusionPattern.append( "," ); | ||||
} | } | ||||
exclusionPattern.append( (String)addedFiles.get( i ) ); | |||||
exclusionPattern.append( (String)m_addedFiles.get( i ) ); | |||||
} | } | ||||
oldFiles.setExcludes( exclusionPattern.toString() ); | oldFiles.setExcludes( exclusionPattern.toString() ); | ||||
ArrayList tmp = new ArrayList(); | ArrayList tmp = new ArrayList(); | ||||
@@ -354,17 +356,17 @@ public class Zip extends MatchingTask | |||||
} | } | ||||
catch( IOException ioe ) | catch( IOException ioe ) | ||||
{ | { | ||||
String msg = "Problem creating " + archiveType + ": " + ioe.getMessage(); | |||||
String msg = "Problem creating " + m_archiveType + ": " + ioe.getMessage(); | |||||
// delete a bogus ZIP file | // delete a bogus ZIP file | ||||
if( !zipFile.delete() ) | |||||
if( !m_file.delete() ) | |||||
{ | { | ||||
msg += " (and the archive is probably corrupt but I could not delete it)"; | msg += " (and the archive is probably corrupt but I could not delete it)"; | ||||
} | } | ||||
if( doUpdate ) | |||||
if( m_update ) | |||||
{ | { | ||||
if( !renamedFile.renameTo( zipFile ) ) | |||||
if( !renamedFile.renameTo( m_file ) ) | |||||
{ | { | ||||
msg += " (and I couldn't rename the temporary file " + | msg += " (and I couldn't rename the temporary file " + | ||||
renamedFile.getName() + " back)"; | renamedFile.getName() + " back)"; | ||||
@@ -373,13 +375,9 @@ public class Zip extends MatchingTask | |||||
throw new TaskException( msg, ioe ); | throw new TaskException( msg, ioe ); | ||||
} | } | ||||
finally | |||||
{ | |||||
cleanUp(); | |||||
} | |||||
// If we've been successful on an update, delete the temporary file | // If we've been successful on an update, delete the temporary file | ||||
if( success && doUpdate ) | |||||
if( success && m_update ) | |||||
{ | { | ||||
if( !renamedFile.delete() ) | if( !renamedFile.delete() ) | ||||
{ | { | ||||
@@ -390,15 +388,26 @@ public class Zip extends MatchingTask | |||||
} | } | ||||
} | } | ||||
protected void addFileAs( final File file, final String name ) | |||||
throws TaskException | |||||
{ | |||||
// Create a ZipFileSet for this file, and pass it up. | |||||
final ZipFileSet fs = new ZipFileSet(); | |||||
fs.setDir( file.getParentFile() ); | |||||
fs.setIncludes( file.getName() ); | |||||
fs.setFullpath( name ); | |||||
addFileset( fs ); | |||||
} | |||||
/** | /** | ||||
* Indicates if the task is adding new files into the archive as opposed to | * Indicates if the task is adding new files into the archive as opposed to | ||||
* copying back unchanged files from the backup copy | * copying back unchanged files from the backup copy | ||||
* | * | ||||
* @return The AddingNewFiles value | * @return The AddingNewFiles value | ||||
*/ | */ | ||||
protected boolean isAddingNewFiles() | |||||
protected final boolean isAddingNewFiles() | |||||
{ | { | ||||
return addingNewFiles; | |||||
return m_addingNewFiles; | |||||
} | } | ||||
/** | /** | ||||
@@ -418,16 +427,16 @@ public class Zip extends MatchingTask | |||||
File[] files = grabFiles( scanners, fileNames ); | File[] files = grabFiles( scanners, fileNames ); | ||||
if( files.length == 0 ) | if( files.length == 0 ) | ||||
{ | { | ||||
if( emptyBehavior.equals( "skip" ) ) | |||||
if( m_emptyBehavior.equals( "skip" ) ) | |||||
{ | { | ||||
final String message = "Warning: skipping " + archiveType + " archive " + zipFile + | |||||
final String message = "Warning: skipping " + m_archiveType + " archive " + zipFile + | |||||
" because no files were included."; | " because no files were included."; | ||||
getLogger().warn( message ); | getLogger().warn( message ); | ||||
return true; | return true; | ||||
} | } | ||||
else if( emptyBehavior.equals( "fail" ) ) | |||||
else if( m_emptyBehavior.equals( "fail" ) ) | |||||
{ | { | ||||
throw new TaskException( "Cannot create " + archiveType + " archive " + zipFile + | |||||
throw new TaskException( "Cannot create " + m_archiveType + " archive " + zipFile + | |||||
": no files were included." ); | ": no files were included." ); | ||||
} | } | ||||
else | else | ||||
@@ -597,7 +606,7 @@ public class Zip extends MatchingTask | |||||
ZipOutputStream zOut, String prefix ) | ZipOutputStream zOut, String prefix ) | ||||
throws IOException | throws IOException | ||||
{ | { | ||||
if( !doFilesonly ) | |||||
if( !m_filesonly ) | |||||
{ | { | ||||
Stack directories = new Stack(); | Stack directories = new Stack(); | ||||
int slashPos = entry.length(); | int slashPos = entry.length(); | ||||
@@ -605,7 +614,7 @@ public class Zip extends MatchingTask | |||||
while( ( slashPos = entry.lastIndexOf( (int)'/', slashPos - 1 ) ) != -1 ) | while( ( slashPos = entry.lastIndexOf( (int)'/', slashPos - 1 ) ) != -1 ) | ||||
{ | { | ||||
String dir = entry.substring( 0, slashPos + 1 ); | String dir = entry.substring( 0, slashPos + 1 ); | ||||
if( addedDirs.get( prefix + dir ) != null ) | |||||
if( m_addedDirs.get( prefix + dir ) != null ) | |||||
{ | { | ||||
break; | break; | ||||
} | } | ||||
@@ -677,26 +686,6 @@ public class Zip extends MatchingTask | |||||
} | } | ||||
} | } | ||||
/** | |||||
* Do any clean up necessary to allow this instance to be used again. <p> | |||||
* | |||||
* When we get here, the Zip file has been closed and all we need to do is | |||||
* to reset some globals.</p> | |||||
*/ | |||||
protected void cleanUp() | |||||
{ | |||||
addedDirs = new Hashtable(); | |||||
addedFiles = new ArrayList(); | |||||
filesets = new ArrayList(); | |||||
zipFile = null; | |||||
baseDir = null; | |||||
doCompress = true; | |||||
doUpdate = false; | |||||
doFilesonly = false; | |||||
addingNewFiles = false; | |||||
encoding = null; | |||||
} | |||||
/** | /** | ||||
* Create an empty zip file | * Create an empty zip file | ||||
* | * | ||||
@@ -709,7 +698,7 @@ public class Zip extends MatchingTask | |||||
// In this case using java.util.zip will not work | // In this case using java.util.zip will not work | ||||
// because it does not permit a zero-entry archive. | // because it does not permit a zero-entry archive. | ||||
// Must create it manually. | // Must create it manually. | ||||
getLogger().info( "Note: creating empty " + archiveType + " archive " + zipFile ); | |||||
getLogger().info( "Note: creating empty " + m_archiveType + " archive " + zipFile ); | |||||
try | try | ||||
{ | { | ||||
OutputStream os = new FileOutputStream( zipFile ); | OutputStream os = new FileOutputStream( zipFile ); | ||||
@@ -749,13 +738,13 @@ public class Zip extends MatchingTask | |||||
protected void zipDir( File dir, ZipOutputStream zOut, String vPath ) | protected void zipDir( File dir, ZipOutputStream zOut, String vPath ) | ||||
throws IOException | throws IOException | ||||
{ | { | ||||
if( addedDirs.get( vPath ) != null ) | |||||
if( m_addedDirs.get( vPath ) != null ) | |||||
{ | { | ||||
// don't add directories we've already added. | // don't add directories we've already added. | ||||
// no warning if we try, it is harmless in and of itself | // no warning if we try, it is harmless in and of itself | ||||
return; | return; | ||||
} | } | ||||
addedDirs.put( vPath, vPath ); | |||||
m_addedDirs.put( vPath, vPath ); | |||||
ZipEntry ze = new ZipEntry( vPath ); | ZipEntry ze = new ZipEntry( vPath ); | ||||
if( dir != null && dir.exists() ) | if( dir != null && dir.exists() ) | ||||
@@ -794,7 +783,7 @@ public class Zip extends MatchingTask | |||||
* I couldn't find any documentation on this, just found out by try | * I couldn't find any documentation on this, just found out by try | ||||
* and error. | * and error. | ||||
*/ | */ | ||||
if( !doCompress ) | |||||
if( !m_compress ) | |||||
{ | { | ||||
long size = 0; | long size = 0; | ||||
CRC32 cal = new CRC32(); | CRC32 cal = new CRC32(); | ||||
@@ -844,13 +833,13 @@ public class Zip extends MatchingTask | |||||
} | } | ||||
count = in.read( buffer, 0, buffer.length ); | count = in.read( buffer, 0, buffer.length ); | ||||
} while( count != -1 ); | } while( count != -1 ); | ||||
addedFiles.add( vPath ); | |||||
m_addedFiles.add( vPath ); | |||||
} | } | ||||
protected void zipFile( File file, ZipOutputStream zOut, String vPath ) | protected void zipFile( File file, ZipOutputStream zOut, String vPath ) | ||||
throws IOException, TaskException | throws IOException, TaskException | ||||
{ | { | ||||
if( file.equals( zipFile ) ) | |||||
if( file.equals( m_file ) ) | |||||
{ | { | ||||
throw new TaskException( "A zip file cannot include itself" ); | throw new TaskException( "A zip file cannot include itself" ); | ||||
} | } | ||||
@@ -865,17 +854,4 @@ public class Zip extends MatchingTask | |||||
fIn.close(); | fIn.close(); | ||||
} | } | ||||
} | } | ||||
/** | |||||
* Possible behaviors when there are no matching files for the task. | |||||
* | |||||
* @author RT | |||||
*/ | |||||
public static class WhenEmpty extends EnumeratedAttribute | |||||
{ | |||||
public String[] getValues() | |||||
{ | |||||
return new String[]{"fail", "skip", "create"}; | |||||
} | |||||
} | |||||
} | } |
@@ -21,30 +21,27 @@ import org.apache.tools.zip.ZipOutputStream; | |||||
*/ | */ | ||||
public class Ear extends Jar | public class Ear extends Jar | ||||
{ | { | ||||
private File deploymentDescriptor; | |||||
private boolean descriptorAdded; | |||||
private File m_appxml; | |||||
private boolean m_descriptorAdded; | |||||
public Ear() | public Ear() | ||||
{ | { | ||||
super(); | |||||
archiveType = "ear"; | |||||
emptyBehavior = "create"; | |||||
m_archiveType = "ear"; | |||||
m_emptyBehavior = "create"; | |||||
} | } | ||||
public void setAppxml( File descr ) | |||||
public void setAppxml( final File appxml ) | |||||
throws TaskException | throws TaskException | ||||
{ | { | ||||
deploymentDescriptor = descr; | |||||
if( !deploymentDescriptor.exists() ) | |||||
throw new TaskException( "Deployment descriptor: " + deploymentDescriptor + " does not exist." ); | |||||
m_appxml = appxml; | |||||
if( !m_appxml.exists() ) | |||||
{ | |||||
final String message = "Deployment descriptor: " + | |||||
m_appxml + " does not exist."; | |||||
throw new TaskException( message ); | |||||
} | |||||
// Create a ZipFileSet for this file, and pass it up. | |||||
ZipFileSet fs = new ZipFileSet(); | |||||
fs.setDir( new File( deploymentDescriptor.getParent() ) ); | |||||
fs.setIncludes( deploymentDescriptor.getName() ); | |||||
fs.setFullpath( "META-INF/application.xml" ); | |||||
super.addFileset( fs ); | |||||
addFileAs( m_appxml, "META-INF/application.xml" ); | |||||
} | } | ||||
public void addArchives( ZipFileSet fs ) | public void addArchives( ZipFileSet fs ) | ||||
@@ -56,23 +53,13 @@ public class Ear extends Jar | |||||
super.addFileset( fs ); | super.addFileset( fs ); | ||||
} | } | ||||
/** | |||||
* Make sure we don't think we already have a web.xml next time this task | |||||
* gets executed. | |||||
*/ | |||||
protected void cleanUp() | |||||
{ | |||||
descriptorAdded = false; | |||||
super.cleanUp(); | |||||
} | |||||
protected void initZipOutputStream( ZipOutputStream zOut ) | |||||
protected void initZipOutputStream( final ZipOutputStream zOut ) | |||||
throws IOException, TaskException | throws IOException, TaskException | ||||
{ | { | ||||
// If no webxml file is specified, it's an error. | |||||
if( deploymentDescriptor == null && !isInUpdateMode() ) | |||||
if( m_appxml == null && !isInUpdateMode() ) | |||||
{ | { | ||||
throw new TaskException( "appxml attribute is required" ); | |||||
final String message = "appxml attribute is required"; | |||||
throw new TaskException( message ); | |||||
} | } | ||||
super.initZipOutputStream( zOut ); | super.initZipOutputStream( zOut ); | ||||
@@ -87,17 +74,19 @@ public class Ear extends Jar | |||||
// a <fileset> element. | // a <fileset> element. | ||||
if( vPath.equalsIgnoreCase( "META-INF/aplication.xml" ) ) | if( vPath.equalsIgnoreCase( "META-INF/aplication.xml" ) ) | ||||
{ | { | ||||
if( deploymentDescriptor == null || !deploymentDescriptor.equals( file ) || descriptorAdded ) | |||||
if( m_appxml == null || | |||||
!m_appxml.equals( file ) || | |||||
m_descriptorAdded ) | |||||
{ | { | ||||
final String message = "Warning: selected " + archiveType + | |||||
final String message = "Warning: selected " + m_archiveType + | |||||
" files include a META-INF/application.xml which will be ignored " + | " files include a META-INF/application.xml which will be ignored " + | ||||
"(please use appxml attribute to " + archiveType + " task)"; | |||||
"(please use appxml attribute to " + m_archiveType + " task)"; | |||||
getLogger().warn( message ); | getLogger().warn( message ); | ||||
} | } | ||||
else | else | ||||
{ | { | ||||
super.zipFile( file, zOut, vPath ); | super.zipFile( file, zOut, vPath ); | ||||
descriptorAdded = true; | |||||
m_descriptorAdded = true; | |||||
} | } | ||||
} | } | ||||
else | else | ||||
@@ -29,7 +29,8 @@ import org.apache.tools.zip.ZipOutputStream; | |||||
* | * | ||||
* @author James Davidson <a href="mailto:duncan@x180.com">duncan@x180.com</a> | * @author James Davidson <a href="mailto:duncan@x180.com">duncan@x180.com</a> | ||||
*/ | */ | ||||
public class Jar extends Zip | |||||
public class Jar | |||||
extends Zip | |||||
{ | { | ||||
/** | /** | ||||
* The index file name. | * The index file name. | ||||
@@ -39,16 +40,15 @@ public class Jar extends Zip | |||||
/** | /** | ||||
* true if a manifest has been specified in the task | * true if a manifest has been specified in the task | ||||
*/ | */ | ||||
private boolean buildFileManifest = false; | |||||
private boolean buildFileManifest; | |||||
/** | /** | ||||
* jar index is JDK 1.3+ only | * jar index is JDK 1.3+ only | ||||
*/ | */ | ||||
private boolean index = false; | |||||
private Manifest execManifest; | |||||
private Manifest manifest; | |||||
private File manifestFile; | |||||
private boolean m_index; | |||||
private Manifest m_execManifest; | |||||
private Manifest m_manifest; | |||||
private File m_manifestFile; | |||||
/** | /** | ||||
* constructor | * constructor | ||||
@@ -56,8 +56,8 @@ public class Jar extends Zip | |||||
public Jar() | public Jar() | ||||
{ | { | ||||
super(); | super(); | ||||
archiveType = "jar"; | |||||
emptyBehavior = "create"; | |||||
m_archiveType = "jar"; | |||||
m_emptyBehavior = "create"; | |||||
setEncoding( "UTF8" ); | setEncoding( "UTF8" ); | ||||
} | } | ||||
@@ -69,7 +69,7 @@ public class Jar extends Zip | |||||
*/ | */ | ||||
public void setIndex( boolean flag ) | public void setIndex( boolean flag ) | ||||
{ | { | ||||
index = flag; | |||||
m_index = flag; | |||||
} | } | ||||
public void setManifest( File manifestFile ) | public void setManifest( File manifestFile ) | ||||
@@ -77,30 +77,33 @@ public class Jar extends Zip | |||||
{ | { | ||||
if( !manifestFile.exists() ) | if( !manifestFile.exists() ) | ||||
{ | { | ||||
throw new TaskException( "Manifest file: " + manifestFile + " does not exist." ); | |||||
final String message = "Manifest file: " + manifestFile + " does not exist."; | |||||
throw new TaskException( message ); | |||||
} | } | ||||
this.manifestFile = manifestFile; | |||||
this.m_manifestFile = manifestFile; | |||||
Reader r = null; | Reader r = null; | ||||
try | try | ||||
{ | { | ||||
r = new FileReader( manifestFile ); | r = new FileReader( manifestFile ); | ||||
Manifest newManifest = new Manifest( r ); | Manifest newManifest = new Manifest( r ); | ||||
if( manifest == null ) | |||||
if( m_manifest == null ) | |||||
{ | { | ||||
manifest = Manifest.getDefaultManifest(); | |||||
m_manifest = Manifest.getDefaultManifest(); | |||||
} | } | ||||
manifest.merge( newManifest ); | |||||
m_manifest.merge( newManifest ); | |||||
} | } | ||||
catch( ManifestException e ) | catch( ManifestException e ) | ||||
{ | { | ||||
getLogger().error( "Manifest is invalid: " + e.getMessage() ); | |||||
throw new TaskException( "Invalid Manifest: " + manifestFile, e ); | |||||
final String message = "Manifest " + manifestFile + " is invalid: " + e.getMessage(); | |||||
getLogger().error( message ); | |||||
throw new TaskException( message, e ); | |||||
} | } | ||||
catch( IOException e ) | catch( IOException e ) | ||||
{ | { | ||||
throw new TaskException( "Unable to read manifest file: " + manifestFile, e ); | |||||
final String message = "Unable to read manifest file: " + manifestFile; | |||||
throw new TaskException( message, e ); | |||||
} | } | ||||
finally | finally | ||||
{ | { | ||||
@@ -127,11 +130,11 @@ public class Jar extends Zip | |||||
public void addConfiguredManifest( Manifest newManifest ) | public void addConfiguredManifest( Manifest newManifest ) | ||||
throws ManifestException, TaskException | throws ManifestException, TaskException | ||||
{ | { | ||||
if( manifest == null ) | |||||
if( m_manifest == null ) | |||||
{ | { | ||||
manifest = Manifest.getDefaultManifest(); | |||||
m_manifest = Manifest.getDefaultManifest(); | |||||
} | } | ||||
manifest.merge( newManifest ); | |||||
m_manifest.merge( newManifest ); | |||||
buildFileManifest = true; | buildFileManifest = true; | ||||
} | } | ||||
@@ -155,7 +158,7 @@ public class Jar extends Zip | |||||
throws TaskException | throws TaskException | ||||
{ | { | ||||
// need to handle manifest as a special check | // need to handle manifest as a special check | ||||
if( buildFileManifest || manifestFile == null ) | |||||
if( buildFileManifest || m_manifestFile == null ) | |||||
{ | { | ||||
java.util.zip.ZipFile theZipFile = null; | java.util.zip.ZipFile theZipFile = null; | ||||
try | try | ||||
@@ -168,11 +171,11 @@ public class Jar extends Zip | |||||
return false; | return false; | ||||
} | } | ||||
Manifest currentManifest = new Manifest( new InputStreamReader( theZipFile.getInputStream( entry ) ) ); | Manifest currentManifest = new Manifest( new InputStreamReader( theZipFile.getInputStream( entry ) ) ); | ||||
if( manifest == null ) | |||||
if( m_manifest == null ) | |||||
{ | { | ||||
manifest = Manifest.getDefaultManifest(); | |||||
m_manifest = Manifest.getDefaultManifest(); | |||||
} | } | ||||
if( !currentManifest.equals( manifest ) ) | |||||
if( !currentManifest.equals( m_manifest ) ) | |||||
{ | { | ||||
getLogger().debug( "Updating jar since jar manifest has changed" ); | getLogger().debug( "Updating jar since jar manifest has changed" ); | ||||
return false; | return false; | ||||
@@ -199,22 +202,13 @@ public class Jar extends Zip | |||||
} | } | ||||
} | } | ||||
} | } | ||||
else if( manifestFile.lastModified() > zipFile.lastModified() ) | |||||
else if( m_manifestFile.lastModified() > zipFile.lastModified() ) | |||||
{ | { | ||||
return false; | return false; | ||||
} | } | ||||
return super.isUpToDate( scanners, zipFile ); | return super.isUpToDate( scanners, zipFile ); | ||||
} | } | ||||
/** | |||||
* Make sure we don't think we already have a MANIFEST next time this task | |||||
* gets executed. | |||||
*/ | |||||
protected void cleanUp() | |||||
{ | |||||
super.cleanUp(); | |||||
} | |||||
protected boolean createEmptyZip( File zipFile ) | protected boolean createEmptyZip( File zipFile ) | ||||
{ | { | ||||
// Jar files always contain a manifest and can never be empty | // Jar files always contain a manifest and can never be empty | ||||
@@ -224,7 +218,7 @@ public class Jar extends Zip | |||||
protected void finalizeZipOutputStream( ZipOutputStream zOut ) | protected void finalizeZipOutputStream( ZipOutputStream zOut ) | ||||
throws IOException, TaskException | throws IOException, TaskException | ||||
{ | { | ||||
if( index ) | |||||
if( m_index ) | |||||
{ | { | ||||
createIndexList( zOut ); | createIndexList( zOut ); | ||||
} | } | ||||
@@ -235,13 +229,13 @@ public class Jar extends Zip | |||||
{ | { | ||||
try | try | ||||
{ | { | ||||
execManifest = Manifest.getDefaultManifest(); | |||||
m_execManifest = Manifest.getDefaultManifest(); | |||||
if( manifest != null ) | |||||
if( m_manifest != null ) | |||||
{ | { | ||||
execManifest.merge( manifest ); | |||||
m_execManifest.merge( m_manifest ); | |||||
} | } | ||||
for( Iterator e = execManifest.getWarnings(); e.hasNext(); ) | |||||
for( Iterator e = m_execManifest.getWarnings(); e.hasNext(); ) | |||||
{ | { | ||||
getLogger().warn( "Manifest warning: " + (String)e.next() ); | getLogger().warn( "Manifest warning: " + (String)e.next() ); | ||||
} | } | ||||
@@ -250,7 +244,7 @@ public class Jar extends Zip | |||||
// time to write the manifest | // time to write the manifest | ||||
ByteArrayOutputStream baos = new ByteArrayOutputStream(); | ByteArrayOutputStream baos = new ByteArrayOutputStream(); | ||||
PrintWriter writer = new PrintWriter( baos ); | PrintWriter writer = new PrintWriter( baos ); | ||||
execManifest.write( writer ); | |||||
m_execManifest.write( writer ); | |||||
writer.flush(); | writer.flush(); | ||||
ByteArrayInputStream bais = new ByteArrayInputStream( baos.toByteArray() ); | ByteArrayInputStream bais = new ByteArrayInputStream( baos.toByteArray() ); | ||||
@@ -273,9 +267,9 @@ public class Jar extends Zip | |||||
// a <fileset> element. | // a <fileset> element. | ||||
if( vPath.equalsIgnoreCase( "META-INF/MANIFEST.MF" ) ) | if( vPath.equalsIgnoreCase( "META-INF/MANIFEST.MF" ) ) | ||||
{ | { | ||||
final String message = "Warning: selected " + archiveType + | |||||
final String message = "Warning: selected " + m_archiveType + | |||||
" files include a META-INF/MANIFEST.MF which will be ignored " + | " files include a META-INF/MANIFEST.MF which will be ignored " + | ||||
"(please use manifest attribute to " + archiveType + " task)"; | |||||
"(please use manifest attribute to " + m_archiveType + " task)"; | |||||
getLogger().warn( message ); | getLogger().warn( message ); | ||||
} | } | ||||
else | else | ||||
@@ -328,12 +322,12 @@ public class Jar extends Zip | |||||
writer.println(); | writer.println(); | ||||
// header newline | // header newline | ||||
writer.println( zipFile.getName() ); | |||||
writer.println( m_file.getName() ); | |||||
// JarIndex is sorting the directories by ascending order. | // JarIndex is sorting the directories by ascending order. | ||||
// it's painful to do in JDK 1.1 and it has no value but cosmetic | // it's painful to do in JDK 1.1 and it has no value but cosmetic | ||||
// since it will be read into a hashtable by the classloader. | // since it will be read into a hashtable by the classloader. | ||||
Enumeration enum = addedDirs.keys(); | |||||
Enumeration enum = m_addedDirs.keys(); | |||||
while( enum.hasMoreElements() ) | while( enum.hasMoreElements() ) | ||||
{ | { | ||||
String dir = (String)enum.nextElement(); | String dir = (String)enum.nextElement(); | ||||
@@ -377,13 +371,13 @@ public class Jar extends Zip | |||||
{ | { | ||||
try | try | ||||
{ | { | ||||
if( execManifest == null ) | |||||
if( m_execManifest == null ) | |||||
{ | { | ||||
execManifest = new Manifest( new InputStreamReader( is ) ); | |||||
m_execManifest = new Manifest( new InputStreamReader( is ) ); | |||||
} | } | ||||
else if( isAddingNewFiles() ) | else if( isAddingNewFiles() ) | ||||
{ | { | ||||
execManifest.merge( new Manifest( new InputStreamReader( is ) ) ); | |||||
m_execManifest.merge( new Manifest( new InputStreamReader( is ) ) ); | |||||
} | } | ||||
} | } | ||||
catch( ManifestException e ) | catch( ManifestException e ) | ||||
@@ -18,70 +18,59 @@ import org.apache.tools.zip.ZipOutputStream; | |||||
* | * | ||||
* @author <a href="mailto:stefan.bodewig@epost.de">Stefan Bodewig</a> | * @author <a href="mailto:stefan.bodewig@epost.de">Stefan Bodewig</a> | ||||
*/ | */ | ||||
public class War extends Jar | |||||
public class War | |||||
extends Jar | |||||
{ | { | ||||
private File deploymentDescriptor; | |||||
private boolean descriptorAdded; | |||||
private File m_webxml; | |||||
private boolean m_descriptorAdded; | |||||
public War() | public War() | ||||
{ | { | ||||
super(); | super(); | ||||
archiveType = "war"; | |||||
emptyBehavior = "create"; | |||||
m_archiveType = "war"; | |||||
m_emptyBehavior = "create"; | |||||
} | } | ||||
public void setWebxml( File descr ) | |||||
public void setWebxml( final File descr ) | |||||
throws TaskException | throws TaskException | ||||
{ | { | ||||
deploymentDescriptor = descr; | |||||
if( !deploymentDescriptor.exists() ) | |||||
throw new TaskException( "Deployment descriptor: " + deploymentDescriptor + " does not exist." ); | |||||
m_webxml = descr; | |||||
if( !m_webxml.exists() ) | |||||
{ | |||||
final String message = "Deployment descriptor: " + | |||||
m_webxml + " does not exist."; | |||||
throw new TaskException( message ); | |||||
} | |||||
// Create a ZipFileSet for this file, and pass it up. | |||||
ZipFileSet fs = new ZipFileSet(); | |||||
fs.setDir( new File( deploymentDescriptor.getParent() ) ); | |||||
fs.setIncludes( deploymentDescriptor.getName() ); | |||||
fs.setFullpath( "WEB-INF/web.xml" ); | |||||
super.addFileset( fs ); | |||||
addFileAs(descr, "WEB-INF/web.xml" ); | |||||
} | } | ||||
public void addClasses( ZipFileSet fs ) | |||||
public void addClasses( final ZipFileSet fs ) | |||||
{ | { | ||||
// We just set the prefix for this fileset, and pass it up. | // We just set the prefix for this fileset, and pass it up. | ||||
fs.setPrefix( "WEB-INF/classes/" ); | fs.setPrefix( "WEB-INF/classes/" ); | ||||
super.addFileset( fs ); | super.addFileset( fs ); | ||||
} | } | ||||
public void addLib( ZipFileSet fs ) | |||||
public void addLib( final ZipFileSet fs ) | |||||
{ | { | ||||
// We just set the prefix for this fileset, and pass it up. | // We just set the prefix for this fileset, and pass it up. | ||||
fs.setPrefix( "WEB-INF/lib/" ); | fs.setPrefix( "WEB-INF/lib/" ); | ||||
super.addFileset( fs ); | super.addFileset( fs ); | ||||
} | } | ||||
public void addWebinf( ZipFileSet fs ) | |||||
public void addWebinf( final ZipFileSet fs ) | |||||
{ | { | ||||
// We just set the prefix for this fileset, and pass it up. | // We just set the prefix for this fileset, and pass it up. | ||||
fs.setPrefix( "WEB-INF/" ); | fs.setPrefix( "WEB-INF/" ); | ||||
super.addFileset( fs ); | super.addFileset( fs ); | ||||
} | } | ||||
/** | |||||
* Make sure we don't think we already have a web.xml next time this task | |||||
* gets executed. | |||||
*/ | |||||
protected void cleanUp() | |||||
{ | |||||
descriptorAdded = false; | |||||
super.cleanUp(); | |||||
} | |||||
protected void initZipOutputStream( ZipOutputStream zOut ) | |||||
protected void initZipOutputStream( final ZipOutputStream zOut ) | |||||
throws IOException, TaskException | throws IOException, TaskException | ||||
{ | { | ||||
// If no webxml file is specified, it's an error. | // If no webxml file is specified, it's an error. | ||||
if( deploymentDescriptor == null && !isInUpdateMode() ) | |||||
if( m_webxml == null && !isInUpdateMode() ) | |||||
{ | { | ||||
throw new TaskException( "webxml attribute is required" ); | throw new TaskException( "webxml attribute is required" ); | ||||
} | } | ||||
@@ -89,7 +78,9 @@ public class War extends Jar | |||||
super.initZipOutputStream( zOut ); | super.initZipOutputStream( zOut ); | ||||
} | } | ||||
protected void zipFile( File file, ZipOutputStream zOut, String vPath ) | |||||
protected void zipFile( final File file, | |||||
final ZipOutputStream zOut, | |||||
final String vPath ) | |||||
throws IOException, TaskException | throws IOException, TaskException | ||||
{ | { | ||||
// If the file being added is WEB-INF/web.xml, we warn if it's not the | // If the file being added is WEB-INF/web.xml, we warn if it's not the | ||||
@@ -98,17 +89,17 @@ public class War extends Jar | |||||
// a <fileset> element. | // a <fileset> element. | ||||
if( vPath.equalsIgnoreCase( "WEB-INF/web.xml" ) ) | if( vPath.equalsIgnoreCase( "WEB-INF/web.xml" ) ) | ||||
{ | { | ||||
if( deploymentDescriptor == null || !deploymentDescriptor.equals( file ) || descriptorAdded ) | |||||
if( m_webxml == null || !m_webxml.equals( file ) || m_descriptorAdded ) | |||||
{ | { | ||||
final String message = "Warning: selected " + archiveType + | |||||
final String message = "Warning: selected " + m_archiveType + | |||||
" files include a WEB-INF/web.xml which will be ignored " + | " files include a WEB-INF/web.xml which will be ignored " + | ||||
"(please use webxml attribute to " + archiveType + " task)"; | |||||
"(please use webxml attribute to " + m_archiveType + " task)"; | |||||
getLogger().warn( message ); | getLogger().warn( message ); | ||||
} | } | ||||
else | else | ||||
{ | { | ||||
super.zipFile( file, zOut, vPath ); | super.zipFile( file, zOut, vPath ); | ||||
descriptorAdded = true; | |||||
m_descriptorAdded = true; | |||||
} | } | ||||
} | } | ||||
else | else | ||||
@@ -0,0 +1,22 @@ | |||||
/* | |||||
* Copyright (C) The Apache Software Foundation. All rights reserved. | |||||
* | |||||
* This software is published under the terms of the Apache Software License | |||||
* version 1.1, a copy of which has been included with this distribution in | |||||
* the LICENSE.txt file. | |||||
*/ | |||||
package org.apache.tools.ant.taskdefs; | |||||
import org.apache.tools.ant.types.EnumeratedAttribute; | |||||
/** | |||||
* Possible behaviors when there are no matching files for the task. | |||||
*/ | |||||
public class WhenEmpty | |||||
extends EnumeratedAttribute | |||||
{ | |||||
public String[] getValues() | |||||
{ | |||||
return new String[]{"fail", "skip", "create"}; | |||||
} | |||||
} |
@@ -23,12 +23,11 @@ import java.util.zip.ZipInputStream; | |||||
import org.apache.myrmidon.api.TaskException; | import org.apache.myrmidon.api.TaskException; | ||||
import org.apache.tools.ant.types.DirectoryScanner; | import org.apache.tools.ant.types.DirectoryScanner; | ||||
import org.apache.tools.ant.types.FileScanner; | import org.apache.tools.ant.types.FileScanner; | ||||
import org.apache.tools.ant.types.EnumeratedAttribute; | |||||
import org.apache.tools.ant.types.FileSet; | import org.apache.tools.ant.types.FileSet; | ||||
import org.apache.tools.ant.types.SourceFileScanner; | |||||
import org.apache.tools.ant.types.ZipFileSet; | import org.apache.tools.ant.types.ZipFileSet; | ||||
import org.apache.tools.ant.types.ZipScanner; | import org.apache.tools.ant.types.ZipScanner; | ||||
import org.apache.tools.ant.util.mappers.MergingMapper; | import org.apache.tools.ant.util.mappers.MergingMapper; | ||||
import org.apache.tools.ant.types.SourceFileScanner; | |||||
import org.apache.tools.zip.ZipEntry; | import org.apache.tools.zip.ZipEntry; | ||||
import org.apache.tools.zip.ZipOutputStream; | import org.apache.tools.zip.ZipOutputStream; | ||||
@@ -39,36 +38,35 @@ import org.apache.tools.zip.ZipOutputStream; | |||||
* @author Jon S. Stevens <a href="mailto:jon@clearink.com">jon@clearink.com</a> | * @author Jon S. Stevens <a href="mailto:jon@clearink.com">jon@clearink.com</a> | ||||
* @author <a href="mailto:stefan.bodewig@epost.de">Stefan Bodewig</a> | * @author <a href="mailto:stefan.bodewig@epost.de">Stefan Bodewig</a> | ||||
*/ | */ | ||||
public class Zip extends MatchingTask | |||||
public class Zip | |||||
extends MatchingTask | |||||
{ | { | ||||
// For directories: | // For directories: | ||||
private final static long EMPTY_CRC = new CRC32().getValue(); | private final static long EMPTY_CRC = new CRC32().getValue(); | ||||
private boolean doCompress = true; | |||||
private boolean doUpdate = false; | |||||
private boolean doFilesonly = false; | |||||
protected String archiveType = "zip"; | |||||
protected String emptyBehavior = "skip"; | |||||
private ArrayList filesets = new ArrayList(); | |||||
protected Hashtable addedDirs = new Hashtable(); | |||||
private ArrayList addedFiles = new ArrayList(); | |||||
protected File zipFile; | |||||
private boolean m_compress = true; | |||||
private boolean m_update; | |||||
private boolean m_filesonly; | |||||
protected String m_archiveType = "zip"; | |||||
protected String m_emptyBehavior = "skip"; | |||||
private ArrayList m_filesets = new ArrayList(); | |||||
protected Hashtable m_addedDirs = new Hashtable(); | |||||
private ArrayList m_addedFiles = new ArrayList(); | |||||
protected File m_file; | |||||
/** | /** | ||||
* true when we are adding new files into the Zip file, as opposed to adding | * true when we are adding new files into the Zip file, as opposed to adding | ||||
* back the unchanged files | * back the unchanged files | ||||
*/ | */ | ||||
private boolean addingNewFiles; | |||||
private File baseDir; | |||||
private boolean m_addingNewFiles; | |||||
private File m_baseDir; | |||||
/** | /** | ||||
* Encoding to use for filenames, defaults to the platform's default | * Encoding to use for filenames, defaults to the platform's default | ||||
* encoding. | * encoding. | ||||
*/ | */ | ||||
private String encoding; | |||||
private String m_encoding; | |||||
protected static String[][] grabFileNames( FileScanner[] scanners ) | |||||
private static String[][] grabFileNames( final FileScanner[] scanners ) | |||||
throws TaskException | throws TaskException | ||||
{ | { | ||||
String[][] result = new String[ scanners.length ][]; | String[][] result = new String[ scanners.length ][]; | ||||
@@ -83,15 +81,17 @@ public class Zip extends MatchingTask | |||||
return result; | return result; | ||||
} | } | ||||
protected static File[] grabFiles( FileScanner[] scanners, | |||||
String[][] fileNames ) | |||||
private static File[] grabFiles( final FileScanner[] scanners, | |||||
final String[][] filenames ) | |||||
{ | { | ||||
ArrayList files = new ArrayList(); | |||||
for( int i = 0; i < fileNames.length; i++ ) | |||||
final ArrayList files = new ArrayList(); | |||||
for( int i = 0; i < filenames.length; i++ ) | |||||
{ | { | ||||
File thisBaseDir = scanners[ i ].getBasedir(); | |||||
for( int j = 0; j < fileNames[ i ].length; j++ ) | |||||
files.add( new File( thisBaseDir, fileNames[ i ][ j ] ) ); | |||||
final File baseDir = scanners[ i ].getBasedir(); | |||||
for( int j = 0; j < filenames[ i ].length; j++ ) | |||||
{ | |||||
files.add( new File( baseDir, filenames[ i ][ j ] ) ); | |||||
} | |||||
} | } | ||||
final File[] toret = new File[ files.size() ]; | final File[] toret = new File[ files.size() ]; | ||||
return (File[])files.toArray( toret ); | return (File[])files.toArray( toret ); | ||||
@@ -102,9 +102,9 @@ public class Zip extends MatchingTask | |||||
* | * | ||||
* @param baseDir The new Basedir value | * @param baseDir The new Basedir value | ||||
*/ | */ | ||||
public void setBasedir( File baseDir ) | |||||
public void setBasedir( final File baseDir ) | |||||
{ | { | ||||
this.baseDir = baseDir; | |||||
m_baseDir = baseDir; | |||||
} | } | ||||
/** | /** | ||||
@@ -112,9 +112,9 @@ public class Zip extends MatchingTask | |||||
* | * | ||||
* @param c The new Compress value | * @param c The new Compress value | ||||
*/ | */ | ||||
public void setCompress( boolean c ) | |||||
public void setCompress( final boolean compress ) | |||||
{ | { | ||||
doCompress = c; | |||||
m_compress = compress; | |||||
} | } | ||||
/** | /** | ||||
@@ -128,9 +128,9 @@ public class Zip extends MatchingTask | |||||
* | * | ||||
* @param encoding The new Encoding value | * @param encoding The new Encoding value | ||||
*/ | */ | ||||
public void setEncoding( String encoding ) | |||||
public void setEncoding( final String encoding ) | |||||
{ | { | ||||
this.encoding = encoding; | |||||
m_encoding = encoding; | |||||
} | } | ||||
/** | /** | ||||
@@ -138,9 +138,9 @@ public class Zip extends MatchingTask | |||||
* | * | ||||
* @param file The new File value | * @param file The new File value | ||||
*/ | */ | ||||
public void setFile( File file ) | |||||
public void setFile( final File file ) | |||||
{ | { | ||||
this.zipFile = file; | |||||
m_file = file; | |||||
} | } | ||||
/** | /** | ||||
@@ -148,9 +148,9 @@ public class Zip extends MatchingTask | |||||
* | * | ||||
* @param f The new Filesonly value | * @param f The new Filesonly value | ||||
*/ | */ | ||||
public void setFilesonly( boolean f ) | |||||
public void setFilesonly( final boolean filesonly ) | |||||
{ | { | ||||
doFilesonly = f; | |||||
m_filesonly = filesonly; | |||||
} | } | ||||
/** | /** | ||||
@@ -159,9 +159,9 @@ public class Zip extends MatchingTask | |||||
* | * | ||||
* @param c The new Update value | * @param c The new Update value | ||||
*/ | */ | ||||
public void setUpdate( boolean c ) | |||||
public void setUpdate( final boolean update ) | |||||
{ | { | ||||
doUpdate = c; | |||||
m_update = update; | |||||
} | } | ||||
/** | /** | ||||
@@ -173,9 +173,9 @@ public class Zip extends MatchingTask | |||||
* | * | ||||
* @param we The new Whenempty value | * @param we The new Whenempty value | ||||
*/ | */ | ||||
public void setWhenempty( WhenEmpty we ) | |||||
public void setWhenempty( final WhenEmpty we ) | |||||
{ | { | ||||
emptyBehavior = we.getValue(); | |||||
m_emptyBehavior = we.getValue(); | |||||
} | } | ||||
/** | /** | ||||
@@ -183,19 +183,17 @@ public class Zip extends MatchingTask | |||||
* | * | ||||
* @return The InUpdateMode value | * @return The InUpdateMode value | ||||
*/ | */ | ||||
public boolean isInUpdateMode() | |||||
protected final boolean isInUpdateMode() | |||||
{ | { | ||||
return doUpdate; | |||||
return m_update; | |||||
} | } | ||||
/** | /** | ||||
* Adds a set of files (nested fileset attribute). | * Adds a set of files (nested fileset attribute). | ||||
* | |||||
* @param set The feature to be added to the Fileset attribute | |||||
*/ | */ | ||||
public void addFileset( FileSet set ) | |||||
public void addFileset( final FileSet set ) | |||||
{ | { | ||||
filesets.add( set ); | |||||
m_filesets.add( set ); | |||||
} | } | ||||
/** | /** | ||||
@@ -204,23 +202,27 @@ public class Zip extends MatchingTask | |||||
* | * | ||||
* @param set The feature to be added to the Zipfileset attribute | * @param set The feature to be added to the Zipfileset attribute | ||||
*/ | */ | ||||
public void addZipfileset( ZipFileSet set ) | |||||
public void addZipfileset( final ZipFileSet set ) | |||||
{ | { | ||||
filesets.add( set ); | |||||
m_filesets.add( set ); | |||||
} | } | ||||
public void execute() | public void execute() | ||||
throws TaskException | throws TaskException | ||||
{ | { | ||||
if( baseDir == null && filesets.size() == 0 && "zip".equals( archiveType ) ) | |||||
if( m_baseDir == null && m_filesets.size() == 0 && | |||||
"zip".equals( m_archiveType ) ) | |||||
{ | { | ||||
throw new TaskException( "basedir attribute must be set, or at least " + | |||||
"one fileset must be given!" ); | |||||
final String message = "basedir attribute must be set, or at least " + | |||||
"one fileset must be given!"; | |||||
throw new TaskException( message ); | |||||
} | } | ||||
if( zipFile == null ) | |||||
if( m_file == null ) | |||||
{ | { | ||||
throw new TaskException( "You must specify the " + archiveType + " file to create!" ); | |||||
final String message = "You must specify the " + | |||||
m_archiveType + " file to create!"; | |||||
throw new TaskException( message ); | |||||
} | } | ||||
// Renamed version of original file, if it exists | // Renamed version of original file, if it exists | ||||
@@ -228,14 +230,14 @@ public class Zip extends MatchingTask | |||||
// Whether or not an actual update is required - | // Whether or not an actual update is required - | ||||
// we don't need to update if the original file doesn't exist | // we don't need to update if the original file doesn't exist | ||||
addingNewFiles = true; | |||||
doUpdate = doUpdate && zipFile.exists(); | |||||
if( doUpdate ) | |||||
m_addingNewFiles = true; | |||||
m_update = m_update && m_file.exists(); | |||||
if( m_update ) | |||||
{ | { | ||||
try | try | ||||
{ | { | ||||
renamedFile = File.createTempFile( "zip", ".tmp", | renamedFile = File.createTempFile( "zip", ".tmp", | ||||
zipFile.getParentFile() ); | |||||
m_file.getParentFile() ); | |||||
} | } | ||||
catch( final IOException ioe ) | catch( final IOException ioe ) | ||||
{ | { | ||||
@@ -244,7 +246,7 @@ public class Zip extends MatchingTask | |||||
try | try | ||||
{ | { | ||||
if( !zipFile.renameTo( renamedFile ) ) | |||||
if( !m_file.renameTo( renamedFile ) ) | |||||
{ | { | ||||
throw new TaskException( "Unable to rename old file to temporary file" ); | throw new TaskException( "Unable to rename old file to temporary file" ); | ||||
} | } | ||||
@@ -257,13 +259,13 @@ public class Zip extends MatchingTask | |||||
// Create the scanners to pass to isUpToDate(). | // Create the scanners to pass to isUpToDate(). | ||||
ArrayList dss = new ArrayList(); | ArrayList dss = new ArrayList(); | ||||
if( baseDir != null ) | |||||
if( m_baseDir != null ) | |||||
{ | { | ||||
dss.add( getDirectoryScanner( baseDir ) ); | |||||
dss.add( getDirectoryScanner( m_baseDir ) ); | |||||
} | } | ||||
for( int i = 0; i < filesets.size(); i++ ) | |||||
for( int i = 0; i < m_filesets.size(); i++ ) | |||||
{ | { | ||||
FileSet fs = (FileSet)filesets.get( i ); | |||||
FileSet fs = (FileSet)m_filesets.get( i ); | |||||
dss.add( fs.getDirectoryScanner() ); | dss.add( fs.getDirectoryScanner() ); | ||||
} | } | ||||
int dssSize = dss.size(); | int dssSize = dss.size(); | ||||
@@ -272,24 +274,24 @@ public class Zip extends MatchingTask | |||||
// quick exit if the target is up to date | // quick exit if the target is up to date | ||||
// can also handle empty archives | // can also handle empty archives | ||||
if( isUpToDate( scanners, zipFile ) ) | |||||
if( isUpToDate( scanners, m_file ) ) | |||||
{ | { | ||||
return; | return; | ||||
} | } | ||||
String action = doUpdate ? "Updating " : "Building "; | |||||
String action = m_update ? "Updating " : "Building "; | |||||
getLogger().info( action + archiveType + ": " + zipFile.getAbsolutePath() ); | |||||
getLogger().info( action + m_archiveType + ": " + m_file.getAbsolutePath() ); | |||||
boolean success = false; | boolean success = false; | ||||
try | try | ||||
{ | { | ||||
ZipOutputStream zOut = | ZipOutputStream zOut = | ||||
new ZipOutputStream( new FileOutputStream( zipFile ) ); | |||||
zOut.setEncoding( encoding ); | |||||
new ZipOutputStream( new FileOutputStream( m_file ) ); | |||||
zOut.setEncoding( m_encoding ); | |||||
try | try | ||||
{ | { | ||||
if( doCompress ) | |||||
if( m_compress ) | |||||
{ | { | ||||
zOut.setMethod( ZipOutputStream.DEFLATED ); | zOut.setMethod( ZipOutputStream.DEFLATED ); | ||||
} | } | ||||
@@ -300,26 +302,26 @@ public class Zip extends MatchingTask | |||||
initZipOutputStream( zOut ); | initZipOutputStream( zOut ); | ||||
// Add the implicit fileset to the archive. | // Add the implicit fileset to the archive. | ||||
if( baseDir != null ) | |||||
if( m_baseDir != null ) | |||||
{ | { | ||||
addFiles( getDirectoryScanner( baseDir ), zOut, "", "" ); | |||||
addFiles( getDirectoryScanner( m_baseDir ), zOut, "", "" ); | |||||
} | } | ||||
// Add the explicit filesets to the archive. | // Add the explicit filesets to the archive. | ||||
addFiles( filesets, zOut ); | |||||
if( doUpdate ) | |||||
addFiles( m_filesets, zOut ); | |||||
if( m_update ) | |||||
{ | { | ||||
addingNewFiles = false; | |||||
m_addingNewFiles = false; | |||||
ZipFileSet oldFiles = new ZipFileSet(); | ZipFileSet oldFiles = new ZipFileSet(); | ||||
oldFiles.setSrc( renamedFile ); | oldFiles.setSrc( renamedFile ); | ||||
StringBuffer exclusionPattern = new StringBuffer(); | StringBuffer exclusionPattern = new StringBuffer(); | ||||
for( int i = 0; i < addedFiles.size(); i++ ) | |||||
for( int i = 0; i < m_addedFiles.size(); i++ ) | |||||
{ | { | ||||
if( i != 0 ) | if( i != 0 ) | ||||
{ | { | ||||
exclusionPattern.append( "," ); | exclusionPattern.append( "," ); | ||||
} | } | ||||
exclusionPattern.append( (String)addedFiles.get( i ) ); | |||||
exclusionPattern.append( (String)m_addedFiles.get( i ) ); | |||||
} | } | ||||
oldFiles.setExcludes( exclusionPattern.toString() ); | oldFiles.setExcludes( exclusionPattern.toString() ); | ||||
ArrayList tmp = new ArrayList(); | ArrayList tmp = new ArrayList(); | ||||
@@ -354,17 +356,17 @@ public class Zip extends MatchingTask | |||||
} | } | ||||
catch( IOException ioe ) | catch( IOException ioe ) | ||||
{ | { | ||||
String msg = "Problem creating " + archiveType + ": " + ioe.getMessage(); | |||||
String msg = "Problem creating " + m_archiveType + ": " + ioe.getMessage(); | |||||
// delete a bogus ZIP file | // delete a bogus ZIP file | ||||
if( !zipFile.delete() ) | |||||
if( !m_file.delete() ) | |||||
{ | { | ||||
msg += " (and the archive is probably corrupt but I could not delete it)"; | msg += " (and the archive is probably corrupt but I could not delete it)"; | ||||
} | } | ||||
if( doUpdate ) | |||||
if( m_update ) | |||||
{ | { | ||||
if( !renamedFile.renameTo( zipFile ) ) | |||||
if( !renamedFile.renameTo( m_file ) ) | |||||
{ | { | ||||
msg += " (and I couldn't rename the temporary file " + | msg += " (and I couldn't rename the temporary file " + | ||||
renamedFile.getName() + " back)"; | renamedFile.getName() + " back)"; | ||||
@@ -373,13 +375,9 @@ public class Zip extends MatchingTask | |||||
throw new TaskException( msg, ioe ); | throw new TaskException( msg, ioe ); | ||||
} | } | ||||
finally | |||||
{ | |||||
cleanUp(); | |||||
} | |||||
// If we've been successful on an update, delete the temporary file | // If we've been successful on an update, delete the temporary file | ||||
if( success && doUpdate ) | |||||
if( success && m_update ) | |||||
{ | { | ||||
if( !renamedFile.delete() ) | if( !renamedFile.delete() ) | ||||
{ | { | ||||
@@ -390,15 +388,26 @@ public class Zip extends MatchingTask | |||||
} | } | ||||
} | } | ||||
protected void addFileAs( final File file, final String name ) | |||||
throws TaskException | |||||
{ | |||||
// Create a ZipFileSet for this file, and pass it up. | |||||
final ZipFileSet fs = new ZipFileSet(); | |||||
fs.setDir( file.getParentFile() ); | |||||
fs.setIncludes( file.getName() ); | |||||
fs.setFullpath( name ); | |||||
addFileset( fs ); | |||||
} | |||||
/** | /** | ||||
* Indicates if the task is adding new files into the archive as opposed to | * Indicates if the task is adding new files into the archive as opposed to | ||||
* copying back unchanged files from the backup copy | * copying back unchanged files from the backup copy | ||||
* | * | ||||
* @return The AddingNewFiles value | * @return The AddingNewFiles value | ||||
*/ | */ | ||||
protected boolean isAddingNewFiles() | |||||
protected final boolean isAddingNewFiles() | |||||
{ | { | ||||
return addingNewFiles; | |||||
return m_addingNewFiles; | |||||
} | } | ||||
/** | /** | ||||
@@ -418,16 +427,16 @@ public class Zip extends MatchingTask | |||||
File[] files = grabFiles( scanners, fileNames ); | File[] files = grabFiles( scanners, fileNames ); | ||||
if( files.length == 0 ) | if( files.length == 0 ) | ||||
{ | { | ||||
if( emptyBehavior.equals( "skip" ) ) | |||||
if( m_emptyBehavior.equals( "skip" ) ) | |||||
{ | { | ||||
final String message = "Warning: skipping " + archiveType + " archive " + zipFile + | |||||
final String message = "Warning: skipping " + m_archiveType + " archive " + zipFile + | |||||
" because no files were included."; | " because no files were included."; | ||||
getLogger().warn( message ); | getLogger().warn( message ); | ||||
return true; | return true; | ||||
} | } | ||||
else if( emptyBehavior.equals( "fail" ) ) | |||||
else if( m_emptyBehavior.equals( "fail" ) ) | |||||
{ | { | ||||
throw new TaskException( "Cannot create " + archiveType + " archive " + zipFile + | |||||
throw new TaskException( "Cannot create " + m_archiveType + " archive " + zipFile + | |||||
": no files were included." ); | ": no files were included." ); | ||||
} | } | ||||
else | else | ||||
@@ -597,7 +606,7 @@ public class Zip extends MatchingTask | |||||
ZipOutputStream zOut, String prefix ) | ZipOutputStream zOut, String prefix ) | ||||
throws IOException | throws IOException | ||||
{ | { | ||||
if( !doFilesonly ) | |||||
if( !m_filesonly ) | |||||
{ | { | ||||
Stack directories = new Stack(); | Stack directories = new Stack(); | ||||
int slashPos = entry.length(); | int slashPos = entry.length(); | ||||
@@ -605,7 +614,7 @@ public class Zip extends MatchingTask | |||||
while( ( slashPos = entry.lastIndexOf( (int)'/', slashPos - 1 ) ) != -1 ) | while( ( slashPos = entry.lastIndexOf( (int)'/', slashPos - 1 ) ) != -1 ) | ||||
{ | { | ||||
String dir = entry.substring( 0, slashPos + 1 ); | String dir = entry.substring( 0, slashPos + 1 ); | ||||
if( addedDirs.get( prefix + dir ) != null ) | |||||
if( m_addedDirs.get( prefix + dir ) != null ) | |||||
{ | { | ||||
break; | break; | ||||
} | } | ||||
@@ -677,26 +686,6 @@ public class Zip extends MatchingTask | |||||
} | } | ||||
} | } | ||||
/** | |||||
* Do any clean up necessary to allow this instance to be used again. <p> | |||||
* | |||||
* When we get here, the Zip file has been closed and all we need to do is | |||||
* to reset some globals.</p> | |||||
*/ | |||||
protected void cleanUp() | |||||
{ | |||||
addedDirs = new Hashtable(); | |||||
addedFiles = new ArrayList(); | |||||
filesets = new ArrayList(); | |||||
zipFile = null; | |||||
baseDir = null; | |||||
doCompress = true; | |||||
doUpdate = false; | |||||
doFilesonly = false; | |||||
addingNewFiles = false; | |||||
encoding = null; | |||||
} | |||||
/** | /** | ||||
* Create an empty zip file | * Create an empty zip file | ||||
* | * | ||||
@@ -709,7 +698,7 @@ public class Zip extends MatchingTask | |||||
// In this case using java.util.zip will not work | // In this case using java.util.zip will not work | ||||
// because it does not permit a zero-entry archive. | // because it does not permit a zero-entry archive. | ||||
// Must create it manually. | // Must create it manually. | ||||
getLogger().info( "Note: creating empty " + archiveType + " archive " + zipFile ); | |||||
getLogger().info( "Note: creating empty " + m_archiveType + " archive " + zipFile ); | |||||
try | try | ||||
{ | { | ||||
OutputStream os = new FileOutputStream( zipFile ); | OutputStream os = new FileOutputStream( zipFile ); | ||||
@@ -749,13 +738,13 @@ public class Zip extends MatchingTask | |||||
protected void zipDir( File dir, ZipOutputStream zOut, String vPath ) | protected void zipDir( File dir, ZipOutputStream zOut, String vPath ) | ||||
throws IOException | throws IOException | ||||
{ | { | ||||
if( addedDirs.get( vPath ) != null ) | |||||
if( m_addedDirs.get( vPath ) != null ) | |||||
{ | { | ||||
// don't add directories we've already added. | // don't add directories we've already added. | ||||
// no warning if we try, it is harmless in and of itself | // no warning if we try, it is harmless in and of itself | ||||
return; | return; | ||||
} | } | ||||
addedDirs.put( vPath, vPath ); | |||||
m_addedDirs.put( vPath, vPath ); | |||||
ZipEntry ze = new ZipEntry( vPath ); | ZipEntry ze = new ZipEntry( vPath ); | ||||
if( dir != null && dir.exists() ) | if( dir != null && dir.exists() ) | ||||
@@ -794,7 +783,7 @@ public class Zip extends MatchingTask | |||||
* I couldn't find any documentation on this, just found out by try | * I couldn't find any documentation on this, just found out by try | ||||
* and error. | * and error. | ||||
*/ | */ | ||||
if( !doCompress ) | |||||
if( !m_compress ) | |||||
{ | { | ||||
long size = 0; | long size = 0; | ||||
CRC32 cal = new CRC32(); | CRC32 cal = new CRC32(); | ||||
@@ -844,13 +833,13 @@ public class Zip extends MatchingTask | |||||
} | } | ||||
count = in.read( buffer, 0, buffer.length ); | count = in.read( buffer, 0, buffer.length ); | ||||
} while( count != -1 ); | } while( count != -1 ); | ||||
addedFiles.add( vPath ); | |||||
m_addedFiles.add( vPath ); | |||||
} | } | ||||
protected void zipFile( File file, ZipOutputStream zOut, String vPath ) | protected void zipFile( File file, ZipOutputStream zOut, String vPath ) | ||||
throws IOException, TaskException | throws IOException, TaskException | ||||
{ | { | ||||
if( file.equals( zipFile ) ) | |||||
if( file.equals( m_file ) ) | |||||
{ | { | ||||
throw new TaskException( "A zip file cannot include itself" ); | throw new TaskException( "A zip file cannot include itself" ); | ||||
} | } | ||||
@@ -865,17 +854,4 @@ public class Zip extends MatchingTask | |||||
fIn.close(); | fIn.close(); | ||||
} | } | ||||
} | } | ||||
/** | |||||
* Possible behaviors when there are no matching files for the task. | |||||
* | |||||
* @author RT | |||||
*/ | |||||
public static class WhenEmpty extends EnumeratedAttribute | |||||
{ | |||||
public String[] getValues() | |||||
{ | |||||
return new String[]{"fail", "skip", "create"}; | |||||
} | |||||
} | |||||
} | } |