jdk/test/tools/jlink/plugins/CompressorPluginTest.java
author sundar
Mon, 16 May 2016 14:47:27 +0530
changeset 38320 e24c7029e8ba
parent 36511 9d0388c6b336
child 39042 52db877f18db
permissions -rw-r--r--
8156914: jlink API minor cleanups Reviewed-by: mchung
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
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    31
 * @run main CompressorPluginTest
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    32
 */
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    33
import java.net.URI;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    34
import java.nio.ByteOrder;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    35
import java.nio.file.FileSystem;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    36
import java.nio.file.FileSystemNotFoundException;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    37
import java.nio.file.FileSystems;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    38
import java.nio.file.Files;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    39
import java.nio.file.Path;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    40
import java.nio.file.ProviderNotFoundException;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    41
import java.util.Collections;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    42
import java.util.HashMap;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    43
import java.util.Iterator;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    44
import java.util.List;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    45
import java.util.Map;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    46
import java.util.Properties;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    47
import java.util.regex.Pattern;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    48
import java.util.stream.Collectors;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    49
import java.util.stream.Stream;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    50
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    51
import jdk.internal.jimage.decompressor.CompressedResourceHeader;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    52
import jdk.internal.jimage.decompressor.ResourceDecompressor;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    53
import jdk.internal.jimage.decompressor.ResourceDecompressorFactory;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    54
import jdk.internal.jimage.decompressor.StringSharingDecompressorFactory;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    55
import jdk.internal.jimage.decompressor.ZipDecompressorFactory;
38320
e24c7029e8ba 8156914: jlink API minor cleanups
sundar
parents: 36511
diff changeset
    56
import jdk.tools.jlink.internal.ModulePoolImpl;
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    57
import jdk.tools.jlink.internal.StringTable;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    58
import jdk.tools.jlink.internal.plugins.DefaultCompressPlugin;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    59
import jdk.tools.jlink.internal.plugins.StringSharingPlugin;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    60
import jdk.tools.jlink.internal.plugins.ZipPlugin;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    61
import jdk.tools.jlink.plugin.Plugin;
38320
e24c7029e8ba 8156914: jlink API minor cleanups
sundar
parents: 36511
diff changeset
    62
import jdk.tools.jlink.plugin.ModuleEntry;
e24c7029e8ba 8156914: jlink API minor cleanups
sundar
parents: 36511
diff changeset
    63
import jdk.tools.jlink.plugin.ModulePool;
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    64
import jdk.tools.jlink.plugin.TransformerPlugin;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    65
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    66
public class CompressorPluginTest {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    67
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    68
    private static int strID = 1;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    69
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    70
    public static void main(String[] args) throws Exception {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    71
        new CompressorPluginTest().test();
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    72
    }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    73
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    74
    public void test() throws Exception {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    75
        FileSystem fs;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    76
        try {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    77
            fs = FileSystems.getFileSystem(URI.create("jrt:/"));
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    78
        } catch (ProviderNotFoundException | FileSystemNotFoundException e) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    79
            System.err.println("Not an image build, test skipped.");
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    80
            return;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    81
        }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    82
        Path javabase = fs.getPath("/modules/java.base");
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    83
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    84
        checkCompress(gatherResources(javabase), new ZipPlugin(), null,
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    85
                new ResourceDecompressorFactory[]{
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    86
                    new ZipDecompressorFactory()
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    87
                });
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    88
38320
e24c7029e8ba 8156914: jlink API minor cleanups
sundar
parents: 36511
diff changeset
    89
        ModulePool classes = gatherClasses(javabase);
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    90
        // compress = String sharing
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    91
        checkCompress(classes, new StringSharingPlugin(), null,
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    92
                new ResourceDecompressorFactory[]{
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    93
                    new StringSharingDecompressorFactory()});
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    94
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    95
        // compress == ZIP + String sharing
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    96
        Properties options = new Properties();
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    97
        options.setProperty(ZipPlugin.NAME, "");
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    98
        checkCompress(classes, new DefaultCompressPlugin(), options,
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    99
                new ResourceDecompressorFactory[]{
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   100
                    new ZipDecompressorFactory(),
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   101
                    new StringSharingDecompressorFactory()
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   102
                });
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   103
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   104
        // compress == ZIP + String sharing + filter
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   105
        options.setProperty(DefaultCompressPlugin.FILTER,
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   106
                "*Exception.class,^*IOException.class");
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   107
        checkCompress(classes, new DefaultCompressPlugin(), options,
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   108
                new ResourceDecompressorFactory[]{
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   109
                    new ZipDecompressorFactory(),
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   110
                    new StringSharingDecompressorFactory()
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   111
                }, Collections.singletonList(".*Exception.class"),
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   112
                Collections.singletonList(".*IOException.class"));
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   113
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   114
        // compress level 1 == ZIP
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   115
        Properties options1 = new Properties();
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   116
        options1.setProperty(DefaultCompressPlugin.NAME,
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   117
                "1");
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   118
        checkCompress(classes, new DefaultCompressPlugin(),
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   119
                options1,
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   120
                new ResourceDecompressorFactory[]{
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   121
                    new ZipDecompressorFactory()
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   122
                });
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   123
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   124
        // compress level 1 == ZIP
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   125
        options1.setProperty(DefaultCompressPlugin.FILTER,
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   126
                "*Exception.class,^*IOException.class");
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   127
        checkCompress(classes, new DefaultCompressPlugin(),
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   128
                options1,
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   129
                new ResourceDecompressorFactory[]{
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   130
                    new ZipDecompressorFactory()
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   131
                }, Collections.singletonList(".*Exception.class"),
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   132
                Collections.singletonList(".*IOException.class"));
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   133
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   134
        // compress level 2 == ZIP + String sharing
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   135
        Properties options2 = new Properties();
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   136
        options2.setProperty(DefaultCompressPlugin.NAME,
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   137
                "2");
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   138
        checkCompress(classes, new DefaultCompressPlugin(),
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   139
                options2,
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   140
                new ResourceDecompressorFactory[]{
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   141
                    new ZipDecompressorFactory(),
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   142
                    new StringSharingDecompressorFactory()
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   143
                });
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   144
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   145
        // compress level 2 == ZIP + String sharing + filter
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   146
        options2.setProperty(DefaultCompressPlugin.FILTER,
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   147
                "*Exception.class,^*IOException.class");
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   148
        checkCompress(classes, new DefaultCompressPlugin(),
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   149
                options2,
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   150
                new ResourceDecompressorFactory[]{
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   151
                    new ZipDecompressorFactory(),
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   152
                    new StringSharingDecompressorFactory()
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   153
                }, Collections.singletonList(".*Exception.class"),
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   154
                Collections.singletonList(".*IOException.class"));
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   155
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   156
        // compress level 0 == String sharing
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   157
        Properties options0 = new Properties();
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   158
        options0.setProperty(DefaultCompressPlugin.NAME, "0");
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   159
        checkCompress(classes, new DefaultCompressPlugin(),
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   160
                options0,
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   161
                new ResourceDecompressorFactory[]{
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   162
                    new StringSharingDecompressorFactory()
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   163
                });
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   164
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   165
        // compress level 0 == String sharing + filter
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   166
        options0.setProperty(DefaultCompressPlugin.FILTER,
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   167
                "*Exception.class,^*IOException.class");
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   168
        checkCompress(classes, new DefaultCompressPlugin(),
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   169
                options0,
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   170
                new ResourceDecompressorFactory[]{
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   171
                    new StringSharingDecompressorFactory()
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   172
                }, Collections.singletonList(".*Exception.class"),
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   173
                Collections.singletonList(".*IOException.class"));
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   174
    }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   175
