-
Notifications
You must be signed in to change notification settings - Fork 4.5k
/
JavaToolchainQueryService.java
181 lines (157 loc) · 8.24 KB
/
JavaToolchainQueryService.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
/*
* 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.Transformer;
import org.gradle.api.internal.provider.DefaultProvider;
import org.gradle.api.internal.provider.ProviderInternal;
import org.gradle.api.model.ObjectFactory;
import org.gradle.api.provider.Provider;
import org.gradle.api.provider.ProviderFactory;
import org.gradle.internal.deprecation.DocumentedFailure;
import org.gradle.internal.jvm.Jvm;
import org.gradle.internal.jvm.inspection.JvmInstallationMetadata;
import org.gradle.internal.service.scopes.Scopes;
import org.gradle.internal.service.scopes.ServiceScope;
import org.gradle.jvm.toolchain.JavaToolchainSpec;
import org.gradle.jvm.toolchain.internal.install.DefaultJavaToolchainProvisioningService;
import org.gradle.jvm.toolchain.internal.install.JavaToolchainProvisioningService;
import javax.inject.Inject;
import java.io.File;
import java.util.Objects;
import java.util.Optional;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;
@ServiceScope(Scopes.Project.class) //TODO: should be much higher scoped, as many other toolchain related services, but is bogged down by the scope of services it depends on
public class JavaToolchainQueryService {
// A key that matches only the fallback toolchain
private static final JavaToolchainSpecInternal.Key FALLBACK_TOOLCHAIN_KEY = new JavaToolchainSpecInternal.Key() {
@Override
public String toString() {
return "FallbackToolchainSpecKey";
}
};
private final JavaInstallationRegistry registry;
private final JavaToolchainFactory toolchainFactory;
private final JavaToolchainProvisioningService installService;
private final Provider<Boolean> detectEnabled;
private final Provider<Boolean> downloadEnabled;
// Map values are either `JavaToolchain` or `Exception`
private final ConcurrentMap<JavaToolchainSpecInternal.Key, Object> matchingToolchains;
private final CurrentJvmToolchainSpec fallbackToolchainSpec;
@Inject
public JavaToolchainQueryService(
JavaInstallationRegistry registry,
JavaToolchainFactory toolchainFactory,
JavaToolchainProvisioningService provisioningService,
ProviderFactory factory,
ObjectFactory objectFactory
) {
this.registry = registry;
this.toolchainFactory = toolchainFactory;
this.installService = provisioningService;
this.detectEnabled = factory.gradleProperty(AutoDetectingInstallationSupplier.AUTO_DETECT).map(Boolean::parseBoolean);
this.downloadEnabled = factory.gradleProperty(DefaultJavaToolchainProvisioningService.AUTO_DOWNLOAD).map(Boolean::parseBoolean);
this.matchingToolchains = new ConcurrentHashMap<>();
this.fallbackToolchainSpec = objectFactory.newInstance(CurrentJvmToolchainSpec.class);
}
<T> Provider<T> toolFor(
JavaToolchainSpec spec,
Transformer<T, JavaToolchain> toolFunction,
DefaultJavaToolchainUsageProgressDetails.JavaTool requestedTool
) {
return findMatchingToolchain(spec)
.withSideEffect(toolchain -> toolchain.emitUsageEvent(requestedTool))
.map(toolFunction);
}
@VisibleForTesting
ProviderInternal<JavaToolchain> findMatchingToolchain(JavaToolchainSpec filter) {
JavaToolchainSpecInternal filterInternal = (JavaToolchainSpecInternal) Objects.requireNonNull(filter);
return new DefaultProvider<>(() -> resolveToolchain(filterInternal));
}
private JavaToolchain resolveToolchain(JavaToolchainSpecInternal requestedSpec) throws Exception {
requestedSpec.finalizeProperties();
if (!requestedSpec.isValid()) {
throw DocumentedFailure.builder()
.withSummary("Using toolchain specifications without setting a language version is not supported.")
.withAdvice("Consider configuring the language version.")
.withUpgradeGuideSection(7, "invalid_toolchain_specification_deprecation")
.build();
}
boolean useFallback = !requestedSpec.isConfigured();
JavaToolchainSpecInternal actualSpec = useFallback ? fallbackToolchainSpec : requestedSpec;
// We can't use the key of the fallback toolchain spec, because it is a spec that can match configured requests as well
JavaToolchainSpecInternal.Key actualKey = useFallback ? FALLBACK_TOOLCHAIN_KEY : requestedSpec.toKey();
Object resolutionResult = matchingToolchains.computeIfAbsent(actualKey, key -> {
try {
return query(actualSpec, useFallback);
} catch (Exception e) {
return e;
}
});
if (resolutionResult instanceof Exception) {
throw (Exception) resolutionResult;
} else {
return (JavaToolchain) resolutionResult;
}
}
private JavaToolchain query(JavaToolchainSpec spec, boolean isFallback) {
if (spec instanceof CurrentJvmToolchainSpec) {
// TODO (#22023) Look into checking if this optional is present and throwing an exception
return asToolchain(new InstallationLocation(Jvm.current().getJavaHome(), "current JVM"), spec, isFallback).toolchain().get();
}
if (spec instanceof SpecificInstallationToolchainSpec) {
final InstallationLocation installation = new InstallationLocation(((SpecificInstallationToolchainSpec) spec).getJavaHome(), "specific installation");
return asToolchainOrThrow(installation, spec);
}
Optional<JavaToolchain> detectedToolchain = registry.listInstallations().stream()
.map(javaHome -> asToolchain(javaHome, spec))
.filter(Optional::isPresent)
.map(Optional::get)
.filter(new JavaToolchainMatcher(spec))
.min(new JavaToolchainComparator());
if (detectedToolchain.isPresent()) {
return detectedToolchain.get();
}
InstallationLocation downloadedInstallation = downloadToolchain(spec);
JavaToolchain downloadedToolchain = asToolchainOrThrow(downloadedInstallation, spec);
registry.addInstallation(downloadedInstallation);
return downloadedToolchain;
}
private InstallationLocation downloadToolchain(JavaToolchainSpec spec) {
final Optional<File> installation = installService.tryInstall(spec);
if (!installation.isPresent()) {
throw new NoToolchainAvailableException(spec, detectEnabled.getOrElse(true), downloadEnabled.getOrElse(true));
}
return new InstallationLocation(installation.get(), "provisioned toolchain");
}
private Optional<JavaToolchain> asToolchain(InstallationLocation javaHome, JavaToolchainSpec spec) {
return asToolchain(javaHome, spec, false).toolchain();
}
private JavaToolchain asToolchainOrThrow(InstallationLocation javaHome, JavaToolchainSpec spec) {
JavaToolchainInstantiationResult result = asToolchain(javaHome, spec, false);
Optional<JavaToolchain> toolchain = result.toolchain();
if (!toolchain.isPresent()) {
JvmInstallationMetadata metadata = result.metadata();
throw new GradleException("Toolchain installation '" + javaHome.getLocation() + "' could not be probed: " + metadata.getErrorMessage(), metadata.getErrorCause());
}
return toolchain.get();
}
private JavaToolchainInstantiationResult asToolchain(InstallationLocation javaHome, JavaToolchainSpec spec, boolean isFallback) {
return toolchainFactory.newInstance(javaHome, new JavaToolchainInput(spec), isFallback);
}
}