1/*
2 * Copyright (c) 2015, 2017, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation.  Oracle designates this
8 * particular file as subject to the "Classpath" exception as provided
9 * by Oracle in the LICENSE file that accompanied this code.
10 *
11 * This code is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
14 * version 2 for more details (a copy is included in the LICENSE file that
15 * accompanied this code).
16 *
17 * You should have received a copy of the GNU General Public License version
18 * 2 along with this work; if not, write to the Free Software Foundation,
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20 *
21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22 * or visit www.oracle.com if you need additional information or have any
23 * questions.
24 */
25package jdk.tools.jlink.internal.plugins;
26
27import java.io.BufferedReader;
28import java.io.IOException;
29import java.io.InputStreamReader;
30import java.io.UncheckedIOException;
31import java.nio.charset.StandardCharsets;
32import java.util.Comparator;
33import java.util.List;
34import java.util.Map;
35import java.util.TreeSet;
36import java.util.function.Predicate;
37import java.util.stream.Collectors;
38
39import jdk.tools.jlink.internal.Platform;
40import jdk.tools.jlink.plugin.Plugin;
41import jdk.tools.jlink.plugin.ResourcePool;
42import jdk.tools.jlink.plugin.ResourcePoolBuilder;
43import jdk.tools.jlink.plugin.ResourcePoolModule;
44import jdk.tools.jlink.plugin.ResourcePoolEntry;
45import jdk.tools.jlink.plugin.PluginException;
46
47/**
48 *
49 * Exclude VM plugin
50 */
51public final class ExcludeVMPlugin implements Plugin {
52
53    private static final class JvmComparator implements Comparator<Jvm> {
54
55        @Override
56        public int compare(Jvm o1, Jvm o2) {
57            return o1.getEfficience() - o2.getEfficience();
58        }
59    }
60
61    private enum Jvm {
62        // The efficience order server - client - minimal.
63        SERVER("server", 3), CLIENT("client", 2), MINIMAL("minimal", 1);
64        private final String name;
65        private final int efficience;
66
67        Jvm(String name, int efficience) {
68            this.name = name;
69            this.efficience = efficience;
70        }
71
72        private String getName() {
73            return name;
74        }
75
76        private int getEfficience() {
77            return efficience;
78        }
79    }
80
81    private static final String JVM_CFG = "jvm.cfg";
82
83    public static final String NAME = "vm";
84    private static final String ALL = "all";
85    private static final String CLIENT = "client";
86    private static final String SERVER = "server";
87    private static final String MINIMAL = "minimal";
88
89    private Predicate<String> predicate;
90    private Jvm target;
91    private boolean keepAll;
92
93    @Override
94    public String getName() {
95        return NAME;
96    }
97
98    /**
99     * VM paths:
100     * /java.base/lib/{architecture}/{server|client|minimal}/{shared lib}
101     * e.g.: /java.base/lib/server/libjvm.so
102     * /java.base/lib/server/libjvm.dylib
103     */
104    private List<ResourcePoolEntry> getVMs(ResourcePoolModule javaBase, String[] jvmlibs) {
105        List<ResourcePoolEntry> ret = javaBase.entries().filter((t) -> {
106            String path = t.path();
107            for (String jvmlib : jvmlibs) {
108                if (t.path().endsWith("/" + jvmlib)) {
109                    return true;
110                }
111            }
112            return false;
113        }).collect(Collectors.toList());
114        return ret;
115    }
116
117    @Override
118    public ResourcePool transform(ResourcePool in, ResourcePoolBuilder out) {
119        ResourcePoolModule javaBase = in.moduleView().findModule("java.base").get();
120        String[] jvmlibs = jvmlibs(javaBase);
121        TreeSet<Jvm> existing = new TreeSet<>(new JvmComparator());
122        TreeSet<Jvm> removed = new TreeSet<>(new JvmComparator());
123        if (!keepAll) {
124            // First retrieve all available VM names and removed VM
125            List<ResourcePoolEntry> jvms = getVMs(javaBase, jvmlibs);
126            for (Jvm jvm : Jvm.values()) {
127                for (ResourcePoolEntry md : jvms) {
128                    String mdPath = md.path();
129                    for (String jvmlib : jvmlibs) {
130                        if (mdPath.endsWith("/" + jvm.getName() + "/" + jvmlib)) {
131                            existing.add(jvm);
132                            if (isRemoved(md)) {
133                                removed.add(jvm);
134                            }
135                        }
136                    }
137                }
138            }
139        }
140        // Check that target exists
141        if (!keepAll) {
142            if (!existing.contains(target)) {
143                throw new PluginException("Selected VM " + target.getName() + " doesn't exist.");
144            }
145        }
146
147        // Rewrite the jvm.cfg file.
148        in.transformAndCopy((file) -> {
149            if (!keepAll) {
150                if (file.type().equals(ResourcePoolEntry.Type.NATIVE_LIB)) {
151                    if (file.path().endsWith(JVM_CFG)) {
152                        try {
153                            file = handleJvmCfgFile(file, existing, removed);
154                        } catch (IOException ex) {
155                            throw new UncheckedIOException(ex);
156                        }
157                    }
158                }
159                file = isRemoved(file) ? null : file;
160            }
161            return file;
162        }, out);
163
164        return out.build();
165    }
166
167    private boolean isRemoved(ResourcePoolEntry file) {
168        return !predicate.test(file.path());
169    }
170
171    @Override
172    public Category getType() {
173        return Category.FILTER;
174    }
175
176    @Override
177    public String getDescription() {
178        return PluginsResourceBundle.getDescription(NAME);
179    }
180
181    @Override
182    public boolean hasArguments() {
183        return true;
184    }
185
186    @Override
187    public String getArgumentsDescription() {
188       return PluginsResourceBundle.getArgument(NAME);
189    }
190
191    @Override
192    public void configure(Map<String, String> config) {
193        String value = config.get(NAME);
194        String exclude = "";
195        switch (value) {
196            case ALL: {
197                // no filter.
198                keepAll = true;
199                break;
200            }
201            case CLIENT: {
202                target = Jvm.CLIENT;
203                exclude = "/java.base/lib**server/**,/java.base/lib**minimal/**";
204                break;
205            }
206            case SERVER: {
207                target = Jvm.SERVER;
208                exclude = "/java.base/lib**client/**,/java.base/lib**minimal/**";
209                break;
210            }
211            case MINIMAL: {
212                target = Jvm.MINIMAL;
213                exclude = "/java.base/lib**server/**,/java.base/lib**client/**";
214                break;
215            }
216            default: {
217                throw new IllegalArgumentException("Unknown exclude VM option: " + value);
218            }
219        }
220        predicate = ResourceFilter.excludeFilter(exclude);
221    }
222
223    private ResourcePoolEntry handleJvmCfgFile(ResourcePoolEntry orig,
224            TreeSet<Jvm> existing,
225            TreeSet<Jvm> removed) throws IOException {
226        if (keepAll) {
227            return orig;
228        }
229        StringBuilder builder = new StringBuilder();
230        // Keep comments
231        try (BufferedReader reader
232                = new BufferedReader(new InputStreamReader(orig.content(),
233                        StandardCharsets.UTF_8))) {
234            reader.lines().forEach((s) -> {
235                if (s.startsWith("#")) {
236                    builder.append(s).append("\n");
237                }
238            });
239        }
240        TreeSet<Jvm> remaining = new TreeSet<>(new JvmComparator());
241        // Add entry in jvm.cfg file from the more efficient to less efficient.
242        for (Jvm platform : existing) {
243            if (!removed.contains(platform)) {
244                remaining.add(platform);
245                builder.append("-").append(platform.getName()).append(" KNOWN\n");
246            }
247        }
248
249        // removed JVM are aliased to the most efficient remaining JVM (last one).
250        // The order in the file is from most to less efficient platform
251        for (Jvm platform : removed.descendingSet()) {
252            builder.append("-").append(platform.getName()).
253                    append(" ALIASED_TO -").
254                    append(remaining.last().getName()).append("\n");
255        }
256
257        byte[] content = builder.toString().getBytes(StandardCharsets.UTF_8);
258
259        return orig.copyWithContent(content);
260    }
261
262    private static String[] jvmlibs(ResourcePoolModule module) {
263        Platform platform = Platform.getTargetPlatform(module);
264        switch (platform) {
265            case WINDOWS:
266                return new String[] { "jvm.dll" };
267            case MACOS:
268                return new String[] { "libjvm.dylib", "libjvm.a" };
269            default:
270                return new String[] { "libjvm.so", "libjvm.a" };
271        }
272    }
273}
274