38320
e24c7029e8ba 8156914: jlink API minor cleanups
sundar
parents: 36511
diff changeset
   176
    private ModulePool gatherResources(Path module) throws Exception {
e24c7029e8ba 8156914: jlink API minor cleanups
sundar
parents: 36511
diff changeset
   177
        ModulePool pool = new ModulePoolImpl(ByteOrder.nativeOrder(), new StringTable() {
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   178
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   179
            @Override
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   180
            public int addString(String str) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   181
                return -1;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   182
            }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   183
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   184
            @Override
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   185
            public String getString(int id) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   186
                return null;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   187
            }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   188
        });
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   189
        try (Stream<Path> stream = Files.walk(module)) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   190
            for (Iterator<Path> iterator = stream.iterator(); iterator.hasNext();) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   191
                Path p = iterator.next();
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   192
                if (Files.isRegularFile(p)) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   193
                    byte[] content = Files.readAllBytes(p);
38320
e24c7029e8ba 8156914: jlink API minor cleanups
sundar
parents: 36511
diff changeset
   194
                    pool.add(ModuleEntry.create(p.toString(), content));
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   195
                }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   196
            }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   197
        }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   198
        return pool;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   199
    }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   200
38320
e24c7029e8ba 8156914: jlink API minor cleanups
sundar
parents: 36511
diff changeset
   201
    private ModulePool gatherClasses(Path module) throws Exception {
e24c7029e8ba 8156914: jlink API minor cleanups
sundar
parents: 36511
diff changeset
   202
        ModulePool pool = new ModulePoolImpl(ByteOrder.nativeOrder(), new StringTable() {
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   203
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   204
            @Override
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   205
            public int addString(String str) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   206
                return -1;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   207
            }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   208
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   209
            @Override
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   210
            public String getString(int id) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   211
                return null;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   212
            }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   213
        });
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   214
        try (Stream<Path> stream = Files.walk(module)) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   215
            for (Iterator<Path> iterator = stream.iterator(); iterator.hasNext();) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   216
                Path p = iterator.next();
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   217
                if (Files.isRegularFile(p) && p.toString().endsWith(".class")) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   218
                    byte[] content = Files.readAllBytes(p);
38320
e24c7029e8ba 8156914: jlink API minor cleanups
sundar
parents: 36511
diff changeset
   219
                    pool.add(ModuleEntry.create(p.toString(), content));
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   220
                }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   221
            }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   222
        }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   223
        return pool;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   224
    }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   225
38320
e24c7029e8ba 8156914: jlink API minor cleanups
sundar
parents: 36511
diff changeset
   226
    private void checkCompress(ModulePool resources, Plugin prov,
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   227
            Properties config,
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   228
            ResourceDecompressorFactory[] factories) throws Exception {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   229
        checkCompress(resources, prov, config, factories, Collections.emptyList(), Collections.emptyList());
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   230
    }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   231
38320
e24c7029e8ba 8156914: jlink API minor cleanups
sundar
parents: 36511
diff changeset
   232
    private void checkCompress(ModulePool resources, Plugin prov,
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   233
            Properties config,
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   234
            ResourceDecompressorFactory[] factories,
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   235
            List<String> includes,
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   236
            List<String> excludes) throws Exception {
38320
e24c7029e8ba 8156914: jlink API minor cleanups
sundar
parents: 36511
diff changeset
   237
        long[] original = new long[1];
e24c7029e8ba 8156914: jlink API minor cleanups
sundar
parents: 36511
diff changeset
   238
        long[] compressed = new long[1];
e24c7029e8ba 8156914: jlink API minor cleanups
sundar
parents: 36511
diff changeset
   239
        resources.entries().forEach(resource -> {
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   240
            List<Pattern> includesPatterns = includes.stream()
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   241
                    .map(Pattern::compile)
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   242
                    .collect(Collectors.toList());
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   243
            List<Pattern> excludesPatterns = excludes.stream()
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   244
                    .map(Pattern::compile)
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   245
                    .collect(Collectors.toList());
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   246
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   247
            Map<String, String> props = new HashMap<>();
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   248
            if (config != null) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   249
                for (String p : config.stringPropertyNames()) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   250
                    props.put(p, config.getProperty(p));
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   251
                }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   252
            }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   253
            prov.configure(props);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   254
            final Map<Integer, String> strings = new HashMap<>();
38320
e24c7029e8ba 8156914: jlink API minor cleanups
sundar
parents: 36511
diff changeset
   255
            ModulePoolImpl inputResources = new ModulePoolImpl(ByteOrder.nativeOrder(), new StringTable() {
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   256
                @Override
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   257
                public int addString(String str) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   258
                    int id = strID;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   259
                    strID += 1;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   260
                    strings.put(id, str);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   261
                    return id;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   262
                }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   263
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   264
                @Override
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   265
                public String getString(int id) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   266
                    return strings.get(id);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   267
                }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   268
            });
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   269
            inputResources.add(resource);
