jdk/test/tools/jlink/plugins/CompressorPluginTest.java
author alanb
Fri, 07 Apr 2017 08:05:54 +0000
changeset 44545 83b611b88ac8
parent 43791 116beb9c53a5
permissions -rw-r--r--
8177530: Module system implementation refresh (4/2017) Reviewed-by: mchung, alanb Contributed-by: alan.bateman@oracle.com, mandy.chung@oracle.com
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
     1
/*
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
     2
 * Copyright (c) 2015, Oracle and/or its affiliates. All rights reserved.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
     4
 *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
     7
 * published by the Free Software Foundation.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
     8
 *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    13
 * accompanied this code).
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    14
 *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    18
 *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    21
 * questions.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    22
 */
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    23
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    24
/*
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    25
 * @test
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    26
 * @summary Test zip compressor
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    27
 * @author Jean-Francois Denise
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    28
 * @modules java.base/jdk.internal.jimage.decompressor
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    29
 *          jdk.jlink/jdk.tools.jlink.internal
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    30
 *          jdk.jlink/jdk.tools.jlink.internal.plugins
43185
d75d9ff8d4e7 8171380: Remove all exports from jdk.jlink
chegar
parents: 39834
diff changeset
    31
 *          jdk.jlink/jdk.tools.jlink.plugin
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    32
 * @run main CompressorPluginTest
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    33
 */
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    34
import java.net.URI;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    35
import java.nio.ByteOrder;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    36
import java.nio.file.FileSystem;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    37
import java.nio.file.FileSystemNotFoundException;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    38
import java.nio.file.FileSystems;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    39
import java.nio.file.Files;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    40
import java.nio.file.Path;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    41
import java.nio.file.ProviderNotFoundException;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    42
import java.util.Collections;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    43
import java.util.HashMap;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    44
import java.util.Iterator;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    45
import java.util.List;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    46
import java.util.Map;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    47
import java.util.Properties;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    48
import java.util.regex.Pattern;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    49
import java.util.stream.Collectors;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    50
import java.util.stream.Stream;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    51
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    52
import jdk.internal.jimage.decompressor.CompressedResourceHeader;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    53
import jdk.internal.jimage.decompressor.ResourceDecompressor;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    54
import jdk.internal.jimage.decompressor.ResourceDecompressorFactory;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    55
import jdk.internal.jimage.decompressor.StringSharingDecompressorFactory;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    56
import jdk.internal.jimage.decompressor.ZipDecompressorFactory;
39834
53a6fb443c20 8162538: plugin API should avoid read only pool, have module view separated from resource view and have pool builder to modify
sundar
parents: 39321
diff changeset
    57
import jdk.tools.jlink.internal.ResourcePoolManager;
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    58
import jdk.tools.jlink.internal.StringTable;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    59
import jdk.tools.jlink.internal.plugins.DefaultCompressPlugin;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    60
import jdk.tools.jlink.internal.plugins.StringSharingPlugin;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    61
import jdk.tools.jlink.internal.plugins.ZipPlugin;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    62
import jdk.tools.jlink.plugin.Plugin;
39834
53a6fb443c20 8162538: plugin API should avoid read only pool, have module view separated from resource view and have pool builder to modify
sundar
parents: 39321
diff changeset
    63
import jdk.tools.jlink.plugin.ResourcePool;
53a6fb443c20 8162538: plugin API should avoid read only pool, have module view separated from resource view and have pool builder to modify
sundar
parents: 39321
diff changeset
    64
import jdk.tools.jlink.plugin.ResourcePoolBuilder;
53a6fb443c20 8162538: plugin API should avoid read only pool, have module view separated from resource view and have pool builder to modify
sundar
parents: 39321
diff changeset
    65
import jdk.tools.jlink.plugin.ResourcePoolEntry;
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    66
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    67
public class CompressorPluginTest {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    68
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    69
    private static int strID = 1;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    70
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    71
    public static void main(String[] args) throws Exception {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    72
        new CompressorPluginTest().test();
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    73
    }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    74
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    75
    public void test() throws Exception {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    76
        FileSystem fs;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    77
        try {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    78
            fs = FileSystems.getFileSystem(URI.create("jrt:/"));
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    79
        } catch (ProviderNotFoundException | FileSystemNotFoundException e) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    80
            System.err.println("Not an image build, test skipped.");
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    81
            return;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    82
        }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    83
        Path javabase = fs.getPath("/modules/java.base");
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    84
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    85
        checkCompress(gatherResources(javabase), new ZipPlugin(), null,
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    86
                new ResourceDecompressorFactory[]{
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    87
                    new ZipDecompressorFactory()
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    88
                });
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    89
39834
53a6fb443c20 8162538: plugin API should avoid read only pool, have module view separated from resource view and have pool builder to modify
sundar
parents: 39321
diff changeset
    90
        ResourcePool classes = gatherClasses(javabase);
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    91
        // compress = String sharing
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    92
        checkCompress(classes, new StringSharingPlugin(), null,
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    93
                new ResourceDecompressorFactory[]{
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    94
                    new StringSharingDecompressorFactory()});
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    95
43791
116beb9c53a5 8174209: Renumber the compress levels
sundar
parents: 43185
diff changeset
    96
        // compress level 0 == no compression
116beb9c53a5 8174209: Renumber the compress levels
sundar
parents: 43185
diff changeset
    97
        Properties options0 = new Properties();
116beb9c53a5 8174209: Renumber the compress levels
sundar
parents: 43185
diff changeset
    98
        options0.setProperty(DefaultCompressPlugin.NAME,
116beb9c53a5 8174209: Renumber the compress levels
sundar
parents: 43185
diff changeset
    99
                "0");
116beb9c53a5 8174209: Renumber the compress levels
sundar
parents: 43185
diff changeset
   100
        checkCompress(classes, new DefaultCompressPlugin(),
116beb9c53a5 8174209: Renumber the compress levels
sundar
parents: 43185
diff changeset
   101
                options0,
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   102
                new ResourceDecompressorFactory[]{
43791
116beb9c53a5 8174209: Renumber the compress levels
sundar
parents: 43185
diff changeset
   103
                });
116beb9c53a5 8174209: Renumber the compress levels
sundar
parents: 43185
diff changeset
   104
116beb9c53a5 8174209: Renumber the compress levels
sundar
parents: 43185
diff changeset
   105
        // compress level 1 == String sharing
116beb9c53a5 8174209: Renumber the compress levels
sundar
parents: 43185
diff changeset
   106
        Properties options1 = new Properties();
116beb9c53a5 8174209: Renumber the compress levels
sundar
parents: 43185
diff changeset
   107
        options1.setProperty(DefaultCompressPlugin.NAME, "1");
116beb9c53a5 8174209: Renumber the compress levels
sundar
parents: 43185
diff changeset
   108
        checkCompress(classes, new DefaultCompressPlugin(),
116beb9c53a5 8174209: Renumber the compress levels
sundar
parents: 43185
diff changeset
   109
                options1,
116beb9c53a5 8174209: Renumber the compress levels
sundar
parents: 43185
diff changeset
   110
                new ResourceDecompressorFactory[]{
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   111
                    new StringSharingDecompressorFactory()
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   112
                });
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   113
43791
116beb9c53a5 8174209: Renumber the compress levels
sundar
parents: 43185
diff changeset
   114
        // compress level 1 == String sharing + filter
116beb9c53a5 8174209: Renumber the compress levels
sundar
parents: 43185
diff changeset
   115
        options1.setProperty(DefaultCompressPlugin.FILTER,
39042
52db877f18db 8159206: All jlink or jmod tests failing
jlaskey
parents: 38320
diff changeset
   116
                "**Exception.class");
43791
116beb9c53a5 8174209: Renumber the compress levels
sundar
parents: 43185
diff changeset
   117
        options1.setProperty(DefaultCompressPlugin.NAME, "1");
116beb9c53a5 8174209: Renumber the compress levels
sundar
parents: 43185
diff changeset
   118
        checkCompress(classes, new DefaultCompressPlugin(),
116beb9c53a5 8174209: Renumber the compress levels
sundar
parents: 43185
diff changeset
   119
                options1,
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   120
                new ResourceDecompressorFactory[]{
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   121
                    new StringSharingDecompressorFactory()
39042
52db877f18db 8159206: All jlink or jmod tests failing
jlaskey
parents: 38320
diff changeset
   122
                }, Collections.singletonList(".*Exception.class"));
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   123
43791
116beb9c53a5 8174209: Renumber the compress levels
sundar
parents: 43185
diff changeset
   124
        // compress level 2 == ZIP
116beb9c53a5 8174209: Renumber the compress levels
sundar
parents: 43185
diff changeset
   125
        Properties options2 = new Properties();
116beb9c53a5 8174209: Renumber the compress levels
sundar
parents: 43185
diff changeset
   126
        options2.setProperty(DefaultCompressPlugin.FILTER,
116beb9c53a5 8174209: Renumber the compress levels
sundar
parents: 43185
diff changeset
   127
                "**Exception.class");
116beb9c53a5 8174209: Renumber the compress levels
sundar
parents: 43185
diff changeset
   128
        options2.setProperty(DefaultCompressPlugin.NAME, "2");
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   129
        checkCompress(classes, new DefaultCompressPlugin(),
43791
116beb9c53a5 8174209: Renumber the compress levels
sundar
parents: 43185
diff changeset
   130
                options2,
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   131
                new ResourceDecompressorFactory[]{
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   132
                    new ZipDecompressorFactory()
39042
52db877f18db 8159206: All jlink or jmod tests failing
jlaskey
parents: 38320
diff changeset
   133
                }, Collections.singletonList(".*Exception.class"));
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   134
43791
116beb9c53a5 8174209: Renumber the compress levels
sundar
parents: 43185
diff changeset
   135
        // compress level 2 == ZIP + filter
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   136
        options2.setProperty(DefaultCompressPlugin.FILTER,
39042
52db877f18db 8159206: All jlink or jmod tests failing
jlaskey
parents: 38320
diff changeset
   137
                "**Exception.class");
43791
116beb9c53a5 8174209: Renumber the compress levels
sundar
parents: 43185
diff changeset
   138
        options2.setProperty(DefaultCompressPlugin.NAME, "2");
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   139
        checkCompress(classes, new DefaultCompressPlugin(),
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   140
                options2,
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   141
                new ResourceDecompressorFactory[]{
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   142
                    new ZipDecompressorFactory(),
39042
52db877f18db 8159206: All jlink or jmod tests failing
jlaskey
parents: 38320
diff changeset
   143
                }, Collections.singletonList(".*Exception.class"));
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   144
    }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   145
39834
53a6fb443c20 8162538: plugin API should avoid read only pool, have module view separated from resource view and have pool builder to modify
sundar
parents: 39321
diff changeset
   146
    private ResourcePool gatherResources(Path module) throws Exception {
53a6fb443c20 8162538: plugin API should avoid read only pool, have module view separated from resource view and have pool builder to modify
sundar
parents: 39321
diff changeset
   147
        ResourcePoolManager poolMgr = new ResourcePoolManager(ByteOrder.nativeOrder(), new StringTable() {
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   148
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   149
            @Override
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   150
            public int addString(String str) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   151
                return -1;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   152
            }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   153
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   154
            @Override
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   155
            public String getString(int id) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   156
                return null;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   157
            }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   158
        });
39834
53a6fb443c20 8162538: plugin API should avoid read only pool, have module view separated from resource view and have pool builder to modify
sundar
parents: 39321
diff changeset
   159
53a6fb443c20 8162538: plugin API should avoid read only pool, have module view separated from resource view and have pool builder to modify
sundar
parents: 39321
diff changeset
   160
        ResourcePoolBuilder poolBuilder = poolMgr.resourcePoolBuilder();
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   161
        try (Stream<Path> stream = Files.walk(module)) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   162
            for (Iterator<Path> iterator = stream.iterator(); iterator.hasNext();) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   163
                Path p = iterator.next();
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   164
                if (Files.isRegularFile(p)) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   165
                    byte[] content = Files.readAllBytes(p);
39834
53a6fb443c20 8162538: plugin API should avoid read only pool, have module view separated from resource view and have pool builder to modify
sundar
parents: 39321
diff changeset
   166
                    poolBuilder.add(ResourcePoolEntry.create(p.toString(), content));
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   167
                }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   168
            }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   169
        }
39834
53a6fb443c20 8162538: plugin API should avoid read only pool, have module view separated from resource view and have pool builder to modify
sundar
parents: 39321
diff changeset
   170
        return poolBuilder.build();
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   171
    }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   172
39834
53a6fb443c20 8162538: plugin API should avoid read only pool, have module view separated from resource view and have pool builder to modify
sundar
parents: 39321
diff changeset
   173
    private ResourcePool gatherClasses(Path module) throws Exception {
53a6fb443c20 8162538: plugin API should avoid read only pool, have module view separated from resource view and have pool builder to modify
sundar
parents: 39321
diff changeset
   174
        ResourcePoolManager poolMgr = new ResourcePoolManager(ByteOrder.nativeOrder(), new StringTable() {
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   175
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   176
            @Override
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   177
            public int addString(String str) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   178
                return -1;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   179
            }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   180
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   181
            @Override
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   182
            public String getString(int id) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   183
                return null;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   184
            }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   185
        });
39834
53a6fb443c20 8162538: plugin API should avoid read only pool, have module view separated from resource view and have pool builder to modify
sundar
parents: 39321
diff changeset
   186
53a6fb443c20 8162538: plugin API should avoid read only pool, have module view separated from resource view and have pool builder to modify
sundar
parents: 39321
diff changeset
   187
        ResourcePoolBuilder poolBuilder = poolMgr.resourcePoolBuilder();
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   188
        try (Stream<Path> stream = Files.walk(module)) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   189
            for (Iterator<Path> iterator = stream.iterator(); iterator.hasNext();) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   190
                Path p = iterator.next();
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   191
                if (Files.isRegularFile(p) && p.toString().endsWith(".class")) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   192
                    byte[] content = Files.readAllBytes(p);
39834
53a6fb443c20 8162538: plugin API should avoid read only pool, have module view separated from resource view and have pool builder to modify
sundar
parents: 39321
diff changeset
   193
                    poolBuilder.add(ResourcePoolEntry.create(p.toString(), content));
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   194
                }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   195
            }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   196
        }
39834
53a6fb443c20 8162538: plugin API should avoid read only pool, have module view separated from resource view and have pool builder to modify
sundar
parents: 39321
diff changeset
   197
        return poolBuilder.build();
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   198
    }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   199
39834
53a6fb443c20 8162538: plugin API should avoid read only pool, have module view separated from resource view and have pool builder to modify
sundar
parents: 39321
diff changeset
   200
    private void checkCompress(ResourcePool resources, Plugin prov,
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   201
            Properties config,
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   202
            ResourceDecompressorFactory[] factories) throws Exception {
39042
52db877f18db 8159206: All jlink or jmod tests failing
jlaskey
parents: 38320
diff changeset
   203
        checkCompress(resources, prov, config, factories, Collections.emptyList());
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   204
    }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   205
