You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@commons.apache.org by gg...@apache.org on 2022/10/04 13:02:14 UTC

[commons-compress] branch master updated: [COMPRESS-627] Pack200 causes a 'archive.3E' error if it’s not in the system class loader.

This is an automated email from the ASF dual-hosted git repository.

ggregory pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/commons-compress.git


The following commit(s) were added to refs/heads/master by this push:
     new ea9c277f [COMPRESS-627] Pack200 causes a 'archive.3E' error if it’s not in the system class loader.
ea9c277f is described below

commit ea9c277f460fb442c30a93da67c7431dba82d979
Author: Gary Gregory <ga...@gmail.com>
AuthorDate: Tue Oct 4 09:02:09 2022 -0400

    [COMPRESS-627] Pack200 causes a 'archive.3E' error if it’s not in the
    system class loader.
---
 src/changes/changes.xml                            |   3 +
 .../commons/compress/java/util/jar/Pack200.java    | 167 ++++++++++-----------
 2 files changed, 81 insertions(+), 89 deletions(-)

diff --git a/src/changes/changes.xml b/src/changes/changes.xml
index 20451bb4..f81eb86c 100644
--- a/src/changes/changes.xml
+++ b/src/changes/changes.xml
@@ -121,6 +121,9 @@ The <action> type attribute can be add,update,fix,remove.
       <action type="fix" dev="ggregory" due-to="Gary Gregory">
         Some input streams are not closed in org.apache.commons.compress.harmony.pack200.PackingUtils.
       </action>
+      <action issue="COMPRESS-627" type="fix" dev="ggregory" due-to="anatawa12, Gary Gregory">
+        Pack200 causes a 'archive.3E' error if it’s not in the system class loader.
+      </action>
       <!-- ADD -->
       <action issue="COMPRESS-602" type="add" dev="ggregory" due-to="Postelnicu George, Gary Gregory">
         Migrate zip package to use NIO #236.
diff --git a/src/main/java/org/apache/commons/compress/java/util/jar/Pack200.java b/src/main/java/org/apache/commons/compress/java/util/jar/Pack200.java
index 7c39119d..f2955b46 100644
--- a/src/main/java/org/apache/commons/compress/java/util/jar/Pack200.java
+++ b/src/main/java/org/apache/commons/compress/java/util/jar/Pack200.java
@@ -35,68 +35,6 @@ import org.apache.commons.compress.harmony.archive.internal.nls.Messages;
  */
 public abstract class Pack200 {
 
-    private static final String SYSTEM_PROPERTY_PACKER = "java.util.jar.Pack200.Packer"; //$NON-NLS-1$
-
-    private static final String SYSTEM_PROPERTY_UNPACKER = "java.util.jar.Pack200.Unpacker"; //$NON-NLS-1$
-
-    /**
-     * Prevent this class from being instantiated.
-     */
-    private Pack200() {
-        // do nothing
-    }
-
-    /**
-     * Returns a new instance of a packer engine.
-     * <p>
-     * The implementation of the packer engine is defined by the system property
-     * {@code 'java.util.jar.Pack200.Packer'}. If this system property is
-     * defined an instance of the specified class is returned, otherwise the
-     * system's default implementation is returned.
-     *
-     * @return an instance of {@code Packer}
-     */
-    public static Pack200.Packer newPacker() {
-        return (Packer) AccessController
-                .doPrivileged((PrivilegedAction<Object>) () -> {
-                    String className = System
-                            .getProperty(SYSTEM_PROPERTY_PACKER,
-                                    "org.apache.commons.compress.harmony.pack200.Pack200PackerAdapter"); //$NON-NLS-1$
-                    try {
-                        // TODO Not sure if this will cause problems with
-                        // loading the packer
-                        return ClassLoader.getSystemClassLoader().loadClass(className).newInstance();
-                    } catch (Exception e) {
-                        throw new Error(Messages.getString("archive.3E", className), e); //$NON-NLS-1$
-                    }
-                });
-
-    }
-
-    /**
-     * Returns a new instance of a unpacker engine.
-     * <p>
-     * The implementation of the unpacker engine is defined by the system
-     * property {@code 'java.util.jar.Pack200.Unpacker'}. If this system
-     * property is defined an instance of the specified class is returned,
-     * otherwise the system's default implementation is returned.
-     *
-     * @return a instance of {@code Unpacker}.
-     */
-    public static Pack200.Unpacker newUnpacker() {
-        return (Unpacker) AccessController
-                .doPrivileged((PrivilegedAction<Object>) () -> {
-                    String className = System
-                            .getProperty(SYSTEM_PROPERTY_UNPACKER,
-                                    "org.apache.commons.compress.harmony.unpack200.Pack200UnpackerAdapter");//$NON-NLS-1$
-                    try {
-                        return ClassLoader.getSystemClassLoader().loadClass(className).newInstance();
-                    } catch (Exception e) {
-                        throw new Error(Messages.getString("archive.3E", className), e); //$NON-NLS-1$
-                    }
-                });
-    }
-
     /**
      * The interface defining the API for converting a JAR file to an output
      * stream in the Pack200 format.
@@ -200,11 +138,12 @@ public abstract class Pack200 {
         String UNKNOWN_ATTRIBUTE = "pack.unknown.attribute";//$NON-NLS-1$
 
         /**
-         * Returns a sorted map of the properties of this packer.
+         * add a listener for PropertyChange events
          *
-         * @return the properties of the packer.
+         * @param listener
+         *            the listener to listen if PropertyChange events occurs
          */
-        SortedMap<String, String> properties();
+        void addPropertyChangeListener(PropertyChangeListener listener);
 
         /**
          * Pack the specified JAR file to the specified output stream.
@@ -232,12 +171,11 @@ public abstract class Pack200 {
         void pack(JarInputStream in, OutputStream out) throws IOException;
 
         /**
-         * add a listener for PropertyChange events
+         * Returns a sorted map of the properties of this packer.
          *
-         * @param listener
-         *            the listener to listen if PropertyChange events occurs
+         * @return the properties of the packer.
          */
-        void addPropertyChangeListener(PropertyChangeListener listener);
+        SortedMap<String, String> properties();
 
         /**
          * remove a listener
@@ -280,6 +218,15 @@ public abstract class Pack200 {
          */
         String TRUE = "true";//$NON-NLS-1$
 
+        /**
+         * add a listener for {@code PropertyChange} events.
+         *
+         * @param listener
+         *            the listener to listen if {@code PropertyChange} events
+         *            occurs.
+         */
+        void addPropertyChangeListener(PropertyChangeListener listener);
+
         /**
          * Returns a sorted map of the properties of this unpacker.
          *
@@ -288,16 +235,12 @@ public abstract class Pack200 {
         SortedMap<String, String> properties();
 
         /**
-         * Unpack the specified stream to the specified JAR output stream.
+         * remove a listener.
          *
-         * @param in
-         *            stream to uncompressed.
-         * @param out
-         *            JAR output stream of uncompressed data.
-         * @throws IOException
-         *             if I/O exception occurs.
+         * @param listener
+         *            listener to remove.
          */
-        void unpack(InputStream in, JarOutputStream out) throws IOException;
+        void removePropertyChangeListener(PropertyChangeListener listener);
 
         /**
          * Unpack the contents of the specified {@code File} to the specified
@@ -313,21 +256,67 @@ public abstract class Pack200 {
         void unpack(File in, JarOutputStream out) throws IOException;
 
         /**
-         * add a listener for {@code PropertyChange} events.
+         * Unpack the specified stream to the specified JAR output stream.
          *
-         * @param listener
-         *            the listener to listen if {@code PropertyChange} events
-         *            occurs.
+         * @param in
+         *            stream to uncompressed.
+         * @param out
+         *            JAR output stream of uncompressed data.
+         * @throws IOException
+         *             if I/O exception occurs.
          */
-        void addPropertyChangeListener(PropertyChangeListener listener);
+        void unpack(InputStream in, JarOutputStream out) throws IOException;
+    }
 