38320
e24c7029e8ba 8156914: jlink API minor cleanups
sundar
parents: 36511
diff changeset
   270
            ModulePool compressedResources = applyCompressor(prov, inputResources, resource, includesPatterns, excludesPatterns);
e24c7029e8ba 8156914: jlink API minor cleanups
sundar
parents: 36511
diff changeset
   271
            original[0] += resource.getLength();
e24c7029e8ba 8156914: jlink API minor cleanups
sundar
parents: 36511
diff changeset
   272
            compressed[0] += compressedResources.findEntry(resource.getPath()).get().getLength();
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   273
            applyDecompressors(factories, inputResources, compressedResources, strings, includesPatterns, excludesPatterns);
38320
e24c7029e8ba 8156914: jlink API minor cleanups
sundar
parents: 36511
diff changeset
   274
        });
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   275
        String compressors = Stream.of(factories)
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   276
                .map(Object::getClass)
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   277
                .map(Class::getSimpleName)
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   278
                .collect(Collectors.joining(", "));
38320
e24c7029e8ba 8156914: jlink API minor cleanups
sundar
parents: 36511
diff changeset
   279
        String size = "Compressed size: " + compressed[0] + ", original size: " + original[0];
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   280
        System.out.println("Used " + compressors + ". " + size);
38320
e24c7029e8ba 8156914: jlink API minor cleanups
sundar
parents: 36511
diff changeset
   281
        if (original[0] <= compressed[0]) {
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   282
            throw new AssertionError("java.base not compressed.");
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   283
        }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   284
    }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   285
38320
e24c7029e8ba 8156914: jlink API minor cleanups
sundar
parents: 36511
diff changeset
   286
    private ModulePool applyCompressor(Plugin plugin,
e24c7029e8ba 8156914: jlink API minor cleanups
sundar
parents: 36511
diff changeset
   287
            ModulePoolImpl inputResources,
e24c7029e8ba 8156914: jlink API minor cleanups
sundar
parents: 36511
diff changeset
   288
            ModuleEntry res,
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   289
            List<Pattern> includesPatterns,
38320
e24c7029e8ba 8156914: jlink API minor cleanups
sundar
parents: 36511
diff changeset
   290
            List<Pattern> excludesPatterns) {
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   291
        TransformerPlugin compressor = (TransformerPlugin) plugin;
38320
e24c7029e8ba 8156914: jlink API minor cleanups
sundar
parents: 36511
diff changeset
   292
        ModulePool compressedModulePool = new ModulePoolImpl(ByteOrder.nativeOrder(), inputResources.getStringTable());
e24c7029e8ba 8156914: jlink API minor cleanups
sundar
parents: 36511
diff changeset
   293
        compressor.visit(inputResources, compressedModulePool);
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   294
        String path = res.getPath();
38320
e24c7029e8ba 8156914: jlink API minor cleanups
sundar
parents: 36511
diff changeset
   295
        ModuleEntry compressed = compressedModulePool.findEntry(path).get();
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   296
        CompressedResourceHeader header
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   297
                = CompressedResourceHeader.readFromResource(ByteOrder.nativeOrder(), compressed.getBytes());
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   298
        if (isIncluded(includesPatterns, excludesPatterns, path)) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   299
            if (header == null) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   300
                throw new AssertionError("Path should be compressed: " + path);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   301
            }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   302
            if (header.getDecompressorNameOffset() == 0) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   303
                throw new AssertionError("Invalid plugin offset "
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   304
                        + header.getDecompressorNameOffset());
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   305
            }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   306
            if (header.getResourceSize() <= 0) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   307
                throw new AssertionError("Invalid compressed size "
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   308
                        + header.getResourceSize());
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   309
            }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   310
        } else if (header != null) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   311
            throw new AssertionError("Path should not be compressed: " + path);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   312
        }
38320
e24c7029e8ba 8156914: jlink API minor cleanups
sundar
parents: 36511
diff changeset
   313
        return compressedModulePool;
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   314
    }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   315
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   316
    private void applyDecompressors(ResourceDecompressorFactory[] decompressors,
38320
e24c7029e8ba 8156914: jlink API minor cleanups
sundar
parents: 36511
diff changeset
   317
            ModulePool inputResources,
e24c7029e8ba 8156914: jlink API minor cleanups
sundar
parents: 36511
diff changeset
   318
            ModulePool compressedResources,
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   319
            Map<Integer, String> strings,
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   320
            List<Pattern> includesPatterns,
38320
e24c7029e8ba 8156914: jlink API minor cleanups
sundar
parents: 36511
diff changeset
   321
            List<Pattern> excludesPatterns) {
e24c7029e8ba 8156914: jlink API minor cleanups
sundar
parents: 36511
diff changeset
   322
        compressedResources.entries().forEach(compressed -> {
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   323
            CompressedResourceHeader header = CompressedResourceHeader.readFromResource(
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   324
                    ByteOrder.nativeOrder(), compressed.getBytes());
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   325
            String path = compressed.getPath();
38320
e24c7029e8ba 8156914: jlink API minor cleanups
sundar
parents: 36511
diff changeset
   326
            ModuleEntry orig = inputResources.findEntry(path).get();
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   327
            if (!isIncluded(includesPatterns, excludesPatterns, path)) {
38320
e24c7029e8ba 8156914: jlink API minor cleanups
sundar
parents: 36511
diff changeset
   328
                return;
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   329
            }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   330
            byte[] decompressed = compressed.getBytes();
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   331
            for (ResourceDecompressorFactory factory : decompressors) {
38320
e24c7029e8ba 8156914: jlink API minor cleanups
sundar
parents: 36511
diff changeset
   332
                try {
e24c7029e8ba 8156914: jlink API minor cleanups
sundar
parents: 36511
diff changeset
   333
                    ResourceDecompressor decompressor = factory.newDecompressor(new Properties());
e24c7029e8ba 8156914: jlink API minor cleanups
sundar
parents: 36511
diff changeset
   334
                    decompressed = decompressor.decompress(
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   335
                        strings::get, decompressed,
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   336
                        CompressedResourceHeader.getSize(), header.getUncompressedSize());
38320
e24c7029e8ba 8156914: jlink API minor cleanups
sundar
parents: 36511
diff changeset
   337
                } catch (Exception exp) {
e24c7029e8ba 8156914: jlink API minor cleanups
sundar
parents: 36511
diff changeset
   338
                    throw new RuntimeException(exp);
e24c7029e8ba 8156914: jlink API minor cleanups
sundar
parents: 36511
diff changeset
   339
                }
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   340
            }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   341
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   342
            if (decompressed.length != orig.getLength()) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   343
                throw new AssertionError("Invalid uncompressed size "
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   344
                        + header.getUncompressedSize());
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   345
            }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   346
            byte[] origContent = orig.getBytes();
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   347
            for (int i = 0; i < decompressed.length; i++) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   348
                if (decompressed[i] != origContent[i]) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   349
                    throw new AssertionError("Decompressed and original differ at index " + i);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   350
                }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   351
            }
38320
e24c7029e8ba 8156914: jlink API minor cleanups
sundar
parents: 36511
diff changeset
   352
        });
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   353
    }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   354
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   355
    private boolean isIncluded(List<Pattern> includesPatterns, List<Pattern> excludesPatterns, String path) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   356
        return !excludesPatterns.stream().anyMatch((pattern) -> pattern.matcher(path).matches())
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   357
                && (includesPatterns.isEmpty()
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   358
                || includesPatterns.stream().anyMatch((pattern) -> pattern.matcher(path).matches()));
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   359
    }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   360
}