rmannibucau commented on code in PR #34: URL: https://github.com/apache/geronimo-xbean/pull/34#discussion_r1051356926
########## xbean-finder/src/main/java/org/apache/xbean/finder/archive/JarArchive.java: ########## @@ -41,22 +44,63 @@ public class JarArchive implements Archive, AutoCloseable { private final JarFile jar; private final MJarSupport mjar = new MJarSupport(); - public JarArchive(ClassLoader loader, URL url) { + /* + * Supports only 'file:/...' or 'jar:file:/...!/' URLs + */ + public JarArchive(ClassLoader loader, URL url){ // if (!"jar".equals(url.getProtocol())) throw new IllegalArgumentException("not a jar url: " + url); - try { - this.loader = loader; - this.url = url; - URL u = url; + this.loader = loader; + this.url = url; + File jarFile = null; + String jarPath; + int idx; - String jarPath = url.getFile(); - if (jarPath.contains("!")) { - jarPath = jarPath.substring(0, jarPath.indexOf("!")); - u = new URL(jarPath); + // Wipe out 'jar:' prefix AND '!/{...}' suffix(if any) + if("jar".equalsIgnoreCase(url.getProtocol())){ + + try{ + jarPath = url.getPath(); + url = new URL(jarPath.substring(0, jarPath.lastIndexOf("!/"))); + }catch(MalformedURLException ex){ + // Probably CPU overheat and/or DRAM undervoltage + throw new UnsupportedOperationException( + "Please provide 'file:/...' or 'jar:file:/...!/' URL" + + " instead of '" + FileArchive.decode(String.valueOf(url)) + "'"); } - jar = new JarFile(FileArchive.decode(u.getFile())); // no more an url - } catch (IOException e) { - throw new IllegalStateException(e); + } + + try{ + // handle 'file:/...' URL + if("file".equalsIgnoreCase(url.getProtocol())){ + + // Testing if file DOEN't exists AND cutting !/{...}' + // suffix-by-suffix until file discovered or run out of suffixes + for(jarPath = FileArchive.decode(url.getPath()); + !(jarFile = new File(jarPath)).exists() + && (idx = jarPath.lastIndexOf("!/")) > 0; + jarPath = jarPath.substring(0, idx)){ } + + // No more suffixes to cut, but referenced file wasn't discovered + if(!jarFile.exists()){ + + // To be caught later and wrapped into IllegalStateEx - default behavior + throw new FileNotFoundException(FileArchive.decode(String.valueOf(url))); + } + + }else{ + throw new UnsupportedOperationException( + "Please provide 'file:/...' or 'jar:file:/...!/' URL" + + " instead of '" + FileArchive.decode(String.valueOf(url)) + "'"); + } + + jar = new JarFile(jarFile); + + }catch(IOException e){ + throw new IllegalStateException("Cannot open jar(zip) '" + + jarFile != null ? // why can it be null? but since compiler thinks so... Review Comment: Can if none of protocols match so no compiler issue ;) ########## xbean-finder/src/test/java/org/apache/xbean/finder/archive/JarArchiveTest.java: ########## @@ -63,12 +64,12 @@ public void setUp() throws Exception { public void testGetBytecode() throws Exception { for (Class clazz : classes) { - assertNotNull(clazz.getName(), archive.getBytecode(clazz.getName())); + Assert.assertNotNull(clazz.getName(), archive.getBytecode(clazz.getName())); Review Comment: Shouldnt change, please fix the unexpected diff parts ########## xbean-finder/src/test/java/org/apache/xbean/finder/archive/JarArchiveTest.java: ########## @@ -95,14 +96,65 @@ public void testIterator() throws Exception { actual.add(entry.getName()); } - assertFalse(0 == actual.size()); + Assert.assertFalse(0 == actual.size()); for (Class clazz : classes) { - assertTrue(clazz.getName(), actual.contains(clazz.getName())); + Assert.assertTrue(clazz.getName(), actual.contains(clazz.getName())); } - assertEquals(classes.length, actual.size()); + Assert.assertEquals(JarArchiveTest.classes.length, actual.size()); } + @Test + public void testXBEAN337() throws Exception { + + + // Virtual path + + JarArchive jar; + String path = "/this!/!file!/does!/!not/exist.jar"; + URL[] urls = {new URL("jar:file:" + path + "!/some!/!inner!/.jar!/file.jar")}; + + try { + jar = new JarArchive(new URLClassLoader(urls), urls[0]); Review Comment: Dont forget to close jar files ########## xbean-finder/src/test/java/org/apache/xbean/finder/archive/JarArchiveTest.java: ########## @@ -95,14 +96,65 @@ public void testIterator() throws Exception { actual.add(entry.getName()); } - assertFalse(0 == actual.size()); + Assert.assertFalse(0 == actual.size()); for (Class clazz : classes) { - assertTrue(clazz.getName(), actual.contains(clazz.getName())); + Assert.assertTrue(clazz.getName(), actual.contains(clazz.getName())); } - assertEquals(classes.length, actual.size()); + Assert.assertEquals(JarArchiveTest.classes.length, actual.size()); } + @Test + public void testXBEAN337() throws Exception { Review Comment: Maybe get the missing cases from the diff, it covers a bit more the pr cases IIRC ########## xbean-finder/src/main/java/org/apache/xbean/finder/archive/JarArchive.java: ########## @@ -41,22 +44,63 @@ public class JarArchive implements Archive, AutoCloseable { private final JarFile jar; private final MJarSupport mjar = new MJarSupport(); - public JarArchive(ClassLoader loader, URL url) { + /* + * Supports only 'file:/...' or 'jar:file:/...!/' URLs + */ + public JarArchive(ClassLoader loader, URL url){ // if (!"jar".equals(url.getProtocol())) throw new IllegalArgumentException("not a jar url: " + url); - try { - this.loader = loader; - this.url = url; - URL u = url; + this.loader = loader; + this.url = url; + File jarFile = null; + String jarPath; + int idx; - String jarPath = url.getFile(); - if (jarPath.contains("!")) { - jarPath = jarPath.substring(0, jarPath.indexOf("!")); - u = new URL(jarPath); + // Wipe out 'jar:' prefix AND '!/{...}' suffix(if any) + if("jar".equalsIgnoreCase(url.getProtocol())){ + + try{ + jarPath = url.getPath(); + url = new URL(jarPath.substring(0, jarPath.lastIndexOf("!/"))); + }catch(MalformedURLException ex){ + // Probably CPU overheat and/or DRAM undervoltage Review Comment: Is it really the cause or just the contract which enforces it so it is impossible to get? ########## xbean-finder/src/main/java/org/apache/xbean/finder/archive/JarArchive.java: ########## @@ -41,22 +44,63 @@ public class JarArchive implements Archive, AutoCloseable { private final JarFile jar; private final MJarSupport mjar = new MJarSupport(); - public JarArchive(ClassLoader loader, URL url) { + /* + * Supports only 'file:/...' or 'jar:file:/...!/' URLs + */ + public JarArchive(ClassLoader loader, URL url){ // if (!"jar".equals(url.getProtocol())) throw new IllegalArgumentException("not a jar url: " + url); - try { - this.loader = loader; - this.url = url; - URL u = url; + this.loader = loader; + this.url = url; + File jarFile = null; + String jarPath; + int idx; - String jarPath = url.getFile(); - if (jarPath.contains("!")) { - jarPath = jarPath.substring(0, jarPath.indexOf("!")); - u = new URL(jarPath); + // Wipe out 'jar:' prefix AND '!/{...}' suffix(if any) + if("jar".equalsIgnoreCase(url.getProtocol())){ + + try{ + jarPath = url.getPath(); + url = new URL(jarPath.substring(0, jarPath.lastIndexOf("!/"))); Review Comment: indexOf and not lastI indexOf as shown by shared tests -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: dev-unsubscr...@geronimo.apache.org For queries about this service, please contact Infrastructure at: us...@infra.apache.org