39834
53a6fb443c20 8162538: plugin API should avoid read only pool, have module view separated from resource view and have pool builder to modify
sundar
parents: 39321
diff changeset
   206
    private void checkCompress(ResourcePool resources, Plugin prov,
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   207
            Properties config,
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   208
            ResourceDecompressorFactory[] factories,
39042
52db877f18db 8159206: All jlink or jmod tests failing
jlaskey
parents: 38320
diff changeset
   209
            List<String> includes) throws Exception {
43791
116beb9c53a5 8174209: Renumber the compress levels
sundar
parents: 43185
diff changeset
   210
        if (factories.length == 0) {
116beb9c53a5 8174209: Renumber the compress levels
sundar
parents: 43185
diff changeset
   211
            // no compression, nothing to check!
116beb9c53a5 8174209: Renumber the compress levels
sundar
parents: 43185
diff changeset
   212
            return;
116beb9c53a5 8174209: Renumber the compress levels
sundar
parents: 43185
diff changeset
   213
        }
116beb9c53a5 8174209: Renumber the compress levels
sundar
parents: 43185
diff changeset
   214
38320
e24c7029e8ba 8156914: jlink API minor cleanups
sundar
parents: 36511
diff changeset
   215
        long[] original = new long[1];
e24c7029e8ba 8156914: jlink API minor cleanups
sundar
parents: 36511
diff changeset
   216
        long[] compressed = new long[1];
e24c7029e8ba 8156914: jlink API minor cleanups
sundar
parents: 36511
diff changeset
   217
        resources.entries().forEach(resource -> {
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   218
            List<Pattern> includesPatterns = includes.stream()
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   219
                    .map(Pattern::compile)
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   220
                    .collect(Collectors.toList());
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   221
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   222
            Map<String, String> props = new HashMap<>();
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   223
            if (config != null) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   224
                for (String p : config.stringPropertyNames()) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   225
                    props.put(p, config.getProperty(p));
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   226
                }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   227
            }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   228
            prov.configure(props);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   229
            final Map<Integer, String> strings = new HashMap<>();
39834
53a6fb443c20 8162538: plugin API should avoid read only pool, have module view separated from resource view and have pool builder to modify
sundar
parents: 39321
diff changeset
   230
            ResourcePoolManager inputResourcesMgr = new ResourcePoolManager(ByteOrder.nativeOrder(), new StringTable() {
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   231
                @Override
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   232
                public int addString(String str) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   233
                    int id = strID;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   234
                    strID += 1;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   235
                    strings.put(id, str);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   236
                    return id;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   237
                }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   238
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   239
                @Override
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   240
                public String getString(int id) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   241
                    return strings.get(id);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   242
                }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   243
            });
39834
53a6fb443c20 8162538: plugin API should avoid read only pool, have module view separated from resource view and have pool builder to modify
sundar
parents: 39321
diff changeset
   244
            inputResourcesMgr.add(resource);
53a6fb443c20 8162538: plugin API should avoid read only pool, have module view separated from resource view and have pool builder to modify
sundar
parents: 39321
diff changeset
   245
            ResourcePool compressedResources = applyCompressor(prov, inputResourcesMgr, resource, includesPatterns);
53a6fb443c20 8162538: plugin API should avoid read only pool, have module view separated from resource view and have pool builder to modify
sundar
parents: 39321
diff changeset
   246
            original[0] += resource.contentLength();
53a6fb443c20 8162538: plugin API should avoid read only pool, have module view separated from resource view and have pool builder to modify
sundar
parents: 39321
diff changeset
   247
            compressed[0] += compressedResources.findEntry(resource.path()).get().contentLength();
53a6fb443c20 8162538: plugin API should avoid read only pool, have module view separated from resource view and have pool builder to modify
sundar
parents: 39321
diff changeset
   248
            applyDecompressors(factories, inputResourcesMgr.resourcePool(), compressedResources, strings, includesPatterns);
38320
e24c7029e8ba 8156914: jlink API minor cleanups
sundar
parents: 36511
diff changeset
   249
        });
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   250
        String compressors = Stream.of(factories)
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   251
                .map(Object::getClass)
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   252
                .map(Class::getSimpleName)
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   253
                .collect(Collectors.joining(", "));
38320
e24c7029e8ba 8156914: jlink API minor cleanups
sundar
parents: 36511
diff changeset
   254
        String size = "Compressed size: " + compressed[0] + ", original size: " + original[0];
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   255
        System.out.println("Used " + compressors + ". " + size);
38320
e24c7029e8ba 8156914: jlink API minor cleanups
sundar
parents: 36511
diff changeset
   256
        if (original[0] <= compressed[0]) {
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   257
            throw new AssertionError("java.base not compressed.");
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   258
        }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   259
    }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   260
