- Revision
- 154
- Author
- mward
- Date
- 2007-06-16 23:50:59 -0500 (Sat, 16 Jun 2007)
Log Message
initial checkin of ERB spike
Modified Paths
- trunk/core/core.iml
- trunk/examples/jruby-example/jruby-example.iml
- trunk/examples/jruby-example/src/main/java/org/codehaus/waffle/example/jruby/JRubyRegistrar.java
- trunk/examples/jruby-example/src/main/ruby/ruby/foo_bar.rb
- trunk/examples/jruby-example/src/main/webapp/WEB-INF/web.xml
- trunk/examples/jruby-example/src/test/java/org/codehaus/waffle/example/jruby/RubyRegistrarTest.java
Added Paths
Diff
Modified: trunk/core/core.iml (153 => 154)
--- trunk/core/core.iml 2007-06-13 13:11:29 UTC (rev 153) +++ trunk/core/core.iml 2007-06-17 04:50:59 UTC (rev 154) @@ -153,7 +153,7 @@ <orderEntry type="module-library"> <library> <CLASSES> - <root url="" /> + <root url="" /> </CLASSES> <JAVADOC /> <SOURCES>
Added: trunk/core/src/main/java/org/codehaus/waffle/servlet/RhtmlServlet.java (0 => 154)
--- trunk/core/src/main/java/org/codehaus/waffle/servlet/RhtmlServlet.java (rev 0) +++ trunk/core/src/main/java/org/codehaus/waffle/servlet/RhtmlServlet.java 2007-06-17 04:50:59 UTC (rev 154) @@ -0,0 +1,73 @@ +package org.codehaus.waffle.servlet; + +import org.codehaus.waffle.WaffleException; +import org.codehaus.waffle.context.RequestLevelContainer; +import org.jruby.Ruby; +import org.jruby.RubyModule; +import org.jruby.javasupport.JavaEmbedUtils; +import org.jruby.runtime.builtin.IRubyObject; + +import javax.servlet.ServletException; +import javax.servlet.http.HttpServlet; +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; +import java.io.BufferedReader; +import java.io.IOException; +import java.io.InputStream; +import java.io.InputStreamReader; + +public class RhtmlServlet extends HttpServlet { + + @Override + protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { + + System.out.println(request.getRequestURI()); // TODO determine what the requested rhtml is + String template = loadRhtml("hello.rhtml"); + + Ruby runtime = RequestLevelContainer.get().getComponentInstanceOfType(Ruby.class); + + runtime.evalScript("require 'erb'\n"); + RubyModule module = runtime.getClassFromPath("ERB"); + + IRubyObject erb = (IRubyObject) JavaEmbedUtils.invokeMethod(runtime, module, "new", + new Object[] {JavaEmbedUtils.javaToRuby(runtime, template)}, IRubyObject.class); // cache? + + // Need to include ERB::Util on whats being bound + // TODO bind the controller ... not self + runtime.evalScript("self.send :include, ERB::Util"); + + String out = (String)JavaEmbedUtils.invokeMethod(runtime, erb, "result", + new Object[] {(Object) runtime.evalScript("self.send :binding")}, String.class); + + response.getOutputStream().println(out); + response.getOutputStream().flush(); + } + + private String loadRhtml(String fileName) { + BufferedReader bufferedReader = null; + InputStream inputStream = null; + + try { + inputStream = getServletContext().getResourceAsStream(fileName); + 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(); + } + + return 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/examples/jruby-example/jruby-example.iml (153 => 154)
--- trunk/examples/jruby-example/jruby-example.iml 2007-06-13 13:11:29 UTC (rev 153) +++ trunk/examples/jruby-example/jruby-example.iml 2007-06-17 04:50:59 UTC (rev 154) @@ -56,7 +56,7 @@ <orderEntry type="module-library"> <library> <CLASSES> - <root url="" /> + <root url="" /> </CLASSES> <JAVADOC /> <SOURCES /> @@ -65,7 +65,7 @@ <orderEntry type="module-library"> <library> <CLASSES> - <root url="" /> + <root url="" /> </CLASSES> <JAVADOC /> <SOURCES /> @@ -74,7 +74,7 @@ <orderEntry type="module-library"> <library> <CLASSES> - <root url="" /> + <root url="" /> </CLASSES> <JAVADOC /> <SOURCES /> @@ -158,6 +158,11 @@ </containerElement> <containerElement type="library" level="module"> <attribute name="method" value="1" /> + <attribute name="URI" value="/WEB-INF/lib/jruby-complete-1.0.jar" /> + <url>jar://$M2_REPOSITORY$/org/jruby/jruby-complete/1.0/jruby-complete-1.0.jar!/</url> + </containerElement> + <containerElement type="library" level="module"> + <attribute name="method" value="1" /> <attribute name="URI" value="/WEB-INF/lib/jruby-complete-1.0RC3.jar" /> <url>jar://$M2_REPOSITORY$/org/jruby/jruby-complete/1.0RC3/jruby-complete-1.0RC3.jar!/</url> </containerElement>
Added: trunk/examples/jruby-example/resources/hello.rhtml (0 => 154)
--- trunk/examples/jruby-example/resources/hello.rhtml (rev 0) +++ trunk/examples/jruby-example/resources/hello.rhtml 2007-06-17 04:50:59 UTC (rev 154) @@ -0,0 +1,14 @@ +<html> + <head><title>Ruby Toys -- </title></head> + <body> + + <%= self.inspect %> + <%= h('<[EMAIL PROTECTED]') %> + + <% (1..10).each do |i| %> + <%= i %> + <% end %> + +Works + </body> +</html> \ No newline at end of file
Modified: trunk/examples/jruby-example/src/main/java/org/codehaus/waffle/example/jruby/JRubyRegistrar.java (153 => 154)
--- trunk/examples/jruby-example/src/main/java/org/codehaus/waffle/example/jruby/JRubyRegistrar.java 2007-06-13 13:11:29 UTC (rev 153) +++ trunk/examples/jruby-example/src/main/java/org/codehaus/waffle/example/jruby/JRubyRegistrar.java 2007-06-17 04:50:59 UTC (rev 154) @@ -28,6 +28,7 @@ registerInstance(List.class, myList); picoRegistrar.registerRubyScript("foobar", "FooBar"); // register the controller! + picoRegistrar.registerRubyScript("hello", "HelloController"); // register the controller! picoRegistrar.registerRubyScript("person", "PersonController"); // register the controller! }
Modified: trunk/examples/jruby-example/src/main/ruby/ruby/foo_bar.rb (153 => 154)
--- trunk/examples/jruby-example/src/main/ruby/ruby/foo_bar.rb 2007-06-13 13:11:29 UTC (rev 153) +++ trunk/examples/jruby-example/src/main/ruby/ruby/foo_bar.rb 2007-06-17 04:50:59 UTC (rev 154) @@ -1,4 +1,3 @@ - class FooBar def index @@ -19,8 +18,8 @@ session[:bar] = 'foo' session[:baz] = 'foo' p session - begin - %{ + + %{ HELLO WORLD from the index method look up from pico: #{find_chicago} parameters: #{parameters} @@ -38,11 +37,8 @@ #{cls = Java::JavaClass.for_name('java.util.Vector')} #{locate(Java::JavaClass.for_name('java.util.List'))} + } - } - rescue Exception => e - "ERROR #{e}" - end end def bar
Added: trunk/examples/jruby-example/src/main/ruby/ruby/hello.rb (0 => 154)
--- trunk/examples/jruby-example/src/main/ruby/ruby/hello.rb (rev 0) +++ trunk/examples/jruby-example/src/main/ruby/ruby/hello.rb 2007-06-17 04:50:59 UTC (rev 154) @@ -0,0 +1,7 @@ +class HelloController + + def index + render("hello.rhtml") + end + +end \ No newline at end of file
Modified: trunk/examples/jruby-example/src/main/webapp/WEB-INF/web.xml (153 => 154)
--- trunk/examples/jruby-example/src/main/webapp/WEB-INF/web.xml 2007-06-13 13:11:29 UTC (rev 153) +++ trunk/examples/jruby-example/src/main/webapp/WEB-INF/web.xml 2007-06-17 04:50:59 UTC (rev 154) @@ -53,11 +53,21 @@ <servlet-class>org.codehaus.waffle.servlet.WaffleServlet</servlet-class> <load-on-startup>1</load-on-startup> </servlet> + <servlet> + <servlet-name>rhtml</servlet-name> + <servlet-class>org.codehaus.waffle.servlet.RhtmlServlet</servlet-class> + <load-on-startup>1</load-on-startup> + </servlet> <servlet-mapping> <servlet-name>waffle</servlet-name> <url-pattern>*.waffle</url-pattern> </servlet-mapping> + + <servlet-mapping> + <servlet-name>rhtml</servlet-name> + <url-pattern>*.rhtml</url-pattern> + </servlet-mapping> <welcome-file-list> <welcome-file>index.html</welcome-file>
Modified: trunk/examples/jruby-example/src/test/java/org/codehaus/waffle/example/jruby/RubyRegistrarTest.java (153 => 154)
--- trunk/examples/jruby-example/src/test/java/org/codehaus/waffle/example/jruby/RubyRegistrarTest.java 2007-06-13 13:11:29 UTC (rev 153) +++ trunk/examples/jruby-example/src/test/java/org/codehaus/waffle/example/jruby/RubyRegistrarTest.java 2007-06-17 04:50:59 UTC (rev 154) @@ -3,6 +3,8 @@ import junit.framework.TestCase; import org.jruby.Ruby; import org.jruby.RubyArray; +import org.jruby.RubyModule; +import org.jruby.javasupport.JavaEmbedUtils; import org.jruby.javasupport.JavaUtil; import org.jruby.runtime.builtin.IRubyObject; @@ -103,5 +105,101 @@ System.out.println("javaObj = " + javaObj); } + public void testJavaObjectExposesFieldsAsInstanceVariables() { + Ruby runtime = Ruby.getDefaultInstance(); + String script = + "require 'erb'\n" + + "require 'java'\n" + + "include_class java.util.ArrayList\n" + + "f = ArrayList.new\n" + + "f.instance_variable_get(:@java_object)"; + + IRubyObject rubyObject = runtime.evalScript(script); + + System.out.println("rubyObject = " + rubyObject); + + RubyModule erb = runtime.getClassFromPath("ERB"); + System.out.println("erb = " + erb); + + IRubyObject iRubyObject = (IRubyObject) JavaEmbedUtils.invokeMethod(runtime, erb, "new", + new Object[] {JavaEmbedUtils.javaToRuby(runtime, "<html>foobar <%= @java_object %></html>")}, IRubyObject.class); + + String o = (String)JavaEmbedUtils.invokeMethod(runtime, iRubyObject, "result", new Object[] {runtime.evalScript("f.send :binding")}, String.class); + System.out.println("o = " + o); + } + + public void testRubyErb() { + String script = + "require \"erb\"\n" + + "\n" + + " # Build template data class.\n" + + " class Product\n" + + " def initialize( code, name, desc, cost)\n" + + " @code = code\n" + + " @name = name\n" + + " @desc = desc\n" + + " @cost = cost\n" + + "\n" + + " @features = [ ]\n" + + " end\n" + + "\n" + + " def add_feature( feature )\n" + + " @features << feature\n" + + " end\n" + + "\n" + + " # ...\n" + + " end\n" + + "template = %{\n" + + " <html>\n" + + " <head><title>Ruby Toys -- <%= @name %></title></head>\n" + + " <body>\n" + + "\n" + + " <h1><%= @name %> (<%= @code %>)</h1>\n" + + " <p><%= @desc %></p>\n" + + "\n" + + " <ul>\n" + + " <% @features.each do |f| %>\n" + + " <li><b><%= f %></b></li>\n" + + " <% end %>\n" + + " </ul>\n" + + "\n" + + " <p>\n" + + " <% if @cost < 10 %>\n" + + " <b>Only <%= @cost %>!!!</b>\n" + + " <% else %>\n" + + " Call for a price, today!\n" + + " <% end %>\n" + + " </p>\n" + + "\n" + + " </body>\n" + + " </html>\n" + + " }.gsub(/^ /, '')\n" + + "\n" + + "rhtml = ERB.new(template)\n" + + "# Set up template data.\n" + + " toy = Product.new( \"TZ-1002\",\n" + + " \"Rubysapien\",\n" + + " \"Geek's Best Friend! Responds to Ruby commands...\",\n" + + " 999.95 )\n" + + " toy.add_feature(\"Listens for verbal commands in the Ruby language!\")\n" + + " toy.add_feature(\"Ignores Perl, Java, and all C variants.\")\n" + + " toy.add_feature(\"Karate-Chop Action!!!\")\n" + + " toy.add_feature(\"Matz signature on left leg.\")\n" + + " toy.add_feature(\"Gem studded eyes... Rubies, of course!\")\n" + + "\n" + + " # Produce result.\n" + + "rhtml.run(toy.send(:binding))"; + + Ruby runtime = Ruby.getDefaultInstance(); + runtime.evalScript(script); + } + + + + + + + + }
To unsubscribe from this list please visit:
