diff options
author | David Ostrovsky <david@ostrovsky.org> | 2015-02-15 20:31:29 +0100 |
---|---|---|
committer | David Ostrovsky <david@ostrovsky.org> | 2015-02-20 01:40:06 +0100 |
commit | c0c4c6f09ac1d601a5d9fe9855e36e4b96b10335 (patch) | |
tree | ca02a75898d38203c510efef4037553e597691b4 /org.eclipse.jgit.archive/src | |
parent | 6c1f7393882baf8464859136a70199ea96fcae0f (diff) | |
download | jgit-c0c4c6f09ac1d601a5d9fe9855e36e4b96b10335.tar.gz jgit-c0c4c6f09ac1d601a5d9fe9855e36e4b96b10335.zip |
ArchiveCommand: Allow to pass options to underlying stream
Current ArchiveCommand design doesn't allow to pass in options to
underlying stream implementations. To overcome this, client has to
implement custom format implementation (it cannot be derived from
the existing one, because the classes are marked as final), and set
the options using ThreadLocal, before the method
ArchiveOutputStream createArchiveOutputStream(OutputStream s)
is get called.
This change extends the ArchiveCommand.Format by allowing to pass
option map during creation of ArchiveOutputStream.
ArchiveCommand is extended correspondingly. That way client can
easily pass options to the underlying streams:
Map<String, Object> level = ImmutableMap.<String, Object> of(
"level", new Integer(9));
new ArchiveCommand(repo)
.setFormat("zip")
.setFormatOptions(level)
.setTree(tree)
.setPaths(paths)
.setPrefix(prefix)
.setOutputStream(sidebandOut)
.call();
Change-Id: I1d92a1e5249117487da39d19c7593e4b812ad97a
Signed-off-by: David Ostrovsky <david@ostrovsky.org>
Diffstat (limited to 'org.eclipse.jgit.archive/src')
6 files changed, 136 insertions, 13 deletions
diff --git a/org.eclipse.jgit.archive/src/org/eclipse/jgit/archive/BaseFormat.java b/org.eclipse.jgit.archive/src/org/eclipse/jgit/archive/BaseFormat.java new file mode 100644 index 0000000000..6803251f93 --- /dev/null +++ b/org.eclipse.jgit.archive/src/org/eclipse/jgit/archive/BaseFormat.java @@ -0,0 +1,81 @@ +/* + * Copyright (C) 2015, David Ostrovsky <david@ostrovsky.org> + * and other copyright owners as documented in the project's IP log. + * + * This program and the accompanying materials are made available + * under the terms of the Eclipse Distribution License v1.0 which + * accompanies this distribution, is reproduced below, and is + * available at http://www.eclipse.org/org/documents/edl-v10.php + * + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or + * without modification, are permitted provided that the following + * conditions are met: + * + * - Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * + * - Redistributions in binary form must reproduce the above + * copyright notice, this list of conditions and the following + * disclaimer in the documentation and/or other materials provided + * with the distribution. + * + * - Neither the name of the Eclipse Foundation, Inc. nor the + * names of its contributors may be used to endorse or promote + * products derived from this software without specific prior + * written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND + * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, + * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES + * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT + * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; + * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER + * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, + * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF + * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +package org.eclipse.jgit.archive; + +import java.beans.Statement; +import java.io.IOException; +import java.util.Map; + +import org.apache.commons.compress.archivers.ArchiveOutputStream; +import org.eclipse.jgit.util.StringUtils; + +/** + * Base format class + */ +public class BaseFormat { + + /** + * Apply options to archive output stream + * + * @param s + * stream to apply options to + * @param o + * options map + * @return stream with option applied + * @throws IOException + */ + protected ArchiveOutputStream applyFormatOptions(ArchiveOutputStream s, + Map<String, Object> o) throws IOException { + for (Map.Entry<String, Object> p : o.entrySet()) { + try { + new Statement(s, + "set" + StringUtils.capitalize(p.getKey()), + new Object[]{p.getValue()}).execute(); + } catch (Exception e) { + throw new IOException("cannot set option: " + p.getKey(), e); + } + } + return s; + } +} diff --git a/org.eclipse.jgit.archive/src/org/eclipse/jgit/archive/TarFormat.java b/org.eclipse.jgit.archive/src/org/eclipse/jgit/archive/TarFormat.java index 9a0e3040fa..c510c64c9d 100644 --- a/org.eclipse.jgit.archive/src/org/eclipse/jgit/archive/TarFormat.java +++ b/org.eclipse.jgit.archive/src/org/eclipse/jgit/archive/TarFormat.java @@ -44,10 +44,11 @@ package org.eclipse.jgit.archive; import java.io.IOException; import java.io.OutputStream; +import java.text.MessageFormat; import java.util.Arrays; import java.util.Collections; import java.util.List; -import java.text.MessageFormat; +import java.util.Map; import org.apache.commons.compress.archivers.ArchiveOutputStream; import org.apache.commons.compress.archivers.tar.TarArchiveEntry; @@ -61,15 +62,23 @@ import org.eclipse.jgit.lib.ObjectLoader; /** * Unix TAR format (ustar + some PAX extensions). */ -public final class TarFormat implements ArchiveCommand.Format<ArchiveOutputStream> { +public final class TarFormat extends BaseFormat implements + ArchiveCommand.Format<ArchiveOutputStream> { private static final List<String> SUFFIXES = Collections .unmodifiableList(Arrays.asList(".tar")); //$NON-NLS-1$ - public ArchiveOutputStream createArchiveOutputStream(OutputStream s) { + public ArchiveOutputStream createArchiveOutputStream(OutputStream s) + throws IOException { + return createArchiveOutputStream(s, + Collections.<String, Object> emptyMap()); + } + + public ArchiveOutputStream createArchiveOutputStream(OutputStream s, + Map<String, Object> o) throws IOException { TarArchiveOutputStream out = new TarArchiveOutputStream(s, "UTF-8"); //$NON-NLS-1$ out.setLongFileMode(TarArchiveOutputStream.LONGFILE_POSIX); out.setBigNumberMode(TarArchiveOutputStream.BIGNUMBER_POSIX); - return out; + return applyFormatOptions(out, o); } public void putEntry(ArchiveOutputStream out, diff --git a/org.eclipse.jgit.archive/src/org/eclipse/jgit/archive/Tbz2Format.java b/org.eclipse.jgit.archive/src/org/eclipse/jgit/archive/Tbz2Format.java index 65e1e79642..ba5348a154 100644 --- a/org.eclipse.jgit.archive/src/org/eclipse/jgit/archive/Tbz2Format.java +++ b/org.eclipse.jgit.archive/src/org/eclipse/jgit/archive/Tbz2Format.java @@ -47,6 +47,7 @@ import java.io.OutputStream; import java.util.Arrays; import java.util.Collections; import java.util.List; +import java.util.Map; import org.apache.commons.compress.archivers.ArchiveOutputStream; import org.apache.commons.compress.compressors.bzip2.BZip2CompressorOutputStream; @@ -57,7 +58,8 @@ import org.eclipse.jgit.lib.ObjectLoader; /** * bzip2-compressed tarball (tar.bz2) format. */ -public final class Tbz2Format implements ArchiveCommand.Format<ArchiveOutputStream> { +public final class Tbz2Format extends BaseFormat implements + ArchiveCommand.Format<ArchiveOutputStream> { private static final List<String> SUFFIXES = Collections .unmodifiableList(Arrays.asList(".tar.bz2", ".tbz", ".tbz2")); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ @@ -65,8 +67,14 @@ public final class Tbz2Format implements ArchiveCommand.Format<ArchiveOutputStre public ArchiveOutputStream createArchiveOutputStream(OutputStream s) throws IOException { + return createArchiveOutputStream(s, + Collections.<String, Object> emptyMap()); + } + + public ArchiveOutputStream createArchiveOutputStream(OutputStream s, + Map<String, Object> o) throws IOException { BZip2CompressorOutputStream out = new BZip2CompressorOutputStream(s); - return tarFormat.createArchiveOutputStream(out); + return tarFormat.createArchiveOutputStream(out, o); } public void putEntry(ArchiveOutputStream out, diff --git a/org.eclipse.jgit.archive/src/org/eclipse/jgit/archive/TgzFormat.java b/org.eclipse.jgit.archive/src/org/eclipse/jgit/archive/TgzFormat.java index e13c88a044..71adca0e5d 100644 --- a/org.eclipse.jgit.archive/src/org/eclipse/jgit/archive/TgzFormat.java +++ b/org.eclipse.jgit.archive/src/org/eclipse/jgit/archive/TgzFormat.java @@ -47,6 +47,7 @@ import java.io.OutputStream; import java.util.Arrays; import java.util.Collections; import java.util.List; +import java.util.Map; import org.apache.commons.compress.archivers.ArchiveOutputStream; import org.apache.commons.compress.compressors.gzip.GzipCompressorOutputStream; @@ -57,7 +58,8 @@ import org.eclipse.jgit.lib.ObjectLoader; /** * gzip-compressed tarball (tar.gz) format. */ -public final class TgzFormat implements ArchiveCommand.Format<ArchiveOutputStream> { +public final class TgzFormat extends BaseFormat implements + ArchiveCommand.Format<ArchiveOutputStream> { private static final List<String> SUFFIXES = Collections .unmodifiableList(Arrays.asList(".tar.gz", ".tgz")); //$NON-NLS-1$ //$NON-NLS-2$ @@ -65,8 +67,14 @@ public final class TgzFormat implements ArchiveCommand.Format<ArchiveOutputStrea public ArchiveOutputStream createArchiveOutputStream(OutputStream s) throws IOException { + return createArchiveOutputStream(s, + Collections.<String, Object> emptyMap()); + } + + public ArchiveOutputStream createArchiveOutputStream(OutputStream s, + Map<String, Object> o) throws IOException { GzipCompressorOutputStream out = new GzipCompressorOutputStream(s); - return tarFormat.createArchiveOutputStream(out); + return tarFormat.createArchiveOutputStream(out, o); } public void putEntry(ArchiveOutputStream out, diff --git a/org.eclipse.jgit.archive/src/org/eclipse/jgit/archive/TxzFormat.java b/org.eclipse.jgit.archive/src/org/eclipse/jgit/archive/TxzFormat.java index d74ca9ba08..1972359135 100644 --- a/org.eclipse.jgit.archive/src/org/eclipse/jgit/archive/TxzFormat.java +++ b/org.eclipse.jgit.archive/src/org/eclipse/jgit/archive/TxzFormat.java @@ -47,6 +47,7 @@ import java.io.OutputStream; import java.util.Arrays; import java.util.Collections; import java.util.List; +import java.util.Map; import org.apache.commons.compress.archivers.ArchiveOutputStream; import org.apache.commons.compress.compressors.xz.XZCompressorOutputStream; @@ -57,7 +58,8 @@ import org.eclipse.jgit.lib.ObjectLoader; /** * Xz-compressed tar (tar.xz) format. */ -public final class TxzFormat implements ArchiveCommand.Format<ArchiveOutputStream> { +public final class TxzFormat extends BaseFormat implements + ArchiveCommand.Format<ArchiveOutputStream> { private static final List<String> SUFFIXES = Collections .unmodifiableList(Arrays.asList(".tar.xz", ".txz")); //$NON-NLS-1$ //$NON-NLS-2$ @@ -65,8 +67,14 @@ public final class TxzFormat implements ArchiveCommand.Format<ArchiveOutputStrea public ArchiveOutputStream createArchiveOutputStream(OutputStream s) throws IOException { + return createArchiveOutputStream(s, + Collections.<String, Object> emptyMap()); + } + + public ArchiveOutputStream createArchiveOutputStream(OutputStream s, + Map<String, Object> o) throws IOException { XZCompressorOutputStream out = new XZCompressorOutputStream(s); - return tarFormat.createArchiveOutputStream(out); + return tarFormat.createArchiveOutputStream(out, o); } public void putEntry(ArchiveOutputStream out, diff --git a/org.eclipse.jgit.archive/src/org/eclipse/jgit/archive/ZipFormat.java b/org.eclipse.jgit.archive/src/org/eclipse/jgit/archive/ZipFormat.java index 988ef90f9d..0b5108ad04 100644 --- a/org.eclipse.jgit.archive/src/org/eclipse/jgit/archive/ZipFormat.java +++ b/org.eclipse.jgit.archive/src/org/eclipse/jgit/archive/ZipFormat.java @@ -48,6 +48,7 @@ import java.text.MessageFormat; import java.util.Arrays; import java.util.Collections; import java.util.List; +import java.util.Map; import org.apache.commons.compress.archivers.ArchiveOutputStream; import org.apache.commons.compress.archivers.zip.ZipArchiveEntry; @@ -60,12 +61,20 @@ import org.eclipse.jgit.lib.ObjectLoader; /** * PKWARE's ZIP format. */ -public final class ZipFormat implements ArchiveCommand.Format<ArchiveOutputStream> { +public final class ZipFormat extends BaseFormat implements + ArchiveCommand.Format<ArchiveOutputStream> { private static final List<String> SUFFIXES = Collections .unmodifiableList(Arrays.asList(".zip")); //$NON-NLS-1$ - public ArchiveOutputStream createArchiveOutputStream(OutputStream s) { - return new ZipArchiveOutputStream(s); + public ArchiveOutputStream createArchiveOutputStream(OutputStream s) + throws IOException { + return createArchiveOutputStream(s, + Collections.<String, Object> emptyMap()); + } + + public ArchiveOutputStream createArchiveOutputStream(OutputStream s, + Map<String, Object> o) throws IOException { + return applyFormatOptions(new ZipArchiveOutputStream(s), o); } public void putEntry(ArchiveOutputStream out, |