Skip to content

Commit

Permalink
ThreadLocal MessageDigest, JarFile for accessing manifest, template l…
Browse files Browse the repository at this point in the history
…og messages
  • Loading branch information
jack-berg committed Sep 5, 2023
1 parent b90cd2c commit a559225
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -141,16 +141,16 @@ private void handleInternal(ProtectionDomain protectionDomain) {
return;
}
if ("jrt".equals(archiveUrl.getProtocol())) {
logger.log(Level.FINEST, "Skipping processing for java runtime module: " + archiveUrl);
logger.log(Level.FINEST, "Skipping processing for java runtime module: {0}", archiveUrl);
return;
}
String file = archiveUrl.getFile();
if (file.endsWith("/")) {
logger.log(Level.FINEST, "Skipping processing non-archive code location: " + archiveUrl);
logger.log(Level.FINEST, "Skipping processing non-archive code location: {0}", archiveUrl);
return;
}
if (!file.endsWith(JAR_EXTENSION) && !file.endsWith(WAR_EXTENSION)) {
logger.log(Level.INFO, "Skipping processing unrecognized code location: " + archiveUrl);
logger.log(Level.INFO, "Skipping processing unrecognized code location: {0}", archiveUrl);
return;
}

Expand Down Expand Up @@ -209,7 +209,7 @@ static void processUrl(EventEmitter eventEmitter, URL archiveUrl) {
try {
addPackageType(builder, archiveUrl);
} catch (Exception e) {
logger.log(Level.WARNING, "Error adding package type for archive URL: " + archiveUrl, e);
logger.log(Level.WARNING, "Error adding package type for archive URL: {0}" + archiveUrl, e);
}

try {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
import java.security.NoSuchAlgorithmException;
import java.util.Properties;
import java.util.jar.JarEntry;
import java.util.jar.JarFile;
import java.util.jar.JarInputStream;
import java.util.jar.Manifest;

Expand All @@ -29,6 +30,18 @@ final class JarAnalyzerUtil {
static final AttributeKey<String> PACKAGE_CHECKSUM = AttributeKey.stringKey("package.checksum");
static final AttributeKey<String> PACKAGE_PATH = AttributeKey.stringKey("package.path");

private static final ThreadLocal<MessageDigest> MESSAGE_DIGEST_THREAD_LOCAL =
ThreadLocal.withInitial(JarAnalyzerUtil::createSha1MessageDigest);

private static MessageDigest createSha1MessageDigest() {
try {
return MessageDigest.getInstance("SHA1");
} catch (NoSuchAlgorithmException e) {
throw new IllegalStateException(
"Unexpected error. Checksum algorithm SHA1 does not exist.", e);
}
}

/**
* Set the attributes {@link #PACKAGE_TYPE} from the {@code archiveUrl}.
*
Expand All @@ -55,13 +68,8 @@ static void addPackageChecksum(AttributesBuilder builder, URL archiveUrl) throws
}

private static String computeSha1(URL archiveUrl) throws IOException {
MessageDigest md;
try {
md = MessageDigest.getInstance("SHA1");
} catch (NoSuchAlgorithmException e) {
throw new IllegalStateException(
"Unexpected error. Checksum algorithm SHA1 does not exist.", e);
}
MessageDigest md = MESSAGE_DIGEST_THREAD_LOCAL.get();
md.reset(); // Reset reused thread local message digest instead

try (InputStream is = new DigestInputStream(archiveUrl.openStream(), md)) {
byte[] buffer = new byte[1024 * 8];
Expand Down Expand Up @@ -106,9 +114,8 @@ private static String archiveFilename(URL archiveUrl) throws Exception {
* {Implementation-Vendor}", e.g. {@code Jackson datatype: JSR310 by FasterXML}.
*/
static void addPackageDescription(AttributesBuilder builder, URL archiveUrl) throws IOException {
try (InputStream inputStream = archiveUrl.openStream();
JarInputStream jarInputStream = new JarInputStream(inputStream)) {
Manifest manifest = jarInputStream.getManifest();
try (JarFile jarFile = new JarFile(archiveUrl.getFile())) {
Manifest manifest = jarFile.getManifest();
if (manifest == null) {
return;
}
Expand Down

0 comments on commit a559225

Please sign in to comment.