1 /* 2 * Copyright (c) 2015, 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 */ 25 package jdk.tools.jlink.internal.plugins; 26 27 import java.io.BufferedReader; 28 import java.io.IOException; 29 import java.io.InputStreamReader; 30 import java.io.UncheckedIOException; 31 import java.nio.charset.StandardCharsets; 32 import java.util.Comparator; 33 import java.util.List; 34 import java.util.Map; 35 import java.util.TreeSet; 36 import java.util.function.Predicate; 37 import java.util.stream.Collectors; 38 import jdk.tools.jlink.plugin.Plugin; 39 import jdk.tools.jlink.plugin.ResourcePool; 40 import jdk.tools.jlink.plugin.ResourcePoolBuilder; 41 import jdk.tools.jlink.plugin.ResourcePoolModule; 42 import jdk.tools.jlink.plugin.ResourcePoolEntry; 43 import jdk.tools.jlink.plugin.PluginException; 44 45 /** 46 * 47 * Exclude VM plugin 48 */ 49 public final class ExcludeVMPlugin implements Plugin { 50 51 private static final class JvmComparator implements Comparator<Jvm> { 52 53 @Override 54 public int compare(Jvm o1, Jvm o2) { 55 return o1.getEfficience() - o2.getEfficience(); 56 } 57 } 58 59 private enum Jvm { 60 // The efficience order server - client - minimal. 61 SERVER("server", 3), CLIENT("client", 2), MINIMAL("minimal", 1); 62 private final String name; 63 private final int efficience; 64 65 Jvm(String name, int efficience) { 66 this.name = name; 67 this.efficience = efficience; 68 } 69 70 private String getName() { 71 return name; 72 } 73 74 private int getEfficience() { 75 return efficience; 76 } 77 } 78 79 private static final String JVM_CFG = "jvm.cfg"; 80 81 public static final String NAME = "vm"; 82 private static final String ALL = "all"; 83 private static final String CLIENT = "client"; 84 private static final String SERVER = "server"; 85 private static final String MINIMAL = "minimal"; 86 87 private Predicate<String> predicate; 88 private Jvm target; 89 private boolean keepAll; 90 91 @Override 92 public String getName() { 93 return NAME; 94 } 95 96 /** 97 * VM paths: 98 * /java.base/lib/{architecture}/{server|client|minimal}/{shared lib} 99 * e.g.: /java.base/lib/server/libjvm.so 100 * /java.base/lib/server/libjvm.dylib 101 */ 102 private List<ResourcePoolEntry> getVMs(ResourcePoolModule javaBase, String[] jvmlibs) { 103 List<ResourcePoolEntry> ret = javaBase.entries().filter((t) -> { 104 String path = t.path(); 105 for (String jvmlib : jvmlibs) { 106 if (t.path().endsWith("/" + jvmlib)) { 107 return true; 108 } 109 } 110 return false; 111 }).collect(Collectors.toList()); 112 return ret; 113 } 114 115 @Override 116 public ResourcePool transform(ResourcePool in, ResourcePoolBuilder out) { 117 ResourcePoolModule javaBase = in.moduleView().findModule("java.base").get(); 118 String[] jvmlibs = jvmlibs(javaBase.descriptor().osName().get()); 119 TreeSet<Jvm> existing = new TreeSet<>(new JvmComparator()); 120 TreeSet<Jvm> removed = new TreeSet<>(new JvmComparator()); 121 if (!keepAll) { 122 // First retrieve all available VM names and removed VM 123 List<ResourcePoolEntry> jvms = getVMs(javaBase, jvmlibs); 124 for (Jvm jvm : Jvm.values()) { 125 for (ResourcePoolEntry md : jvms) { 126 String mdPath = md.path(); 127 for (String jvmlib : jvmlibs) { 128 if (mdPath.endsWith("/" + jvm.getName() + "/" + jvmlib)) { 129 existing.add(jvm); 130 if (isRemoved(md)) { 131 removed.add(jvm); 132 } 133 } 134 } 135 } 136 } 137 } 138 // Check that target exists 139 if (!keepAll) { 140 if (!existing.contains(target)) { 141 throw new PluginException("Selected VM " + target.getName() + " doesn't exist."); 142 } 143 } 144 145 // Rewrite the jvm.cfg file. 146 in.transformAndCopy((file) -> { 147 if (!keepAll) { 148 if (file.type().equals(ResourcePoolEntry.Type.NATIVE_LIB)) { 149 if (file.path().endsWith(JVM_CFG)) { 150 try { 151 file = handleJvmCfgFile(file, existing, removed); 152 } catch (IOException ex) { 153 throw new UncheckedIOException(ex); 154 } 155 } 156 } 157 file = isRemoved(file) ? null : file; 158 } 159 return file; 160 }, out); 161 162 return out.build(); 163 } 164 165 private boolean isRemoved(ResourcePoolEntry file) { 166 return !predicate.test(file.path()); 167 } 168 169 @Override 170 public Category getType() { 171 return Category.FILTER; 172 } 173 174 @Override 175 public String getDescription() { 176 return PluginsResourceBundle.getDescription(NAME); 177 } 178 179 @Override 180 public boolean hasArguments() { 181 return true; 182 } 183 184 @Override 185 public String getArgumentsDescription() { 186 return PluginsResourceBundle.getArgument(NAME); 187 } 188 189 @Override 190 public void configure(Map<String, String> config) { 191 String value = config.get(NAME); 192 String exclude = ""; 193 switch (value) { 194 case ALL: { 195 // no filter. 196 keepAll = true; 197 break; 198 } 199 case CLIENT: { 200 target = Jvm.CLIENT; 201 exclude = "/java.base/lib**server/**,/java.base/lib**minimal/**"; 202 break; 203 } 204 case SERVER: { 205 target = Jvm.SERVER; 206 exclude = "/java.base/lib**client/**,/java.base/lib**minimal/**"; 207 break; 208 } 209 case MINIMAL: { 210 target = Jvm.MINIMAL; 211 exclude = "/java.base/lib**server/**,/java.base/lib**client/**"; 212 break; 213 } 214 default: { 215 throw new IllegalArgumentException("Unknown exclude VM option: " + value); 216 } 217 } 218 predicate = ResourceFilter.excludeFilter(exclude); 219 } 220 221 private ResourcePoolEntry handleJvmCfgFile(ResourcePoolEntry orig, 222 TreeSet<Jvm> existing, 223 TreeSet<Jvm> removed) throws IOException { 224 if (keepAll) { 225 return orig; 226 } 227 StringBuilder builder = new StringBuilder(); 228 // Keep comments 229 try (BufferedReader reader 230 = new BufferedReader(new InputStreamReader(orig.content(), 231 StandardCharsets.UTF_8))) { 232 reader.lines().forEach((s) -> { 233 if (s.startsWith("#")) { 234 builder.append(s).append("\n"); 235 } 236 }); 237 } 238 TreeSet<Jvm> remaining = new TreeSet<>(new JvmComparator()); 239 // Add entry in jvm.cfg file from the more efficient to less efficient. 240 for (Jvm platform : existing) { 241 if (!removed.contains(platform)) { 242 remaining.add(platform); 243 builder.append("-").append(platform.getName()).append(" KNOWN\n"); 244 } 245 } 246 247 // removed JVM are aliased to the most efficient remaining JVM (last one). 248 // The order in the file is from most to less efficient platform 249 for (Jvm platform : removed.descendingSet()) { 250 builder.append("-").append(platform.getName()). 251 append(" ALIASED_TO -"). 252 append(remaining.last().getName()).append("\n"); 253 } 254 255 byte[] content = builder.toString().getBytes(StandardCharsets.UTF_8); 256 257 return orig.copyWithContent(content); 258 } 259 260 private static String[] jvmlibs(String osName) { 261 if (isWindows(osName)) { 262 return new String[] { "jvm.dll" }; 263 } else if (isMac(osName)) { 264 return new String[] { "libjvm.dylib", "libjvm.a" }; 265 } else { 266 return new String[] { "libjvm.so", "libjvm.a" }; 267 } 268 } 269 270 private static boolean isWindows(String osName) { 271 return osName.startsWith("Windows"); 272 } 273 274 private static boolean isMac(String osName) { 275 return osName.startsWith("Mac OS") || osName.startsWith("Darwin"); 276 } 277 }