-        /**
-         * remove a listener.
-         *
-         * @param listener
-         *            listener to remove.
-         */
-        void removePropertyChangeListener(PropertyChangeListener listener);
+    private static final String SYSTEM_PROPERTY_PACKER = "java.util.jar.Pack200.Packer"; //$NON-NLS-1$
+
+    private static final String SYSTEM_PROPERTY_UNPACKER = "java.util.jar.Pack200.Unpacker"; //$NON-NLS-1$
+
+    static Object newInstance(final String systemProperty, final String defaultClassName) {
+        return AccessController.doPrivileged((PrivilegedAction<Object>) () -> {
+            String className = System.getProperty(systemProperty, defaultClassName);
+            try {
+                // TODO Not sure if this will cause problems loading the class
+                return Pack200.class.getClassLoader().loadClass(className).newInstance();
+            } catch (Exception e) {
+                throw new Error(Messages.getString("archive.3E", className), e); //$NON-NLS-1$
+            }
+        });
+    }
+
+    /**
+     * Returns a new instance of a packer engine.
+     * <p>
+     * The implementation of the packer engine is defined by the system property
+     * {@code 'java.util.jar.Pack200.Packer'}. If this system property is
+     * defined an instance of the specified class is returned, otherwise the
+     * system's default implementation is returned.
+     *
+     * @return an instance of {@code Packer}
+     */
+    public static Pack200.Packer newPacker() {
+        return (Packer) newInstance(SYSTEM_PROPERTY_PACKER, "org.apache.commons.compress.harmony.pack200.Pack200PackerAdapter"); //$NON-NLS-1$
+    }
+
+    /**
+     * Returns a new instance of a unpacker engine.
+     * <p>
+     * The implementation of the unpacker engine is defined by the system
+     * property {@code 'java.util.jar.Pack200.Unpacker'}. If this system
+     * property is defined an instance of the specified class is returned,
+     * otherwise the system's default implementation is returned.
+     *
+     * @return a instance of {@code Unpacker}.
+     */
+    public static Pack200.Unpacker newUnpacker() {
+        return (Unpacker) newInstance(SYSTEM_PROPERTY_UNPACKER, "org.apache.commons.compress.harmony.unpack200.Pack200UnpackerAdapter"); //$NON-NLS-1$
+    }
+
+    /**
+     * Prevent this class from being instantiated.
+     */
+    private Pack200() {
+        // do nothing
     }
 
 }