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