Title: [waffle-scm] [113] trunk/core/src/main/ruby: custom ruby files for an application are now loaded from Waffle::ScriptLoader.load_all (via ruby 'require')

Diff

Modified: trunk/core/src/main/java/org/codehaus/waffle/context/pico/RubyAwarePicoContextContainerFactory.java (112 => 113)

--- trunk/core/src/main/java/org/codehaus/waffle/context/pico/RubyAwarePicoContextContainerFactory.java	2007-05-31 04:01:35 UTC (rev 112)
+++ trunk/core/src/main/java/org/codehaus/waffle/context/pico/RubyAwarePicoContextContainerFactory.java	2007-05-31 04:24:07 UTC (rev 113)
@@ -21,10 +21,15 @@
         ContextContainer contextContainer = super.buildApplicationContextContainer();
 
         Ruby runtime = Ruby.getDefaultInstance();
-
         loadRubyScriptFromClassLoader("string.rb", runtime);
         loadRubyScriptFromClassLoader("waffle.rb", runtime);
 
+        // I'd prefer to do the following:
+        //      runtime.evalScript("require 'string'\nrequire 'waffle'"); // load Waffle custom scripts
+        //
+        // but JRuby fails when web app is reloaded...
+        // <script>:1:in `require': JAR entry string.rb not found in ~/jruby-example/exploded/WEB-INF/lib/core.jar (IOError)
+
         // Register RubyRuntime at Application level
         MutablePicoContainer picoContainer = (MutablePicoContainer) contextContainer.getDelegate();
         picoContainer.registerComponentInstance(Ruby.class, runtime);

Modified: trunk/core/src/main/java/org/codehaus/waffle/context/pico/RubyScriptLoader.java (112 => 113)

--- trunk/core/src/main/java/org/codehaus/waffle/context/pico/RubyScriptLoader.java	2007-05-31 04:01:35 UTC (rev 112)
+++ trunk/core/src/main/java/org/codehaus/waffle/context/pico/RubyScriptLoader.java	2007-05-31 04:24:07 UTC (rev 113)
@@ -1,15 +1,12 @@
 package org.codehaus.waffle.context.pico;
 
-import org.picocontainer.Startable;
 import org.jruby.Ruby;
-import org.codehaus.waffle.WaffleException;
+import org.jruby.javasupport.JavaEmbedUtils;
+import org.jruby.runtime.builtin.IRubyObject;
+import org.picocontainer.Startable;
 
 import javax.servlet.ServletContext;
 import java.util.Set;
-import java.io.BufferedReader;
-import java.io.InputStream;
-import java.io.InputStreamReader;
-import java.io.IOException;
 
 public class RubyScriptLoader implements Startable {
     private final ServletContext servletContext;
@@ -21,47 +18,20 @@
     }
 
     public void start() {
+        String path = "/WEB-INF/classes/ruby/";
         // noinspection unchecked
-        Set<String> resourcePaths = servletContext.getResourcePaths("/WEB-INF/classes/ruby"); // todo should be able to override ruby location through a key in the web.xml
+        Set<String> resourcePaths = servletContext.getResourcePaths(path); // todo should be able to override ruby location through a key in the web.xml
 
-        for (String path : resourcePaths) {
-            // todo cache path and file create time
-            loadRubyScript(path);
-        }
+        runtime.getClassFromPath("Waffle::ScriptLoader")
+                .callMethod(runtime.getCurrentContext(), "load_all",
+                        new IRubyObject[]{
+                                JavaEmbedUtils.javaToRuby(runtime, path),
+                                JavaEmbedUtils.javaToRuby(runtime, resourcePaths)
+                        });
     }
 
-    // todo implement a reload which checks files to ensure they are upto date in the runtime
-
     public void stop() {
         // does nothing
     }
 
-    private void loadRubyScript(String path) {
-        BufferedReader bufferedReader = null;
-        InputStream inputStream = null;
-
-        try {
-            inputStream = servletContext.getResourceAsStream(path);
-            bufferedReader = new BufferedReader(new InputStreamReader(inputStream));
-            StringBuilder script = new StringBuilder();
-            String line = bufferedReader.readLine();
-
-            while (line != null) {
-                script.append(line).append("\n");
-                line = bufferedReader.readLine();
-            }
-
-            runtime.evalScript(script.toString());
-        } catch (IOException e) {
-            throw new WaffleException(e);
-        } finally {
-            try {
-                if(inputStream != null) inputStream.close();
-                if(bufferedReader != null) bufferedReader.close();
-            } catch (IOException ignore) {
-                // ignore
-            }
-        }
-
-    }
 }

Modified: trunk/core/src/main/ruby/waffle.rb (112 => 113)

--- trunk/core/src/main/ruby/waffle.rb	2007-05-31 04:01:35 UTC (rev 112)
+++ trunk/core/src/main/ruby/waffle.rb	2007-05-31 04:24:07 UTC (rev 113)
@@ -2,6 +2,16 @@
 
 module Waffle
 
