Skip to content

Commit

Permalink
Showing 3 changed files with 20 additions and 5 deletions.
14 changes: 14 additions & 0 deletions core/src/main/java/org/jruby/Main.java
Original file line number Diff line number Diff line change
@@ -282,6 +282,8 @@ public void run() {
}

try {
doSetContextClassLoader(runtime);

if (in == null) {
// no script to run, return success
return new Status();
@@ -450,6 +452,18 @@ private boolean checkStreamSyntax(Ruby runtime, InputStream in, String filename)
}
}

private void doSetContextClassLoader(Ruby runtime) {
// set thread context JRuby classloader here, for the main thread
try {
Thread.currentThread().setContextClassLoader(runtime.getJRubyClassLoader());
} catch (SecurityException se) {
// can't set TC classloader
if (runtime.getInstanceConfig().isVerbose()) {
config.getError().println("WARNING: Security restrictions disallowed setting context classloader for main thread.");
}
}
}

private void doProcessArguments(InputStream in) {
config.processArguments(config.parseShebangOptions(in));
}
6 changes: 2 additions & 4 deletions spec/java_integration/reify/become_java_spec.rb
Original file line number Diff line number Diff line change
@@ -112,10 +112,8 @@ def blah_with_args(arg_one,arg_two)
class JRUBY5564; end
a_class = JRUBY5564.become_java!(false)

# load the java class from the jruby-classloader
# this diverts from the original issue 5564 since
# JRuby can run without any context_class_loader involved
cl = JRuby.runtime.jruby_class_loader
# load the java class from the classloader
cl = java.lang.Thread.current_thread.getContextClassLoader
cl.load_class(a_class.get_name).should == a_class
end

5 changes: 4 additions & 1 deletion test/jruby/test_context_classloader.rb
Original file line number Diff line number Diff line change
@@ -12,7 +12,10 @@ def setup
end

def test_main_thread
assert_equal(JRuby.runtime.jruby_class_loader.parent, org.jruby.Ruby.java_class.class_loader || java.lang.Thread.current_thread.context_class_loader)
# This launches externally because our test run doesn't call through Main,
# so the executing thread's context classloader is not set to JRuby's.
assert_equal("true\n",
jruby('-rjava -rjruby -e "p(JRuby.runtime.jruby_class_loader == java.lang.Thread.current_thread.context_class_loader)"'))
end

def test_ruby_thread

0 comments on commit 7d3bc47

Please sign in to comment.