Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Initialize the Ruby runtime directly without the ScriptingContainer. …

…Fixes #34
  • Loading branch information...
commit 385ae17d7c860c5151742e1d72b80d7e40771744 1 parent 1b58f23
David Calavera authored April 28, 2011
55  src/main/java/org/jruby/rack/DefaultRackApplicationFactory.java
@@ -10,13 +10,10 @@
10 10
 import org.jruby.Ruby;
11 11
 import org.jruby.RubyInstanceConfig;
12 12
 import org.jruby.exceptions.RaiseException;
13  
-import org.jruby.embed.ScriptingContainer;
14  
-import org.jruby.embed.LocalContextScope;
  13
+import org.jruby.javasupport.JavaUtil;
15 14
 import org.jruby.runtime.ThreadContext;
16 15
 import org.jruby.runtime.builtin.IRubyObject;
17  
-import org.jruby.util.ClassCache;
18 16
 
19  
-import java.lang.reflect.Method;
20 17
 import java.io.IOException;
21 18
 import java.io.InputStream;
22 19
 import java.io.InputStreamReader;
@@ -35,20 +32,13 @@
35 32
     private String rackupScript, rackupLocation;
36 33
     private RackContext rackContext;
37 34
     private RubyInstanceConfig defaultConfig;
38  
-    private ClassCache classCache;
39 35
     private RackApplication errorApplication;
40  
-    private Method getRequiredLibraries;
41 36
 
42 37
     public void init(RackContext rackContext) {
43 38
         this.rackContext = rackContext;
44 39
         this.rackupScript = findRackupScript();
45 40
         this.defaultConfig = createDefaultConfig();
46 41
         rackContext.log(defaultConfig.getVersionString());
47  
-        try {
48  
-            this.getRequiredLibraries = RubyInstanceConfig.class.getMethod("requiredLibraries", new Class[0]);
49  
-        } catch (Exception e) {
50  
-            throw (Error) (new NoSuchMethodError("JRuby is missing RubyInstanceConfig#requiredLibraries").initCause(e));
51  
-        }
52 42
     }
53 43
 
54 44
     public RackApplication newApplication() throws RackInitializationException {
@@ -180,54 +170,35 @@ private RubyInstanceConfig createDefaultConfig() {
180 170
         return config;
181 171
     }
182 172
 
183  
-    @SuppressWarnings("unchecked")
184  
-    private void configureContainer(ScriptingContainer container) throws RackInitializationException {
185  
-        container.setClassLoader(defaultConfig.getLoader());
186  
-        container.setClassCache(defaultConfig.getClassCache());
187  
-        container.setCompatVersion(defaultConfig.getCompatVersion());
188  
-        container.setHomeDirectory(defaultConfig.getJRubyHome());
189  
-        container.setEnvironment(defaultConfig.getEnvironment());
190  
-        container.setLoadPaths(defaultConfig.loadPaths());
191  
-        // Reflection hack due to sig change of requiredLibraries between
192  
-        // 1.5 and 1.6: remove when JRuby 1.7 is available
193  
-        try {
194  
-            Collection defLibraries = (Collection) getRequiredLibraries.invoke(defaultConfig, new Object[0]);
195  
-            Collection containerLibraries = (Collection) getRequiredLibraries.invoke(container.getProvider().getRubyInstanceConfig(), new Object[0]);
196  
-            containerLibraries.addAll(defLibraries);
197  
-        } catch (Exception e) {
198  
-            throw new RackInitializationException("Error configuring new scripting container", e);
199  
-        }
200  
-    }
201  
-
202  
-    private void initializeContainer(ScriptingContainer container) throws RackInitializationException {
  173
+    private void initializeRuntime(Ruby runtime) throws RackInitializationException {
203 174
         try {
204  
-            container.put("$servlet_context", rackContext);
  175
+            IRubyObject context = JavaUtil.convertJavaToRuby(runtime, rackContext);
  176
+            runtime.getGlobalVariables().set("$servlet_context", context);
205 177
             if (rackContext.getConfig().isIgnoreEnvironment()) {
206  
-                container.runScriptlet("ENV.clear");
  178
+                runtime.evalScriptlet("ENV.clear");
207 179
             }
208  
-            container.runScriptlet("require 'rack/handler/servlet'");
  180
+            runtime.evalScriptlet("require 'rack/handler/servlet'");
209 181
         } catch (RaiseException re) {
210 182
             throw new RackInitializationException(re);
211 183
         }
212 184
     }
213 185
 
214 186
     /** This method is only public for unit tests */
215  
-    public ScriptingContainer newContainer() throws RackInitializationException {
216  
-        ScriptingContainer container = new ScriptingContainer(LocalContextScope.SINGLETHREAD);
217  
-        configureContainer(container);
218  
-        initializeContainer(container);
219  
-        return container;
  187
+    public Ruby newRuntime() throws RackInitializationException {
  188
+        Ruby runtime = Ruby.newInstance(defaultConfig);
  189
+        initializeRuntime(runtime);
  190
+        return runtime;
220 191
     }
221 192
 
222 193
     private RackApplication createApplication(final ApplicationObjectFactory appfact)
223 194
             throws RackInitializationException {
224 195
         try {
225  
-            final ScriptingContainer container = newContainer();
  196
+            final Ruby runtime = newRuntime();
226 197
             return new DefaultRackApplication() {
227 198
                 @Override
228 199
                 public void init() throws RackInitializationException {
229 200
                     try {
230  
-                        setApplication(appfact.create(container.getProvider().getRuntime()));
  201
+                        setApplication(appfact.create(runtime));
231 202
                     } catch (RaiseException re) {
232 203
                         captureMessage(re);
233 204
                         throw new RackInitializationException(re);
@@ -235,7 +206,7 @@ public void init() throws RackInitializationException {
235 206
                 }
236 207
                 @Override
237 208
                 public void destroy() {
238  
-                    container.terminate();
  209
+                    runtime.tearDown(false);
239 210
                 }
240 211
             };
241 212
         } catch (RackInitializationException rie) {
28  src/spec/ruby/rack/application_spec.rb
@@ -91,30 +91,34 @@
91 91
       end
92 92
     end
93 93
 
94  
-    describe "newContainer" do
95  
-      it "should create a new Ruby container with the rack environment pre-loaded" do
96  
-        container = app_factory.newContainer
97  
-        container.runScriptlet("defined?(::Rack)").should be_true
98  
-        container.runScriptlet("defined?(::Rack::Handler::Servlet)").should be_true
99  
-        container.runScriptlet("defined?(Rack::Handler::Bogus)").should_not be_true
  94
+    describe "newRuntime" do
  95
+      it "should create a new Ruby runtime with the rack environment pre-loaded" do
  96
+        runtime = app_factory.new_runtime
  97
+        lambda { runtime.evalScriptlet("defined?(::Rack)") != nil }.should be_true
  98
+        lambda { runtime.evalScriptlet("defined?(::Rack::Handler::Servlet)") != nil }.should be_true
  99
+        lambda { runtime.evalScriptlet("defined?(Rack::Handler::Bogus)") == nil }.should be_true
100 100
       end
101 101
 
102 102
       it "should initialize the $servlet_context global variable" do
103  
-        container = app_factory.newContainer
104  
-        container.runScriptlet("defined?($servlet_context)").should_not be_empty
  103
+        runtime = app_factory.new_runtime
  104
+        lambda { runtime.evalScriptlet("defined?($servlet_context)") != nil }.should be_true
105 105
       end
106 106
 
107 107
       it "should handle jruby.compat.version == '1.9' and start up in 1.9 mode" do
108 108
         @rack_config.stub!(:getCompatVersion).and_return org.jruby.CompatVersion::RUBY1_9
109  
-        container = app_factory.newContainer
110  
-        container.compat_version.should == org.jruby.CompatVersion::RUBY1_9
  109
+        runtime = app_factory.new_runtime
  110
+        runtime.is1_9.should be_true
111 111
       end
112 112
 
113 113
       it "should have environment variables cleared if the configuration ignores the environment" do
114 114
         ENV["HOME"].should_not == ""
115 115
         @rack_config.stub!(:isIgnoreEnvironment).and_return true
116  
-        container = app_factory.newContainer
117  
-        container.runScriptlet('ENV["HOME"]').should be_nil
  116
+        runtime = app_factory.new_runtime
  117
+        lambda { runtime.evalScriptlet('ENV["HOME"]') == nil }.should be_true
  118
+      end
  119
+
  120
+      def eval_should_be_true(value, expected)
  121
+        (value == expected).should be_true
118 122
       end
119 123
     end
120 124
   end

1 note on commit 385ae17

David Calavera
Owner

As a side note, I haven't seen the errors we found using bundler before we started to use the ScriptingContainer either.

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