jdk/src/jdk.jlink/share/classes/jdk/tools/jlink/internal/plugins/ExcludeVMPlugin.java
author alanb
Thu, 17 Mar 2016 19:04:16 +0000
changeset 36511 9d0388c6b336
child 37779 7c84df693837
permissions -rw-r--r--
8142968: Module System implementation Summary: Initial integration of JEP 200, JEP 260, JEP 261, and JEP 282 Reviewed-by: alanb, mchung, naoto, rriggs, psandoz, plevart, mullan, ascarpino, vinnie, prr, sherman, dfuchs, mhaupt Contributed-by: alan.bateman@oracle.com, alex.buckley@oracle.com, jonathan.gibbons@oracle.com, karen.kinnear@oracle.com, mandy.chung@oracle.com, mark.reinhold@oracle.com, chris.hegarty@oracle.com, alexandr.scherbatiy@oracle.com, amy.lu@oracle.com, calvin.cheung@oracle.com, daniel.fuchs@oracle.com, erik.joelsson@oracle.com, harold.seigel@oracle.com, jaroslav.bachorik@oracle.com, jean-francois.denise@oracle.com, jan.lahoda@oracle.com, james.laskey@oracle.com, lois.foltan@oracle.com, miroslav.kos@oracle.com, huaming.li@oracle.com, sean.mullan@oracle.com, naoto.sato@oracle.com, masayoshi.okutsu@oracle.com, peter.levart@gmail.com, philip.race@oracle.com, claes.redestad@oracle.com, sergey.bylokhov@oracle.com, alexandre.iline@oracle.com, volker.simonis@gmail.com, staffan.larsen@oracle.com, stuart.marks@oracle.com, semyon.sadetsky@oracle.com, serguei.spitsyn@oracle.com, sundararajan.athijegannathan@oracle.com, valerie.peng@oracle.com, vincent.x.ryan@oracle.com, weijun.wang@oracle.com, yuri.nesterenko@oracle.com, yekaterina.kantserova@oracle.com, alexander.kulyakhtin@oracle.com, felix.yang@oracle.com, andrei.eremeev@oracle.com, frank.yuan@oracle.com, sergei.pikalev@oracle.com, sibabrata.sahoo@oracle.com, tiantian.du@oracle.com, sha.jiang@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.  Oracle designates this
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    10
 *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    15
 * accompanied this code).
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    16
 *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    20
 *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    23
 * questions.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    24
 */
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    25
package jdk.tools.jlink.internal.plugins;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    26
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    27
import java.io.BufferedReader;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    28
import java.io.ByteArrayInputStream;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    29
import java.io.IOException;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    30
import java.io.InputStreamReader;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    31
import java.io.UncheckedIOException;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    32
import java.nio.charset.StandardCharsets;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    33
import java.util.Collections;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    34
import java.util.Comparator;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    35
import java.util.HashSet;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    36
import java.util.List;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    37
import java.util.Map;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    38
import java.util.Set;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    39
import java.util.TreeSet;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    40
import java.util.function.Predicate;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    41
import java.util.stream.Collectors;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    42
import jdk.tools.jlink.plugin.TransformerPlugin;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    43
import jdk.tools.jlink.plugin.Pool;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    44
import jdk.tools.jlink.plugin.Pool.ModuleDataType;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    45
import jdk.tools.jlink.internal.Utils;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    46
import jdk.tools.jlink.plugin.PluginException;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    47
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    48
/**
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    49
 *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    50
 * Exclude VM plugin
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    51
 */
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    52
public final class ExcludeVMPlugin implements TransformerPlugin {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    53
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    54
    private static final class JvmComparator implements Comparator<Jvm> {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    55
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    56
        @Override
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    57
        public int compare(Jvm o1, Jvm o2) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    58
            return o1.getEfficience() - o2.getEfficience();
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    59
        }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    60
    }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    61
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    62
    private enum Jvm {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    63
        // The efficience order server - client - minimal.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    64
        SERVER("server", 3), CLIENT("client", 2), MINIMAL("minimal", 1);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    65
        private final String name;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    66
        private final int efficience;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    67
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    68
        Jvm(String name, int efficience) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    69
            this.name = name;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    70
            this.efficience = efficience;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    71
        }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    72
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    73
        private String getName() {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    74
            return name;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    75
        }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    76
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    77
        private int getEfficience() {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    78
            return efficience;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    79
        }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    80
    }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    81
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    82
    private static final String JVM_CFG = "jvm.cfg";
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    83
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    84
    public static final String NAME = "vm";
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    85
    private static final String ALL = "all";
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    86
    private static final String CLIENT = "client";
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    87
    private static final String SERVER = "server";
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    88
    private static final String MINIMAL = "minimal";
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    89
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    90
    private Predicate<String> predicate;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    91
    private Jvm target;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    92
    private boolean keepAll;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    93
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    94
    @Override
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    95
    public String getName() {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    96
        return NAME;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    97
    }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    98
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    99
    /**
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   100
     * VM paths:
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   101
     * /java.base/native/{architecture}/{server|client|minimal}/{shared lib}
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   102
     * e.g.: /java.base/native/amd64/server/libjvm.so
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   103
     * /java.base/native/server/libjvm.dylib
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   104
     */
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   105
    private List<Pool.ModuleData> getVMs(Pool in) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   106
        String jvmlib = jvmlib();
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   107
        List<Pool.ModuleData> ret = in.getModule("java.base").getContent().stream().filter((t) -> {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   108
            return t.getPath().endsWith("/" + jvmlib);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   109
        }).collect(Collectors.toList());
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   110
        return ret;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   111
    }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   112
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   113
    @Override
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   114
    public void visit(Pool in, Pool out) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   115
        String jvmlib = jvmlib();
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   116
        TreeSet<Jvm> existing = new TreeSet<>(new JvmComparator());
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   117
        TreeSet<Jvm> removed = new TreeSet<>(new JvmComparator());
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   118
        if (!keepAll) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   119
            // First retrieve all available VM names and removed VM
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   120
            List<Pool.ModuleData> jvms = getVMs(in);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   121
            for (Jvm jvm : Jvm.values()) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   122
                for (Pool.ModuleData md : jvms) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   123
                    if (md.getPath().endsWith("/" + jvm.getName() + "/" + jvmlib)) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   124
                        existing.add(jvm);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   125
                        if (isRemoved(md)) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   126
                            removed.add(jvm);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   127
                        }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   128
                    }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   129
                }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   130
            }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   131
        }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   132
        // Check that target exists
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   133
        if (!keepAll) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   134
            if (!existing.contains(target)) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   135
                throw new PluginException("Selected VM " + target.getName() + " doesn't exist.");
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   136
            }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   137
        }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   138
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   139
        // Rewrite the jvm.cfg file.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   140
        in.visit((file) -> {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   141
            if (!keepAll) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   142
                if (file.getType().equals(ModuleDataType.NATIVE_LIB)) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   143
                    if (file.getPath().endsWith(JVM_CFG)) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   144
                        try {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   145
                            file = handleJvmCfgFile(file, existing, removed);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   146
                        } catch (IOException ex) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   147
                            throw new UncheckedIOException(ex);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   148
                        }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   149
                    }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   150
                }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   151
                file = isRemoved(file) ? null : file;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   152
            }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   153
            return file;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   154
        }, out);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   155
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   156
    }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   157
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   158
    private boolean isRemoved(Pool.ModuleData file) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   159
        return !predicate.test(file.getPath());
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   160
    }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   161
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   162
    @Override
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   163
    public Set<PluginType> getType() {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   164
        Set<PluginType> set = new HashSet<>();
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   165
        set.add(CATEGORY.FILTER);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   166
        return Collections.unmodifiableSet(set);
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
    @Override
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   170
    public String getDescription() {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   171
        return PluginsResourceBundle.getDescription(NAME);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   172
    }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   173
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   174
    @Override
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   175
    public boolean hasArguments() {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   176
        return true;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   177
    }
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 String getArgumentsDescription() {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   181
       return PluginsResourceBundle.getArgument(NAME);
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 void configure(Map<String, String> config) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   186
        try {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   187
            String value = config.get(NAME);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   188
            String exclude = "";
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   189
            switch (value) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   190
                case ALL: {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   191
                    // no filter.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   192
                    keepAll = true;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   193
                    break;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   194
                }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   195
                case CLIENT: {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   196
                    target = Jvm.CLIENT;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   197
                    exclude = "/java.base/native*server/*,/java.base/native*minimal/*";
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   198
                    break;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   199
                }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   200
                case SERVER: {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   201
                    target = Jvm.SERVER;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   202
                    exclude = "/java.base/native*client/*,/java.base/native*minimal/*";
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   203
                    break;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   204
                }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   205
                case MINIMAL: {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   206
                    target = Jvm.MINIMAL;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   207
                    exclude = "/java.base/native*server/*,/java.base/native*client/*";
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   208
                    break;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   209
                }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   210
                default: {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   211
                    throw new PluginException("Unknown option " + value);
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
            predicate = new ResourceFilter(Utils.listParser.apply(exclude), true);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   215
        } catch (IOException ex) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   216
            throw new UncheckedIOException(ex);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   217
        }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   218
    }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   219
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   220
    private Pool.ModuleData handleJvmCfgFile(Pool.ModuleData orig,
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   221
            TreeSet<Jvm> existing,
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   222
            TreeSet<Jvm> removed) throws IOException {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   223
        if (keepAll) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   224
            return orig;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   225
        }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   226
        StringBuilder builder = new StringBuilder();
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   227
        // Keep comments
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   228
        try (BufferedReader reader
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   229
                = new BufferedReader(new InputStreamReader(orig.stream(),
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   230
                        StandardCharsets.UTF_8))) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   231
            reader.lines().forEach((s) -> {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   232
                if (s.startsWith("#")) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   233
                    builder.append(s).append("\n");
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   234
                }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   235
            });
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   236
        }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   237
        TreeSet<Jvm> remaining = new TreeSet<>(new JvmComparator());
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   238
        // Add entry in jvm.cfg file from the more efficient to less efficient.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   239
        for (Jvm platform : existing) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   240
            if (!removed.contains(platform)) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   241
                remaining.add(platform);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   242
                builder.append("-").append(platform.getName()).append(" KNOWN\n");
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   243
            }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   244
        }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   245
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   246
        // removed JVM are aliased to the most efficient remaining JVM (last one).
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   247
        // The order in the file is from most to less efficient platform
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   248
        for (Jvm platform : removed.descendingSet()) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   249
            builder.append("-").append(platform.getName()).
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   250
                    append(" ALIASED_TO -").
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   251
                    append(remaining.last().getName()).append("\n");
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   252
        }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   253
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   254
        byte[] content = builder.toString().getBytes(StandardCharsets.UTF_8);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   255
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   256
        return Pool.newImageFile(orig.getModule(),
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   257
                orig.getPath(),
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   258
                orig.getType(),
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   259
                new ByteArrayInputStream(content), content.length);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   260
    }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   261
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   262
    private static String jvmlib() {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   263
        String lib = "libjvm.so";
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   264
        if (isWindows()) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   265
            lib = "jvm.dll";
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   266
        } else if (isMac()) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   267
            lib = "libjvm.dylib";
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   268
        }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   269
        return lib;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   270
    }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   271
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   272
    private static boolean isWindows() {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   273
        return System.getProperty("os.name").startsWith("Windows");
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   274
    }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   275
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   276
    private static boolean isMac() {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   277
        return System.getProperty("os.name").startsWith("Mac OS");
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   278
    }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   279
}