Title: [waffle-scm] [126] trunk/core/src/main/ruby: supports dynamic reloading of ruby scripts when 'org.codehaus.waffle.ruby.path' points to a folder (value must be prefixed with 'dir:')

Diff

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

--- trunk/core/src/main/java/org/codehaus/waffle/context/pico/RubyScriptLoader.java	2007-06-01 03:20:55 UTC (rev 125)
+++ trunk/core/src/main/java/org/codehaus/waffle/context/pico/RubyScriptLoader.java	2007-06-01 04:25:05 UTC (rev 126)
@@ -6,27 +6,34 @@
 import org.picocontainer.Startable;
 
 import javax.servlet.ServletContext;
-import java.util.Set;
 
 public class RubyScriptLoader implements Startable {
+    public static final String RUBY_SCRIPT_PATH_KEY = "org.codehaus.waffle.ruby.path";
+    public static final String DEFAULT_RUBY_SCRIPT_PATH = "/WEB-INF/classes/ruby/";
+
     private final ServletContext servletContext;
     private final Ruby runtime;
+    private final String rubyScriptPath;
 
     public RubyScriptLoader(ServletContext servletContext, Ruby runtime) {
         this.servletContext = servletContext;
         this.runtime = runtime;
+
+        String path = servletContext.getInitParameter(RUBY_SCRIPT_PATH_KEY);
+        
+        if(path == null) {
+            rubyScriptPath = DEFAULT_RUBY_SCRIPT_PATH;
+        } else {
+            rubyScriptPath = path;
+        }
     }
 
     public void start() {
-        String path = "/WEB-INF/classes/ruby/";
-        // noinspection unchecked
-        Set<String> resourcePaths = servletContext.getResourcePaths(path); // todo should be able to override ruby location through a key in the web.xml
-
         runtime.getClassFromPath("Waffle::ScriptLoader")
                 .callMethod(runtime.getCurrentContext(), "load_all",
                         new IRubyObject[]{
-                                JavaEmbedUtils.javaToRuby(runtime, path),
-                                JavaEmbedUtils.javaToRuby(runtime, resourcePaths)
+                                JavaEmbedUtils.javaToRuby(runtime, rubyScriptPath),
+                                JavaEmbedUtils.javaToRuby(runtime, servletContext)
                         });
     }
 

Modified: trunk/core/src/main/ruby/waffle.rb (125 => 126)

--- trunk/core/src/main/ruby/waffle.rb	2007-06-01 03:20:55 UTC (rev 125)
+++ trunk/core/src/main/ruby/waffle.rb	2007-06-01 04:25:05 UTC (rev 126)
@@ -4,12 +4,24 @@
 
   # 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/')
+    def ScriptLoader.load_all(prefix, servlet_context)
+
+      if (prefix.gsub!(/^dir:/, ''))
+        @@_ruby_script_path = prefix
+        ScriptLoader.load_from_file_system
+      else
+        servlet_context.getResourcePaths(prefix).each do |path| # this would be for production!!
+          require path.gsub(Regexp.new("^#{prefix}"), 'ruby/')
+        end
       end
     end
+
+    def ScriptLoader.load_from_file_system
+      Dir.new(@@_ruby_script_path).each do |entry|
+        file = "#{@@_ruby_script_path}#{entry}"
+        load(file) if File.file?(file) # TODO need to ensure it is a *.rb file ... need to recursively search directories
+      end
+    end
   end
 
   ##

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

--- trunk/core/src/test/java/org/codehaus/waffle/context/pico/RubyScriptLoaderTest.java	2007-06-01 03:20:55 UTC (rev 125)
+++ trunk/core/src/test/java/org/codehaus/waffle/context/pico/RubyScriptLoaderTest.java	2007-06-01 04:25:05 UTC (rev 126)
@@ -11,8 +11,6 @@
 import org.junit.runner.RunWith;
 
 import javax.servlet.ServletContext;
-import java.util.HashSet;
-import java.util.Set;
 
 @RunWith(JMock.class)
 public class RubyScriptLoaderTest {
@@ -23,10 +21,8 @@
         final ServletContext servletContext = context.mock(ServletContext.class);
 
         context.checking(new Expectations() {{
-            one (servletContext).getResourcePaths("/WEB-INF/classes/ruby/");
-            Set<String> paths = new HashSet<String>();
-            paths.add("/WEB-INF/classes/ruby/fake_script.rb");
-            will(returnValue(paths));
+            one (servletContext).getInitParameter(RubyScriptLoader.RUBY_SCRIPT_PATH_KEY);
+            will(returnValue(null));
         }});
 
         String script =
@@ -47,8 +43,6 @@
 
         // 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")));
+        Assert.assertEquals(servletContext, JavaUtil.convertRubyToJava(runtime.evalScript("$arg2")));
     }
 }