39834
53a6fb443c20 8162538: plugin API should avoid read only pool, have module view separated from resource view and have pool builder to modify
sundar
parents: 39321
diff changeset
   261
    private ResourcePool applyCompressor(Plugin plugin,
53a6fb443c20 8162538: plugin API should avoid read only pool, have module view separated from resource view and have pool builder to modify
sundar
parents: 39321
diff changeset
   262
            ResourcePoolManager inputResources,
53a6fb443c20 8162538: plugin API should avoid read only pool, have module view separated from resource view and have pool builder to modify
sundar
parents: 39321
diff changeset
   263
            ResourcePoolEntry res,
39042
52db877f18db 8159206: All jlink or jmod tests failing
jlaskey
parents: 38320
diff changeset
   264
            List<Pattern> includesPatterns) {
39834
53a6fb443c20 8162538: plugin API should avoid read only pool, have module view separated from resource view and have pool builder to modify
sundar
parents: 39321
diff changeset
   265
        ResourcePoolManager resMgr = new ResourcePoolManager(ByteOrder.nativeOrder(),
53a6fb443c20 8162538: plugin API should avoid read only pool, have module view separated from resource view and have pool builder to modify
sundar
parents: 39321
diff changeset
   266
                inputResources.getStringTable());
53a6fb443c20 8162538: plugin API should avoid read only pool, have module view separated from resource view and have pool builder to modify
sundar
parents: 39321
diff changeset
   267
        ResourcePool compressedResourcePool = plugin.transform(inputResources.resourcePool(),
53a6fb443c20 8162538: plugin API should avoid read only pool, have module view separated from resource view and have pool builder to modify
sundar
parents: 39321
diff changeset
   268
            resMgr.resourcePoolBuilder());
53a6fb443c20 8162538: plugin API should avoid read only pool, have module view separated from resource view and have pool builder to modify
sundar
parents: 39321
diff changeset
   269
        String path = res.path();
53a6fb443c20 8162538: plugin API should avoid read only pool, have module view separated from resource view and have pool builder to modify
sundar
parents: 39321
diff changeset
   270
        ResourcePoolEntry compressed = compressedResourcePool.findEntry(path).get();
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   271
        CompressedResourceHeader header
39834
53a6fb443c20 8162538: plugin API should avoid read only pool, have module view separated from resource view and have pool builder to modify
sundar
parents: 39321
diff changeset
   272
                = CompressedResourceHeader.readFromResource(ByteOrder.nativeOrder(), compressed.contentBytes());
39042
52db877f18db 8159206: All jlink or jmod tests failing
jlaskey
parents: 38320
diff changeset
   273
        if (isIncluded(includesPatterns, path)) {
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   274
            if (header == null) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   275
                throw new AssertionError("Path should be compressed: " + path);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   276
            }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   277
            if (header.getDecompressorNameOffset() == 0) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   278
                throw new AssertionError("Invalid plugin offset "
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   279
                        + header.getDecompressorNameOffset());
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   280
            }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   281
            if (header.getResourceSize() <= 0) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   282
                throw new AssertionError("Invalid compressed size "
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   283
                        + header.getResourceSize());
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   284
            }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   285
        } else if (header != null) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   286
            throw new AssertionError("Path should not be compressed: " + path);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   287
        }
