-
Notifications
You must be signed in to change notification settings - Fork 2.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Handle the case where the file resolver is used with a classloader th…
…at return a URL for directories. When unpackFromJarURL is called with a directory, the method throws an exception as the entry does not really exist. By checking the length we can detect the case and not try to retrieve the JarFile from the connection.
- Loading branch information
1 parent
05a35aa
commit 0eeba1a
Showing
4 changed files
with
83 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
61 changes: 61 additions & 0 deletions
61
src/test/java/io/vertx/core/file/JarFileResolverWithOddClassLoaderTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
package io.vertx.core.file; | ||
|
||
import io.vertx.core.file.impl.FileResolverImpl; | ||
import io.vertx.core.spi.file.FileResolver; | ||
import org.assertj.core.api.Assertions; | ||
import org.junit.Test; | ||
|
||
import java.io.File; | ||
import java.io.IOException; | ||
import java.net.MalformedURLException; | ||
import java.net.URL; | ||
import java.net.URLClassLoader; | ||
|
||
public class JarFileResolverWithOddClassLoaderTest { | ||
|
||
@Test | ||
public void testWhenClassLoaderReturnUrlsForDirectory() throws IOException { | ||
File webjar = new File("src/test/resources/jars/wc-chatbot-0.1.2.jar"); | ||
Assertions.assertThat(webjar).exists(); | ||
ClassLoader cl = new DecoratedClassLoader(new URLClassLoader(new URL[]{webjar.toURI().toURL()}, Thread.currentThread().getContextClassLoader())); | ||
|
||
ClassLoader orig = Thread.currentThread().getContextClassLoader(); | ||
try (FileResolver resolver = new FileResolverImpl()) { | ||
Thread.currentThread().setContextClassLoader(cl); | ||
String fileName = "META-INF/resources/_static/wc-chatbot/0.1.2/LICENSE"; | ||
File resolved = resolver.resolveFile(fileName); | ||
Assertions.assertThat(resolved).exists(); | ||
} finally { | ||
Thread.currentThread().setContextClassLoader(orig); | ||
} | ||
} | ||
|
||
/** | ||
* A somewhat broken classloader returning an URL even for directory included in the given jars. | ||
*/ | ||
private static class DecoratedClassLoader extends ClassLoader { | ||
private final URL[] urls; | ||
|
||
public DecoratedClassLoader(URLClassLoader parent) { | ||
super(parent); | ||
this.urls = parent.getURLs(); | ||
} | ||
|
||
@Override | ||
protected URL findResource(String name) { | ||
if (! name.contains("META-INF/resources")) { | ||
return super.findResource(name); | ||
} | ||
for (URL url : urls) { | ||
if (url.getFile().endsWith(".jar")) { | ||
try { | ||
return new URL("jar:" + url + "!/" + name); | ||
} catch (MalformedURLException e) { | ||
return super.findResource(name); | ||
} | ||
} | ||
} | ||
return super.findResource(name); | ||
} | ||
} | ||
} |
Binary file not shown.