Modified: trunk/examples/jruby-example/src/main/java/org/codehaus/waffle/example/jruby/JRubyRegistrar.java (125 => 126)

--- trunk/examples/jruby-example/src/main/java/org/codehaus/waffle/example/jruby/JRubyRegistrar.java	2007-06-01 03:20:55 UTC (rev 125)
+++ trunk/examples/jruby-example/src/main/java/org/codehaus/waffle/example/jruby/JRubyRegistrar.java	2007-06-01 04:25:05 UTC (rev 126)
@@ -20,4 +20,9 @@
 
         picoRegistrar.registerRubyScript("foobar", "FooBar"); // register the controller!
     }
+
+    @Override
+    public void request() {
+        register(RubyScriptReloader.class);
+    }
 }

Added: trunk/examples/jruby-example/src/main/java/org/codehaus/waffle/example/jruby/RubyScriptReloader.java (0 => 126)

--- trunk/examples/jruby-example/src/main/java/org/codehaus/waffle/example/jruby/RubyScriptReloader.java	                        (rev 0)
+++ trunk/examples/jruby-example/src/main/java/org/codehaus/waffle/example/jruby/RubyScriptReloader.java	2007-06-01 04:25:05 UTC (rev 126)
@@ -0,0 +1,24 @@
+package org.codehaus.waffle.example.jruby;
+
+import org.jruby.Ruby;
+import org.codehaus.waffle.Startable;
+
+/**
+ * This is a temporary way of allowing scripts to be reloaded ... helpful for development only!
+ *
+ * TODO move this up and allow it to be auto registered depending on servlet context init params
+ */
+public class RubyScriptReloader implements Startable {
+    private final Ruby runtime;
+
+    public RubyScriptReloader(Ruby runtime) {
+        this.runtime = runtime;
+    }
+
+    public void start() {
+        runtime.evalScript("Waffle::ScriptLoader.load_from_file_system");
+    }
+
+    public void stop() {
+    }
+}

Modified: trunk/examples/jruby-example/src/main/webapp/WEB-INF/web.xml (125 => 126)

--- trunk/examples/jruby-example/src/main/webapp/WEB-INF/web.xml	2007-06-01 03:20:55 UTC (rev 125)
+++ trunk/examples/jruby-example/src/main/webapp/WEB-INF/web.xml	2007-06-01 04:25:05 UTC (rev 126)
@@ -5,56 +5,63 @@
          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
          xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
 
-    <display-name>Waffle JRuby Example</display-name>
+  <display-name>Waffle JRuby Example</display-name>
 
-    <!-- 1. This is how an application registers its custom Registrar -->
-    <context-param>
-        <param-name>org.codehaus.waffle.registrar.Registrar</param-name>
-        <param-value>org.codehaus.waffle.example.jruby.JRubyRegistrar</param-value>
-    </context-param>
 
-    <!-- Ruby Specifics components -->
-    <context-param>
-        <param-name>org.codehaus.waffle.context.ContextContainerFactory</param-name>
-        <param-value>org.codehaus.waffle.context.pico.RubyAwarePicoContextContainerFactory</param-value>
-    </context-param>
-    <context-param>
-        <param-name>org.codehaus.waffle.bind.DataBinder</param-name>
-        <param-value>org.codehaus.waffle.bind.RubyDataBinder</param-value>
-    </context-param>
-    <context-param>
-        <param-name>org.codehaus.waffle.controller.ControllerDefinitionFactory</param-name>
-        <param-value>org.codehaus.waffle.controller.RubyControllerDefinitionFactory</param-value>
-    </context-param>
 
