powerbroker commented on code in PR #34:
URL: https://github.com/apache/geronimo-xbean/pull/34#discussion_r1045268062


##########
xbean-finder/src/main/java/org/apache/xbean/finder/archive/JarArchive.java:
##########
@@ -41,22 +43,52 @@ public class JarArchive implements Archive {
     private final JarFile jar;
     private final MJarSupport mjar = new MJarSupport();
 
-    public JarArchive(ClassLoader loader, URL url) {
+    public JarArchive(ClassLoader loader, URL url){
 //        if (!"jar".equals(url.getProtocol())) throw new 
IllegalArgumentException("not a jar url: " + url);
 
+        this.loader = loader;
+        this.url = url;
+        File jf;
+        int idx;
+
+        /*
+         * True URL.getPath(...) wiping out protocol prefixes:
+         *
+         *  1. 'jar:file:/c:/temp/...' -> 'jar' + 'file:/c:/temp/...'
+         *  2. 'file:/c:/temp/...' -> 'file' + '/c:/temp/...'
+         *
+         *  assuming we accept 'file:/...${xxx.jar}!/' URLs
+         *      AND 'zip:file:/...!/' would be cool too, if
+         *      allowed by new URL(...)
+         */
         try {
-            this.loader = loader;
-            this.url = url;
-            URL u = url;
-
-            String jarPath = url.getFile();
-            if (jarPath.contains("!")) {
-                jarPath = jarPath.substring(0, jarPath.lastIndexOf("!"));
-                u = new URL(jarPath);
+            // Underlying JarFile(...) requires RandomAccessFile,
+            //  so only local file URLs here...
+            while(!"file".equalsIgnoreCase(url.getProtocol())) {
+                // no need here in .getQuery() tail, appended by getFile()
+                url = new URL(url.getPath());
             }
-            jar = new JarFile(FileArchive.decode(u.getFile())); // no more an 
url
-        } catch (IOException e) {
-            throw new IllegalStateException(e);
+        }catch(MalformedURLException ex){
+            // No more protocol(s) in path
+            throw new UnsupportedOperationException("Please provide 
'file:/...' or 'jar:file:/...!/' URL");
+        }
+
+        // TODO: drop FileArchive.decode?
+        //  URLDecoder.decode(url.getPath(), "UTF-8");
+        String jarPath = FileArchive.decode(url.getPath());
+
+        while(!(jf = new File(jarPath)).exists()){
+            if((idx = jarPath.lastIndexOf('!')) > 0){
+                jarPath = jarPath.substring(0, idx);
+            }else{
+                throw new IllegalStateException("Cannot find any files by '%s' 
url".formatted(url));
+            }
+        }
+
+        try{
+            this.jar = new JarFile(jf);
+        }catch(IOException e){
+            throw new IllegalStateException("Cannot open jar 
'%s'".formatted(jf.getAbsolutePath()), e);

Review Comment:
   .formatted(), i guess



##########
xbean-finder/src/test/java/org/apache/xbean/finder/archive/JarArchiveTest.java:
##########
@@ -42,27 +44,27 @@ public class JarArchiveTest {
     @BeforeClass
     public static void classSetUp() throws Exception {
 
-        JarArchiveTest.classpath = Archives.jarArchive(JarArchiveTest.classes);
+        classpath = Archives.jarArchive(classes);
     }
 
     @Before
     public void setUp() throws Exception {
 
-        URL[] urls = {new URL("jar:" + 
JarArchiveTest.classpath.toURI().toURL() + "!/")};
+        URL[] urls = {new URL("jar:" + classpath.toURI().toURL() + "!/")};
 
-        this.archive = new JarArchive(new URLClassLoader(urls), urls[0]);
+        archive = new JarArchive(new URLClassLoader(urls), urls[0]);
     }
 
 
     @Test
     public void testGetBytecode() throws Exception {
 
-        for (Class clazz : JarArchiveTest.classes) {
+        for (Class clazz : classes) {
             Assert.assertNotNull(clazz.getName(), 
this.archive.getBytecode(clazz.getName()));
         }
 
         try {
-            this.archive.getBytecode("Fake");

Review Comment:
   +3



-- 
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: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to