+    # load/require files
+    class ScriptLoader
+        def ScriptLoader.load_all(prefix, paths)
+            paths.each do |path|
+                # require is what should be used in production ... development should allow for 'load
+                require path.gsub(Regexp.new("^#{prefix}"), 'ruby/')
+            end
+        end
+    end
+
     ##
     # This is a generic class for making HttpServletRequest's, HttpSession's and ServletContext's to act like Hash's
     class WebContext < Hash

Modified: trunk/core/src/test/java/org/codehaus/waffle/context/pico/RubyScriptLoaderTest.java (112 => 113)

--- trunk/core/src/test/java/org/codehaus/waffle/context/pico/RubyScriptLoaderTest.java	2007-05-31 04:01:35 UTC (rev 112)
+++ trunk/core/src/test/java/org/codehaus/waffle/context/pico/RubyScriptLoaderTest.java	2007-05-31 04:24:07 UTC (rev 113)
@@ -1,12 +1,14 @@
 package org.codehaus.waffle.context.pico;
 
-import org.junit.runner.RunWith;
-import org.junit.Test;
+import org.jmock.Expectations;
+import org.jmock.Mockery;
 import org.jmock.integration.junit4.JMock;
 import org.jmock.integration.junit4.JUnit4Mockery;
-import org.jmock.Mockery;
-import org.jmock.Expectations;
 import org.jruby.Ruby;
+import org.jruby.javasupport.JavaUtil;
+import org.junit.Assert;
+import org.junit.Test;
+import org.junit.runner.RunWith;
 
 import javax.servlet.ServletContext;
 import java.util.HashSet;
@@ -21,19 +23,32 @@
         final ServletContext servletContext = context.mock(ServletContext.class);
 
         context.checking(new Expectations() {{
-            one (servletContext).getResourcePaths("/WEB-INF/classes/ruby");
+            one (servletContext).getResourcePaths("/WEB-INF/classes/ruby/");
             Set<String> paths = new HashSet<String>();
-            paths.add("waffle.rb");
+            paths.add("/WEB-INF/classes/ruby/fake_script.rb");
             will(returnValue(paths));
-
-            one (servletContext).getResourceAsStream("waffle.rb");
-            will(returnValue(this.getClass().getClassLoader().getResourceAsStream("waffle.rb"))); // actually get it
         }});
 
+        String script =
+                "module Waffle\n" +
+                "  class ScriptLoader\n" +
+                "    def ScriptLoader.load_all(*args)\n" +
+                "      $arg1 = args[0]\n" +
+                "      $arg2 = args[1]\n" +
+                "    end\n" +
+                "  end\n" +
+                "end\n";
 
         Ruby runtime = Ruby.getDefaultInstance();
+        runtime.evalScript(script);
 
         RubyScriptLoader loader = new RubyScriptLoader(servletContext, runtime);
         loader.start();
+
+        // Ensure Waffle::ScriptLoader.load_all was called
+        Assert.assertEquals("/WEB-INF/classes/ruby/", JavaUtil.convertRubyToJava(runtime.evalScript("$arg1")));
+        Set<String> expected = new HashSet<String>();
+        expected.add("/WEB-INF/classes/ruby/fake_script.rb");
+        Assert.assertEquals(expected, JavaUtil.convertRubyToJava(runtime.evalScript("$arg2")));
     }
 }

Modified: trunk/examples/jruby-example/src/test/java/org/codehaus/waffle/example/jruby/RubyRegistrarTest.java (112 => 113)

--- trunk/examples/jruby-example/src/test/java/org/codehaus/waffle/example/jruby/RubyRegistrarTest.java	2007-05-31 04:01:35 UTC (rev 112)
+++ trunk/examples/jruby-example/src/test/java/org/codehaus/waffle/example/jruby/RubyRegistrarTest.java	2007-05-31 04:24:07 UTC (rev 113)
@@ -47,15 +47,16 @@
     public void testJRuby() {
         Ruby runtime = Ruby.getDefaultInstance();
 
-        String script = "class Foo\n" +
-                        "  attr_accessor :salmon\n" +
-                        "  def initialize\n" +
-                        "    @salmon = 'fish'\n" +
-                        "  end\n" +
-                        "  def bar\n" +
-                        "    return \"HELLO #{salmon}!\"\n" +
-                        "  end\n" +
-                        "end\n";
+        String script =
+                "class Foo\n" +
+                "  attr_accessor :salmon\n" +
+                "  def initialize\n" +
+                "    @salmon = 'fish'\n" +
+                "  end\n" +
+                "  def bar\n" +
+                "    return \"HELLO #{salmon}!\"\n" +
+                "  end\n" +
+                "end\n";
 
         runtime.evalScript(script);
 


To unsubscribe from this list please visit:

http://xircles.codehaus.org/manage_email

Reply via email to