-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathDownloadRuntimesMojo.java
More file actions
162 lines (129 loc) · 5.41 KB
/
DownloadRuntimesMojo.java
File metadata and controls
162 lines (129 loc) · 5.41 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
/*
* NodeJS Maven Plugin
* Copyright (C) SonarSource Sàrl
* mailto:info AT sonarsource DOT com
*
* You can redistribute and/or modify this program under the terms of
* the Sonar Source-Available License Version 1, as published by SonarSource Sàrl.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
* See the Sonar Source-Available License for more details.
*
* You should have received a copy of the Sonar Source-Available License
* along with this program; if not, see https://sonarsource.com/license/ssal/
*/
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URI;
import java.nio.channels.Channels;
import java.nio.channels.FileChannel;
import java.nio.channels.ReadableByteChannel;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.List;
import org.apache.commons.codec.digest.DigestUtils;
import org.apache.maven.plugin.AbstractMojo;
import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.plugins.annotations.Mojo;
import org.apache.maven.plugins.annotations.Parameter;
import org.codehaus.plexus.archiver.tar.TarGZipUnArchiver;
import org.codehaus.plexus.archiver.zip.ZipUnArchiver;
import org.codehaus.plexus.components.io.filemappers.FileMapper;
import org.codehaus.plexus.components.io.fileselectors.FileInfo;
import org.codehaus.plexus.components.io.fileselectors.FileSelector;
@Mojo(name = "download-runtimes")
public class DownloadRuntimesMojo extends AbstractMojo {
@Parameter(required = true)
private String targetDirectory;
@Parameter(required = true)
private List<RuntimeFlavor> flavors;
@Parameter(required = true)
private String version;
@Parameter
private String proxyToken;
@Override
public void execute() throws MojoExecutionException {
for (var flavor : this.flavors) {
try {
var url = URI.create(flavor.getUrl()).toURL();
var filename = url.getFile();
var destinationFile = Path.of(this.targetDirectory, filename);
destinationFile.toFile().getParentFile().mkdirs();
this.download(flavor, destinationFile);
this.extract(flavor, destinationFile.toFile());
} catch (Exception e) {
throw new MojoExecutionException("Error while downloading " + flavor.getUrl(), e);
}
}
}
private void download(RuntimeFlavor flavor, Path destinationFile) throws IOException, ChecksumException {
if (Files.exists(destinationFile)) {
getLog().info(String.format("Skipping download, file %s already exists.", destinationFile));
return;
}
InputStream inputStream = null;
var proxyUrl = flavor.getProxyUrl();
if (proxyUrl != null) {
getLog().info("Downloading from " + proxyUrl);
var url = URI.create(proxyUrl).toURL();
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setRequestProperty("Authorization", String.format("Bearer %s", this.proxyToken));
connection.setRequestMethod("GET");
var responseCode = connection.getResponseCode();
if (responseCode == 200) {
inputStream = connection.getInputStream();
}
}
if (inputStream == null) {
var url = URI.create(flavor.getUrl()).toURL();
getLog().info("Downloading from " + url);
inputStream = url.openStream();
}
try (ReadableByteChannel readableByteChannel = Channels.newChannel(inputStream)) {
FileChannel fileChannel;
try (var fileOutputStream = new FileOutputStream(destinationFile.toString())) {
fileChannel = fileOutputStream.getChannel();
getLog().info("Downloading to " + destinationFile);
fileChannel.transferFrom(readableByteChannel, 0, Long.MAX_VALUE);
var md5 = DigestUtils.sha256Hex(new FileInputStream(destinationFile.toFile()));
if (!md5.equals(flavor.getChecksum())) {
throw new ChecksumException(String.format("Invalid checksum for %s", flavor.getUrl()));
}
}
}
}
private void extract(RuntimeFlavor flavor, File sourceFile) throws IOException {
var unArchiver = sourceFile.getName().endsWith("zip")
? new ZipUnArchiver(sourceFile)
: new TarGZipUnArchiver(sourceFile);
var fileSelector = new FileSelector() {
public boolean isSelected(FileInfo fileInfo) throws IOException {
return fileInfo.getName().endsWith(flavor.getBinaryName());
}
};
var fileMapper = (FileMapper) filename -> {
var filenamePath = Path.of(filename);
var subpath = filenamePath.subpath(1, filenamePath.getNameCount());
return Path.of(flavor.getName(), subpath.toString()).toString();
};
unArchiver.setDestDirectory(new File(this.targetDirectory));
unArchiver.setFileSelectors(new FileSelector[] { fileSelector });
unArchiver.setFileMappers(new FileMapper[] { fileMapper });
unArchiver.extract();
this.writeManifest(flavor);
}
private void writeManifest(RuntimeFlavor flavor) throws IOException {
var versionOutputFilePath = Path.of(this.targetDirectory, flavor.getName(), "version.txt");
this.getLog()
.info(
String.format("Deploying version \"%s\" to %s", this.version, versionOutputFilePath)
);
Files.writeString(versionOutputFilePath, this.version);
}
}