Skip to content

Commit

Permalink
Showing 4 changed files with 61 additions and 26 deletions.
43 changes: 18 additions & 25 deletions core/src/main/java/org/jruby/RubyInstanceConfig.java
Original file line number Diff line number Diff line change
@@ -35,6 +35,7 @@
import org.jruby.runtime.backtrace.TraceType;
import org.jruby.runtime.load.LoadService;
import org.jruby.runtime.profile.builtin.ProfileOutput;
import org.jruby.util.ClasspathLauncher;
import org.jruby.util.FileResource;
import org.jruby.util.InputStreamMarkCursor;
import org.jruby.util.JRubyFile;
@@ -59,8 +60,6 @@
import java.io.InputStreamReader;
import java.io.PrintStream;
import java.math.BigDecimal;
import java.net.URL;
import java.net.URLClassLoader;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
@@ -120,6 +119,7 @@ public RubyInstanceConfig() {
environment.putAll(System.getenv());
} catch (SecurityException se) {
}
setupEnvironment(getJRubyHome());
}

public RubyInstanceConfig(RubyInstanceConfig parentConfig) {
@@ -141,11 +141,12 @@ public RubyInstanceConfig(RubyInstanceConfig parentConfig) {
profilingService = parentConfig.profilingService;
profilingMode = parentConfig.profilingMode;

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) {
@@ -481,6 +482,7 @@ public String getJRubyHome() {

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

public CompileMode getCompileMode() {
@@ -660,31 +662,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
StringBuilder command = new StringBuilder("java -cp ");
if (defaultClassLoader() instanceof URLClassLoader) {
for(URL url : ((URLClassLoader) defaultClassLoader()).getURLs()) {
if (url.getProtocol().equals("file")) {
command.append(File.pathSeparatorChar).append(url.getPath());
}
}
}
else {
command.append(File.pathSeparatorChar).append(SafePropertyAccessor.getProperty("java.class.path"));
}
command.append(" org.jruby.Main");
environment.put("RUBY", command.toString() );
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;
}

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();
}
}
5 changes: 4 additions & 1 deletion lib/ruby/stdlib/rubygems/defaults/jruby.rb
Original file line number Diff line number Diff line change
@@ -11,7 +11,10 @@ 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

0 comments on commit c1b7ea0

Please sign in to comment.