-
-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathJdkInstallers.java
More file actions
163 lines (139 loc) · 4.27 KB
/
JdkInstallers.java
File metadata and controls
163 lines (139 loc) · 4.27 KB
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
package dev.jbang.devkitman;
import static dev.jbang.devkitman.util.FileUtils.deleteOnExit;
import java.nio.file.Path;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.HashMap;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Map;
import java.util.ServiceLoader;
import java.util.function.BiFunction;
import org.jspecify.annotations.NonNull;
import org.jspecify.annotations.Nullable;
public class JdkInstallers {
private List<JdkInstallers.Discovery> discoveries;
private static final JdkInstallers INSTANCE = new JdkInstallers();
private JdkInstallers() {
}
public static JdkInstallers instance() {
return INSTANCE;
}
public static Discovery.Config config(JdkProvider jdkProvider, Map<String, String> properties, Path cachePath) {
return new Discovery.Config(jdkProvider, properties, cachePath);
}
/**
* Returns a list of names of all available installers.
*
* @return a list of installer names
*/
public List<String> allNames() {
LinkedHashSet<String> names = new LinkedHashSet<>();
ArrayList<String> sorted = new ArrayList<>();
for (Discovery discovery : discoveries()) {
sorted.add(discovery.name());
}
names.addAll(sorted);
return new ArrayList<>(names);
}
public List<JdkInstaller> all(Discovery.Config config) {
return parseNames(config, allNames().toArray(new String[0]));
}
public List<JdkInstaller> parseNames(Discovery.Config config, String names) {
return parseNames(config, names.split(","));
}
public List<JdkInstaller> parseNames(Discovery.Config config, String... names) {
ArrayList<JdkInstaller> installers = new ArrayList<>();
if (names != null) {
for (String nameAndConfig : names) {
JdkInstaller installer = parseName(config, nameAndConfig);
if (installer != null) {
installers.add(installer);
}
}
}
return installers;
}
public JdkInstaller parseName(Discovery.Config config, String nameAndConfig) {
return parseName(config, nameAndConfig, this::byName);
}
JdkInstaller parseName(
Discovery.Config config,
String nameAndConfig,
BiFunction<String, Discovery.Config, JdkInstaller> action) {
String[] parts = nameAndConfig.split(";");
String name = parts[0];
Discovery.Config cfg = config.copy();
for (int i = 1; i < parts.length; i++) {
String[] keyValue = parts[i].split("=");
if (keyValue.length == 2) {
cfg.properties().put(keyValue[0], keyValue[1]);
}
}
return action.apply(name, cfg);
}
public JdkInstaller byName(String name, Discovery.Config config) {
for (Discovery discovery : discoveries()) {
if (discovery.name().equals(name)) {
JdkInstaller installer = discovery.create(config);
if (installer != null) {
return installer;
}
}
}
return null;
}
private synchronized List<Discovery> discoveries() {
if (discoveries == null) {
ServiceLoader<Discovery> loader = ServiceLoader.load(Discovery.class);
discoveries = new ArrayList<>();
for (Discovery discovery : loader) {
discoveries.add(discovery);
}
discoveries.sort(Comparator.comparing(Discovery::name));
}
return discoveries;
}
public interface Discovery {
@NonNull
String name();
@Nullable
JdkInstaller create(Config config);
class Config {
@NonNull
private final JdkProvider jdkProvider;
@NonNull
private final Map<String, String> properties;
private Path cachePath;
public Config(@NonNull JdkProvider jdkProvider, @Nullable Map<String, String> properties, Path cachePath) {
this.jdkProvider = jdkProvider;
this.properties = new HashMap<>();
if (properties != null) {
this.properties.putAll(properties);
}
this.cachePath = cachePath;
}
public @NonNull JdkProvider jdkProvider() {
return jdkProvider;
}
public @NonNull Map<String, String> properties() {
return properties;
}
public @NonNull Path cachePath() {
if (cachePath == null) {
// If no cache path is set, we create a temp dir as a curtesy that will be
// deleted on exit
try {
cachePath = deleteOnExit(java.nio.file.Files.createTempDirectory("jdk-installer-cache"));
} catch (java.io.IOException e) {
throw new RuntimeException(e);
}
}
return cachePath;
}
public Config copy() {
return new Config(jdkProvider, properties, cachePath);
}
}
}
}