groovy-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From pascalschumac...@apache.org
Subject incubator-groovy git commit: GROOVY-7018: GroovyClassLoader addClasspath RegexPattern issue (closes #43)
Date Thu, 02 Jul 2015 19:23:07 GMT
Repository: incubator-groovy
Updated Branches:
  refs/heads/GROOVY_2_4_X dadc738f0 -> b639db96c


GROOVY-7018: GroovyClassLoader addClasspath RegexPattern issue (closes #43)

GroovyMain and GroovyClassLoader had problems in Cygwin environments
with finding Scripts.


Project: http://git-wip-us.apache.org/repos/asf/incubator-groovy/repo
Commit: http://git-wip-us.apache.org/repos/asf/incubator-groovy/commit/b639db96
Tree: http://git-wip-us.apache.org/repos/asf/incubator-groovy/tree/b639db96
Diff: http://git-wip-us.apache.org/repos/asf/incubator-groovy/diff/b639db96

Branch: refs/heads/GROOVY_2_4_X
Commit: b639db96c1e87cd04835e2de75b31581a9debfcc
Parents: dadc738
Author: Persi <the_hammer1@freenet.de>
Authored: Wed Jun 24 11:09:35 2015 +0200
Committer: pascalschumacher <pascalschumacher@gmx.net>
Committed: Thu Jul 2 21:22:52 2015 +0200

----------------------------------------------------------------------
 src/main/groovy/lang/GroovyClassLoader.java | 63 ++++++++++++------------
 src/main/groovy/ui/GroovyMain.java          | 11 ++---
 2 files changed, 37 insertions(+), 37 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-groovy/blob/b639db96/src/main/groovy/lang/GroovyClassLoader.java
----------------------------------------------------------------------
diff --git a/src/main/groovy/lang/GroovyClassLoader.java b/src/main/groovy/lang/GroovyClassLoader.java
index db3d5d7..7d2c29e 100644
--- a/src/main/groovy/lang/GroovyClassLoader.java
+++ b/src/main/groovy/lang/GroovyClassLoader.java
@@ -905,48 +905,49 @@ public class GroovyClassLoader extends URLClassLoader {
     public void addClasspath(final String path) {
         AccessController.doPrivileged(new PrivilegedAction<Void>() {
             public Void run() {
+
+                URI newURI;
                 try {
-                    // As the java.net.URL Javadoc says, the recommended way to get a URL
is via URI.
-                    // http://docs.oracle.com/javase/7/docs/api/java/net/URL.html
-                    // "Note, the URI class does perform escaping of its component fields
in certain circumstances.
-                    // The recommended way to manage the encoding and decoding of URLs is
to use URI, and to convert
-                    // between these two classes using toURI() and URI.toURL()."
-                    // A possibly better approach here is to construct a URI and then resolve
it against
-                    // a URI for the current working directory.
-                    // But we use this string match for now so everyone can see it doesn't
hurt file-only classpaths.
-                    URI newURI;
-                    if (!URI_PATTERN.matcher(path).matches()) {
-                        newURI = new File(path).toURI();
-                    } else {
-                        newURI = new URI(path);
-                    }
-                    URL[] urls = getURLs();
-                    for (URL url : urls) {
-                        // Do not use URL.equals.  It uses the network to resolve names and
compares ip addresses!
-                        // That is a violation of RFC and just plain evil.
-                        // http://michaelscharf.blogspot.com/2006/11/javaneturlequals-and-hashcode-make.html
-                        // http://docs.oracle.com/javase/7/docs/api/java/net/URL.html#equals(java.lang.Object)
-                        // "Since hosts comparison requires name resolution, this operation
is a blocking operation.
-                        // Note: The defined behavior for equals is known to be inconsistent
with virtual hosting in HTTP."
+                    newURI = new URI(path);
+                    // check if we can create a URL from that URI
+                    newURI.toURL();
+                } catch (URISyntaxException e) {
+                    // the URI has a false format, so lets try it with files ...
+                    newURI=new File(path).toURI();
+                } catch (MalformedURLException e) {
+                    // the URL has a false format, so lets try it with files ...
+                    newURI=new File(path).toURI();
+                } catch (IllegalArgumentException e) {
+                    // the URL is not absolute, so lets try it with files ...
+                    newURI=new File(path).toURI();
+                }
+
+                URL[] urls = getURLs();
+                for (URL url : urls) {
+                    // Do not use URL.equals.  It uses the network to resolve names and compares
ip addresses!
+                    // That is a violation of RFC and just plain evil.
+                    // http://michaelscharf.blogspot.com/2006/11/javaneturlequals-and-hashcode-make.html
+                    // http://docs.oracle.com/javase/7/docs/api/java/net/URL.html#equals(java.lang.Object)
+                    // "Since hosts comparison requires name resolution, this operation is
a blocking operation.
+                    // Note: The defined behavior for equals is known to be inconsistent
with virtual hosting in HTTP."
+                    try {
                         if (newURI.equals(url.toURI())) return null;
+                    } catch (URISyntaxException e) {
+                        // fail fast! if we got a malformed URI the Classloader has to tell
it
+                        throw new RuntimeException( e );
                     }
+                }
+                try {
                     addURL(newURI.toURL());
                 } catch (MalformedURLException e) {
-                    //TODO: fail through ?
-                } catch (URISyntaxException e) {
-                    // Just doing the same thing...
+                    // fail fast! if we got a malformed URL the Classloader has to tell it
+                    throw new RuntimeException( e );
                 }
                 return null;
             }
         });
     }
 
-    // TODO remove duplication with GroovyMain#uriPattern
-    // RFC2396
-    // scheme        = alpha *( alpha | digit | "+" | "-" | "." )
-    // match URIs but not Windows filenames, e.g.: http://cnn.com but not C:\xxx\file.ext
-    private static final Pattern URI_PATTERN = Pattern.compile("\\p{Alpha}[-+.\\p{Alnum}]*:[^\\\\]*");
-
     /**
      * <p>Returns all Groovy classes loaded by this class loader.
      *

http://git-wip-us.apache.org/repos/asf/incubator-groovy/blob/b639db96/src/main/groovy/ui/GroovyMain.java
----------------------------------------------------------------------
diff --git a/src/main/groovy/ui/GroovyMain.java b/src/main/groovy/ui/GroovyMain.java
index eac5338..4cf1d70 100644
--- a/src/main/groovy/ui/GroovyMain.java
+++ b/src/main/groovy/ui/GroovyMain.java
@@ -449,17 +449,16 @@ public class GroovyMain {
     protected GroovyCodeSource getScriptSource(boolean isScriptFile, String script) throws
IOException, URISyntaxException {
         //check the script is currently valid before starting a server against the script
         if (isScriptFile) {
-            if (uriPattern.matcher(script).matches()) {
+            // search for the file and if it exists don't try to use URIs ...
+            File scriptFile = huntForTheScriptFile(script);
+            if (!scriptFile.exists() && uriPattern.matcher(script).matches()) {
                 return new GroovyCodeSource(new URI(script));
-            } else {
-                return new GroovyCodeSource(huntForTheScriptFile(script));
             }
-        } else {
-            return new GroovyCodeSource(script, "script_from_command_line", GroovyShell.DEFAULT_CODE_BASE);
+            return new GroovyCodeSource( scriptFile );
         }
+        return new GroovyCodeSource(script, "script_from_command_line", GroovyShell.DEFAULT_CODE_BASE);
     }
 
-    // TODO remove duplication with GroovyClassLoader#uriPattern
     // RFC2396
     // scheme        = alpha *( alpha | digit | "+" | "-" | "." )
     // match URIs but not Windows filenames, e.g.: http://cnn.com but not C:\xxx\file.ext


Mime
View raw message