Created
March 31, 2012 20:29
-
-
Save jkutner/2268163 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$LOAD_PATH << $APP_LOAD_PATH #set the load path from the APP_LOAD_PATH binding | |
if $RACK_ENV == 'production' | |
ENV['GEM_PATH'] = "#{File.join($RULE_SERVER_HOME, 'vendor/jruby/1.8')}:#{File.join($TORQUEBOX_HOME, 'jruby/lib/ruby/gems/1.8')}" | |
ENV['GEM_HOME'] = File.join($RULE_SERVER_HOME, 'vendor/jruby/1.8') | |
else | |
puts 'Using global GEM_HOME directory.' | |
end | |
require 'rubygems' | |
require 'java' |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
java_import "javax.script.ScriptContext" | |
java_import "javax.script.ScriptEngine" | |
java_import "javax.script.ScriptEngineManager" | |
java_import "javax.script.ScriptException" | |
java_import "java.io.FileNotFoundException" | |
java_import "javax.script.Bindings" | |
java_import "javax.script.SimpleBindings" | |
java_import "java.io.FileReader" | |
java_import "java.io.Reader" | |
def create_jruby_engine | |
# this means that we take all responsibility for concurrency | |
java.lang.System.setProperty("org.jruby.embed.localcontext.scope", 'singlethread') | |
m = ScriptEngineManager.new() | |
e = m.getEngineByName("jruby") | |
bindings = SimpleBindings.new() | |
bindings.put("TORQUEBOX_HOME", ENV['TORQUEBOX_HOME']) | |
bindings.put("RACK_ENV", ENV['RACK_ENV']) | |
bindings.put("INSTANCE_ID", ENV['INSTANCE_ID']) | |
app_load_path = File.expand_path('../..', File.dirname(__FILE__)) | |
bindings.put("APP_LOAD_PATH", app_load_path) | |
yield e, bindings | |
e | |
end | |
def eval_file(engine, relative_path, bindings) | |
file = File.expand_path(relative_path, __FILE__) | |
reader = FileReader.new(file) | |
engine.eval(reader, bindings) | |
end | |
def load_gems(engine, gems) | |
s = gems ? gems.map do |gem| | |
gem_name = gem.gsub(/ (\(.+\))?\z/, "").strip | |
version = gem =~ /\((.*\s?.+)\)\z/ ? $1 : ">= 0" | |
"gem '#{gem_name}', '#{version}'; require '#{gem_name}'; " | |
end : [] | |
engine.eval("begin; #{s.join('; ')} rescue => e; puts e.message; end") | |
end | |
e = create_jruby_engine do |e, bindings| | |
eval_file(e, "../../engine/embedded/boot.rb", bindings) | |
load_gems(e, gems) | |
end | |
e.invokeFunction(:do_something, params) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment