SF.net SVN: jython:[7141] trunk/jython

[email protected] Sat, 09 Oct 2010 07:53:42 +0000
Newsgroups gmane.comp.lang.jython.cvs
Message-ID <[email protected]>
Revision: 7141
          http://jython.svn.sourceforge.net/jython/?rev=7141&view=rev
Author:   otmarhumbel
Date:     2010-10-09 07:53:42 +0000 (Sat, 09 Oct 2010)

Log Message:
-----------
handle standalone recognition in the JBoss 5 virtual file system 

fixes issue #1639

many thanks to Eric Lentz!

Modified Paths:
--------------
    trunk/jython/NEWS
    trunk/jython/src/org/python/core/PySystemState.java

Added Paths:
-----------
    trunk/jython/tests/java/org/python/core/PySystemStateTest.java

Modified: trunk/jython/NEWS
===================================================================
--- trunk/jython/NEWS	2010-10-07 20:46:21 UTC (rev 7140)
+++ trunk/jython/NEWS	2010-10-09 07:53:42 UTC (rev 7141)
@@ -2,6 +2,7 @@
 
 Jython 2.5.2rc1
   Bugs Fixed
+    - [ 1639 ] JBoss 5, vfszip protocol in use for jarFileName in PySystemState
     - [ 1660 ] threading module memory leak
     - [ 1452 ] pydoc help() function fails because sys.executable is None in stand-alone Jython
     - [ 1568 ] sys.stdout.encoding returns wrong value in Windows with Jython 2.5.1 (fixed on Java 6 only)

Modified: trunk/jython/src/org/python/core/PySystemState.java
===================================================================
--- trunk/jython/src/org/python/core/PySystemState.java	2010-10-07 20:46:21 UTC (rev 7140)
+++ trunk/jython/src/org/python/core/PySystemState.java	2010-10-09 07:53:42 UTC (rev 7141)
@@ -16,19 +16,17 @@
 import java.nio.charset.Charset;
 import java.nio.charset.UnsupportedCharsetException;
 import java.security.AccessControlException;
-import java.util.concurrent.Callable;
-import java.util.ArrayList;
 import java.util.LinkedHashSet;
 import java.util.Map;
-import java.util.Map.Entry;
 import java.util.Properties;
 import java.util.Set;
 import java.util.StringTokenizer;
+import java.util.Map.Entry;
+import java.util.concurrent.Callable;
 import java.util.concurrent.ConcurrentMap;
 import java.util.jar.JarEntry;
 import java.util.jar.JarFile;
 
-import com.google.common.collect.MapMaker;
 import org.jruby.ext.posix.util.Platform;
 import org.python.Version;
 import org.python.core.adapter.ClassicPyObjectAdapter;
@@ -55,6 +53,7 @@
 
     private static final String JAR_URL_PREFIX = "jar:file:";
     private static final String JAR_SEPARATOR = "!";
