diff --git a/emjar/src/main/java/com/comoyo/emjar/EmJarClassLoader.java b/emjar/src/main/java/com/comoyo/emjar/EmJarClassLoader.java index dff0a80..1a04f32 100644 --- a/emjar/src/main/java/com/comoyo/emjar/EmJarClassLoader.java +++ b/emjar/src/main/java/com/comoyo/emjar/EmJarClassLoader.java @@ -75,6 +75,7 @@ public class EmJarClassLoader private final static Logger logger = Logger.getLogger(EmJarClassLoader.class.getName()); private final static HandlerFactory factory = new HandlerFactory(); + private final static Handler handler = new Handler(); static { try { @@ -119,7 +120,8 @@ private static URL[] getClassPath(final Properties props) while (embedded.hasMoreElements()) { final JarEntry entry = embedded.nextElement(); if (entry.getName().endsWith(".jar")) { - urls.add(new URI("jar:file", full + SEPARATOR + entry.getName(), null).toURL()); + final URL url = new URI("jar:file", full + SEPARATOR + entry.getName(), null).toURL(); + urls.add(new URL(url.getProtocol(), url.getHost(), url.getPort(), url.getFile(), handler)); } } jar.close(); @@ -142,8 +144,6 @@ public Class loadClass(String name) private static class HandlerFactory implements URLStreamHandlerFactory { - private final Handler handler = new Handler(); - @Override public URLStreamHandler createURLStreamHandler(String protocol) { @@ -164,7 +164,7 @@ protected URLConnection openConnection(URL url) throws IOException { final URI bundle; - final URI file; + final String path; try { final URI nested = url.toURI(); if (!"jar".equals(nested.getScheme())) { @@ -173,22 +173,29 @@ protected URLConnection openConnection(URL url) + nested.getScheme()); } bundle = new URI(nested.getRawSchemeSpecificPart()); - if (!"jar".equals(bundle.getScheme())) { - throw new IOException( - "Unexpected bundle scheme passed to openConnection (expected jar): " - + bundle.getScheme()); + if ("jar".equals(bundle.getScheme())) { + final URI file = new URI(bundle.getRawSchemeSpecificPart()); + if (!"file".equals(file.getScheme())) { + throw new IOException( + "Unexpected location scheme passed to openConnection (expected file): " + + file.getScheme()); + } + path = file.getSchemeSpecificPart(); } - file = new URI(bundle.getRawSchemeSpecificPart()); - if (!"file".equals(file.getScheme())) { - throw new IOException( - "Unexpected location scheme passed to openConnection (expected file): " - + file.getScheme()); + else { + if ("file".equals(bundle.getScheme())) { + path = bundle.getSchemeSpecificPart() + SEPARATOR; + } + else { + throw new IOException( + "Unexpected bundle scheme passed to openConnection (expected jar or file): " + + bundle.getScheme()); + } } } catch (URISyntaxException e) { throw new IOException(e); } - final String path = file.getSchemeSpecificPart(); JarURLConnection conn = connections.get(path); if (conn == null) { synchronized (connections) { diff --git a/emjar/src/test/java/com/comoyo/emjar/EmJarClassLoaderTest.java b/emjar/src/test/java/com/comoyo/emjar/EmJarClassLoaderTest.java index 9bab4ab..357a6fe 100644 --- a/emjar/src/test/java/com/comoyo/emjar/EmJarClassLoaderTest.java +++ b/emjar/src/test/java/com/comoyo/emjar/EmJarClassLoaderTest.java @@ -19,10 +19,15 @@ import java.io.BufferedReader; import java.io.File; import java.io.FilenameFilter; -import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; +import java.net.JarURLConnection; +import java.net.URISyntaxException; +import java.net.URL; +import java.net.URLConnection; import java.util.Properties; +import java.util.jar.JarFile; +import java.util.jar.Manifest; import org.junit.Test; import org.junit.runner.RunWith; @@ -35,9 +40,8 @@ @RunWith(JUnit4.class) public class EmJarClassLoaderTest extends EmJarTest { - @Test - public void testClassPathQuoting() - throws Exception + private EmJarClassLoader testLoader() + throws URISyntaxException { final FilenameFilter jarFilter = new FilenameFilter() { @Override @@ -53,9 +57,39 @@ public boolean accept(File dir, String name) { props.setProperty("path.separator", ":"); props.setProperty("file.separator", "/"); props.setProperty("user.dir", "/tmp"); - final EmJarClassLoader loader = new EmJarClassLoader(props); + return new EmJarClassLoader(props); + } + + @Test + public void testClassPathQuoting() + throws Exception + { + final EmJarClassLoader loader = testLoader(); final InputStream is = loader.getResourceAsStream("entry-" + WEIRD + ".txt"); final BufferedReader entry = new BufferedReader(new InputStreamReader(is)); assertEquals("Contents mismatch for weird entry", WEIRD, entry.readLine()); } + + @Test + public void testOpenConnection() + throws Exception + { + final EmJarClassLoader loader = testLoader(); + final URL urls[] = loader.getURLs(); + for (URL url : urls) { + if ("jar".equals(url.getProtocol())) { + final URLConnection conn = url.openConnection(); + assertTrue("Connection not of type JarURLConnection", + conn instanceof JarURLConnection); + final JarURLConnection jarConn = (JarURLConnection) conn; + final JarFile jarFile = jarConn.getJarFile(); + final Manifest mf = jarFile.getManifest(); + final String attr = mf.getMainAttributes().getValue("X-EmJar-Test"); + assertEquals("Invalid inner lib manifest structure", + "inner", attr); + return; + } + } + fail("Did not find any elements in classpath with protocol jar"); + } } diff --git a/emjar/src/test/resources/com/comoyo/emjar/bundle-MSLC.jar b/emjar/src/test/resources/com/comoyo/emjar/bundle-MSLC.jar index 7659bd0..c76b53c 100644 Binary files a/emjar/src/test/resources/com/comoyo/emjar/bundle-MSLC.jar and b/emjar/src/test/resources/com/comoyo/emjar/bundle-MSLC.jar differ diff --git a/emjar/src/test/resources/com/comoyo/emjar/bundle-MSLc.jar b/emjar/src/test/resources/com/comoyo/emjar/bundle-MSLc.jar index 5fa3154..91a5ae7 100644 Binary files a/emjar/src/test/resources/com/comoyo/emjar/bundle-MSLc.jar and b/emjar/src/test/resources/com/comoyo/emjar/bundle-MSLc.jar differ diff --git a/emjar/src/test/resources/com/comoyo/emjar/bundle-MSlC.jar b/emjar/src/test/resources/com/comoyo/emjar/bundle-MSlC.jar index 4175458..602c06c 100644 Binary files a/emjar/src/test/resources/com/comoyo/emjar/bundle-MSlC.jar and b/emjar/src/test/resources/com/comoyo/emjar/bundle-MSlC.jar differ diff --git a/emjar/src/test/resources/com/comoyo/emjar/bundle-MSlc.jar b/emjar/src/test/resources/com/comoyo/emjar/bundle-MSlc.jar index 4857880..58d8712 100644 Binary files a/emjar/src/test/resources/com/comoyo/emjar/bundle-MSlc.jar and b/emjar/src/test/resources/com/comoyo/emjar/bundle-MSlc.jar differ diff --git a/emjar/src/test/resources/com/comoyo/emjar/bundle-MsLC.jar b/emjar/src/test/resources/com/comoyo/emjar/bundle-MsLC.jar index 64a3fb2..30d0f53 100644 Binary files a/emjar/src/test/resources/com/comoyo/emjar/bundle-MsLC.jar and b/emjar/src/test/resources/com/comoyo/emjar/bundle-MsLC.jar differ diff --git a/emjar/src/test/resources/com/comoyo/emjar/bundle-MsLc.jar b/emjar/src/test/resources/com/comoyo/emjar/bundle-MsLc.jar index 194696e..5459863 100644 Binary files a/emjar/src/test/resources/com/comoyo/emjar/bundle-MsLc.jar and b/emjar/src/test/resources/com/comoyo/emjar/bundle-MsLc.jar differ diff --git a/emjar/src/test/resources/com/comoyo/emjar/bundle-MslC.jar b/emjar/src/test/resources/com/comoyo/emjar/bundle-MslC.jar index 602e856..eef5bac 100644 Binary files a/emjar/src/test/resources/com/comoyo/emjar/bundle-MslC.jar and b/emjar/src/test/resources/com/comoyo/emjar/bundle-MslC.jar differ diff --git a/emjar/src/test/resources/com/comoyo/emjar/bundle-Mslc.jar b/emjar/src/test/resources/com/comoyo/emjar/bundle-Mslc.jar index f08adbf..c899082 100644 Binary files a/emjar/src/test/resources/com/comoyo/emjar/bundle-Mslc.jar and b/emjar/src/test/resources/com/comoyo/emjar/bundle-Mslc.jar differ diff --git a/emjar/src/test/resources/com/comoyo/emjar/bundle-S-large.jar b/emjar/src/test/resources/com/comoyo/emjar/bundle-S-large.jar index 1b39fb7..724e659 100644 Binary files a/emjar/src/test/resources/com/comoyo/emjar/bundle-S-large.jar and b/emjar/src/test/resources/com/comoyo/emjar/bundle-S-large.jar differ diff --git a/emjar/src/test/resources/com/comoyo/emjar/bundle-Z-large.jar b/emjar/src/test/resources/com/comoyo/emjar/bundle-Z-large.jar index 252fa74..416d509 100644 Binary files a/emjar/src/test/resources/com/comoyo/emjar/bundle-Z-large.jar and b/emjar/src/test/resources/com/comoyo/emjar/bundle-Z-large.jar differ diff --git a/emjar/src/test/resources/com/comoyo/emjar/bundle-mSLC.jar b/emjar/src/test/resources/com/comoyo/emjar/bundle-mSLC.jar index b09f8bc..a36a7cf 100644 Binary files a/emjar/src/test/resources/com/comoyo/emjar/bundle-mSLC.jar and b/emjar/src/test/resources/com/comoyo/emjar/bundle-mSLC.jar differ diff --git a/emjar/src/test/resources/com/comoyo/emjar/bundle-mSLc.jar b/emjar/src/test/resources/com/comoyo/emjar/bundle-mSLc.jar index d2d6c39..e18c71f 100644 Binary files a/emjar/src/test/resources/com/comoyo/emjar/bundle-mSLc.jar and b/emjar/src/test/resources/com/comoyo/emjar/bundle-mSLc.jar differ diff --git a/emjar/src/test/resources/com/comoyo/emjar/bundle-mSlC.jar b/emjar/src/test/resources/com/comoyo/emjar/bundle-mSlC.jar index 4424886..a7dbd7c 100644 Binary files a/emjar/src/test/resources/com/comoyo/emjar/bundle-mSlC.jar and b/emjar/src/test/resources/com/comoyo/emjar/bundle-mSlC.jar differ diff --git a/emjar/src/test/resources/com/comoyo/emjar/bundle-mSlc.jar b/emjar/src/test/resources/com/comoyo/emjar/bundle-mSlc.jar index 08ed9ca..797b689 100644 Binary files a/emjar/src/test/resources/com/comoyo/emjar/bundle-mSlc.jar and b/emjar/src/test/resources/com/comoyo/emjar/bundle-mSlc.jar differ diff --git a/emjar/src/test/resources/com/comoyo/emjar/bundle-msLC.jar b/emjar/src/test/resources/com/comoyo/emjar/bundle-msLC.jar index fc2823e..c98c290 100644 Binary files a/emjar/src/test/resources/com/comoyo/emjar/bundle-msLC.jar and b/emjar/src/test/resources/com/comoyo/emjar/bundle-msLC.jar differ diff --git a/emjar/src/test/resources/com/comoyo/emjar/bundle-msLc.jar b/emjar/src/test/resources/com/comoyo/emjar/bundle-msLc.jar index 49be56a..ff37985 100644 Binary files a/emjar/src/test/resources/com/comoyo/emjar/bundle-msLc.jar and b/emjar/src/test/resources/com/comoyo/emjar/bundle-msLc.jar differ diff --git a/emjar/src/test/resources/com/comoyo/emjar/bundle-mslC.jar b/emjar/src/test/resources/com/comoyo/emjar/bundle-mslC.jar index a895b83..6cac8f9 100644 Binary files a/emjar/src/test/resources/com/comoyo/emjar/bundle-mslC.jar and b/emjar/src/test/resources/com/comoyo/emjar/bundle-mslC.jar differ diff --git a/emjar/src/test/resources/com/comoyo/emjar/bundle-mslc.jar b/emjar/src/test/resources/com/comoyo/emjar/bundle-mslc.jar index 1fcb310..615521d 100644 Binary files a/emjar/src/test/resources/com/comoyo/emjar/bundle-mslc.jar and b/emjar/src/test/resources/com/comoyo/emjar/bundle-mslc.jar differ diff --git a/emjar/src/test/resources/com/comoyo/emjar/bundle-s-large.jar b/emjar/src/test/resources/com/comoyo/emjar/bundle-s-large.jar index 6551cd9..66eb08d 100644 Binary files a/emjar/src/test/resources/com/comoyo/emjar/bundle-s-large.jar and b/emjar/src/test/resources/com/comoyo/emjar/bundle-s-large.jar differ diff --git "a/emjar/src/test/resources/com/comoyo/emjar/bundle-\303\246\303\270\303\245\360\237\230\261 %&;*+`\"\\-weird.jar" "b/emjar/src/test/resources/com/comoyo/emjar/bundle-\303\246\303\270\303\245\360\237\230\261 %&;*+`\"\\-weird.jar" index 10454d1..006d309 100644 Binary files "a/emjar/src/test/resources/com/comoyo/emjar/bundle-\303\246\303\270\303\245\360\237\230\261 %&;*+`\"\\-weird.jar" and "b/emjar/src/test/resources/com/comoyo/emjar/bundle-\303\246\303\270\303\245\360\237\230\261 %&;*+`\"\\-weird.jar" differ diff --git a/emjar/src/test/resources/com/comoyo/emjar/create-zips.pl b/emjar/src/test/resources/com/comoyo/emjar/create-zips.pl index 3f1a051..6aea0e4 100755 --- a/emjar/src/test/resources/com/comoyo/emjar/create-zips.pl +++ b/emjar/src/test/resources/com/comoyo/emjar/create-zips.pl @@ -13,7 +13,7 @@ sub createZip { my $buf = ""; my $inner = Archive::Zip::SimpleZip->new(\$buf, %$initargs) or die "Unable to create Zip"; - $inner->addString($DEFAULT_MANIFEST, + $inner->addString("X-EmJar-Test: inner\r\n".$DEFAULT_MANIFEST, Name => "META-INF/MANIFEST.MF", Time => $TIMESTAMP + 1); if ($oversize) { @@ -29,7 +29,7 @@ sub createZip { my $outer = Archive::Zip::SimpleZip->new("bundle-$name.jar", %$initargs) or die "Unable to create Zip"; - $outer->addString($DEFAULT_MANIFEST, + $outer->addString("X-EmJar-Test: bundle\r\n".$DEFAULT_MANIFEST, Name => "META-INF/MANIFEST.MF", Time => $TIMESTAMP + 4); $outer->addString($buf,