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: 5b461844815c
Choose a base ref
...
head repository: jruby/jruby
Failed to load repositories. Confirm that selected head ref is valid, then try again.
Loading
compare: 311f12f1689a
Choose a head ref
  • 3 commits
  • 5 files changed
  • 1 contributor

Commits on Jun 8, 2015

  1. Copy the full SHA
    e03303a View commit details
  2. Copy the full SHA
    abaa0db View commit details
  3. factor out the classpath launcher so it can be used by Gem.ruby as well

    also makes sure we are using platform dependent paths when craeting
    the classpath launcher for ENV['RUBY']. put the helper into its own
    class so the Gem.ruby (rubygems/defaults/jruby.rb) can use it.
    
    Sponsored by Lookout Inc.
    mkristian committed Jun 8, 2015
    1
    Copy the full SHA
    311f12f View commit details
42 changes: 30 additions & 12 deletions core/src/main/java/org/jruby/RubyInstanceConfig.java
Original file line number Diff line number Diff line change
@@ -41,6 +41,7 @@
import org.jruby.runtime.load.LoadService19;
import org.jruby.runtime.profile.builtin.ProfileOutput;
import org.jruby.util.ClassCache;
import org.jruby.util.ClasspathLauncher;
import org.jruby.util.FileResource;
import org.jruby.util.InputStreamMarkCursor;
import org.jruby.util.JRubyFile;
@@ -65,7 +66,6 @@
import java.io.InputStreamReader;
import java.io.PrintStream;
import java.math.BigDecimal;
import java.net.URL;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
@@ -148,6 +148,7 @@ public RubyInstanceConfig() {
environment.putAll(System.getenv());
} catch (SecurityException se) {
}
setupEnvironment(getJRubyHome());
}

public RubyInstanceConfig(RubyInstanceConfig parentConfig) {
@@ -172,11 +173,12 @@ public RubyInstanceConfig(RubyInstanceConfig parentConfig) {

classCache = new ClassCache<Script>(loader, jitMax);

environment = new HashMap<String, String>();
try {
environment = System.getenv();
environment.putAll(System.getenv());
} catch (SecurityException se) {
environment = new HashMap();
}
setupEnvironment(getJRubyHome());
}

public RubyInstanceConfig(final InputStream in, final PrintStream out, final PrintStream err) {
@@ -511,6 +513,7 @@ public String getJRubyHome() {

public void setJRubyHome(String home) {
jrubyHome = verifyHome(home, error);
setupEnvironment(jrubyHome);
}

public CompileMode getCompileMode() {
@@ -697,19 +700,22 @@ public boolean isSiphashEnabled() {
}

public void setEnvironment(Map newEnvironment) {
if (newEnvironment == null) {
newEnvironment = new HashMap<String, String>();
environment = new HashMap<String, String>();
if (newEnvironment != null) {
environment.putAll(newEnvironment);
}
this.environment = newEnvironment;
setupEnvironment(getJRubyHome());
}

public Map getEnvironment() {
if (!new File(getJRubyHome()).exists() && !environment.containsKey("RUBY")) {
// the assumption that if JRubyHome is not a regular file that java.class.path
// is the one which launched jruby is probably wrong. but is sufficient for
// java -jar jruby-complete.jar
environment.put("RUBY", "java -cp " + System.getProperty("java.class.path") + " org.jruby.Main");
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()) );
}
}

public Map getEnvironment() {
return environment;
}

@@ -1421,6 +1427,18 @@ public void setProfilingService( String service ) {
this.profilingService = service;
}

public static ClassLoader defaultClassLoader() {
ClassLoader loader = RubyInstanceConfig.class.getClassLoader();

// loader can be null for example when jruby comes from the boot-classLoader

if (loader == null) {
loader = Thread.currentThread().getContextClassLoader();
}

return loader;
}

////////////////////////////////////////////////////////////////////////////
// Configuration fields.
////////////////////////////////////////////////////////////////////////////
29 changes: 29 additions & 0 deletions core/src/main/java/org/jruby/util/ClasspathLauncher.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package org.jruby.util;

import java.io.File;
import java.net.URL;
import java.net.URLClassLoader;

import org.jruby.Ruby;

public class ClasspathLauncher {

public static String jrubyCommand(ClassLoader classLoader) {
StringBuilder command = new StringBuilder("java -cp ");
if (classLoader instanceof URLClassLoader) {
for(URL url : ((URLClassLoader) classLoader).getURLs()) {
String path = URLUtil.getPlatformPath(url);
if (path != null) command.append(File.pathSeparatorChar).append(path);
}
}
else {
command.append(File.pathSeparatorChar).append(SafePropertyAccessor.getProperty("java.class.path"));
}
command.append(" org.jruby.Main");
return command.toString();
}

public static String jrubyCommand(Ruby runtime) {
return jrubyCommand(runtime.getJRubyClassLoader().getParent());
}
}
10 changes: 10 additions & 0 deletions core/src/main/java/org/jruby/util/URLUtil.java
Original file line number Diff line number Diff line change
@@ -28,6 +28,7 @@
***** END LICENSE BLOCK *****/
package org.jruby.util;

import java.io.File;
import java.net.URISyntaxException;
import java.net.URL;

@@ -36,6 +37,7 @@
*
* See also:
* http://weblogs.java.net/blog/2007/04/25/how-convert-javaneturl-javaiofile
* http://stackoverflow.com/questions/18520972/converting-java-file-url-to-file-path-platform-independent-including-u
*/
public class URLUtil {
public static String getPath(URL url) {
@@ -45,4 +47,12 @@ public static String getPath(URL url) {
return url.getPath();
}
}

public static String getPlatformPath(URL url) {
if (!url.getProtocol().equals("file")) {
return null;
}
// the getPath from File uses the platform name-separator
return new File(getPath(url)).getPath();
}
}
6 changes: 5 additions & 1 deletion core/src/main/java/org/jruby/util/cli/ArgumentProcessor.java
Original file line number Diff line number Diff line change
@@ -227,8 +227,12 @@ private void processArgument() {
config.getLoadPaths().addAll(Arrays.asList(ls));
break FOR;
case 'J':
grabOptionalValue();
String js = grabOptionalValue();
config.getError().println("warning: " + argument + " argument ignored (launched in same VM?)");
if (js.equals("-cp") || js.equals("-classpath")) {
for(;grabOptionalValue() != null;) {}
grabValue(getArgumentError(" -J-cp must be followed by a path expression"));
}
break FOR;
case 'K':
// FIXME: No argument seems to work for -K in MRI plus this should not
7 changes: 5 additions & 2 deletions lib/ruby/shared/rubygems/defaults/jruby.rb
Original file line number Diff line number Diff line change
@@ -11,12 +11,15 @@ class << self
alias_method :original_ruby, :ruby
def ruby
ruby_path = original_ruby
ruby_path = "java -jar #{jar_path(ruby_path)}" if jarred_path?(ruby_path)
if jarred_path?(ruby_path)
# use quote as the original_ruby does it
ruby_path = "\"#{org.jruby.util.Classpath.jrubyCommand(JRuby.runtime)}\""
end
ruby_path
end

def jarred_path?(p)
p =~ /^file:/
p =~ /^(file|uri|jar|classpath):/
end

# A jar path looks like this on non-Windows platforms: