/
AbstractDownloadingJavaRuntimeProvider.java
192 lines (173 loc) · 6.32 KB
/
AbstractDownloadingJavaRuntimeProvider.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
/*
* Copyright © 2018-2021 Jesse Gallagher
*
* 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.openntf.openliberty.domino.jvm;
import static java.text.MessageFormat.format;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.Reader;
import java.io.UncheckedIOException;
import java.net.URL;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.StandardCopyOption;
import java.nio.file.attribute.PosixFilePermission;
import java.util.EnumSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.util.zip.GZIPInputStream;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;
import org.openntf.openliberty.domino.log.OpenLibertyLog;
import org.openntf.openliberty.domino.runtime.Messages;
import org.openntf.openliberty.domino.util.OpenLibertyUtil;
import org.openntf.openliberty.domino.util.commons.apache.tar.TarArchiveEntry;
import org.openntf.openliberty.domino.util.commons.apache.tar.TarArchiveInputStream;
import org.openntf.openliberty.domino.util.commons.ibm.StringUtil;
import org.openntf.openliberty.domino.util.json.parser.JSONParser;
import org.openntf.openliberty.domino.util.json.parser.ParseException;
public abstract class AbstractDownloadingJavaRuntimeProvider implements JavaRuntimeProvider {
private static final Logger log = OpenLibertyLog.instance.log;
@SuppressWarnings("unchecked")
protected static List<Map<String, Object>> fetchGitHubReleasesList(String providerName, String releasesUrl) {
if(log.isLoggable(Level.FINE)) {
log.fine(format(Messages.getString("JavaRuntimeProvider.downloadingReleaseListFrom"), providerName, releasesUrl)); //$NON-NLS-1$
}
try {
return OpenLibertyUtil.download(new URL(releasesUrl), is -> {
try(Reader r = new InputStreamReader(is)) {
return (List<Map<String, Object>>)new JSONParser().parse(r);
} catch (ParseException e) {
throw new IOException(e);
}
});
} catch(IOException e) {
throw new UncheckedIOException(e);
}
}
protected static void download(String url, String contentType, Path jvmDir) {
// TODO consider replacing with NIO filesystem operations, though they don't inherently support .tar.gz
try {
OpenLibertyUtil.download(new URL(url), is -> {
if("application/zip".equals(contentType)) { //$NON-NLS-1$
try(ZipInputStream zis = new ZipInputStream(is)) {
extract(zis, jvmDir);
}
} else if("application/x-compressed-tar".equals(contentType)) { //$NON-NLS-1$
try(GZIPInputStream gzis = new GZIPInputStream(is)) {
try(TarArchiveInputStream tis = new TarArchiveInputStream(gzis)) {
extract(tis, jvmDir);
}
}
} else {
throw new IllegalStateException(format(Messages.getString("JavaRuntimeProvider.unsupportedContentType"), contentType)); //$NON-NLS-1$
}
return null;
});
} catch(IOException e) {
throw new UncheckedIOException(e);
}
}
protected static void markExecutables(Path jvmDir) {
Path bin = jvmDir.resolve("bin"); //$NON-NLS-1$
if(Files.isDirectory(bin)) {
markExecutablesInBinDir(bin);
}
Path jreBin = jvmDir.resolve("jre").resolve("bin"); //$NON-NLS-1$ //$NON-NLS-2$
if(Files.isDirectory(jreBin)) {
markExecutablesInBinDir(jreBin);
}
}
protected static void markExecutablesInBinDir(Path bin) {
if(bin.getFileSystem().supportedFileAttributeViews().contains("posix")) { //$NON-NLS-1$
try {
Files.list(bin)
.filter(Files::isRegularFile)
.forEach(p -> {
try {
Set<PosixFilePermission> perms = EnumSet.copyOf(Files.getPosixFilePermissions(p));
perms.add(PosixFilePermission.OWNER_EXECUTE);
perms.add(PosixFilePermission.GROUP_EXECUTE);
perms.add(PosixFilePermission.OTHERS_EXECUTE);
Files.setPosixFilePermissions(p, perms);
} catch (IOException e) {
throw new RuntimeException(e);
}
});
} catch(IOException e) {
throw new UncheckedIOException(e);
}
}
}
protected static void extract(ZipInputStream zis, Path dest) throws IOException {
ZipEntry entry = zis.getNextEntry();
while(entry != null) {
String name = entry.getName();
if(StringUtil.isNotEmpty(name)) {
// The first directory is a container
int slashIndex = name.indexOf('/');
if(slashIndex > -1) {
name = name.substring(slashIndex+1);
}
if(StringUtil.isNotEmpty(name)) {
if(log.isLoggable(Level.FINER)) {
log.finer(format(Messages.getString("JavaRuntimeProvider.deployingFile"), name)); //$NON-NLS-1$
}
Path path = dest.resolve(name);
if(entry.isDirectory()) {
Files.createDirectories(path);
} else {
Files.createDirectories(path.getParent());
Files.copy(zis, path, StandardCopyOption.REPLACE_EXISTING);
}
}
}
zis.closeEntry();
entry = zis.getNextEntry();
}
}
protected static void extract(TarArchiveInputStream tis, Path dest) throws IOException {
TarArchiveEntry entry = tis.getNextTarEntry();
while(entry != null) {
String name = entry.getName();
if(StringUtil.isNotEmpty(name)) {
// The first directory is a container
int slashIndex = name.indexOf('/');
if(slashIndex > -1) {
name = name.substring(slashIndex+1);
}
if(StringUtil.isNotEmpty(name)) {
if(log.isLoggable(Level.FINER)) {
log.finer(format(Messages.getString("JavaRuntimeProvider.deployingFile"), name)); //$NON-NLS-1$
}
Path path = dest.resolve(name);
if(entry.isDirectory()) {
Files.createDirectories(path);
} else {
Files.createDirectories(path.getParent());
Files.copy(tis, path, StandardCopyOption.REPLACE_EXISTING);
}
}
}
entry = tis.getNextTarEntry();
}
}
protected static String getOsName() {
return OpenLibertyUtil.IS_LINUX ? "linux" : "windows"; //$NON-NLS-1$ //$NON-NLS-2$
}
}