Author: bodewig Date: Wed Aug 5 15:43:42 2009 New Revision: 801271 URL: http://svn.apache.org/viewvc?rev=801271&view=rev Log: TarEntry resource
Added: ant/sandbox/antlibs/compress/trunk/src/main/org/apache/ant/compress/resources/ ant/sandbox/antlibs/compress/trunk/src/main/org/apache/ant/compress/resources/CommonsCompressArchiveResource.java (contents, props changed) - copied, changed from r801240, ant/core/trunk/src/main/org/apache/tools/ant/types/resources/TarResource.java ant/sandbox/antlibs/compress/trunk/src/main/org/apache/ant/compress/resources/TarResource.java (contents, props changed) - copied, changed from r801240, ant/core/trunk/src/main/org/apache/tools/ant/types/resources/TarResource.java ant/sandbox/antlibs/compress/trunk/src/tests/antunit/tarentry-test.xml (contents, props changed) - copied, changed from r801240, ant/core/trunk/src/etc/testcases/types/resources/tarentry.xml Modified: ant/sandbox/antlibs/compress/trunk/src/main/org/apache/ant/compress/antlib.xml Modified: ant/sandbox/antlibs/compress/trunk/src/main/org/apache/ant/compress/antlib.xml URL: http://svn.apache.org/viewvc/ant/sandbox/antlibs/compress/trunk/src/main/org/apache/ant/compress/antlib.xml?rev=801271&r1=801270&r2=801271&view=diff ============================================================================== --- ant/sandbox/antlibs/compress/trunk/src/main/org/apache/ant/compress/antlib.xml (original) +++ ant/sandbox/antlibs/compress/trunk/src/main/org/apache/ant/compress/antlib.xml Wed Aug 5 15:43:42 2009 @@ -32,4 +32,9 @@ name="unzip" classname="org.apache.ant.compress.taskdefs.Unzip" /> + + <typedef + name="tarentry" + classname="org.apache.ant.compress.resources.TarResource" + /> </antlib> Copied: ant/sandbox/antlibs/compress/trunk/src/main/org/apache/ant/compress/resources/CommonsCompressArchiveResource.java (from r801240, ant/core/trunk/src/main/org/apache/tools/ant/types/resources/TarResource.java) URL: http://svn.apache.org/viewvc/ant/sandbox/antlibs/compress/trunk/src/main/org/apache/ant/compress/resources/CommonsCompressArchiveResource.java?p2=ant/sandbox/antlibs/compress/trunk/src/main/org/apache/ant/compress/resources/CommonsCompressArchiveResource.java&p1=ant/core/trunk/src/main/org/apache/tools/ant/types/resources/TarResource.java&r1=801240&r2=801271&rev=801271&view=diff ============================================================================== --- ant/core/trunk/src/main/org/apache/tools/ant/types/resources/TarResource.java (original) +++ ant/sandbox/antlibs/compress/trunk/src/main/org/apache/ant/compress/resources/CommonsCompressArchiveResource.java Wed Aug 5 15:43:42 2009 @@ -15,63 +15,65 @@ * limitations under the License. * */ -package org.apache.tools.ant.types.resources; +package org.apache.ant.compress.resources; import java.io.File; import java.io.InputStream; import java.io.OutputStream; import java.io.IOException; +import java.util.Date; import org.apache.tools.ant.Project; import org.apache.tools.ant.BuildException; import org.apache.tools.ant.types.Resource; +import org.apache.tools.ant.types.resources.ArchiveResource; import org.apache.tools.ant.util.FileUtils; -import org.apache.tools.tar.TarEntry; -import org.apache.tools.tar.TarInputStream; +import org.apache.commons.compress.archivers.ArchiveEntry; +import org.apache.commons.compress.archivers.ArchiveInputStream; /** - * A Resource representation of an entry in a tar archive. - * @since Ant 1.7 + * A Resource representation of an entry in a commons compress archive. */ -public class TarResource extends ArchiveResource { - - private String userName = ""; - private String groupName = ""; - private int uid; - private int gid; +public abstract class CommonsCompressArchiveResource extends ArchiveResource { /** * Default constructor. */ - public TarResource() { + public CommonsCompressArchiveResource() { } /** - * Construct a TarResource representing the specified + * Construct a Resource representing the specified * entry in the specified archive. * @param a the archive as File. - * @param e the TarEntry. + * @param e the ArchiveEntry. */ - public TarResource(File a, TarEntry e) { + public CommonsCompressArchiveResource(File a, ArchiveEntry e) { super(a, true); setEntry(e); } /** - * Construct a TarResource representing the specified + * Construct a Resource representing the specified * entry in the specified archive. * @param a the archive as Resource. - * @param e the TarEntry. + * @param e the ArchiveEntry. */ - public TarResource(Resource a, TarEntry e) { + public CommonsCompressArchiveResource(Resource a, ArchiveEntry e) { super(a, true); setEntry(e); } /** + * Provides an ArchiveInputStream to a given archive. + */ + protected abstract ArchiveInputStream getArchiveStream(InputStream is) + throws IOException; + + /** * Return an InputStream for reading the contents of this Resource. * @return an InputStream object. - * @throws IOException if the tar file cannot be opened, + * @throws IOException if the archive cannot be opened, * or the entry cannot be read. */ public InputStream getInputStream() throws IOException { @@ -79,10 +81,10 @@ return ((Resource) getCheckedRef()).getInputStream(); } Resource archive = getArchive(); - final TarInputStream i = new TarInputStream(archive.getInputStream()); - TarEntry te = null; - while ((te = i.getNextEntry()) != null) { - if (te.getName().equals(getName())) { + final ArchiveInputStream i = getArchiveStream(archive.getInputStream()); + ArchiveEntry ae = null; + while ((ae = i.getNextEntry()) != null) { + if (ae.getName().equals(getName())) { return i; } } @@ -93,6 +95,11 @@ } /** + * The name of the archive type. + */ + protected abstract String getArchiveType(); + + /** * Get an OutputStream for the Resource. * @return an OutputStream to which content can be written. * @throws IOException if unable to provide the content of this @@ -104,48 +111,10 @@ if (isReference()) { return ((Resource) getCheckedRef()).getOutputStream(); } - throw new UnsupportedOperationException( - "Use the tar task for tar output."); - } - - /** - * @return the user name for the tar entry - */ - public String getUserName() { - if (isReference()) { - return ((TarResource) getCheckedRef()).getUserName(); - } - return userName; - } - - /** - * @return the group name for the tar entry - */ - public String getGroup() { - if (isReference()) { - return ((TarResource) getCheckedRef()).getGroup(); - } - return groupName; - } - - /** - * @return the uid for the tar entry - */ - public int getUid() { - if (isReference()) { - return ((TarResource) getCheckedRef()).getUid(); - } - return uid; - } - - /** - * @return the uid for the tar entry - */ - public int getGid() { - if (isReference()) { - return ((TarResource) getCheckedRef()).getGid(); - } - return uid; + throw new UnsupportedOperationException("Use the " + getArchiveType() + + " task for " + + getArchiveType() + + " output."); } /** @@ -153,13 +122,13 @@ */ protected void fetchEntry() { Resource archive = getArchive(); - TarInputStream i = null; + ArchiveInputStream i = null; try { - i = new TarInputStream(archive.getInputStream()); - TarEntry te = null; - while ((te = i.getNextEntry()) != null) { - if (te.getName().equals(getName())) { - setEntry(te); + i = getArchiveStream(archive.getInputStream()); + ArchiveEntry ae = null; + while ((ae = i.getNextEntry()) != null) { + if (ae.getName().equals(getName())) { + setEntry(ae); return; } } @@ -174,21 +143,27 @@ setEntry(null); } - private void setEntry(TarEntry e) { + /** + * Determines the mode for the given entry. + */ + protected abstract int getMode(ArchiveEntry e); + + /** + * Determines the last modified time for the given entry. + */ + protected abstract Date getLastModified(ArchiveEntry entry); + + protected void setEntry(ArchiveEntry e) { if (e == null) { setExists(false); return; } setName(e.getName()); setExists(true); - setLastModified(e.getModTime().getTime()); + setLastModified(getLastModified(e).getTime()); setDirectory(e.isDirectory()); setSize(e.getSize()); - setMode(e.getMode()); - userName = e.getUserName(); - groupName = e.getGroupName(); - uid = e.getUserId(); - gid = e.getGroupId(); + setMode(getMode(e)); } } Propchange: ant/sandbox/antlibs/compress/trunk/src/main/org/apache/ant/compress/resources/CommonsCompressArchiveResource.java ------------------------------------------------------------------------------ svn:eol-style = native Propchange: ant/sandbox/antlibs/compress/trunk/src/main/org/apache/ant/compress/resources/CommonsCompressArchiveResource.java ------------------------------------------------------------------------------ svn:mergeinfo = Copied: ant/sandbox/antlibs/compress/trunk/src/main/org/apache/ant/compress/resources/TarResource.java (from r801240, ant/core/trunk/src/main/org/apache/tools/ant/types/resources/TarResource.java) URL: http://svn.apache.org/viewvc/ant/sandbox/antlibs/compress/trunk/src/main/org/apache/ant/compress/resources/TarResource.java?p2=ant/sandbox/antlibs/compress/trunk/src/main/org/apache/ant/compress/resources/TarResource.java&p1=ant/core/trunk/src/main/org/apache/tools/ant/types/resources/TarResource.java&r1=801240&r2=801271&rev=801271&view=diff ============================================================================== --- ant/core/trunk/src/main/org/apache/tools/ant/types/resources/TarResource.java (original) +++ ant/sandbox/antlibs/compress/trunk/src/main/org/apache/ant/compress/resources/TarResource.java Wed Aug 5 15:43:42 2009 @@ -15,25 +15,23 @@ * limitations under the License. * */ -package org.apache.tools.ant.types.resources; +package org.apache.ant.compress.resources; import java.io.File; import java.io.InputStream; -import java.io.OutputStream; import java.io.IOException; +import java.util.Date; -import org.apache.tools.ant.Project; -import org.apache.tools.ant.BuildException; import org.apache.tools.ant.types.Resource; -import org.apache.tools.ant.util.FileUtils; -import org.apache.tools.tar.TarEntry; -import org.apache.tools.tar.TarInputStream; +import org.apache.commons.compress.archivers.ArchiveEntry; +import org.apache.commons.compress.archivers.ArchiveInputStream; +import org.apache.commons.compress.archivers.tar.TarArchiveEntry; +import org.apache.commons.compress.archivers.tar.TarArchiveInputStream; /** * A Resource representation of an entry in a tar archive. - * @since Ant 1.7 */ -public class TarResource extends ArchiveResource { +public class TarResource extends CommonsCompressArchiveResource { private String userName = ""; private String groupName = ""; @@ -52,9 +50,8 @@ * @param a the archive as File. * @param e the TarEntry. */ - public TarResource(File a, TarEntry e) { - super(a, true); - setEntry(e); + public TarResource(File a, TarArchiveEntry e) { + super(a, e); } /** @@ -63,49 +60,8 @@ * @param a the archive as Resource. * @param e the TarEntry. */ - public TarResource(Resource a, TarEntry e) { - super(a, true); - setEntry(e); - } - - /** - * Return an InputStream for reading the contents of this Resource. - * @return an InputStream object. - * @throws IOException if the tar file cannot be opened, - * or the entry cannot be read. - */ - public InputStream getInputStream() throws IOException { - if (isReference()) { - return ((Resource) getCheckedRef()).getInputStream(); - } - Resource archive = getArchive(); - final TarInputStream i = new TarInputStream(archive.getInputStream()); - TarEntry te = null; - while ((te = i.getNextEntry()) != null) { - if (te.getName().equals(getName())) { - return i; - } - } - - FileUtils.close(i); - throw new BuildException("no entry " + getName() + " in " - + getArchive()); - } - - /** - * Get an OutputStream for the Resource. - * @return an OutputStream to which content can be written. - * @throws IOException if unable to provide the content of this - * Resource as a stream. - * @throws UnsupportedOperationException if OutputStreams are not - * supported for this Resource type. - */ - public OutputStream getOutputStream() throws IOException { - if (isReference()) { - return ((Resource) getCheckedRef()).getOutputStream(); - } - throw new UnsupportedOperationException( - "Use the tar task for tar output."); + public TarResource(Resource a, TarArchiveEntry e) { + super(a, e); } /** @@ -148,47 +104,31 @@ return uid; } - /** - * fetches information from the named entry inside the archive. - */ - protected void fetchEntry() { - Resource archive = getArchive(); - TarInputStream i = null; - try { - i = new TarInputStream(archive.getInputStream()); - TarEntry te = null; - while ((te = i.getNextEntry()) != null) { - if (te.getName().equals(getName())) { - setEntry(te); - return; - } - } - } catch (IOException e) { - log(e.getMessage(), Project.MSG_DEBUG); - throw new BuildException(e); - } finally { - if (i != null) { - FileUtils.close(i); - } + protected void setEntry(ArchiveEntry e) { + super.setEntry(e); + if (e != null) { + TarArchiveEntry te = (TarArchiveEntry) e; + userName = te.getUserName(); + groupName = te.getGroupName(); + uid = te.getUserId(); + gid = te.getGroupId(); } - setEntry(null); } - private void setEntry(TarEntry e) { - if (e == null) { - setExists(false); - return; - } - setName(e.getName()); - setExists(true); - setLastModified(e.getModTime().getTime()); - setDirectory(e.isDirectory()); - setSize(e.getSize()); - setMode(e.getMode()); - userName = e.getUserName(); - groupName = e.getGroupName(); - uid = e.getUserId(); - gid = e.getGroupId(); + protected ArchiveInputStream getArchiveStream(InputStream is) + throws IOException { + return new TarArchiveInputStream(is); } + protected Date getLastModified(ArchiveEntry entry) { + return ((TarArchiveEntry) entry).getModTime(); + } + + protected int getMode(ArchiveEntry e) { + return ((TarArchiveEntry) e).getMode(); + } + + protected String getArchiveType() { + return "tar"; + } } Propchange: ant/sandbox/antlibs/compress/trunk/src/main/org/apache/ant/compress/resources/TarResource.java ------------------------------------------------------------------------------ svn:eol-style = native Propchange: ant/sandbox/antlibs/compress/trunk/src/main/org/apache/ant/compress/resources/TarResource.java ------------------------------------------------------------------------------ svn:mergeinfo = Copied: ant/sandbox/antlibs/compress/trunk/src/tests/antunit/tarentry-test.xml (from r801240, ant/core/trunk/src/etc/testcases/types/resources/tarentry.xml) URL: http://svn.apache.org/viewvc/ant/sandbox/antlibs/compress/trunk/src/tests/antunit/tarentry-test.xml?p2=ant/sandbox/antlibs/compress/trunk/src/tests/antunit/tarentry-test.xml&p1=ant/core/trunk/src/etc/testcases/types/resources/tarentry.xml&r1=801240&r2=801271&rev=801271&view=diff ============================================================================== --- ant/core/trunk/src/etc/testcases/types/resources/tarentry.xml (original) +++ ant/sandbox/antlibs/compress/trunk/src/tests/antunit/tarentry-test.xml Wed Aug 5 15:43:42 2009 @@ -15,29 +15,42 @@ See the License for the specific language governing permissions and limitations under the License. --> -<project default="not me"> - <target name="not me"> - <fail>only use from within unit tests</fail> - </target> +<project default="antunit" + xmlns:au="antlib:org.apache.ant.antunit" + xmlns:cmp="antlib:org.apache.ant.compress"> + + <import file="antunit-base.xml" /> - <property name="testout" location="testout"/> <target name="setUp"> - <mkdir dir="${testout}"/> + <mkdir dir="${output}"/> + <mkdir dir="${input}"/> </target> - <target name="uncompressSource" depends="setUp"> - <ant antfile="../../taskdefs/tar.xml" target="feather" /> - <copy todir="${testout}"> - <tarentry name="asf-logo.gif"> - <gzipresource> - <file file="../../taskdefs/expected/asf-logo.gif.tar.gz"/> - </gzipresource> - </tarentry> + <target name="testTarSource" depends="setUp"> + <copy todir="${output}"> + <cmp:tarentry name="asf-logo.gif"> + <file file="../resources/asf-logo.gif.tar"/> + </cmp:tarentry> </copy> + <au:assertFilesMatch + actual="${output}/asf-logo.gif" + expected="../resources/asf-logo.gif" + /> </target> - <target name="tearDown"> - <delete dir="${testout}"/> - <ant antfile="../../taskdefs/tar.xml" target="cleanup" /> + <target name="testUncompressSource" depends="setUp"> + <gzip destfile="${input}/asf-logo.gif.tar.gz" + src="../resources/asf-logo.gif.tar"/> + <copy todir="${output}"> + <cmp:tarentry name="asf-logo.gif"> + <gzipresource> + <file file="${input}/asf-logo.gif.tar.gz"/> + </gzipresource> + </cmp:tarentry> + </copy> + <au:assertFilesMatch + actual="${output}/asf-logo.gif" + expected="../resources/asf-logo.gif" + /> </target> -</project> \ No newline at end of file +</project> Propchange: ant/sandbox/antlibs/compress/trunk/src/tests/antunit/tarentry-test.xml ------------------------------------------------------------------------------ svn:eol-style = native Propchange: ant/sandbox/antlibs/compress/trunk/src/tests/antunit/tarentry-test.xml ------------------------------------------------------------------------------ svn:executable = * Propchange: ant/sandbox/antlibs/compress/trunk/src/tests/antunit/tarentry-test.xml ------------------------------------------------------------------------------ svn:mergeinfo =