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


##########
xbean-finder/src/test/java/org/apache/xbean/finder/archive/JarArchiveTest.java:
##########
@@ -54,7 +54,8 @@ public class JarArchiveTest {
     private JarArchive archive;
 
     @Rule
-    public final TemporaryFolder tmp = new TemporaryFolder();
+    public TemporaryFolder testTmpDir = new TemporaryFolder();

Review Comment:
   final was better ;)



##########
xbean-finder/src/main/java/org/apache/xbean/finder/archive/JarArchive.java:
##########
@@ -41,22 +44,65 @@ 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.endsWith("!/") ?
+                        jarPath.substring(0, jarPath.lastIndexOf("!/"))
+                        : jarPath);
+            }catch(MalformedURLException ex){
+                throw new IllegalArgumentException(
+                        "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 trying
+                //  substrings up to every '!/{...}' as path
+                idx = 0;
+                jarPath = FileArchive.decode(url.getPath());
+                for(String jp = jarPath; !(jarFile = new File(jp)).exists()
+                        && (idx = jarPath.indexOf("!/", idx + 1)) > 0;
+                        jp = jarPath.substring(0, idx)){}
+
+                // All substrings attempted, 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(

Review Comment:
   Didn't we prefer IllegalArgumentException over UnsupportedOperationException 
which has another meaning?



-- 
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

Reply via email to