Skip to content

Commit

Permalink
Appease URLClassLoader which requires directories to end in slashes
Browse files Browse the repository at this point in the history
  • Loading branch information
nicksieger authored and BanzaiMan committed Dec 13, 2010
1 parent 9cf97c3 commit 8740a6b
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 7 deletions.
21 changes: 21 additions & 0 deletions spec/java_integration/extensions/classpath_spec.rb
@@ -0,0 +1,21 @@
require File.dirname(__FILE__) + "/../spec_helper"

require 'java'

describe "The $CLASSPATH variable" do
it "appends URLs unmodified" do
$CLASSPATH << "http://jruby.org/"
$CLASSPATH.should include("http://jruby.org/")
end

it "assumes entries without URL protocols are files" do
$CLASSPATH << __FILE__
$CLASSPATH.should include("file:#{__FILE__}")
end

it "appends slashes to directory names" do
d = File.expand_path(File.dirname(__FILE__))
$CLASSPATH << d
$CLASSPATH.should include("file:#{d}/")
end
end
21 changes: 14 additions & 7 deletions src/org/jruby/RubyClassPathVariable.java
Expand Up @@ -12,7 +12,7 @@
* rights and limitations under the License.
*
* Copyright (C) 2007 Ola Bini <ola@ologix.com>
*
*
* Alternatively, the contents of this file may be used under the terms of
* either of the GNU General Public License Version 2 or later (the "GPL"),
* or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
Expand Down Expand Up @@ -44,7 +44,6 @@ public static void createClassPathVariable(Ruby runtime) {
RubyClassPathVariable self = new RubyClassPathVariable(runtime);
runtime.getEnumerable().extend_object(self);
runtime.defineReadonlyVariable("$CLASSPATH", self);

self.getMetaClass().defineAnnotatedMethods(RubyClassPathVariable.class);
}

Expand All @@ -65,10 +64,18 @@ public IRubyObject append(IRubyObject obj) {
}

private URL getURL(String target) throws MalformedURLException {
if (target.indexOf("://") == -1) {
return new URL("file", null, target);
} else {
try {
// First try assuming a protocol is included
return new URL(target);
} catch (MalformedURLException e) {
// Assume file: protocol
File f = new File(target);
String path = target;
if (f.exists() && f.isDirectory() && !path.endsWith("/")) {
// URLClassLoader requires that directores end with slashes
path = path + "/";
}
return new URL("file", null, path);
}
}

Expand All @@ -90,10 +97,10 @@ public IRubyObject each(Block block) {
@JRubyMethod(name = "to_s")
public IRubyObject to_s() {
return callMethod(getRuntime().getCurrentContext(), "to_a").callMethod(getRuntime().getCurrentContext(), "to_s");
}
}

@JRubyMethod(name = "inspect")
public IRubyObject inspect() {
return callMethod(getRuntime().getCurrentContext(), "to_a").callMethod(getRuntime().getCurrentContext(), "inspect");
}
}
}// RubyClassPathVariable

0 comments on commit 8740a6b

Please sign in to comment.