Skip to content

Commit

Permalink
Fix Manifest version reading error to support jar:file protocol (#601)
Browse files Browse the repository at this point in the history
  • Loading branch information
eyalkoren authored and felixbarny committed Apr 23, 2019
1 parent 1ca0be8 commit 831ab8f
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,15 @@
import com.blogspot.mydailyjava.weaklockfree.WeakConcurrentMap;
import net.bytebuddy.description.NamedElement;
import net.bytebuddy.description.method.MethodDescription;
import net.bytebuddy.dynamic.ClassFileLocator;
import net.bytebuddy.matcher.ElementMatcher;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.annotation.Nullable;
import java.io.IOException;
import java.net.JarURLConnection;
import java.net.URL;
import java.net.URLConnection;
import java.security.CodeSource;
import java.security.ProtectionDomain;
import java.util.Collection;
Expand Down Expand Up @@ -135,14 +137,21 @@ public boolean matches(@Nullable ProtectionDomain protectionDomain) {

private static @Nullable Version readImplementationVersionFromManifest(@Nullable ProtectionDomain protectionDomain) {
Version version = null;
JarFile jarFile = null;
try {
if (protectionDomain != null) {
CodeSource codeSource = protectionDomain.getCodeSource();
if (codeSource != null) {
URL jarUrl = codeSource.getLocation();
if (jarUrl != null) {
JarFile jar = new JarFile(jarUrl.getFile());
Manifest manifest = jar.getManifest();
// does not yet establish an actual connection
URLConnection urlConnection = jarUrl.openConnection();
if (urlConnection instanceof JarURLConnection) {
jarFile = ((JarURLConnection) urlConnection).getJarFile();
} else {
jarFile = new JarFile(jarUrl.getFile());
}
Manifest manifest = jarFile.getManifest();
if (manifest != null) {
String implementationVersion = manifest.getMainAttributes().getValue("Implementation-Version");
if (implementationVersion != null) {
Expand All @@ -156,6 +165,14 @@ public boolean matches(@Nullable ProtectionDomain protectionDomain) {
}
} catch (Exception e) {
logger.error("Cannot read implementation version based on ProtectionDomain " + protectionDomain, e);
} finally {
if (jarFile != null) {
try {
jarFile.close();
} catch (IOException e) {
logger.error("Error closing JarFile", e);
}
}
}
return version;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,10 @@
import org.apache.http.client.HttpClient;
import org.junit.jupiter.api.Test;

import java.net.MalformedURLException;
import java.net.URL;
import java.security.CodeSigner;
import java.security.CodeSource;
import java.security.ProtectionDomain;
import java.util.List;

Expand Down Expand Up @@ -50,9 +54,19 @@ void testClassLoaderCanLoadClass() {
}

@Test
void testSemVerLteMatcher() {
void testSemVerLteWithFileUrl() {
// Relying on Apache httpclient-4.5.6.jar
ProtectionDomain protectionDomain = HttpClient.class.getProtectionDomain();
testSemVerLteMatcher(HttpClient.class.getProtectionDomain());
}

@Test
void testSemVerLteWithJarFileUrl() throws MalformedURLException {
URL originalUrl = HttpClient.class.getProtectionDomain().getCodeSource().getLocation();
URL jarFileUrl = new URL("jar:" + originalUrl.toString() + "!/");
testSemVerLteMatcher(new ProtectionDomain(new CodeSource(jarFileUrl, new CodeSigner[0]), null));
}

private void testSemVerLteMatcher(ProtectionDomain protectionDomain) {
assertThat(implementationVersionLte("3").matches(protectionDomain)).isFalse();
assertThat(implementationVersionLte("3.2").matches(protectionDomain)).isFalse();
assertThat(implementationVersionLte("3.15.10").matches(protectionDomain)).isFalse();
Expand Down

0 comments on commit 831ab8f

Please sign in to comment.