-
Notifications
You must be signed in to change notification settings - Fork 4.6k
/
Jvm.java
383 lines (341 loc) · 12.7 KB
/
Jvm.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
/*
* Copyright 2012 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.gradle.internal.jvm;
import com.google.common.base.Optional;
import com.google.common.collect.Sets;
import org.gradle.api.JavaVersion;
import org.gradle.internal.FileUtils;
import org.gradle.internal.SystemProperties;
import org.gradle.internal.os.OperatingSystem;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import javax.annotation.Nullable;
import java.io.File;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.concurrent.atomic.AtomicReference;
import java.util.regex.Pattern;
public class Jvm implements JavaInfo {
private static final Logger LOGGER = LoggerFactory.getLogger(Jvm.class);
private static final HashSet<String> VENDOR_PROPERTIES = Sets.newHashSet("java.vendor", "java.vm.vendor");
private static final AtomicReference<Jvm> CURRENT = new AtomicReference<Jvm>();
private static final Pattern APP_NAME_REGEX = Pattern.compile("APP_NAME_\\d+");
private static final Pattern JAVA_MAIN_CLASS_REGEX = Pattern.compile("JAVA_MAIN_CLASS_\\d+");
private final OperatingSystem os;
//supplied java location
private final File javaBase;
//discovered java location
private final File javaHome;
private final boolean userSupplied;
private final String implementationJavaVersion;
private final JavaVersion javaVersion;
// Cached resolved executables
private File javaExecutable;
private File javacExecutable;
private File javadocExecutable;
private Optional<File> toolsJar;
private Boolean jdk;
public static Jvm current() {
Jvm jvm = CURRENT.get();
if (jvm == null) {
CURRENT.compareAndSet(null, new Jvm(OperatingSystem.current()));
jvm = CURRENT.get();
}
return jvm;
}
private static Jvm create(File javaBase, @Nullable String implementationJavaVersion, @Nullable JavaVersion javaVersion) {
Jvm jvm = new Jvm(OperatingSystem.current(), javaBase, implementationJavaVersion, javaVersion);
Jvm current = current();
return jvm.getJavaHome().equals(current.getJavaHome()) ? current : jvm;
}
/**
* Constructs JVM details by inspecting the current JVM.
*/
Jvm(OperatingSystem os) {
this(os, FileUtils.canonicalize(new File(System.getProperty("java.home"))), System.getProperty("java.version"), JavaVersion.current(), false);
}
/**
* Constructs JVM details from the given values
*/
Jvm(OperatingSystem os, File suppliedJavaBase, String implementationJavaVersion, JavaVersion javaVersion) {
this(os, suppliedJavaBase, implementationJavaVersion, javaVersion, true);
}
private Jvm(OperatingSystem os, File suppliedJavaBase, String implementationJavaVersion, JavaVersion javaVersion, boolean userSupplied) {
this.os = os;
this.javaBase = suppliedJavaBase;
this.implementationJavaVersion = implementationJavaVersion;
this.javaVersion = javaVersion;
this.userSupplied = userSupplied;
this.javaHome = findJavaHome(suppliedJavaBase);
}
/**
* Creates JVM instance for given java home. Attempts to validate if provided javaHome is a valid jdk or jre location.
* This method is intended to be used for user supplied java homes.
*
* @param javaHome - location of your jdk or jre (jdk is safer), cannot be null
* @return jvm for given java home
* @throws org.gradle.internal.jvm.JavaHomeException when supplied javaHome does not seem to be a valid jdk or jre location
* @throws IllegalArgumentException when supplied javaHome is not a valid folder
*/
public static JavaInfo forHome(File javaHome) throws JavaHomeException, IllegalArgumentException {
if (javaHome == null || !javaHome.isDirectory()) {
throw new IllegalArgumentException("Supplied javaHome must be a valid directory. You supplied: " + javaHome);
}
Jvm jvm = create(javaHome, null, null);
//some validation:
jvm.getJavaExecutable();
return jvm;
}
/**
* Creates JVM instance for given values. This method is intended to be used for discovered java homes.
*/
public static Jvm discovered(File javaHome, String implementationJavaVersion, JavaVersion javaVersion) {
return create(javaHome, implementationJavaVersion, javaVersion);
}
@Override
public String toString() {
if (userSupplied) {
return "User-supplied java: " + javaBase;
}
return SystemProperties.getInstance().getJavaVersion() + " (" + System.getProperty("java.vm.vendor") + " " + System.getProperty("java.vm.version") + ")";
}
@Override
public boolean equals(Object obj) {
if (obj == this) {
return true;
}
if (obj == null || obj.getClass() != getClass()) {
return false;
}
Jvm other = (Jvm) obj;
return other.javaHome.equals(javaHome);
}
@Override
public int hashCode() {
return javaHome.hashCode();
}
public boolean isJdk() {
if (jdk == null) {
jdk = findExecutableInJavaHome("javac") != null;
}
return jdk;
}
@Nullable
private File findExecutableInJavaHome(String command) {
File executable = commandLocation(command);
if (executable.isFile()) {
return executable;
}
return null;
}
private File commandLocation(String command) {
File exec = new File(getJavaHome(), "bin/" + command);
return new File(os.getExecutableName(exec.getAbsolutePath()));
}
private File findExecutable(String command) {
File executable = commandLocation(command);
if (executable.isFile()) {
return executable;
}
if (userSupplied) { //then we want to validate strictly
throw new JavaHomeException(String.format("The supplied javaHome seems to be invalid."
+ " I cannot find the %s executable. Tried location: %s", command, executable.getAbsolutePath()));
}
File pathExecutable = os.findInPath(command);
if (pathExecutable != null) {
LOGGER.info(String.format("Unable to find the '%s' executable using home: %s. We found it on the PATH: %s.",
command, getJavaHome(), pathExecutable));
return pathExecutable;
}
LOGGER.warn("Unable to find the '{}' executable. Tried the java home: {} and the PATH."
+ " We will assume the executable can be run in the current working folder.",
command, getJavaHome());
return new File(os.getExecutableName(command));
}
/**
* {@inheritDoc}
*/
@Override
public File getJavaExecutable() throws JavaHomeException {
if (javaExecutable != null) {
return javaExecutable;
}
javaExecutable = findExecutable("java");
return javaExecutable;
}
/**
* {@inheritDoc}
*/
@Override
public File getJavacExecutable() throws JavaHomeException {
if (javacExecutable != null) {
return javacExecutable;
}
javacExecutable = findExecutable("javac");
return javacExecutable;
}
/**
* {@inheritDoc}
*/
@Override
public File getJavadocExecutable() throws JavaHomeException {
if (javadocExecutable != null) {
return javadocExecutable;
}
javadocExecutable = findExecutable("javadoc");
return javadocExecutable;
}
/**
* {@inheritDoc}
*/
@Override
public File getExecutable(String name) throws JavaHomeException {
return findExecutable(name);
}
/**
* @return the {@link JavaVersion} information
*/
@Nullable
public JavaVersion getJavaVersion() {
return javaVersion;
}
/**
* {@inheritDoc}
*/
@Override
public File getJavaHome() {
return javaHome;
}
private File findJavaHome(File javaBase) {
Optional<File> toolsJar = findToolsJar(javaBase);
if (toolsJar.isPresent()) {
return toolsJar.get().getParentFile().getParentFile();
} else if (javaBase.getName().equalsIgnoreCase("jre") && new File(javaBase.getParentFile(), "bin/java").exists()) {
return javaBase.getParentFile();
} else {
return javaBase;
}
}
/**
* {@inheritDoc}
*/
@Override
public File getToolsJar() {
if (toolsJar != null) {
return toolsJar.orNull();
} else {
toolsJar = findToolsJar(javaHome);
}
return toolsJar.orNull();
}
/**
* Locates a stand-alone JRE installation for this JVM. Returns null if not found. This is the JRE installed outside the JDK installation.
*/
@Nullable
public File getStandaloneJre() {
if (javaVersion.isJava9Compatible()) {
return null;
}
if (os.isWindows()) {
File jreDir;
if (javaVersion.isJava5()) {
jreDir = new File(javaHome.getParentFile(), "jre" + implementationJavaVersion);
} else {
jreDir = new File(javaHome.getParentFile(), "jre" + javaVersion.getMajorVersion());
}
if (jreDir.isDirectory()) {
return jreDir;
}
}
return null;
}
/**
* Locates the JRE installation contained within this JVM. Returns null if no JRE installation is available.
*/
@Nullable
public File getEmbeddedJre() {
File jreDir = new File(javaHome, "jre");
if (jreDir.isDirectory()) {
return jreDir;
}
return null;
}
@Nullable
public File getJre() {
File standaloneJre = getStandaloneJre();
if (standaloneJre != null) {
return standaloneJre;
}
return getEmbeddedJre();
}
private Optional<File> findToolsJar(File javaHome) {
File toolsJar = new File(javaHome, "lib/tools.jar");
if (toolsJar.exists()) {
return Optional.of(toolsJar);
}
if (javaHome.getName().equalsIgnoreCase("jre")) {
javaHome = javaHome.getParentFile();
toolsJar = new File(javaHome, "lib/tools.jar");
if (toolsJar.exists()) {
return Optional.of(toolsJar);
}
}
if (os.isWindows()) {
String version = implementationJavaVersion;
if (javaHome.getName().matches("jre\\d+") || javaHome.getName().equals("jre" + version)) {
javaHome = new File(javaHome.getParentFile(), "jdk" + version);
toolsJar = new File(javaHome, "lib/tools.jar");
if (toolsJar.exists()) {
return Optional.of(toolsJar);
}
}
}
return Optional.absent();
}
public static Map<String, ?> getInheritableEnvironmentVariables(Map<String, ?> envVars) {
Map<String, Object> vars = new HashMap<String, Object>();
for (Map.Entry<String, ?> entry : envVars.entrySet()) {
// The following are known variables that can change between builds and should not be inherited
if (APP_NAME_REGEX.matcher(entry.getKey()).matches()
|| JAVA_MAIN_CLASS_REGEX.matcher(entry.getKey()).matches()
|| entry.getKey().equals("TERM_SESSION_ID")
|| entry.getKey().equals("ITERM_SESSION_ID")) {
continue;
}
vars.put(entry.getKey(), entry.getValue());
}
return vars;
}
public boolean isIbmJvm() {
for (String vendorProperty : VENDOR_PROPERTIES) {
if (System.getProperties().containsKey(vendorProperty)
&& System.getProperty(vendorProperty).toLowerCase().startsWith("ibm corporation")) {
return true;
}
}
return false;
}
@Nullable
public String getVendor() {
for (String vendorProperty : VENDOR_PROPERTIES) {
if (System.getProperties().containsKey(vendorProperty) && !System.getProperty(vendorProperty).isEmpty()) {
return System.getProperty(vendorProperty);
}
}
return null;
}
}