Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 21 additions & 14 deletions emjar/src/main/java/com/comoyo/emjar/EmJarClassLoader.java
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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();
Expand All @@ -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)
{
Expand All @@ -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())) {
Expand All @@ -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) {
Expand Down
44 changes: 39 additions & 5 deletions emjar/src/test/java/com/comoyo/emjar/EmJarClassLoaderTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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
Expand All @@ -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");
}
}
Binary file modified emjar/src/test/resources/com/comoyo/emjar/bundle-MSLC.jar
Binary file not shown.
Binary file modified emjar/src/test/resources/com/comoyo/emjar/bundle-MSLc.jar
Binary file not shown.
Binary file modified emjar/src/test/resources/com/comoyo/emjar/bundle-MSlC.jar
Binary file not shown.
Binary file modified emjar/src/test/resources/com/comoyo/emjar/bundle-MSlc.jar
Binary file not shown.
Binary file modified emjar/src/test/resources/com/comoyo/emjar/bundle-MsLC.jar
Binary file not shown.
Binary file modified emjar/src/test/resources/com/comoyo/emjar/bundle-MsLc.jar
Binary file not shown.
Binary file modified emjar/src/test/resources/com/comoyo/emjar/bundle-MslC.jar
Binary file not shown.
Binary file modified emjar/src/test/resources/com/comoyo/emjar/bundle-Mslc.jar
Binary file not shown.
Binary file modified emjar/src/test/resources/com/comoyo/emjar/bundle-S-large.jar
Binary file not shown.
Binary file modified emjar/src/test/resources/com/comoyo/emjar/bundle-Z-large.jar
Binary file not shown.
Binary file modified emjar/src/test/resources/com/comoyo/emjar/bundle-mSLC.jar
Binary file not shown.
Binary file modified emjar/src/test/resources/com/comoyo/emjar/bundle-mSLc.jar
Binary file not shown.
Binary file modified emjar/src/test/resources/com/comoyo/emjar/bundle-mSlC.jar
Binary file not shown.
Binary file modified emjar/src/test/resources/com/comoyo/emjar/bundle-mSlc.jar
Binary file not shown.
Binary file modified emjar/src/test/resources/com/comoyo/emjar/bundle-msLC.jar
Binary file not shown.
Binary file modified emjar/src/test/resources/com/comoyo/emjar/bundle-msLc.jar
Binary file not shown.
Binary file modified emjar/src/test/resources/com/comoyo/emjar/bundle-mslC.jar
Binary file not shown.
Binary file modified emjar/src/test/resources/com/comoyo/emjar/bundle-mslc.jar
Binary file not shown.
Binary file modified emjar/src/test/resources/com/comoyo/emjar/bundle-s-large.jar
Binary file not shown.
Binary file not shown.
4 changes: 2 additions & 2 deletions emjar/src/test/resources/com/comoyo/emjar/create-zips.pl
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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,
Expand Down