-    <listener>
-        <listener-class>org.codehaus.waffle.context.pico.PicoWaffleContextListener</listener-class>
-    </listener>
+  <!-- 1. This is how an application registers its custom Registrar -->
+  <context-param>
+    <param-name>org.codehaus.waffle.registrar.Registrar</param-name>
+    <param-value>org.codehaus.waffle.example.jruby.JRubyRegistrar</param-value>
+  </context-param>
 
-    <!-- 4. Waffle request filter (responsible for request level context) -->
-    <filter>
-        <filter-name>WaffleRequestFilter</filter-name>
-        <filter-class>org.codehaus.waffle.context.WaffleRequestFilter</filter-class>
-    </filter>
-    <filter-mapping>
-        <filter-name>WaffleRequestFilter</filter-name>
-        <url-pattern>*.waffle</url-pattern>
-    </filter-mapping>
+  <!-- Ruby Specifics components -->
+  <context-param>
+    <param-name>org.codehaus.waffle.context.ContextContainerFactory</param-name>
+    <param-value>org.codehaus.waffle.context.pico.RubyAwarePicoContextContainerFactory</param-value>
+  </context-param>
+  <context-param>
+    <param-name>org.codehaus.waffle.bind.DataBinder</param-name>
+    <param-value>org.codehaus.waffle.bind.RubyDataBinder</param-value>
+  </context-param>
+  <context-param>
+    <param-name>org.codehaus.waffle.controller.ControllerDefinitionFactory</param-name>
+    <param-value>org.codehaus.waffle.controller.RubyControllerDefinitionFactory</param-value>
+  </context-param>
 
-    <!-- 5. Register Waffle's FrontController servlet -->
-    <servlet>
-        <servlet-name>waffle</servlet-name>
-        <servlet-class>org.codehaus.waffle.servlet.WaffleServlet</servlet-class>
-        <load-on-startup>1</load-on-startup>
-    </servlet>
-    
-    <servlet-mapping>
-        <servlet-name>waffle</servlet-name>
-        <url-pattern>*.waffle</url-pattern>
-    </servlet-mapping>
-        
-    <welcome-file-list>
-        <welcome-file>index.html</welcome-file>
-    </welcome-file-list>
+  <context-param>
+    <param-name>org.codehaus.waffle.ruby.path</param-name>
+    <param-value>dir:/Users/mward/development/waffle/examples/jruby-example/src/main/ruby/ruby/</param-value><!-- this needs to be more forgiving -->
+  </context-param>
 
+  <listener>
+    <listener-class>org.codehaus.waffle.context.pico.PicoWaffleContextListener</listener-class>
+  </listener>
+
+  <!-- 4. Waffle request filter (responsible for request level context) -->
+  <filter>
+    <filter-name>WaffleRequestFilter</filter-name>
+    <filter-class>org.codehaus.waffle.context.WaffleRequestFilter</filter-class>
+  </filter>
+  <filter-mapping>
+    <filter-name>WaffleRequestFilter</filter-name>
+    <url-pattern>*.waffle</url-pattern>
+  </filter-mapping>
+
+  <!-- 5. Register Waffle's FrontController servlet -->
+  <servlet>
+    <servlet-name>waffle</servlet-name>
+    <servlet-class>org.codehaus.waffle.servlet.WaffleServlet</servlet-class>
+    <load-on-startup>1</load-on-startup>
+  </servlet>
+
+  <servlet-mapping>
+    <servlet-name>waffle</servlet-name>
+    <url-pattern>*.waffle</url-pattern>
+  </servlet-mapping>
+
+  <welcome-file-list>
+    <welcome-file>index.html</welcome-file>
+  </welcome-file-list>
+
 </web-app>


To unsubscribe from this list please visit:

http://xircles.codehaus.org/manage_email

Reply via email to