commons-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From bode...@apache.org
Subject svn commit: r1170644 - in /commons/proper/compress/trunk/src: main/java/org/apache/commons/compress/compressors/pack200/ test/java/org/apache/commons/compress/compressors/pack200/
Date Wed, 14 Sep 2011 14:33:49 GMT
Author: bodewig
Date: Wed Sep 14 14:33:48 2011
New Revision: 1170644

URL: http://svn.apache.org/viewvc?rev=1170644&view=rev
Log:
Utility class that "normalizes" a JAR archive so it can be signed without Pack200 breaking
the signature

Added:
    commons/proper/compress/trunk/src/main/java/org/apache/commons/compress/compressors/pack200/Pack200Utils.java
  (with props)
    commons/proper/compress/trunk/src/test/java/org/apache/commons/compress/compressors/pack200/
    commons/proper/compress/trunk/src/test/java/org/apache/commons/compress/compressors/pack200/Pack200UtilsTest.java
  (with props)

Added: commons/proper/compress/trunk/src/main/java/org/apache/commons/compress/compressors/pack200/Pack200Utils.java
URL: http://svn.apache.org/viewvc/commons/proper/compress/trunk/src/main/java/org/apache/commons/compress/compressors/pack200/Pack200Utils.java?rev=1170644&view=auto
==============================================================================
--- commons/proper/compress/trunk/src/main/java/org/apache/commons/compress/compressors/pack200/Pack200Utils.java
(added)
+++ commons/proper/compress/trunk/src/main/java/org/apache/commons/compress/compressors/pack200/Pack200Utils.java
Wed Sep 14 14:33:48 2011
@@ -0,0 +1,84 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+package org.apache.commons.compress.compressors.pack200;
+
+import java.io.File;
+import java.io.FileOutputStream;
+import java.io.IOException;
+import java.io.OutputStream;
+import java.util.Map;
+import java.util.jar.JarFile;
+import java.util.jar.JarOutputStream;
+import java.util.jar.Pack200;
+
+/**
+ * Utility methods for Pack200.
+ *
+ * @ThreadSafe
+ * @since Apache Commons Compress 1.3
+ */
+public class Pack200Utils {
+    private Pack200Utils() { }
+
+    /**
+     * Normalizes a JAR archive so it can be savely signed and packed.
+     *
+     * <p>As stated in <a
+     * href="http://download.oracle.com/javase/1.5.0/docs/api/java/util/jar/Pack200.Packer.html">Pack200.Packer's</a>
+     * javadocs applying a Pack200 compression to a JAR archive will
+     * in general make its sigantures invalid.  In order to prepare a
+     * JAR for signing it should be "normalized" by packing and
+     * unpacking it.  This is what this method does.</p>
+     *
+     * <p>This method does not replace the existing archive but create
+     * a new one.</p>
+     *
+     * @param from the JAR archive to normalize
+     * @param to the normalized archive
+     * @param props properties to set for the pack operation.  This
+     * method will implicitly set the segment limit to -1.
+     */
+    public static void normalize(File from, File to, Map<String, String> props)
+        throws IOException {
+        props.put(Pack200.Packer.SEGMENT_LIMIT, "-1");
+        File f = File.createTempFile("commons-compress", "pack200normalize");
+        f.deleteOnExit();
+        try {
+            OutputStream os = new FileOutputStream(f);
+            try {
+                Pack200.Packer p = Pack200.newPacker();
+                p.properties().putAll(props);
+                p.pack(new JarFile(from), os);
+                os.close();
+                os = null;
+
+                Pack200.Unpacker u = Pack200.newUnpacker();
+                os = new JarOutputStream(new FileOutputStream(to));
+                u.unpack(f, (JarOutputStream) os);
+            } finally {
+                if (os != null) {
+                    os.close();
+                }
+            }
+        } finally {
+            f.delete();
+        }
+    }
+}
\ No newline at end of file

Propchange: commons/proper/compress/trunk/src/main/java/org/apache/commons/compress/compressors/pack200/Pack200Utils.java
------------------------------------------------------------------------------
    svn:eol-style = native

Added: commons/proper/compress/trunk/src/test/java/org/apache/commons/compress/compressors/pack200/Pack200UtilsTest.java
URL: http://svn.apache.org/viewvc/commons/proper/compress/trunk/src/test/java/org/apache/commons/compress/compressors/pack200/Pack200UtilsTest.java?rev=1170644&view=auto
==============================================================================
--- commons/proper/compress/trunk/src/test/java/org/apache/commons/compress/compressors/pack200/Pack200UtilsTest.java
(added)
+++ commons/proper/compress/trunk/src/test/java/org/apache/commons/compress/compressors/pack200/Pack200UtilsTest.java
Wed Sep 14 14:33:48 2011
@@ -0,0 +1,72 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.commons.compress.compressors.pack200;
+
+import java.io.File;
+import java.io.FileInputStream;
+import java.io.FileOutputStream;
+import java.io.InputStream;
+import java.io.OutputStream;
+import java.util.HashMap;
+
+import org.apache.commons.compress.AbstractTestCase;
+import org.apache.commons.compress.archivers.ArchiveEntry;
+import org.apache.commons.compress.archivers.ArchiveInputStream;
+import org.apache.commons.compress.archivers.ArchiveStreamFactory;
+import org.apache.commons.compress.utils.IOUtils;
+
+public final class Pack200UtilsTest extends AbstractTestCase {
+
+    public void testNormalize() throws Throwable {
+        final File input = getFile("bla.jar");
+        final File[] output = createTempDirAndFile();
+        try {
+            Pack200Utils.normalize(input, output[1],
+                                   new HashMap<String, String>());
+            final FileInputStream is = new FileInputStream(output[1]);
+            try {
+                final ArchiveInputStream in = new ArchiveStreamFactory()
+                    .createArchiveInputStream("jar", is);
+
+                ArchiveEntry entry = in.getNextEntry();
+                while (entry != null) {
+                    File archiveEntry = new File(dir, entry.getName());
+                    archiveEntry.getParentFile().mkdirs();
+                    if (entry.isDirectory()) {
+                        archiveEntry.mkdir();
+                        entry = in.getNextEntry();
+                        continue;
+                    }
+                    OutputStream out = new FileOutputStream(archiveEntry);
+                    IOUtils.copy(in, out);
+                    out.close();
+                    entry = in.getNextEntry();
+                }
+
+                in.close();
+            } finally {
+                is.close();
+            }
+        } finally {
+            output[1].delete();
+            output[0].delete();
+        }
+    }
+
+}

Propchange: commons/proper/compress/trunk/src/test/java/org/apache/commons/compress/compressors/pack200/Pack200UtilsTest.java
------------------------------------------------------------------------------
    svn:eol-style = native



Mime
View raw message