Skip to content

Commit

Permalink
[GR-29078] Improved license IDs.
Browse files Browse the repository at this point in the history
  • Loading branch information
sdedic committed Feb 1, 2021
1 parent 4e4c81c commit 599413f
Show file tree
Hide file tree
Showing 7 changed files with 82 additions and 13 deletions.
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# {0} - component ID
# {1} - component version
LICENSE_Path_translation=LICENSE-{0}-{1}
ERROR_LoadLicense=Could not load license from archive path {0}: {1}
ERROR_LicenseNotFound=License not found in archive: {0}
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
import java.util.jar.JarEntry;
import java.util.jar.JarFile;
import java.util.jar.Manifest;
import java.util.stream.Collectors;
import org.graalvm.component.installer.Archive;
import static org.graalvm.component.installer.BundleConstants.META_INF_PATH;
import static org.graalvm.component.installer.BundleConstants.META_INF_PERMISSIONS_PATH;
Expand Down Expand Up @@ -157,13 +158,18 @@ public String getLicenseID() {
return null;
}
if (SystemUtils.isRemotePath(licPath)) {
return licPath;
return super.getLicenseID();
}
JarEntry je = jarFile.getJarEntry(licPath);
if (je == null) {
return null;
throw fb.failure("ERROR_LicenseNotFound", null, getLicensePath());
}
try (BufferedReader r = new BufferedReader(new InputStreamReader(
jarFile.getInputStream(je), "UTF-8"))) {
String text = r.lines().collect(Collectors.joining("\n"));
return SystemUtils.digestString(text, false);
} catch (IOException ex) {
throw fb.failure("ERROR_LoadLicense", ex, getLicensePath(), ex.getLocalizedMessage());
}
long crc = je.getCrc();
return Long.toHexString(crc);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -50,3 +50,6 @@ ERROR_CannotComputeLicenseID=Could not compute ID for license at {0}.

# {0} the error message.
WARNING_CouldNotLoadPlugin=WARNING: could not load or initialize an extension: {0}

LICENSE_RemoteLicenseDescription=Downloading license: {0}
ERROR_DownloadLicense=Could not download license {0}: {1}
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,11 @@
import java.io.IOException;
import java.io.InputStream;
import java.io.StringWriter;
import java.net.URL;
import java.nio.ByteBuffer;
import java.nio.channels.Channels;
import java.nio.channels.ReadableByteChannel;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.attribute.PosixFilePermissions;
import java.security.MessageDigest;
Expand All @@ -60,6 +62,7 @@
import org.graalvm.component.installer.SystemUtils;
import org.graalvm.component.installer.model.ComponentInfo;
import org.graalvm.component.installer.model.DistributionType;
import org.graalvm.component.installer.remote.FileDownloader;

/**
* Loads information from the component's bundle.
Expand Down Expand Up @@ -300,6 +303,26 @@ public String getLicensePath() {
return licensePath;
}

protected FileDownloader createFileDownloader(URL remote, String desc) {
return new FileDownloader(desc, remote, feedback);
}

public String downloadAndHashLicense(String remote) {
String desc = getLicenseType();
if (desc == null) {
desc = remote;
}
try {
URL u = new URL(remote);
FileDownloader dn = createFileDownloader(u, feedback.l10n("LICENSE_RemoteLicenseDescription", desc));
dn.download();
String s = String.join("\n", Files.readAllLines(dn.getLocalFile().toPath()));
return SystemUtils.digestString(s, false) /* + "_" + remote */;
} catch (IOException ex) {
throw feedback.failure("ERROR_DownloadLicense", ex, desc, ex.getLocalizedMessage());
}
}

/**
* License digest or URL.
*/
Expand All @@ -314,7 +337,7 @@ public String getLicenseID() {
if (licPath == null) {
return null;
} else if (SystemUtils.isRemotePath(licPath)) { // NOI18N
return licPath;
return cachedLicenseID = downloadAndHashLicense(licPath);
}
Archive.FileEntry foundEntry = null;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,4 +63,7 @@ REMOTE_FailedToParseParameter=Failed to parse location parameter: {0}
REMOTE_CannotLoadChannel=Could not load channel {0}: {1}

# {0} - edition id (i.e. CE, EE).
ERR_NoSuchEdition=Unkown GraalVM edition: {0}
ERR_NoSuchEdition=Unkown GraalVM edition: {0}

LICENSE_RemoteLicenseDescription=Downloading license: {0}
ERROR_DownloadLicense=Could not download license {0}: {1}
Original file line number Diff line number Diff line change
Expand Up @@ -290,6 +290,12 @@ private void copySubtree(Path from) throws IOException {
}

public void download() throws IOException {
Path localCache = feedback.getLocalCache(sourceURL);
if (localCache != null) {
localFile = localCache.toFile();
return;
}

simpleOutput = Boolean.TRUE.toString().equals(System.getProperty(CommonConstants.SYSPROP_SIMPLE_OUTPUT));
boolean fromFile = sourceURL.getProtocol().equals("file");
if (simpleOutput) {
Expand All @@ -306,12 +312,6 @@ public void download() throws IOException {
}
}

Path localCache = feedback.getLocalCache(sourceURL);
if (localCache != null) {
localFile = localCache.toFile();
return;
}

if (fromFile) {
try {
Path p = Paths.get(sourceURL.toURI());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -302,13 +302,45 @@ public String getLicenseType() {
}
}

protected FileDownloader createFileDownloader(URL remote, String desc, boolean mainFile) {
FileDownloader dn = new FileDownloader(desc, remote, feedback);
if (mainFile) {
configureRelatedDownloader(dn);
}
return dn;
}

public String downloadAndHashLicense(String remote) {
String desc = getLicenseType();
if (desc == null) {
desc = remote;
}
try {
URL u = new URL(remote);
FileDownloader dn = createFileDownloader(u, feedback.l10n("LICENSE_RemoteLicenseDescription", desc), false);
dn.download();
String s = String.join("\n", Files.readAllLines(dn.getLocalFile().toPath()));
return SystemUtils.digestString(s, false) /* + "_" + remote */;
} catch (IOException ex) {
throw feedback.failure("ERROR_DownloadLicense", ex, desc, ex.getLocalizedMessage());
}
}

/**
* License digest or URL.
*/
private String cachedLicenseID;

@Override
public String getLicenseID() {
if (cachedLicenseID != null) {
return cachedLicenseID;
}
String s = getLicensePath();
if (s != null && SystemUtils.isRemotePath(s)) {
// special case, so that the package will not be downloaded, if the
// catalog specifies HTTP remote path.
return s;
return cachedLicenseID = downloadAndHashLicense(s);
}
try {
return createFileLoader().getLicenseID();
Expand Down

0 comments on commit 599413f

Please sign in to comment.