Understands Class-Path: attribute in manifest emul
authorJaroslav Tulach <jaroslav.tulach@apidesign.org>
Tue, 05 Feb 2013 13:20:07 +0100
branchemul
changeset 672add357fd6c5c
parent 671 99fa4fe6b980
child 673 62b514a70382
Understands Class-Path: attribute in manifest
emul/mini/src/main/java/org/apidesign/bck2brwsr/emul/lang/ManifestInputStream.java
vm/pom.xml
vm/src/main/java/org/apidesign/vm4brwsr/ParseCPAttr.java
vm/src/main/java/org/apidesign/vm4brwsr/Zips.java
vmtest/src/main/java/org/apidesign/bck2brwsr/vmtest/impl/GenerateZipProcessor.java
vmtest/src/test/java/org/apidesign/bck2brwsr/vmtest/impl/ZipFileTest.java
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/emul/mini/src/main/java/org/apidesign/bck2brwsr/emul/lang/ManifestInputStream.java	Tue Feb 05 13:20:07 2013 +0100
     1.3 @@ -0,0 +1,228 @@
     1.4 +/*
     1.5 + * Copyright (c) 1997, 2008, Oracle and/or its affiliates. All rights reserved.
     1.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     1.7 + *
     1.8 + * This code is free software; you can redistribute it and/or modify it
     1.9 + * under the terms of the GNU General Public License version 2 only, as
    1.10 + * published by the Free Software Foundation.  Oracle designates this
    1.11 + * particular file as subject to the "Classpath" exception as provided
    1.12 + * by Oracle in the LICENSE file that accompanied this code.
    1.13 + *
    1.14 + * This code is distributed in the hope that it will be useful, but WITHOUT
    1.15 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
    1.16 + * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
    1.17 + * version 2 for more details (a copy is included in the LICENSE file that
    1.18 + * accompanied this code).
    1.19 + *
    1.20 + * You should have received a copy of the GNU General Public License version
    1.21 + * 2 along with this work; if not, write to the Free Software Foundation,
    1.22 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
    1.23 + *
    1.24 + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
    1.25 + * or visit www.oracle.com if you need additional information or have any
    1.26 + * questions.
    1.27 + */
    1.28 +package org.apidesign.bck2brwsr.emul.lang;
    1.29 +
    1.30 +import java.io.FilterInputStream;
    1.31 +import java.io.IOException;
    1.32 +import java.io.InputStream;
    1.33 +
    1.34 +/*
    1.35 + * A fast buffered input stream for parsing manifest files.
    1.36 + * 
    1.37 + * Taken from java.util.jar.Manifest.FastInputStream and modified to be
    1.38 + * independent of other Manifest functionality.
    1.39 + */
    1.40 +public abstract class ManifestInputStream extends FilterInputStream {
    1.41 +    private byte[] buf;
    1.42 +    private int count = 0;
    1.43 +    private int pos = 0;
    1.44 +
    1.45 +    protected ManifestInputStream(InputStream in) {
    1.46 +        this(in, 8192);
    1.47 +    }
    1.48 +
    1.49 +    protected ManifestInputStream(InputStream in, int size) {
    1.50 +        super(in);
    1.51 +        buf = new byte[size];
    1.52 +    }
    1.53 +
    1.54 +    public int read() throws IOException {
    1.55 +        if (pos >= count) {
    1.56 +            fill();
    1.57 +            if (pos >= count) {
    1.58 +                return -1;
    1.59 +            }
    1.60 +        }
    1.61 +        return buf[pos++] & 0xff;
    1.62 +    }
    1.63 +
    1.64 +    public int read(byte[] b, int off, int len) throws IOException {
    1.65 +        int avail = count - pos;
    1.66 +        if (avail <= 0) {
    1.67 +            if (len >= buf.length) {
    1.68 +                return in.read(b, off, len);
    1.69 +            }
    1.70 +            fill();
    1.71 +            avail = count - pos;
    1.72 +            if (avail <= 0) {
    1.73 +                return -1;
    1.74 +            }
    1.75 +        }
    1.76 +        if (len > avail) {
    1.77 +            len = avail;
    1.78 +        }
    1.79 +        System.arraycopy(buf, pos, b, off, len);
    1.80 +        pos += len;
    1.81 +        return len;
    1.82 +    }
    1.83 +
    1.84 +    /*
    1.85 +     * Reads 'len' bytes from the input stream, or until an end-of-line
    1.86 +     * is reached. Returns the number of bytes read.
    1.87 +     */
    1.88 +    public int readLine(byte[] b, int off, int len) throws IOException {
    1.89 +        byte[] tbuf = this.buf;
    1.90 +        int total = 0;
    1.91 +        while (total < len) {
    1.92 +            int avail = count - pos;
    1.93 +            if (avail <= 0) {
    1.94 +                fill();
    1.95 +                avail = count - pos;
    1.96 +                if (avail <= 0) {
    1.97 +                    return -1;
    1.98 +                }
    1.99 +            }
   1.100 +            int n = len - total;
   1.101 +            if (n > avail) {
   1.102 +                n = avail;
   1.103 +            }
   1.104 +            int tpos = pos;
   1.105 +            int maxpos = tpos + n;
   1.106 +            while (tpos < maxpos && tbuf[tpos++] != '\n') {
   1.107 +                ;
   1.108 +            }
   1.109 +            n = tpos - pos;
   1.110 +            System.arraycopy(tbuf, pos, b, off, n);
   1.111 +            off += n;
   1.112 +            total += n;
   1.113 +            pos = tpos;
   1.114 +            if (tbuf[tpos - 1] == '\n') {
   1.115 +                break;
   1.116 +            }
   1.117 +        }
   1.118 +        return total;
   1.119 +    }
   1.120 +
   1.121 +    public byte peek() throws IOException {
   1.122 +        if (pos == count) {
   1.123 +            fill();
   1.124 +        }
   1.125 +        if (pos == count) {
   1.126 +            return -1; // nothing left in buffer
   1.127 +        }
   1.128 +        return buf[pos];
   1.129 +    }
   1.130 +
   1.131 +    public int readLine(byte[] b) throws IOException {
   1.132 +        return readLine(b, 0, b.length);
   1.133 +    }
   1.134 +
   1.135 +    public long skip(long n) throws IOException {
   1.136 +        if (n <= 0) {
   1.137 +            return 0;
   1.138 +        }
   1.139 +        long avail = count - pos;
   1.140 +        if (avail <= 0) {
   1.141 +            return in.skip(n);
   1.142 +        }
   1.143 +        if (n > avail) {
   1.144 +            n = avail;
   1.145 +        }
   1.146 +        pos += n;
   1.147 +        return n;
   1.148 +    }
   1.149 +
   1.150 +    public int available() throws IOException {
   1.151 +        return (count - pos) + in.available();
   1.152 +    }
   1.153 +
   1.154 +    public void close() throws IOException {
   1.155 +        if (in != null) {
   1.156 +            in.close();
   1.157 +            in = null;
   1.158 +            buf = null;
   1.159 +        }
   1.160 +    }
   1.161 +
   1.162 +    private void fill() throws IOException {
   1.163 +        count = pos = 0;
   1.164 +        int n = in.read(buf, 0, buf.length);
   1.165 +        if (n > 0) {
   1.166 +            count = n;
   1.167 +        }
   1.168 +    }
   1.169 +    
   1.170 +    protected abstract String putValue(String key, String value);
   1.171 +
   1.172 +    public void readAttributes(byte[] lbuf) throws IOException {
   1.173 +        ManifestInputStream is = this;
   1.174 +
   1.175 +        String name = null;
   1.176 +        String value = null;
   1.177 +        byte[] lastline = null;
   1.178 +        int len;
   1.179 +        while ((len = is.readLine(lbuf)) != -1) {
   1.180 +            boolean lineContinued = false;
   1.181 +            if (lbuf[--len] != '\n') {
   1.182 +                throw new IOException("line too long");
   1.183 +            }
   1.184 +            if (len > 0 && lbuf[len - 1] == '\r') {
   1.185 +                --len;
   1.186 +            }
   1.187 +            if (len == 0) {
   1.188 +                break;
   1.189 +            }
   1.190 +            int i = 0;
   1.191 +            if (lbuf[0] == ' ') {
   1.192 +                if (name == null) {
   1.193 +                    throw new IOException("misplaced continuation line");
   1.194 +                }
   1.195 +                lineContinued = true;
   1.196 +                byte[] buf = new byte[lastline.length + len - 1];
   1.197 +                System.arraycopy(lastline, 0, buf, 0, lastline.length);
   1.198 +                System.arraycopy(lbuf, 1, buf, lastline.length, len - 1);
   1.199 +                if (is.peek() == ' ') {
   1.200 +                    lastline = buf;
   1.201 +                    continue;
   1.202 +                }
   1.203 +                value = new String(buf, 0, buf.length, "UTF8");
   1.204 +                lastline = null;
   1.205 +            } else {
   1.206 +                while (lbuf[i++] != ':') {
   1.207 +                    if (i >= len) {
   1.208 +                        throw new IOException("invalid header field");
   1.209 +                    }
   1.210 +                }
   1.211 +                if (lbuf[i++] != ' ') {
   1.212 +                    throw new IOException("invalid header field");
   1.213 +                }
   1.214 +                name = new String(lbuf, 0, 0, i - 2);
   1.215 +                if (is.peek() == ' ') {
   1.216 +                    lastline = new byte[len - i];
   1.217 +                    System.arraycopy(lbuf, i, lastline, 0, len - i);
   1.218 +                    continue;
   1.219 +                }
   1.220 +                value = new String(lbuf, i, len - i, "UTF8");
   1.221 +            }
   1.222 +            try {
   1.223 +                if ((putValue(name, value) != null) && (!lineContinued)) {
   1.224 +                    throw new IOException("Duplicate name in Manifest: " + name + ".\n" + "Ensure that the manifest does not " + "have duplicate entries, and\n" + "that blank lines separate " + "individual sections in both your\n" + "manifest and in the META-INF/MANIFEST.MF " + "entry in the jar file.");
   1.225 +                }
   1.226 +            } catch (IllegalArgumentException e) {
   1.227 +                throw new IOException("invalid header field name: " + name);
   1.228 +            }
   1.229 +        }
   1.230 +    }
   1.231 +}
     2.1 --- a/vm/pom.xml	Tue Feb 05 13:19:06 2013 +0100
     2.2 +++ b/vm/pom.xml	Tue Feb 05 13:20:07 2013 +0100
     2.3 @@ -85,19 +85,19 @@
     2.4      <dependency>
     2.5        <groupId>${project.groupId}</groupId>
     2.6        <artifactId>core</artifactId>
     2.7 -      <version>0.3-SNAPSHOT</version>
     2.8 +      <version>${project.version}</version>
     2.9        <type>jar</type>
    2.10      </dependency>
    2.11      <dependency>
    2.12        <groupId>${project.groupId}</groupId>
    2.13        <artifactId>emul.mini</artifactId>
    2.14 -      <version>0.3-SNAPSHOT</version>
    2.15 -      <scope>test</scope>
    2.16 +      <version>${project.version}</version>
    2.17 +      <scope>compile</scope>
    2.18      </dependency>
    2.19      <dependency>
    2.20        <groupId>${project.groupId}</groupId>
    2.21        <artifactId>javap</artifactId>
    2.22 -      <version>0.3-SNAPSHOT</version>
    2.23 +      <version>${project.version}</version>
    2.24      </dependency>
    2.25    </dependencies>
    2.26  </project>
     3.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     3.2 +++ b/vm/src/main/java/org/apidesign/vm4brwsr/ParseCPAttr.java	Tue Feb 05 13:20:07 2013 +0100
     3.3 @@ -0,0 +1,49 @@
     3.4 +/**
     3.5 + * Back 2 Browser Bytecode Translator
     3.6 + * Copyright (C) 2012 Jaroslav Tulach <jaroslav.tulach@apidesign.org>
     3.7 + *
     3.8 + * This program is free software: you can redistribute it and/or modify
     3.9 + * it under the terms of the GNU General Public License as published by
    3.10 + * the Free Software Foundation, version 2 of the License.
    3.11 + *
    3.12 + * This program is distributed in the hope that it will be useful,
    3.13 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
    3.14 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    3.15 + * GNU General Public License for more details.
    3.16 + *
    3.17 + * You should have received a copy of the GNU General Public License
    3.18 + * along with this program. Look for COPYING file in the top folder.
    3.19 + * If not, see http://opensource.org/licenses/GPL-2.0.
    3.20 + */
    3.21 +package org.apidesign.vm4brwsr;
    3.22 +
    3.23 +import java.io.IOException;
    3.24 +import java.io.InputStream;
    3.25 +import org.apidesign.bck2brwsr.emul.lang.ManifestInputStream;
    3.26 +
    3.27 +/**
    3.28 + *
    3.29 + * @author Jaroslav Tulach <jtulach@netbeans.org>
    3.30 + */
    3.31 +final class ParseCPAttr extends ManifestInputStream {
    3.32 +    private String cp;
    3.33 +
    3.34 +    public ParseCPAttr(InputStream is) throws IOException {
    3.35 +        super(is);
    3.36 +        readAttributes(new byte[512]);
    3.37 +    }
    3.38 +
    3.39 +    @Override
    3.40 +    protected String putValue(String key, String value) {
    3.41 +        if ("Class-Path".equals(key)) {
    3.42 +            cp = value;
    3.43 +        }
    3.44 +        return null;
    3.45 +    }
    3.46 +
    3.47 +    @Override
    3.48 +    public String toString() {
    3.49 +        return cp;
    3.50 +    }
    3.51 +    
    3.52 +}
     4.1 --- a/vm/src/main/java/org/apidesign/vm4brwsr/Zips.java	Tue Feb 05 13:19:06 2013 +0100
     4.2 +++ b/vm/src/main/java/org/apidesign/vm4brwsr/Zips.java	Tue Feb 05 13:20:07 2013 +0100
     4.3 @@ -17,7 +17,9 @@
     4.4   */
     4.5  package org.apidesign.vm4brwsr;
     4.6  
     4.7 +import java.io.ByteArrayInputStream;
     4.8  import java.io.IOException;
     4.9 +import java.io.InputStream;
    4.10  import java.net.URL;
    4.11  import java.util.zip.ZipEntry;
    4.12  import java.util.zip.ZipInputStream;
    4.13 @@ -39,7 +41,13 @@
    4.14              Object c = classpath[i];
    4.15              if (c instanceof String) {
    4.16                  try {
    4.17 -                    c = classpath[i] = toZip((String)c);
    4.18 +                    String url = (String)c;
    4.19 +                    final Zips z = toZip(url);
    4.20 +                    c = classpath[i] = z;
    4.21 +                    final byte[] man = z.findRes("META-INF/MANIFEST.MF");
    4.22 +                    if (man != null) {
    4.23 +                        processClassPathAttr(man, url, classpath);
    4.24 +                    }
    4.25                  } catch (IOException ex) {
    4.26                      classpath[i] = ex;
    4.27                  }
    4.28 @@ -87,6 +95,38 @@
    4.29          return z;
    4.30      }
    4.31  
    4.32 +    private static void processClassPathAttr(final byte[] man, String url, Object[] classpath) throws IOException {
    4.33 +        try (InputStream is = initIS(new ByteArrayInputStream(man))) {
    4.34 +            String cp = is.toString();
    4.35 +            if (cp == null) {
    4.36 +                return;
    4.37 +            }
    4.38 +            for (int p = 0; p < cp.length();) {
    4.39 +                int n = cp.indexOf(':', p);
    4.40 +                if (n == -1) {
    4.41 +                    n = cp.length();
    4.42 +                }
    4.43 +                String el = cp.substring(p, n);
    4.44 +                URL u = new URL(new URL(url), el);
    4.45 +                classpath = addToArray(classpath, u.toString());
    4.46 +                p = n;
    4.47 +            }
    4.48 +        }
    4.49 +    }
    4.50 +
    4.51 +    private static InputStream initIS(InputStream is) throws IOException {
    4.52 +        return new ParseCPAttr(is);
    4.53 +    }
    4.54 +    
    4.55 +    private static Object[] addToArray(Object[] arr, String value) {
    4.56 +        final int last = arr.length;
    4.57 +        Object[] ret = enlargeArray(arr, last + 1);
    4.58 +        ret[last] = value;
    4.59 +        return ret;
    4.60 +    }
    4.61 +
    4.62 +    @JavaScriptBody(args = { "arr", "len" }, body = "while (arr.length < len) arr.push(null); return arr;throw('Arr: ' + arr);")
    4.63 +    private static native Object[] enlargeArray(Object[] arr, int len);
    4.64      @JavaScriptBody(args = { "arr", "len" }, body = "while (arr.length < len) arr.push(0);")
    4.65      private static native void enlargeArray(byte[] arr, int len);
    4.66  
     5.1 --- a/vmtest/src/main/java/org/apidesign/bck2brwsr/vmtest/impl/GenerateZipProcessor.java	Tue Feb 05 13:19:06 2013 +0100
     5.2 +++ b/vmtest/src/main/java/org/apidesign/bck2brwsr/vmtest/impl/GenerateZipProcessor.java	Tue Feb 05 13:20:07 2013 +0100
     5.3 @@ -25,6 +25,7 @@
     5.4  import java.util.jar.JarOutputStream;
     5.5  import java.util.jar.Manifest;
     5.6  import javax.annotation.processing.AbstractProcessor;
     5.7 +import javax.annotation.processing.Filer;
     5.8  import javax.annotation.processing.Processor;
     5.9  import javax.annotation.processing.RoundEnvironment;
    5.10  import javax.annotation.processing.SupportedAnnotationTypes;
    5.11 @@ -73,7 +74,8 @@
    5.12      }
    5.13  
    5.14      private void generateJar(PackageElement pe, GenerateZip gz, Element e) throws IOException {
    5.15 -        FileObject res = processingEnv.getFiler().createResource(
    5.16 +        final Filer filer = processingEnv.getFiler();
    5.17 +        FileObject res = filer.createResource(
    5.18              StandardLocation.CLASS_OUTPUT, 
    5.19              pe.getQualifiedName().toString(), 
    5.20              gz.name(), e
    5.21 @@ -93,6 +95,7 @@
    5.22              jar.write(arr[i + 1].getBytes("UTF-8"));
    5.23              jar.closeEntry();
    5.24          }
    5.25 +        jar.close();
    5.26      }
    5.27 -    
    5.28 +
    5.29  }
     6.1 --- a/vmtest/src/test/java/org/apidesign/bck2brwsr/vmtest/impl/ZipFileTest.java	Tue Feb 05 13:19:06 2013 +0100
     6.2 +++ b/vmtest/src/test/java/org/apidesign/bck2brwsr/vmtest/impl/ZipFileTest.java	Tue Feb 05 13:20:07 2013 +0100
     6.3 @@ -76,6 +76,28 @@
     6.4          assertEquals(ret, "Hello World!", "Can read the bytes");
     6.5      }
     6.6      
     6.7 +    @GenerateZip(name = "cpattr.zip", contents = { 
     6.8 +        "META-INF/MANIFEST.MF", "Manifest-Version: 1.0\n"
     6.9 +        + "Created-By: hand\n"
    6.10 +        + "Class-Path: realJar.jar\n\n\n"
    6.11 +    })
    6.12 +    @Http({
    6.13 +        @Http.Resource(path = "/readComplexEntry.jar", mimeType = "x-application/zip", content = "", resource="cpattr.zip"),
    6.14 +        @Http.Resource(path = "/realJar.jar", mimeType = "x-application/zip", content = "", resource="readAnEntry.zip"),
    6.15 +    })
    6.16 +    @BrwsrTest  public void understandsClassPathAttr() throws IOException {
    6.17 +        Object res = loadVMResource("/my/main/file.txt", "/readComplexEntry.jar");
    6.18 +        assert res instanceof InputStream : "Got array of bytes: " + res;
    6.19 +        InputStream is = (InputStream)res;
    6.20 +        
    6.21 +        byte[] arr = new byte[4096];
    6.22 +        int len = is.read(arr);
    6.23 +        
    6.24 +        final String ret = new String(arr, 0, len, "UTF-8");
    6.25 +
    6.26 +        assertEquals(ret, "Hello World!", "Can read the bytes from secondary JAR");
    6.27 +    }
    6.28 +    
    6.29      private static void assertEquals(Object real, Object exp, String msg) {
    6.30          assert Objects.equals(exp, real) : msg + " exp: " + exp + " real: " + real;
    6.31      }