Skip to content
This repository
Browse code

Merge git://github.com/bjeanes/warbler into bjeanes-master

  • Loading branch information...
commit c74d36b03c8dc262694859b1828efd76f72fdcc9 2 parents e4de829 + 3363641
Joe Kutner jkutner authored

Showing 2 changed files with 34 additions and 11 deletions. Show diff stats Hide diff stats

  1. +1 1  ext/JarMain.java
  2. +33 10 ext/WarMain.java
2  ext/JarMain.java
@@ -185,7 +185,7 @@ public static void main(String[] args) {
185 185
186 186 protected static void doStart(final JarMain main) {
187 187 try {
188   - int exit = new JarMain(args).start();
  188 + int exit = main.start();
189 189 if(isSystemExitEnabled()) System.exit(exit);
190 190 } catch (Exception e) {
191 191 System.err.println("error: " + e.toString());
43 ext/WarMain.java
@@ -7,6 +7,8 @@
7 7
8 8 import java.lang.reflect.Method;
9 9 import java.io.InputStream;
  10 +import java.io.ByteArrayInputStream;
  11 +import java.io.SequenceInputStream;
10 12 import java.io.File;
11 13 import java.io.FileNotFoundException;
12 14 import java.io.FileOutputStream;
@@ -210,8 +212,7 @@ protected int launchJRuby(final URL[] jars) throws Exception {
210 212
211 213 invokeMethod(rubyInstanceConfig, "setUpdateNativeENVEnabled", new Class[] { Boolean.TYPE }, false);
212 214
213   - final String executablePath = (String)
214   - invokeMethod(scriptingContainer, "runScriptlet", locateExecutableScript());
  215 + final String executablePath = locateExecutable(scriptingContainer);
215 216 if ( executablePath == null ) {
216 217 throw new IllegalStateException("failed to locate gem executable: '" + executable + "'");
217 218 }
@@ -219,8 +220,10 @@ protected int launchJRuby(final URL[] jars) throws Exception {
219 220
220 221 invokeMethod(rubyInstanceConfig, "processArguments", (Object) arguments);
221 222
222   - Object executableInput = invokeMethod(rubyInstanceConfig, "getScriptSource");
223 223 Object runtime = invokeMethod(scriptingContainer, "getRuntime");
  224 + Object executableInput =
  225 + new SequenceInputStream(new ByteArrayInputStream(executableScriptEnvPrefix().getBytes()),
  226 + (InputStream) invokeMethod(rubyInstanceConfig, "getScriptSource"));
224 227
225 228 debug("invoking " + executablePath + " with: " + Arrays.toString(executableArgv));
226 229 Object outcome = invokeMethod(runtime, "runFromMain",
@@ -230,21 +233,42 @@ protected int launchJRuby(final URL[] jars) throws Exception {
230 233 return ( outcome instanceof Number ) ? ( (Number) outcome ).intValue() : 0;
231 234 }
232 235
233   - protected String locateExecutableScript() {
  236 + protected String locateExecutable(final Object scriptingContainer) throws Exception {
234 237 if ( executable == null ) {
235   - throw new IllegalStateException("no exexutable");
  238 + throw new IllegalStateException("no executable");
236 239 }
  240 + final File exec = new File(extractRoot, executable);
  241 + if ( exec.exists() ) {
  242 + return exec.getAbsolutePath();
  243 + }
  244 + else {
  245 + final String script = locateExecutableScript(executable);
  246 + return (String) invokeMethod(scriptingContainer, "runScriptlet", script);
  247 + }
  248 + }
  249 + protected String executableScriptEnvPrefix() {
237 250 final String gemsDir = new File(extractRoot, "gems").getAbsolutePath();
238 251 final String gemfile = new File(extractRoot, "Gemfile").getAbsolutePath();
239 252 debug("setting GEM_HOME to " + gemsDir);
240 253 debug("... and BUNDLE_GEMFILE to " + gemfile);
241   - return
242   - "ENV['GEM_HOME'] = ENV['GEM_PATH'] = '"+ gemsDir +"' \n" +
243   - "ENV['BUNDLE_GEMFILE'] = '"+ gemfile +"' \n" +
  254 + return "ENV['GEM_HOME'] ||= ENV['GEM_PATH'] = '"+ gemsDir +"' \n" +
  255 + "ENV['BUNDLE_GEMFILE'] ||= '"+ gemfile +"' \n" +
  256 + "require 'META-INF/init.rb' \n";
  257 + }
  258 +
  259 + protected String locateExecutableScript(final String executable) {
  260 + return executableScriptEnvPrefix() +
244 261 "begin\n" +
245   - " require 'META-INF/init.rb' \n" +
246 262 // locate the executable within gemspecs :
247 263 " require 'rubygems' \n" +
  264 + " begin\n" +
  265 + // add bundler gems to load path:
  266 + " require 'bundler' \n" +
  267 + // TODO: environment from web.xml. Any others?
  268 + " Bundler.setup(:default, ENV.values_at('RACK_ENV', 'RAILS_ENV').compact)\n" +
  269 + " rescue LoadError\n" +
  270 + // bundler not used
  271 + " end\n" +
248 272 " exec = '"+ executable +"' \n" +
249 273 " spec = Gem::Specification.find { |s| s.executables.include?(exec) } \n" +
250 274 " spec ? spec.bin_file(exec) : nil \n" +
@@ -286,4 +310,3 @@ public static void main(String[] args) {
286 310 }
287 311
288 312 }
289   -

0 comments on commit c74d36b

Please sign in to comment.
Something went wrong with that request. Please try again.