Changeset 2880d61


Ignore:
Timestamp:
Dec 13, 2010 2:01:46 PM (10 years ago)
Author:
zzz <zzz@…>
Branches:
master
Children:
59af763
Parents:
f194f78
Message:
  • Pack200: Find and use either Oracle or Apache library at runtime;

neither required at compile time.

Files:
3 edited

Legend:

Unmodified
Added
Removed
  • apps/routerconsole/java/src/net/i2p/router/web/ConfigUpdateHandler.java

    rf194f78 r2880d61  
    6767            foo = PACK200_URLS;
    6868        } catch (ClassNotFoundException cnfe) {
    69             foo = NO_PACK200_URLS;
     69            try {
     70                Class.forName("org.apache.harmony.unpack200.Archive", false, ClassLoader.getSystemClassLoader());
     71                foo = PACK200_URLS;
     72            } catch (ClassNotFoundException cnfe2) {
     73                foo = NO_PACK200_URLS;
     74            }
    7075        }
    7176        DEFAULT_UPDATE_URL = foo;
  • build.xml

    rf194f78 r2880d61  
    77        <property name="javac.compilerargs" value="-warn:-unchecked,raw,unused,serial" />
    88    -->
    9     <!-- Add Apache Harmony's Pack200 library if you don't have java.util.jar.Pack200
    10          See core/java/src/net/i2p/util/FileUtil.java for code changes required
    11          to use this library instead of Sun's version.
    12          Or to comment it all out if you don't have either.
     9    <!-- Additional classpath. No longer required; we find pack200 classes at runtime.
     10         See core/java/src/net/i2p/util/FileUtil.java for more info.
    1311    -->
    1412    <!--
  • core/java/src/net/i2p/util/FileUtil.java

    rf194f78 r2880d61  
    1010import java.io.InputStreamReader;
    1111import java.io.OutputStream;
     12import java.lang.reflect.Constructor;
     13import java.lang.reflect.Method;
    1214import java.util.ArrayList;
    1315import java.util.Enumeration;
     
    1719import java.util.zip.ZipFile;
    1820
    19 // Pack200 import
    20 // you must also uncomment the correct line in unpack() below
    21 // For gcj, gij, etc., comment both out
     21// Pack200 now loaded dynamically in unpack() below
    2222//
    2323// For Sun, OpenJDK, IcedTea, etc, use this
    24 import java.util.jar.Pack200;
    25 
     24//import java.util.jar.Pack200;
     25//
    2626// For Apache Harmony or if you put its pack200.jar in your library directory use this
    2727//import org.apache.harmony.unpack200.Archive;
     
    232232   
    233233    /**
    234      * This won't work right if one of the two options in unpack() is commented out.
    235234     * @since 0.8.1
    236235     */
     
    241240        } catch (Exception e) {}
    242241        try {
    243             Class.forName("org.apache.harmony.pack200.Archive", false, ClassLoader.getSystemClassLoader());
     242            Class.forName("org.apache.harmony.unpack200.Archive", false, ClassLoader.getSystemClassLoader());
    244243            return true;
    245244        } catch (Exception e) {}
     
    247246    }
    248247
    249     /**
     248    private static boolean _failedOracle;
     249    private static boolean _failedApache;
     250
     251    /**
     252     * Unpack using either Oracle or Apache's unpack200 library,
     253     * with the classes discovered at runtime so neither is required at compile time.
     254     *
    250255     * Caller must close streams
     256     * @throws IOException on unpack error or if neither library is available.
     257     *         Will not throw ClassNotFoundException.
     258     * @throws org.apache.harmony.pack200.Pack200Exception which is not an IOException
    251259     * @since 0.8.1
    252260     */
    253261    private static void unpack(InputStream in, JarOutputStream out) throws Exception {
    254262        // For Sun, OpenJDK, IcedTea, etc, use this
    255         Pack200.newUnpacker().unpack(in, out);
     263        //Pack200.newUnpacker().unpack(in, out);
     264        if (!_failedOracle) {
     265            try {
     266                Class p200 = Class.forName("java.util.jar.Pack200", true, ClassLoader.getSystemClassLoader());
     267                Method newUnpacker = p200.getMethod("newUnpacker", (Class[]) null);
     268                Object unpacker = newUnpacker.invoke(null,(Object[])  null);
     269                Method unpack = unpacker.getClass().getMethod("unpack", new Class[] {InputStream.class, JarOutputStream.class});
     270                // throws IOException
     271                unpack.invoke(unpacker, new Object[] {in, out});
     272                return;
     273            } catch (ClassNotFoundException e) {
     274                _failedOracle = true;
     275                //e.printStackTrace();
     276            } catch (NoSuchMethodException e) {
     277                _failedOracle = true;
     278                //e.printStackTrace();
     279            }
     280        }
    256281
    257282        // ------------------
    258283        // For Apache Harmony or if you put its pack200.jar in your library directory use this
    259284        //(new Archive(in, out)).unpack();
    260 
     285        if (!_failedApache) {
     286            try {
     287                Class p200 = Class.forName("org.apache.harmony.unpack200.Archive", true, ClassLoader.getSystemClassLoader());
     288                Constructor newUnpacker = p200.getConstructor(new Class[] {InputStream.class, JarOutputStream.class});
     289                Object unpacker = newUnpacker.newInstance(new Object[] {in, out});
     290                Method unpack = unpacker.getClass().getMethod("unpack", (Class[]) null);
     291                // throws IOException or Pack200Exception
     292                unpack.invoke(unpacker, (Object[]) null);
     293                return;
     294            } catch (ClassNotFoundException e) {
     295                _failedApache = true;
     296                //e.printStackTrace();
     297            } catch (NoSuchMethodException e) {
     298                _failedApache = true;
     299                //e.printStackTrace();
     300            }
     301        }
    261302
    262303        // ------------------
    263304        // For gcj, gij, etc., use this
    264         //throw new IOException("Pack200 not supported");
     305        throw new IOException("Unpack200 not supported");
    265306    }
    266307
     
    379420   
    380421    /**
    381      * Usage: FileUtil (delete path | copy source dest)
     422     * Usage: FileUtil (delete path | copy source dest | unzip path.zip)
    382423     *
    383424     */
    384425    public static void main(String args[]) {
    385426        if ( (args == null) || (args.length < 2) ) {
    386             testRmdir();
     427            System.err.println("Usage: delete path | copy source dest | unzip path.zip");
     428            //testRmdir();
    387429        } else if ("delete".equals(args[0])) {
    388430            boolean deleted = FileUtil.rmdir(args[1], false);
     
    408450    }
    409451   
     452  /*****
    410453    private static void testRmdir() {
    411454        File t = new File("rmdirTest/test/subdir/blah");
     
    418461            System.out.println("PASS: rmdirTest deleted");
    419462    }
     463   *****/
    420464}
Note: See TracChangeset for help on using the changeset viewer.