-
Notifications
You must be signed in to change notification settings - Fork 4.5k
/
JavaInstallationRegistry.java
180 lines (149 loc) · 6.62 KB
/
JavaInstallationRegistry.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
/*
* Copyright 2020 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.jvm.toolchain.internal;
import com.google.common.annotations.VisibleForTesting;
import org.gradle.api.GradleException;
import org.gradle.api.logging.Logger;
import org.gradle.api.logging.Logging;
import org.gradle.internal.operations.BuildOperationContext;
import org.gradle.internal.operations.BuildOperationDescriptor;
import org.gradle.internal.operations.BuildOperationExecutor;
import org.gradle.internal.operations.CallableBuildOperation;
import org.gradle.internal.os.OperatingSystem;
import javax.inject.Inject;
import java.io.File;
import java.io.IOException;
import java.util.List;
import java.util.Set;
import java.util.concurrent.Callable;
import java.util.concurrent.ConcurrentHashMap;
import java.util.function.Function;
import java.util.function.Predicate;
import java.util.function.Supplier;
import java.util.stream.Collectors;
public class JavaInstallationRegistry {
private final BuildOperationExecutor executor;
private final Installations installations;
private final Logger logger;
private final OperatingSystem os;
@Inject
public JavaInstallationRegistry(List<InstallationSupplier> suppliers, BuildOperationExecutor executor, OperatingSystem os) {
this(suppliers, Logging.getLogger(JavaInstallationRegistry.class), executor, os);
}
private JavaInstallationRegistry(List<InstallationSupplier> suppliers, Logger logger, BuildOperationExecutor executor, OperatingSystem os) {
this.logger = logger;
this.executor = executor;
this.installations = new Installations(() -> collectInBuildOperation(suppliers));
this.os = os;
}
@VisibleForTesting
static JavaInstallationRegistry withLogger(List<InstallationSupplier> suppliers, Logger logger, BuildOperationExecutor executor) {
return new JavaInstallationRegistry(suppliers, logger, executor, OperatingSystem.current());
}
private Set<InstallationLocation> collectInBuildOperation(List<InstallationSupplier> suppliers) {
return executor.call(new ToolchainDetectionBuildOperation(() -> collectInstallations(suppliers)));
}
public Set<InstallationLocation> listInstallations() {
return installations.get();
}
public void addInstallation(InstallationLocation installation) {
installations.add(installation);
}
private Set<InstallationLocation> collectInstallations(List<InstallationSupplier> suppliers) {
return suppliers.parallelStream()
.map(InstallationSupplier::get)
.flatMap(Set::stream)
.filter(this::installationExists)
.map(this::canonicalize)
.filter(distinctByKey(InstallationLocation::getLocation))
.collect(Collectors.toSet());
}
boolean installationExists(InstallationLocation installationLocation) {
File file = installationLocation.getLocation();
if (!file.exists()) {
logger.warn("Directory {} used for java installations does not exist", installationLocation.getDisplayName());
return false;
}
if (!file.isDirectory()) {
logger.warn("Path for java installation {} points to a file, not a directory", installationLocation.getDisplayName());
return false;
}
return true;
}
private InstallationLocation canonicalize(InstallationLocation location) {
final File file = location.getLocation();
try {
final File canonicalFile = file.getCanonicalFile();
final File javaHome = findJavaHome(canonicalFile);
return new InstallationLocation(javaHome, location.getSource());
} catch (IOException e) {
throw new GradleException(String.format("Could not canonicalize path to java installation: %s.", file), e);
}
}
private File findJavaHome(File potentialHome) {
if (os.isMacOsX() && new File(potentialHome, "Contents/Home").exists()) {
return new File(potentialHome, "Contents/Home");
}
final File standaloneJre = new File(potentialHome, "jre");
if(!hasJavaExecutable(potentialHome) && hasJavaExecutable(standaloneJre)) {
return standaloneJre;
}
return potentialHome;
}
private boolean hasJavaExecutable(File potentialHome) {
return new File(potentialHome, os.getExecutableName("bin/java")).exists();
}
public static <T> Predicate<T> distinctByKey(Function<? super T, ?> keyExtractor) {
Set<Object> seen = ConcurrentHashMap.newKeySet();
return t -> seen.add(keyExtractor.apply(t));
}
private static class ToolchainDetectionBuildOperation implements CallableBuildOperation<Set<InstallationLocation>> {
private final Callable<Set<InstallationLocation>> detectionStrategy;
public ToolchainDetectionBuildOperation(Callable<Set<InstallationLocation>> detectionStrategy) {
this.detectionStrategy = detectionStrategy;
}
@Override
public Set<InstallationLocation> call(BuildOperationContext context) throws Exception {
return detectionStrategy.call();
}
@Override
public BuildOperationDescriptor.Builder description() {
return BuildOperationDescriptor
.displayName("Toolchain detection")
.progressDisplayName("Detecting local java toolchains");
}
}
private static class Installations {
private final Supplier<Set<InstallationLocation>> initializer;
private Set<InstallationLocation> locations = null;
Installations(Supplier<Set<InstallationLocation>> initializer) {
this.initializer = initializer;
}
synchronized Set<InstallationLocation> get() {
initIfNeeded();
return locations;
}
synchronized void add(InstallationLocation location) {
initIfNeeded();
locations.add(location);
}
private void initIfNeeded() {
if (locations == null) {
locations = initializer.get();
}
}
}
}