| 1 |
/* |
| 2 |
* Licensed to the Apache Software Foundation (ASF) under one or more |
| 3 |
* contributor license agreements. See the NOTICE file distributed with |
| 4 |
* this work for additional information regarding copyright ownership. |
| 5 |
* The ASF licenses this file to You under the Apache License, Version 2.0 |
| 6 |
* (the "License"); you may not use this file except in compliance with |
| 7 |
* the License. You may obtain a copy of the License at |
| 8 |
* |
| 9 |
* http://www.apache.org/licenses/LICENSE-2.0 |
| 10 |
* |
| 11 |
* Unless required by applicable law or agreed to in writing, software |
| 12 |
* distributed under the License is distributed on an "AS IS" BASIS, |
| 13 |
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 |
* See the License for the specific language governing permissions and |
| 15 |
* limitations under the License. |
| 16 |
*/ |
| 17 |
package org.apache.lucene.gradle; |
| 18 |
|
| 19 |
import java.io.IOException; |
| 20 |
import java.io.InputStream; |
| 21 |
import java.io.OutputStream; |
| 22 |
import java.net.HttpURLConnection; |
| 23 |
import java.net.URL; |
| 24 |
import java.net.URI; |
| 25 |
import java.net.URLConnection; |
| 26 |
import java.nio.channels.Channels; |
| 27 |
import java.nio.channels.FileChannel; |
| 28 |
import java.nio.channels.ReadableByteChannel; |
| 29 |
import java.nio.charset.StandardCharsets; |
| 30 |
import java.nio.file.Files; |
| 31 |
import java.nio.file.Path; |
| 32 |
import java.nio.file.Paths; |
| 33 |
import java.security.MessageDigest; |
| 34 |
import java.security.NoSuchAlgorithmException; |
| 35 |
import java.util.EnumSet; |
| 36 |
import java.util.Locale; |
| 37 |
import java.util.concurrent.TimeUnit; |
| 38 |
|
| 39 |
import static java.nio.file.StandardCopyOption.REPLACE_EXISTING; |
| 40 |
import static java.nio.file.StandardOpenOption.APPEND; |
| 41 |
|
| 42 |
/** |
| 43 |
* Standalone class that can be used to download a gradle-wrapper.jar |
| 44 |
* <p> |
| 45 |
* Has no dependencies outside of standard java libraries |
| 46 |
*/ |
| 47 |
public class WrapperDownloader { |
| 48 |
public static void main(String[] args) { |
| 49 |
if (args.length != 1) { |
| 50 |
System.err.println("Usage: java WrapperDownloader.java <destination>"); |
| 51 |
System.exit(1); |
| 52 |
} |
| 53 |
|
| 54 |
try { |
| 55 |
new WrapperDownloader().run(Paths.get(args[0])); |
| 56 |
} catch (Exception e) { |
| 57 |
System.err.println("ERROR: " + e.getMessage()); |
| 58 |
System.exit(3); |
| 59 |
} |
| 60 |
} |
| 61 |
|
| 62 |
public static void checkVersion() { |
| 63 |
int major = Runtime.getRuntime().version().feature(); |
| 64 |
if (major < 11 || major > 23) { |
| 65 |
throw new IllegalStateException("java version must be between 17 and 23, your version: " + major); |
| 66 |
} |
| 67 |
} |
| 68 |
|
| 69 |
public void run(Path destination) throws IOException, NoSuchAlgorithmException { |
| 70 |
Path checksumPath = destination.resolveSibling(destination.getFileName().toString() + ".sha256"); |
| 71 |
if (!Files.exists(checksumPath)) { |
| 72 |
throw new IOException("Checksum file not found: " + checksumPath); |
| 73 |
} |
| 74 |
String expectedChecksum = Files.readString(checksumPath, StandardCharsets.UTF_8).trim(); |
| 75 |
|
| 76 |
Path versionPath = destination.resolveSibling(destination.getFileName().toString() + ".version"); |
| 77 |
if (!Files.exists(versionPath)) { |
| 78 |
throw new IOException("Wrapper version file not found: " + versionPath); |
| 79 |
} |
| 80 |
String wrapperVersion = Files.readString(versionPath, StandardCharsets.UTF_8).trim(); |
| 81 |
|
| 82 |
MessageDigest digest = MessageDigest.getInstance("SHA-256"); |
| 83 |
|
| 84 |
if (Files.exists(destination)) { |
| 85 |
if (checksum(digest, destination).equalsIgnoreCase(expectedChecksum)) { |
| 86 |
// File exists, checksum matches, good to go! |
| 87 |
return; |
| 88 |
} else { |
| 89 |
System.err.println("Checksum mismatch, will attempt to re-download gradle-wrapper.jar"); |
| 90 |
System.out.println(destination); |
| 91 |
Files.delete(destination); |
| 92 |
} |
| 93 |
} |
| 94 |
|
| 95 |
URL url = URI.create("https://raw.githubusercontent.com/gradle/gradle/v" + wrapperVersion + "/gradle/wrapper/gradle-wrapper.jar").toURL(); |
| 96 |
System.err.println("Downloading gradle-wrapper.jar from " + url); |
| 97 |
|
| 98 |
// Zero-copy save the jar to a temp file |
| 99 |
Path temp = Files.createTempFile(destination.getParent(), ".gradle-wrapper", ".tmp"); |
| 100 |
try { |
| 101 |
int retries = 3; |
| 102 |
int retryDelay = 30; |
| 103 |
HttpURLConnection connection; |
| 104 |
while (true) { |
| 105 |
connection = (HttpURLConnection) url.openConnection(); |
| 106 |
try { |
| 107 |
connection.connect(); |
| 108 |
} catch (IOException e) { |
| 109 |
if (retries-- > 0) { |
| 110 |
// Retry after a short delay |
| 111 |
System.err.println("Error connecting to server: " + e + ", will retry in " + retryDelay + " seconds."); |
| 112 |
Thread.sleep(TimeUnit.SECONDS.toMillis(retryDelay)); |
| 113 |
continue; |
| 114 |
} |
| 115 |
} |
| 116 |
|
| 117 |
switch (connection.getResponseCode()) { |
| 118 |
case HttpURLConnection.HTTP_INTERNAL_ERROR: |
| 119 |
case HttpURLConnection.HTTP_UNAVAILABLE: |
| 120 |
case HttpURLConnection.HTTP_BAD_GATEWAY: |
| 121 |
if (retries-- > 0) { |
| 122 |
// Retry after a short delay. |
| 123 |
System.err.println("Server returned HTTP " + connection.getResponseCode() + ", will retry in " + retryDelay + " seconds."); |
| 124 |
Thread.sleep(TimeUnit.SECONDS.toMillis(retryDelay)); |
| 125 |
continue; |
| 126 |
} |
| 127 |
} |
| 128 |
|
| 129 |
// fall through, let getInputStream() throw IOException if there's a failure. |
| 130 |
break; |
| 131 |
} |
| 132 |
|
| 133 |
try (InputStream is = connection.getInputStream(); |
| 134 |
OutputStream out = Files.newOutputStream(temp)){ |
| 135 |
is.transferTo(out); |
| 136 |
} |
| 137 |
|
| 138 |
String checksum = checksum(digest, temp); |
| 139 |
if (!checksum.equalsIgnoreCase(expectedChecksum)) { |
| 140 |
throw new IOException(String.format(Locale.ROOT, |
| 141 |
"Checksum mismatch on downloaded gradle-wrapper.jar (was: %s, expected: %s).", |
| 142 |
checksum, |
| 143 |
expectedChecksum)); |
| 144 |
} |
| 145 |
|
| 146 |
Files.move(temp, destination, REPLACE_EXISTING); |
| 147 |
temp = null; |
| 148 |
} catch (IOException | InterruptedException e) { |
| 149 |
throw new IOException("Could not download gradle-wrapper.jar (" + |
| 150 |
e.getClass().getSimpleName() + ": " + e.getMessage() + ")."); |
| 151 |
} finally { |
| 152 |
if (temp != null) { |
| 153 |
Files.deleteIfExists(temp); |
| 154 |
} |
| 155 |
} |
| 156 |
} |
| 157 |
|
| 158 |
private String checksum(MessageDigest messageDigest, Path path) throws IOException { |
| 159 |
try { |
| 160 |
char[] hex = "0123456789abcdef".toCharArray(); |
| 161 |
byte[] digest = messageDigest.digest(Files.readAllBytes(path)); |
| 162 |
StringBuilder sb = new StringBuilder(); |
| 163 |
for (byte b : digest) { |
| 164 |
sb.append(hex[(b >> 4) & 0xf]).append(hex[b & 0xf]); |
| 165 |
} |
| 166 |
return sb.toString(); |
| 167 |
} catch (IOException e) { |
| 168 |
throw new IOException("Could not compute digest of file: " + path + " (" + e.getMessage() + ")"); |
| 169 |
} |
| 170 |
} |
| 171 |
} |