Skip to content
Permalink

Comparing changes

Choose two branches to see what’s changed or to start a new pull request. If you need to, you can also or learn more about diff comparisons.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also . Learn more about diff comparisons here.
base repository: jruby/jruby
Failed to load repositories. Confirm that selected base ref is valid, then try again.
Loading
base: bd945538db9f^
Choose a base ref
...
head repository: jruby/jruby
Failed to load repositories. Confirm that selected head ref is valid, then try again.
Loading
compare: c83e5b819ec5
Choose a head ref
  • 2 commits
  • 6 files changed
  • 1 contributor

Commits on Jul 29, 2015

  1. allow to add classloader to instance config

    when the runtime gets created the classloader root gets added to the LOAD_PATH
    and to the Gem::Specification.dirs in case these uri-like paths are directories
    mkristian committed Jul 29, 2015
    Copy the full SHA
    bd94553 View commit details
  2. revert recent addition of adding classloader to LOAD_PATH or GEM_PATH

    in favour of adding classloader to instance_config
    mkristian committed Jul 29, 2015
    Copy the full SHA
    c83e5b8 View commit details
18 changes: 17 additions & 1 deletion core/src/main/java/org/jruby/RubyInstanceConfig.java
Original file line number Diff line number Diff line change
@@ -44,6 +44,7 @@
import org.jruby.util.NormalizedFile;
import org.jruby.util.SafePropertyAccessor;
import org.jruby.util.URLResource;
import org.jruby.util.UriLikePathHelper;
import org.jruby.util.cli.ArgumentProcessor;
import org.jruby.util.cli.Options;
import org.jruby.util.cli.OutputStrings;
@@ -65,6 +66,7 @@
import java.util.HashMap;
import java.util.HashSet;
import java.util.LinkedHashSet;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Set;
@@ -77,6 +79,7 @@
* embedding.
*/
public class RubyInstanceConfig {

public RubyInstanceConfig() {
currentDirectory = Ruby.isSecurityRestricted() ? "/" : JRubyFile.getFileProperty("user.dir");

@@ -670,7 +673,7 @@ private void setupEnvironment(String jrubyHome) {
if (!new File(jrubyHome).exists() && !environment.containsKey("RUBY")) {
// the assumption that if JRubyHome is not a regular file that jruby
// got launched in an embedded fashion
environment.put("RUBY", ClasspathLauncher.jrubyCommand(defaultClassLoader()) );
environment.put("RUBY", ClasspathLauncher.jrubyCommand(defaultClassLoader()));
}
}

@@ -686,6 +689,19 @@ public void setLoader(ClassLoader loader) {
this.loader = loader;
}

private final List<String> extraLoadPaths = new LinkedList<>();
public List<String> getExtraLoadPaths() {
return extraLoadPaths;
}
private final List<String> extraGemPath = new LinkedList<>();
public void addLoader(ClassLoader loader) {
UriLikePathHelper helper = new UriLikePathHelper(loader);
String uri = helper.getUriLikePath();
if (uri != null) extraLoadPaths.add(uri);
uri = helper.getUriLikePath();
if (uri != null) extraGemPath.add(uri);
}

public String[] getArgv() {
return argv;
}
9 changes: 0 additions & 9 deletions core/src/main/java/org/jruby/javasupport/JavaEmbedUtils.java
Original file line number Diff line number Diff line change
@@ -203,15 +203,6 @@ public IRubyObject run() {
}
}

public static void addLoadPath(Ruby runtime, ClassLoader cl) {
runtime.getLoadService().addPaths(new UriLikePathHelper(cl).getUriLikePath());
}

public static void addGemPath(Ruby runtime, ClassLoader cl) {
String uri = new UriLikePathHelper(cl).getUriLikePath();
runtime.evalScriptlet("Gem::Specification.add_dir '" + uri + "' unless Gem::Specification.dirs.member?( '" + uri + "' )" );
}

/**
* Dispose of the runtime you initialized.
*
1 change: 1 addition & 0 deletions core/src/main/java/org/jruby/runtime/load/LoadService.java
Original file line number Diff line number Diff line change
@@ -251,6 +251,7 @@ public void init(List prependDirectories) {
}

} catch(SecurityException ignore) {}
addPaths(runtime.getInstanceConfig().getExtraLoadPaths());
}

/**
10 changes: 5 additions & 5 deletions core/src/main/java/org/jruby/util/UriLikePathHelper.java
Original file line number Diff line number Diff line change
@@ -43,19 +43,19 @@ public URL getResource(String ref) {
}
return url;
}

public String getUriLikePath() {
return createUri("/.jrubydir");
}

public String getUriLikePath(String ref) {
return createUri(ref);
public String getUriLikeGemPath() {
return createUri("/specifications/.jrubydir");
}

String createUri(String ref) {
URL url = getResource( ref );
if ( url == null ) {
throw new RuntimeException( "reference " + ref + " not found on classloader " + classLoader );
return null;
}
return "uri:" + url.toString().replaceFirst( ref + "$", "" );
}
10 changes: 6 additions & 4 deletions core/src/test/java/org/jruby/javasupport/JavaEmbedUtilsTest.java
Original file line number Diff line number Diff line change
@@ -46,9 +46,11 @@ public static void restClassLoader() {
@Test
public void testAddClassloaderToLoadPathOnTCCL() throws Exception {
Thread.currentThread().setContextClassLoader( cl );
Ruby runtime = JavaEmbedUtils.initialize(EMPTY);
RubyInstanceConfig config = new RubyInstanceConfig();
config.setLoader(RubyInstanceConfig.class.getClassLoader());
URL url = new File("src/test/resources/java_embed_utils").toURI().toURL();
JavaEmbedUtils.addLoadPath(runtime, new URLClassLoader(new URL[] { url }));
config.addLoader(new URLClassLoader(new URL[]{url}));
Ruby runtime = JavaEmbedUtils.initialize(EMPTY, config);
String result = runtime.evalScriptlet("require 'test_me'; $result").toString();
assertEquals(result, "uri:" + url);
}
@@ -57,9 +59,9 @@ public void testAddClassloaderToLoadPathOnTCCL() throws Exception {
public void testAddClassloaderToLoadPathOnNoneTCCL() throws Exception {
RubyInstanceConfig config = new RubyInstanceConfig();
config.setLoader(RubyInstanceConfig.class.getClassLoader());
Ruby runtime = JavaEmbedUtils.initialize(EMPTY, config);
URL url = new File("src/test/resources/java_embed_utils").toURI().toURL();
JavaEmbedUtils.addLoadPath(runtime, new URLClassLoader(new URL[] { url }));
config.addLoader(new URLClassLoader(new URL[]{url}));
Ruby runtime = JavaEmbedUtils.initialize(EMPTY, config);
String result = runtime.evalScriptlet("require 'test_me';$result").toString();
assertEquals(result, "uri:" + url);
}
6 changes: 5 additions & 1 deletion lib/ruby/stdlib/rubygems/defaults/jruby.rb
Original file line number Diff line number Diff line change
@@ -107,7 +107,11 @@ def dirs= dirs
end

def spec_directories_from_classpath
stuff = [ 'uri:classloader://specifications' ] + JRuby::Util.classloader_resources("specifications")
stuff = [ 'uri:classloader://specifications' ]
JRuby.runtime.instance_config.extra_gem_paths.each do |path|
stuff += File.join(path, 'specifications')
end
stuff += JRuby::Util.classloader_resources('specifications')
# some classloader return directory info. use only the "protocols"
# which jruby understands
stuff.select { |s| File.directory?( s ) }