Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion ext/JarMain.java
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,7 @@ public static void main(String[] args) {

protected static void doStart(final JarMain main) {
try {
int exit = new JarMain(args).start();
int exit = main.start();
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I had to make this change to get the *.java files in master branch to compile but it is not directly related to this change. Let me know if you'd like me to break that out as a separate PR.

if(isSystemExitEnabled()) System.exit(exit);
} catch (Exception e) {
System.err.println("error: " + e.toString());
Expand Down
43 changes: 33 additions & 10 deletions ext/WarMain.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@

import java.lang.reflect.Method;
import java.io.InputStream;
import java.io.ByteArrayInputStream;
import java.io.SequenceInputStream;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
Expand Down Expand Up @@ -210,17 +212,18 @@ protected int launchJRuby(final URL[] jars) throws Exception {

invokeMethod(rubyInstanceConfig, "setUpdateNativeENVEnabled", new Class[] { Boolean.TYPE }, false);

final String executablePath = (String)
invokeMethod(scriptingContainer, "runScriptlet", locateExecutableScript());
final String executablePath = locateExecutable(scriptingContainer);
if ( executablePath == null ) {
throw new IllegalStateException("failed to locate gem executable: '" + executable + "'");
}
invokeMethod(scriptingContainer, "setScriptFilename", executablePath);

invokeMethod(rubyInstanceConfig, "processArguments", (Object) arguments);

Object executableInput = invokeMethod(rubyInstanceConfig, "getScriptSource");
Object runtime = invokeMethod(scriptingContainer, "getRuntime");
Object executableInput =
new SequenceInputStream(new ByteArrayInputStream(executableScriptEnvPrefix().getBytes()),
(InputStream) invokeMethod(rubyInstanceConfig, "getScriptSource"));

debug("invoking " + executablePath + " with: " + Arrays.toString(executableArgv));
Object outcome = invokeMethod(runtime, "runFromMain",
Expand All @@ -230,21 +233,42 @@ protected int launchJRuby(final URL[] jars) throws Exception {
return ( outcome instanceof Number ) ? ( (Number) outcome ).intValue() : 0;
}

protected String locateExecutableScript() {
protected String locateExecutable(final Object scriptingContainer) throws Exception {
if ( executable == null ) {
throw new IllegalStateException("no exexutable");
throw new IllegalStateException("no executable");
}
final File exec = new File(extractRoot, executable);
if ( exec.exists() ) {
return exec.getAbsolutePath();
}
else {
final String script = locateExecutableScript(executable);
return (String) invokeMethod(scriptingContainer, "runScriptlet", script);
}
}
protected String executableScriptEnvPrefix() {
final String gemsDir = new File(extractRoot, "gems").getAbsolutePath();
final String gemfile = new File(extractRoot, "Gemfile").getAbsolutePath();
debug("setting GEM_HOME to " + gemsDir);
debug("... and BUNDLE_GEMFILE to " + gemfile);
return
"ENV['GEM_HOME'] = ENV['GEM_PATH'] = '"+ gemsDir +"' \n" +
"ENV['BUNDLE_GEMFILE'] = '"+ gemfile +"' \n" +
return "ENV['GEM_HOME'] ||= ENV['GEM_PATH'] = '"+ gemsDir +"' \n" +
"ENV['BUNDLE_GEMFILE'] ||= '"+ gemfile +"' \n" +
"require 'META-INF/init.rb' \n";
}

protected String locateExecutableScript(final String executable) {
return executableScriptEnvPrefix() +
"begin\n" +
" require 'META-INF/init.rb' \n" +
// locate the executable within gemspecs :
" require 'rubygems' \n" +
" begin\n" +
// add bundler gems to load path:
" require 'bundler' \n" +
// TODO: environment from web.xml. Any others?
" Bundler.setup(:default, ENV.values_at('RACK_ENV', 'RAILS_ENV').compact)\n" +
" rescue LoadError\n" +
// bundler not used
" end\n" +
" exec = '"+ executable +"' \n" +
" spec = Gem::Specification.find { |s| s.executables.include?(exec) } \n" +
" spec ? spec.bin_file(exec) : nil \n" +
Expand Down Expand Up @@ -286,4 +310,3 @@ public static void main(String[] args) {
}

}