forked from oracle-actions/setup-java
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathListOpenJavaDevelopmentKits.java
More file actions
145 lines (132 loc) · 5.02 KB
/
ListOpenJavaDevelopmentKits.java
File metadata and controls
145 lines (132 loc) · 5.02 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
/*
* Copyright (c) 2022, 2026, Oracle and/or its affiliates.
*
* This source code is licensed under the UPL license found in the
* LICENSE.txt file in the root directory of this source tree.
*/
import java.net.URI;
import java.net.http.HttpClient;
import java.net.http.HttpRequest;
import java.net.http.HttpResponse;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;
import java.util.regex.Pattern;
import java.util.stream.Stream;
/**
* List Java Development Kit builds hosted at: {@code https://jdk.java.net}.
*
* <p>Example output:
*
* <pre>{@code
* 17,17.0.2,linux,x64=https://[...]/GPL/openjdk-17.0.2_linux-x64_bin.tar.gz
* 17,latest,linux,x64=https://[...]/GPL/openjdk-17.0.2_linux-x64_bin.tar.gz
* ga,latest,linux,x64=https://[...]/GPL/openjdk-17.0.2_linux-x64_bin.tar.gz
* }</pre>
*
* Keys are composed of {@code RELEASE,VERSION,OS-NAME,OS-ARCH} with:
*
* <ul>
* <li>{@code RELEASE}: Either a release number or a name of an early-access project
* <li>{@code VERSION}: Either a specific version or `latest` or `stable`
* <li>{@code OS-NAME}: An operating system name, usually one of: `linux`, `macos`, `windows`
* <li>{@code OS-ARCH}: An operating system architecture, like: `aarch64`, `x64`, or `x64-musl`
* </ul>
*/
class ListOpenJavaDevelopmentKits {
/** List of pages to visit and parse for JDK archives. */
static final List<Page> PAGES =
List.of(
// JDK: General-Availability Release
Page.of("26") // https://jdk.java.net/26
.withAlias("26,latest")
.withAlias("ga,latest"),
// JDK: Early-Access Releases
Page.of("27") // https://jdk.java.net/27
.withAlias("27,latest")
.withAlias("ea,latest")
.withAlias("ea,stable"),
// Named projects, usually in EA phase
Page.of("jextract") // https://jdk.java.net/jextract
.withAlias("jextract,latest")
.withAlias("jextract,ea"),
Page.of("leyden") // https://jdk.java.net/leyden
.withAlias("leyden,latest")
.withAlias("leyden,ea"),
Page.of("loom") // https://jdk.java.net/loom
.withAlias("loom,latest")
.withAlias("loom,ea"),
Page.of("valhalla") // https://jdk.java.net/valhalla
.withAlias("valhalla,latest")
.withAlias("valhalla,ea"));
/** Regex-based pattern used to find a download URI in an HTML code line. */
static final Pattern URI_PATTERN =
Pattern.compile(
".+?href=\""
+ "(\\Qhttps://download.java.net/\\E.+?/openjdk-.+?_bin\\.(tar\\.gz|zip))"
+ "\".+?");
/** Regex-based pattern used to find all named groups of a download key. */
static final Pattern KEY_PATTERN =
Pattern.compile(".+?openjdk-(?<version>.+?)_(?<os>.+?)-(?<arch>.+?)_bin\\.(?<type>.+?)");
public static void main(String... args) {
var pages = args.length == 1 ? List.of(Page.of(args[0])) : PAGES;
var parser = new Parser();
for (var page : pages) {
var section = parser.parse(page);
System.out.println("#");
System.out.println("# " + page.address());
System.out.println("#");
section.map().forEach((key, uri) -> System.out.printf("%s=%s%n", key, uri));
}
}
record Page(String name, List<String> aliases) {
static Page of(String name) {
return new Page(name, List.of());
}
Page withAlias(String alias) {
return new Page(name, Stream.concat(aliases.stream(), Stream.of(alias)).toList());
}
String address() {
return "https://jdk.java.net/" + name.toLowerCase() + "/";
}
}
record Section(Page page, Map<String, String> map) {}
record Parser(HttpClient http) {
Parser() {
this(HttpClient.newBuilder().followRedirects(HttpClient.Redirect.NORMAL).build());
}
String browse(String uri) {
try {
var request = HttpRequest.newBuilder(URI.create(uri)).build();
return http.send(request, HttpResponse.BodyHandlers.ofString()).body();
} catch (Exception exception) {
return exception.toString();
}
}
Section parse(Page page) {
var map = new TreeMap<String, String>();
var html = browse(page.address());
for (var line : html.lines().toList()) {
var uriMatcher = URI_PATTERN.matcher(line);
if (!uriMatcher.matches()) {
continue;
}
var uri = uriMatcher.group(1);
var keyMatcher = KEY_PATTERN.matcher(uri);
if (!keyMatcher.matches()) {
System.err.println("// no match -> " + uri);
continue;
}
var version = Runtime.Version.parse(keyMatcher.group("version"));
var os = keyMatcher.group("os");
var arch = keyMatcher.group("arch");
var platform = os.equals("osx") ? "macos" : os + "," + arch;
map.put(version.feature() + "," + version + "," + platform, uri);
for (var alias : page.aliases()) {
map.put(alias + "," + platform, uri);
}
}
return new Section(page, map);
}
}
}