+    private static final String VFSZIP_PREFIX = "vfszip:";
 
     public static final PyString version = new PyString(Version.getVersion());
     public static final int hexversion = ((Version.PY_MAJOR_VERSION << 24) |
@@ -1112,12 +1111,15 @@
      * @return the full name of the jar file containing this class, <code>null</code> if not available.
      */
     private static String getJarFileName() {
-        String jarFileName = null;
-        Class thisClass = PySystemState.class;
+        Class<PySystemState> thisClass = PySystemState.class;
         String fullClassName = thisClass.getName();
         String className = fullClassName.substring(fullClassName.lastIndexOf(".") + 1);
         URL url = thisClass.getResource(className + ".class");
-        // we expect an URL like jar:file:/install_dir/jython.jar!/org/python/core/PySystemState.class
+        return getJarFileNameFromURL(url);
+    }
+
+    protected static String getJarFileNameFromURL(URL url) {
+        String jarFileName = null;
         if (url != null) {
             try {
                 // escape plus signs, since the URLDecoder would turn them into spaces
@@ -1129,7 +1131,21 @@
                 urlString = urlString.replaceAll(escapedPlus, plus);
                 int jarSeparatorIndex = urlString.lastIndexOf(JAR_SEPARATOR);
                 if (urlString.startsWith(JAR_URL_PREFIX) && jarSeparatorIndex > 0) {
+                    // jar:file:/install_dir/jython.jar!/org/python/core/PySystemState.class
                     jarFileName = urlString.substring(JAR_URL_PREFIX.length(), jarSeparatorIndex);
+                } else if (urlString.startsWith(VFSZIP_PREFIX)) {
+                    // vfszip:/some/path/jython.jar/org/python/core/PySystemState.class
+                    final String path = PySystemState.class.getName().replace('.', '/');
+                    int jarIndex = urlString.indexOf(".jar/".concat(path));
+                    if (jarIndex > 0) {
+                        jarIndex += 4;
+                        int start = VFSZIP_PREFIX.length();
+                        if (Platform.IS_WINDOWS) {
+                            // vfszip:/C:/some/path/jython.jar/org/python/core/PySystemState.class
+                            start++;
+                        }
+                        jarFileName = urlString.substring(start, jarIndex);
+                    }
                 }
             } catch (Exception e) {}
         }

Added: trunk/jython/tests/java/org/python/core/PySystemStateTest.java
===================================================================
--- trunk/jython/tests/java/org/python/core/PySystemStateTest.java	                        (rev 0)
+++ trunk/jython/tests/java/org/python/core/PySystemStateTest.java	2010-10-09 07:53:42 UTC (rev 7141)
@@ -0,0 +1,81 @@
+package org.python.core;
+
+import java.io.IOException;
+import java.net.URL;
+import java.net.URLConnection;
+import java.net.URLStreamHandler;
+
+import junit.framework.TestCase;
+
+import org.jruby.ext.posix.util.Platform;
+
+public class PySystemStateTest extends TestCase {
+
+    public void testGetJarFileNameFromURL() throws Exception {
+        // null
+        assertNull(PySystemState.getJarFileNameFromURL(null));
+        // plain jar url
+        String urlString = "jar:file:/some_dir/some.jar!/a/package/with/A.class";
+        URL url = new URL(urlString);
+        assertEquals("/some_dir/some.jar", PySystemState.getJarFileNameFromURL(url));
+        // jar url to decode
+        urlString = "jar:file:/some%20dir/some.jar!/a/package/with/A.class";
+        url = new URL(urlString);
+        assertEquals("/some dir/some.jar", PySystemState.getJarFileNameFromURL(url));
+        // jar url with + signs to escape
+        urlString = "jar:file:/some+dir/some.jar!/a/package/with/A.class";
+        url = new URL(urlString);
+        assertEquals("/some+dir/some.jar", PySystemState.getJarFileNameFromURL(url));
+    }
+
+    public void testGetJarFileNameFromURL_jboss() throws Exception {
+        final String protocol = "vfszip";
+        final String host = "";
+        final int port = -1;
+        final URLStreamHandler handler = new TestJBossURLStreamHandler();
+        String file;
+        URL url;
+        if (Platform.IS_WINDOWS) {
+            // plain jboss url
+            file = "/C:/some_dir/some.jar/org/python/core/PySystemState.class";
+            url = new URL(protocol, host, port, file, handler);
+            // tests with jboss on windows gave URL's like this:
+            assertEquals("vfszip:/C:/some_dir/some.jar/org/python/core/PySystemState.class", url.toString());
+            assertEquals("C:/some_dir/some.jar", PySystemState.getJarFileNameFromURL(url));
+            // jboss url to decode
+            file = "/C:/some%20dir/some.jar/org/python/core/PySystemState.class";
+            url = new URL(protocol, host, port, file, handler);
+            assertEquals("vfszip:/C:/some%20dir/some.jar/org/python/core/PySystemState.class", url.toString());
+            assertEquals("C:/some dir/some.jar", PySystemState.getJarFileNameFromURL(url));
+            // jboss url with + to escape
+            file = "/C:/some+dir/some.jar/org/python/core/PySystemState.class";
+            url = new URL(protocol, host, port, file, handler);
+            assertEquals("vfszip:/C:/some+dir/some.jar/org/python/core/PySystemState.class", url.toString());
+            assertEquals("C:/some+dir/some.jar", PySystemState.getJarFileNameFromURL(url));
+        } else {
+            // plain jboss url
+            file = "/some_dir/some.jar/org/python/core/PySystemState.class";
+            url = new URL(protocol, host, port, file, handler);
+            assertEquals("vfszip:/some_dir/some.jar/org/python/core/PySystemState.class", url.toString());
+            assertEquals("/some_dir/some.jar", PySystemState.getJarFileNameFromURL(url));
+            // jboss url to decode
+            file = "/some%20dir/some.jar/org/python/core/PySystemState.class";
+            url = new URL(protocol, host, port, file, handler);
+            assertEquals("vfszip:/some%20dir/some.jar/org/python/core/PySystemState.class", url.toString());
+            assertEquals("/some dir/some.jar", PySystemState.getJarFileNameFromURL(url));
+            // jboss url with + to escape
+            file = "/some+dir/some.jar/org/python/core/PySystemState.class";
+            url = new URL(protocol, host, port, file, handler);
+            assertEquals("vfszip:/some+dir/some.jar/org/python/core/PySystemState.class", url.toString());
+            assertEquals("/some+dir/some.jar", PySystemState.getJarFileNameFromURL(url));
+        }
+    }
+
+    protected static class TestJBossURLStreamHandler extends URLStreamHandler {
+
+        @Override
+        protected URLConnection openConnection(URL u) throws IOException {
+            throw new RuntimeException("unexpected call to openConnection " + u.toString());
+        }
+    }
+}


This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site.

------------------------------------------------------------------------------
Beautiful is writing same markup. Internet Explorer 9 supports
standards for HTML5, CSS3, SVG 1.1,  ECMAScript5, and DOM L2 & L3.
Spend less time writing and  rewriting code and more time creating great
experiences on the web. Be a part of the beta today.
http://p.sf.net/sfu/beautyoftheweb