Commits

Anonymous committed 32ffa66

Normalize JAR Urls to File urls in ClassFinder

git-svn-id: http://svn.opensymphony.com/svn/xwork/trunk@1933e221344d-f017-0410-9bd5-d282ab1896d7

Comments (0)

Files changed (4)

src/java/com/opensymphony/xwork2/util/URLUtil.java

  * Helper class to extract file paths from different urls
  */
 public class URLUtil {
-    public static final Pattern JAR_PATTERN = Pattern.compile("^(jar:|wsjar:|zip:)(file:)?(.*)!/(.*)");
+    public static final Pattern JAR_PATTERN = Pattern.compile("^(jar:|wsjar:|zip:)?(file:)?(.*?)(\\!/)(.*)");
     private static final int JAR_FILE_PATH = 3;
 
     /**
-     * Convert URLs to URLs with "jar" protocol
+     * Convert URLs to URLs with "file" protocol
      * @param url URL to convert to a jar url
-     * @return a URL to a jar, or null if the URL parameter is a regular file
+     * @return a URL to a file, or null if the URL external form cannot be parsed
      */
-    public static URL getJarURL(URL url) {
+    public static URL normalizeToFileProtocol(URL url) {
         String fileName = url.toExternalForm();
         Matcher jarMatcher = JAR_PATTERN.matcher(fileName);
         try {
             if (jarMatcher.matches()) {
                 String path = jarMatcher.group(JAR_FILE_PATH);
-                return new URL("jar:file:" + path + "!/");
+                return new URL("file", "", path);
             } else {
                 //it is not a jar or zip file
                 return null;

src/java/com/opensymphony/xwork2/util/finder/ClassFinder.java

     }
 
     private List<String> jar(URL location) throws IOException {
-        URL url = URLUtil.getJarURL(location);
+        URL url = URLUtil.normalizeToFileProtocol(location);
         InputStream in = url.openStream();
         try {
             JarInputStream jarStream = new JarInputStream(in);

src/test/com/opensymphony/xwork2/util/URLUtilTest.java

 
     public void testSimpleFile() throws MalformedURLException {
         URL url = new URL("file:c:/somefile.txt");
-        URL outputURL = URLUtil.getJarURL(url);
+        URL outputURL = URLUtil.normalizeToFileProtocol(url);
 
         assertNull(outputURL);
     }
 
     public void testJarFile() throws MalformedURLException {
         URL url = new URL("jar:file:/c:/somefile.jar!/");
-        URL outputURL = URLUtil.getJarURL(url);
+        URL outputURL = URLUtil.normalizeToFileProtocol(url);
 
         assertNotNull(outputURL);
-        assertEquals("jar:file:/c:/somefile.jar!/", outputURL.toExternalForm());
+        assertEquals("file:/c:/somefile.jar", outputURL.toExternalForm());
 
         url = new URL("jar:file:/c:/somefile.jar!/somestuf/bla/bla");
-        outputURL = URLUtil.getJarURL(url);
-        assertEquals("jar:file:/c:/somefile.jar!/", outputURL.toExternalForm());
+        outputURL = URLUtil.normalizeToFileProtocol(url);
+        assertEquals("file:/c:/somefile.jar", outputURL.toExternalForm());
 
         url = new URL("jar:file:c:/somefile.jar!/somestuf/bla/bla");
-        outputURL = URLUtil.getJarURL(url);
-        assertEquals("jar:file:c:/somefile.jar!/", outputURL.toExternalForm());
+        outputURL = URLUtil.normalizeToFileProtocol(url);
+        assertEquals("file:c:/somefile.jar", outputURL.toExternalForm());
     }
 
     public void testZipFile() throws MalformedURLException {
         URL url = new URL("zip:/c:/somefile.zip!/");
-        URL outputURL = URLUtil.getJarURL(url);
+        URL outputURL = URLUtil.normalizeToFileProtocol(url);
 
         assertNotNull(outputURL);
-        assertEquals("jar:file:/c:/somefile.zip!/", outputURL.toExternalForm());
+        assertEquals("file:/c:/somefile.zip", outputURL.toExternalForm());
 
         url = new URL("zip:/c:/somefile.zip!/somestuf/bla/bla");
-        outputURL = URLUtil.getJarURL(url);
-        assertEquals("jar:file:/c:/somefile.zip!/", outputURL.toExternalForm());
+        outputURL = URLUtil.normalizeToFileProtocol(url);
+        assertEquals("file:/c:/somefile.zip", outputURL.toExternalForm());
 
         url = new URL("zip:c:/somefile.zip!/somestuf/bla/bla");
-        outputURL = URLUtil.getJarURL(url);
-        assertEquals("jar:file:c:/somefile.zip!/", outputURL.toExternalForm());
+        outputURL = URLUtil.normalizeToFileProtocol(url);
+        assertEquals("file:c:/somefile.zip", outputURL.toExternalForm());
     }
 
     public void testWSJarFile() throws MalformedURLException {
         URL url = new URL("wsjar:file:/c:/somefile.jar!/");
-        URL outputURL = URLUtil.getJarURL(url);
+        URL outputURL = URLUtil.normalizeToFileProtocol(url);
 
         assertNotNull(outputURL);
-        assertEquals("jar:file:/c:/somefile.jar!/", outputURL.toExternalForm());
+        assertEquals("file:/c:/somefile.jar", outputURL.toExternalForm());
 
         url = new URL("wsjar:file:/c:/somefile.jar!/somestuf/bla/bla");
-        outputURL = URLUtil.getJarURL(url);
-        assertEquals("jar:file:/c:/somefile.jar!/", outputURL.toExternalForm());
+        outputURL = URLUtil.normalizeToFileProtocol(url);
+        assertEquals("file:/c:/somefile.jar", outputURL.toExternalForm());
 
         url = new URL("wsjar:file:c:/somefile.jar!/somestuf/bla/bla");
-        outputURL = URLUtil.getJarURL(url);
-        assertEquals("jar:file:c:/somefile.jar!/", outputURL.toExternalForm());
+        outputURL = URLUtil.normalizeToFileProtocol(url);
+        assertEquals("file:c:/somefile.jar", outputURL.toExternalForm());
     }
 
     protected void setUp() throws Exception {
         super.setUp();
+
         try {
             URL.setURLStreamHandlerFactory(new URLStreamHandlerFactory() {
                 public URLStreamHandler createURLStreamHandler(String protocol) {
                     return new URLStreamHandler() {
                         protected URLConnection openConnection(URL u) throws IOException {
-                            return null;  //To change body of implemented methods use File | Settings | File Templates.
+                            return null;
                         }
                     };
                 }
             //the factory cant be set multiple times..just ignore exception no biggie
         }
     }
-
-
 }

src/test/com/opensymphony/xwork2/util/UrlUtilTest2.java

+package com.opensymphony.xwork2.util;
+
+import junit.framework.TestCase;
+
+import java.io.IOException;
+import java.io.InputStream;
+import java.net.URL;
+import java.util.jar.JarInputStream;
+
+/**
+ * Keep these test on a separate class, they can't be in UrlUtilTest because the
+ * registered URLStreamHandlerFactory would make them fail
+ */
+public class UrlUtilTest2 extends TestCase {
+    public void testOpenWithJarProtocol() throws IOException {
+        URL url = ClassLoaderUtil.getResource("xwork-jar.jar", URLUtil.class);
+        URL jarUrl = new URL("jar", "", url.toExternalForm() + "!/");
+        URL outputURL = URLUtil.normalizeToFileProtocol(jarUrl);
+        assertNotNull(outputURL);
+        assertUrlCanBeOpened(outputURL);
+    }
+
+    private void assertUrlCanBeOpened(URL url) throws IOException {
+        InputStream is = url.openStream();
+        JarInputStream jarStream = null;
+        try {
+            jarStream = new JarInputStream(is);
+            assertNotNull(jarStream);
+        } finally {
+            if (jarStream != null)
+                jarStream.close();
+
+        }
+    }
+}
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.