39834
53a6fb443c20 8162538: plugin API should avoid read only pool, have module view separated from resource view and have pool builder to modify
sundar
parents: 39321
diff changeset
   288
        return compressedResourcePool;
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   289
    }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   290
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   291
    private void applyDecompressors(ResourceDecompressorFactory[] decompressors,
39834
53a6fb443c20 8162538: plugin API should avoid read only pool, have module view separated from resource view and have pool builder to modify
sundar
parents: 39321
diff changeset
   292
            ResourcePool inputResources,
53a6fb443c20 8162538: plugin API should avoid read only pool, have module view separated from resource view and have pool builder to modify
sundar
parents: 39321
diff changeset
   293
            ResourcePool compressedResources,
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   294
            Map<Integer, String> strings,
39042
52db877f18db 8159206: All jlink or jmod tests failing
jlaskey
parents: 38320
diff changeset
   295
            List<Pattern> includesPatterns) {
38320
e24c7029e8ba 8156914: jlink API minor cleanups
sundar
parents: 36511
diff changeset
   296
        compressedResources.entries().forEach(compressed -> {
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   297
            CompressedResourceHeader header = CompressedResourceHeader.readFromResource(
39834
53a6fb443c20 8162538: plugin API should avoid read only pool, have module view separated from resource view and have pool builder to modify
sundar
parents: 39321
diff changeset
   298
                    ByteOrder.nativeOrder(), compressed.contentBytes());
53a6fb443c20 8162538: plugin API should avoid read only pool, have module view separated from resource view and have pool builder to modify
sundar
parents: 39321
diff changeset
   299
            String path = compressed.path();
53a6fb443c20 8162538: plugin API should avoid read only pool, have module view separated from resource view and have pool builder to modify
sundar
parents: 39321
diff changeset
   300
            ResourcePoolEntry orig = inputResources.findEntry(path).get();
39042
52db877f18db 8159206: All jlink or jmod tests failing
jlaskey
parents: 38320
diff changeset
   301
            if (!isIncluded(includesPatterns, path)) {
38320
e24c7029e8ba 8156914: jlink API minor cleanups
sundar
parents: 36511
diff changeset
   302
                return;
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   303
            }
39834
53a6fb443c20 8162538: plugin API should avoid read only pool, have module view separated from resource view and have pool builder to modify
sundar
parents: 39321
diff changeset
   304
            byte[] decompressed = compressed.contentBytes();
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   305
            for (ResourceDecompressorFactory factory : decompressors) {
38320
e24c7029e8ba 8156914: jlink API minor cleanups
sundar
parents: 36511
diff changeset
   306
                try {
e24c7029e8ba 8156914: jlink API minor cleanups
sundar
parents: 36511
diff changeset
   307
                    ResourceDecompressor decompressor = factory.newDecompressor(new Properties());
e24c7029e8ba 8156914: jlink API minor cleanups
sundar
parents: 36511
diff changeset
   308
                    decompressed = decompressor.decompress(
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   309
                        strings::get, decompressed,
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   310
                        CompressedResourceHeader.getSize(), header.getUncompressedSize());
38320
e24c7029e8ba 8156914: jlink API minor cleanups
sundar
parents: 36511
diff changeset
   311
                } catch (Exception exp) {
e24c7029e8ba 8156914: jlink API minor cleanups
sundar
parents: 36511
diff changeset
   312
                    throw new RuntimeException(exp);
e24c7029e8ba 8156914: jlink API minor cleanups
sundar
parents: 36511
diff changeset
   313
                }
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   314
            }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   315
39834
53a6fb443c20 8162538: plugin API should avoid read only pool, have module view separated from resource view and have pool builder to modify
sundar
parents: 39321
diff changeset
   316
            if (decompressed.length != orig.contentLength()) {
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   317
                throw new AssertionError("Invalid uncompressed size "
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   318
                        + header.getUncompressedSize());
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   319
            }
39834
53a6fb443c20 8162538: plugin API should avoid read only pool, have module view separated from resource view and have pool builder to modify
sundar
parents: 39321
diff changeset
   320
            byte[] origContent = orig.contentBytes();
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   321
            for (int i = 0; i < decompressed.length; i++) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   322
                if (decompressed[i] != origContent[i]) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   323
                    throw new AssertionError("Decompressed and original differ at index " + i);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   324
                }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   325
            }
38320
e24c7029e8ba 8156914: jlink API minor cleanups
sundar
parents: 36511
diff changeset
   326
        });
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   327
    }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   328
39042
52db877f18db 8159206: All jlink or jmod tests failing
jlaskey
parents: 38320
diff changeset
   329
    private boolean isIncluded(List<Pattern> includesPatterns, String path) {
52db877f18db 8159206: All jlink or jmod tests failing
jlaskey
parents: 38320
diff changeset
   330
        return includesPatterns.isEmpty() ||
52db877f18db 8159206: All jlink or jmod tests failing
jlaskey
parents: 38320
diff changeset
   331
               includesPatterns.stream().anyMatch((pattern) -> pattern.matcher(path).matches());
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   332
    }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   333
}