Nick Sieger: Tag rack http://blog.nicksieger.com/articles/tag/rack?tag=rack en-us 40 Introducing JRuby-Rack <p>Continuing in the spirit of Conference-Driven Development, I&#8217;m happy to announce the first public release of <a href="http://wiki.jruby.org/wiki/JRuby_Rack">JRuby-Rack</a>! You can use it to run Rails, Merb, or any Rack-compatible application inside a Java application server.</p> <p>Also released today is <a href="http://caldersphere.rubyforge.org/warbler/">Warbler 0.9.9</a>, which has been updated to bundle JRuby-Rack.</p> <p>In addition to providing as seamless a connection as possible between the servlet environment and Rack, JRuby-Rack (along with Warbler) is also bridging the gap between Ruby and Java web development. 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. Request attributes can override Rack variables such as <code>PATH_INFO</code>, <code>QUERY_STRING</code> etc.</li> <li>Configures Rails deployment options such as page caching directories and session handling automatically and optimally for the servlet environment.</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. 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. I think this provides a nifty way to start integrating Rails bits into existing applications or reuse existing Java web application code.</p> <p>I&#8217;ve tagged the release with an 0.9 version number. I believe the bits are ready for serious use, but could use some help pounding out a few more bugs before calling it 1.0. 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 Blog Setup <p>The other day <a href="http://blog.nicksieger.com/articles/2008/07/06/this-blog-powered-by-glassfish-jruby-and-jruby-rack#comments">several people chimed in</a> wondering how I set up this blog with JRuby and Glassfish&#46; One of the reasons I didn&#8217;t include the details in the post is that it&#8217;s not really much different than any JRuby/Glassfish/Warbler deployment, but in case you don&#8217;t know what that looks like, here are the basics&#46;</p> <h3>Preconditions (Java)</h3> <p>I&#8217;m running on a <a href="http://joyent.com/accelerator">Joyent Accelerator</a>, which runs <a href="http://opensolaris.org/">OpenSolaris</a>, which has JDK 6 installed by default&#46; If you&#8217;re running on some flavor of Linux, hopefully <a href="https://jdk-distros.dev.java.net/">there&#8217;s a package available for you to install</a>, otherwise you may have to <a href="http://www.java.com/en/download/help/5000010500.xml">download a self&#45;extracting binary</a>&#46;</p> <h3><a href="https://glassfish.dev.java.net/downloads/v2ur2-b04.html">Install Glassfish</a></h3> <p>This step is actually straightforward; not at all as problematic as you might expect of a piece of Java technology! In the parent directory where you want Glassfish to be installed (substituting the name of the Glassfish jar you downloaded as appropriate):</p> <pre><code>java -Xmx256m -jar glassfish-installer-v2ur2-b04-sunos_x86.jar cd glassfish chmod -R +x lib/ant/bin ./lib/ant/bin/ant -f setup.xml </code></pre> <p>Start Glassfish&#46;</p> <pre><code>./bin/asadmin start-domain </code></pre> <p>You may want to add <code>GLASSFISH/bin</code> to your path so that you can run the Glassfish <code>asadmin</code> command from anywhere&#46;</p> <p>On Solaris, <a href="http://www.sun.com/bigadmin/content/selfheal/smf-quickstart.jsp">SMF</a> is the subsystem that is used to ensure services are started at boot time (among other things)&#46; Glassfish <a href="http://blogs.sun.com/bloggerkedar/entry/app_server_and_solaris_10">works nicely with SMF</a>&#46; On other systems, there may be <code>/etc/rc.d</code> init scripts out there, or you can roll your own (<code>asadmin start-domain</code> and <code>asadmin stop-domain</code>)&#46;</p> <h3>Install JRuby</h3> <p><a href="http://dist.codehaus.org/jruby/jruby-bin-1.1.2.tar.gz">Download JRuby</a> and <a href="http://wiki.jruby.org/wiki/Getting_Started">unpack it somewhere</a>&#46; I recommend adding <code>JRUBY_HOME/bin</code> to the end of your path, so it doesn&#8217;t clash with Matz&#45;Ruby&#46;</p> <h3>Install Warbler and activerecord&#45;jdbcmysql&#45;adapter</h3> <p>In addition to Warbler, I&#8217;m using the activerecord&#45;jdbcmysql&#45;adapter to connect to the blog&#8217;s database&#46; Both can be installed with Rubygems:</p> <pre><code>jruby -S gem install warbler activerecord-jdbcmysql-adapter </code></pre> <p>With Rails 2 and up, the application&#8217;s <code>config/database.yml</code> file should be updated for <code>adapter: jdbcmysql</code>:</p> <pre><code>&lt;% jdbc = defined?(JRUBY_VERSION) ? 'jdbc' : '' %&gt; development: adapter: &lt;%= jdbc %&gt;mysql encoding: utf8 database: testapp_development username: root password: socket: /tmp/mysql.sock # same for test/production... </code></pre> <p>Otherwise, you need to <a href="http://wiki.jruby.org/wiki/Running_Rails_with_ActiveRecord-JDBC">jump through some extra environment&#46;rb configuration hoops</a>&#46;</p> <h3>Configure Warbler</h3> <p>Warbler needs to be told about any gems that your application uses&#46; To generate a Warbler configuration file:</p> <pre><code>jruby -S warble config </code></pre> <p>The file is generated at <code>config/warble.rb</code>&#46; In it, modify the following sections:</p> <pre><code>config.gems = ["activerecord-jdbcmysql-adapter"] ... config.webxml.jruby.min.runtimes = 2 config.webxml.jruby.max.runtimes = 4 </code></pre> <h3>Build and deploy the &#46;war</h3> <pre><code>jruby -S warble asadmin deploy --contextroot / blog.war </code></pre> <p>(<code>--contextroot /</code> makes the application rooted at <code>/</code> in the server, rather than at <code>/blog</code> which would be the default&#46;)</p> <p>At this point, the blog application is up and running on port 8080&#46; I had previously been running the blog with an Apache/&#46;htaccess&#45;based setup reverse&#45;proxying to mongrel, so all I had to do was change the port&#46; I haven&#8217;t touched it since&#46;</p> <h3>But is this right for you?</h3> <p>Chances are, this setup is overkill for a simple blog&#46; If you&#8217;re going to try it, I&#8217;d recommend at minimum running on a VPS with at least 1G of memory&#46; But once you get the core pieces in place, updating and re&#45;deploying the application is really just as simple as the last two commands&#46; It&#8217;s mundane and boring in its simplicity&#46; But boring is good when you don&#8217;t want to worry about having to keep Mongrel running, or max out the memory in your server and make it unstable&#46;</p> Thu, 10 Jul 2008 05:10:27 +0000 urn:uuid:e7399075-a1ca-4af7-b122-ede5647c4fcc Nick Sieger http://blog.nicksieger.com/articles/2008/07/10/blog-setup jruby rails warbler rack http://blog.nicksieger.com/articles/trackback/431