Nick Sieger: Tag merb http://blog.nicksieger.com/articles/tag/merb?tag=merb en-us 40 Introducing JRuby-Rack <p>Continuing in the spirit of Conference&#45;Driven Development, I&#8217;m happy to announce the first public release of <a href="http://wiki.jruby.org/wiki/JRuby_Rack">JRuby&#45;Rack</a>! You can use it to run Rails, Merb, or any Rack&#45;compatible application inside a Java application server&#46;</p> <p>Also released today is <a href="http://caldersphere.rubyforge.org/warbler/">Warbler 0&#46;9&#46;9</a>, which has been updated to bundle JRuby&#45;Rack&#46;</p> <p>In addition to providing as seamless a connection as possible between the servlet environment and Rack, JRuby&#45;Rack (along with Warbler) is also bridging the gap between Ruby and Java web development&#46; Some of the things it does are:</p> <ul> <li>Makes the Java servlet context and servlet request available to Ruby through special variables in the Rack environment</li> <li>Servlet request attributes from Java are passed through and available in the Rack environment&#46; Request attributes can override Rack variables such as <code>PATH_INFO</code>, <code>QUERY_STRING</code> etc&#46;</li> <li>Configures Rails deployment options such as page caching directories and session handling automatically and optimally for the servlet environment&#46;</li> </ul> <p>I&#8217;ve also included the beginnings of some extensions that should help integrate Rails with existing Java web frameworks, servlets, JSPs, and other code&#46; For example, you can invoke a Rails request from within a JSP with a tag:</p> <div class="typocode"><pre><code class="typocode_xml "><span class="punct">&lt;</span><span class="namespace">jruby-rack</span><span class="punct">:</span><span class="tag">rails</span> <span class="attribute">path</span><span class="punct">=&quot;</span><span class="string">/projects/activity</span><span class="punct">&quot;</span> <span class="attribute">params</span><span class="punct">=&quot;</span><span class="string">layout=none</span><span class="punct">&quot;/&gt;</span></code></pre></div> <p>You can set servlet and session attributes and forward to other servlets and JSPs from your Rails controllers:</p> <div class="typocode"><pre><code class="typocode_ruby "><span class="keyword">class </span><span class="class">DemoController</span> <span class="punct">&lt;</span> <span class="constant">ApplicationController</span> <span class="keyword">def </span><span class="method">index</span> <span class="ident">servlet_request</span><span class="punct">[&quot;</span><span class="string">hello</span><span class="punct">&quot;]</span> <span class="punct">=</span> <span class="punct">&quot;</span><span class="string">world!</span><span class="punct">&quot;</span> <span class="ident">session</span><span class="punct">[&quot;</span><span class="string">rails</span><span class="punct">&quot;]</span> <span class="punct">=</span> <span class="punct">&quot;</span><span class="string">Visible to java!</span><span class="punct">&quot;</span> <span class="ident">forward_to</span> <span class="punct">&quot;</span><span class="string">/attributes.jsp</span><span class="punct">&quot;</span> <span class="keyword">end</span> <span class="keyword">end</span></code></pre></div> <p>and read them from within the servlet or JSP:</p> <div class="typocode"><pre><code class="typocode_xml "><span class="punct">&lt;</span><span class="tag">dl</span><span class="punct">&gt;</span> <span class="punct">&lt;</span><span class="tag">dt</span><span class="punct">&gt;&lt;</span><span class="tag">tt</span><span class="punct">&gt;</span>servlet_request[&quot;hello&quot;] | request.getAttribute(&quot;hello&quot;)<span class="punct">&lt;/</span><span class="tag">tt</span><span class="punct">&gt;&lt;/</span><span class="tag">dt</span><span class="punct">&gt;</span> <span class="punct">&lt;</span><span class="tag">dd</span><span class="punct">&gt;&lt;%=</span> <span class="attribute">request.getAttribute(</span><span class="punct">&quot;</span><span class="string">hello</span><span class="punct">&quot;)</span> %<span class="punct">&gt;&lt;/</span><span class="tag">dd</span><span class="punct">&gt;</span> <span class="punct">&lt;</span><span class="tag">dt</span><span class="punct">&gt;&lt;</span><span class="tag">tt</span><span class="punct">&gt;</span>session[&quot;rails&quot;] | session.getAttribute(&quot;rails&quot;)<span class="punct">&lt;/</span><span class="tag">tt</span><span class="punct">&gt;&lt;/</span><span class="tag">dt</span><span class="punct">&gt;</span> <span class="punct">&lt;</span><span class="tag">dd</span><span class="punct">&gt;&lt;%=</span> <span class="attribute">session.getAttribute(</span><span class="punct">&quot;</span><span class="string">rails</span><span class="punct">&quot;)</span> %<span class="punct">&gt;&lt;/</span><span class="tag">dd</span><span class="punct">&gt;</span> <span class="punct">&lt;/</span><span class="tag">dl</span><span class="punct">&gt;</span></code></pre></div> <p>This is just the beginning of this kind of integration, and I&#8217;m interested where people take it&#46; I think this provides a nifty way to start integrating Rails bits into existing applications or reuse existing Java web application code&#46;</p> <p>I&#8217;ve tagged the release with an 0&#46;9 version number&#46; I believe the bits are ready for serious use, but could use some help pounding out a few more bugs before calling it 1&#46;0&#46; So <code>jruby -S gem install warbler</code> today, try it out, and bring plenty of feedback to the <a href="http://xircles.codehaus.org/lists/user@jruby.codehaus.org">JRuby user list</a>!</p> Thu, 08 May 2008 17:31:00 +0000 urn:uuid:dbf694f6-1c82-46b8-bffb-ee00a82f8390 Nick Sieger http://blog.nicksieger.com/articles/2008/05/08/introducing-jruby-rack jruby rails rack merb http://blog.nicksieger.com/articles/trackback/398