• Save
Spring into rails
Upcoming SlideShare
Loading in...5
×
 

Spring into rails

on

  • 1,883 views

QCon São Paulo 2011 presentation on converting a Spring MVC application into Rails.

QCon São Paulo 2011 presentation on converting a Spring MVC application into Rails.

Statistics

Views

Total Views
1,883
Views on SlideShare
1,878
Embed Views
5

Actions

Likes
1
Downloads
0
Comments
0

2 Embeds 5

http://us-w1.rockmelt.com 4
http://www.linkedin.com 1

Accessibility

Categories

Upload Details

Uploaded via as Adobe PDF

Usage Rights

© All Rights Reserved

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Processing…
Post Comment
Edit your comment

Spring into rails Spring into rails Document Transcript

  • Spring into RailsHiro AsariEngine Yard, Inc.
  • Imagine…Maintaining enterpriseweb application writtenin Java…
  • Imagine…
  • public class FooTest { static Foo foo; public FooTest() { } @BeforeClass public static void setUpClass() throws Exception { } @AfterClass public static void tearDownClass() throws Exception { } @Before public void setUp() { foo = new Foo(); } @Test public void testReturn42() { assertEquals(…, …); }}
  • public class FooTest { static Foo foo; public FooTest() { } @BeforeClass public static void setUpClass() throws Exception { } @AfterClass public static void tearDownClass() throws Exception { } @Before public void setUp() { foo = new Foo(); } @Test public void testReturn42() { assertEquals(42, foo.return42()); }}
  • public class FooTest { static Foo foo; public FooTest() { } @BeforeClass public static void setUpClass() throws Exception { } @AfterClass public static void tearDownClass() throws Exception { } @Before public void setUp() { foo = new Foo(); } @Test public void testReturn42() { assertEquals(42, foo.return42()); # or is it assertEquals(foo.return42(), 42) ? }}
  • What if…You didn’t have towrite in Java?
  • JRubyA Ruby languageimplementation on JVM10 years (+ 1 day) sincethe first recorded commitSupporting Rails sinceMay, 2006Latest release is 1.6.4 (22August, 2011)
  • JRuby Jam Session 15:00 BST 15 Setembro, 2011 http://pages.engineyard.com/JRubyInDepth.html
  • WarblerFrom a Ruby/Rails/Merb/Rack application,create a WAR file foruse with Javaapplication containersuch as Tomcat andGlassfish http://www.pbase.com/wwcsig/image/59657656
  • Application ServersTrinidad (Tomcat-based)TorqueBox (JBoss-based)Kirk (Jetty-based)
  • $ jruby -S bundle installUsing backports (2.0.3)Using builder (3.0.0)⋮Using cucumber (0.10.2)Using cucumber-rails (0.4.0)Using database_cleaner (0.6.6)Using launchy (1.0.0) from https://github.com/copiousfreetime/launchy.git (at v1.0.0)Using monkey-lib (0.5.4)Using rspec-core (2.1.0)Using rspec-expectations (2.1.0)Using rspec-mocks (2.1.0)Using rspec (2.1.0)Using rspec-rails (2.1.0)Using tilt (1.2.2)Using sinatra (1.2.1)Using sinatra-sugar (0.5.0)Using sinatra-advanced-routes (0.5.1)Using sinatra-reloader (0.5.0)Using spoon (0.0.1)Using bundler (1.0.17)Your bundle is complete! It was installed into ./bundler
  • Rackhttp://rack.rubyforge.org/Specifications for how Ruby-based webapplications should behavejruby-rack 1.0.10 (based on rack 1.3.2)
  • Pet Clinic Example database- backed web app for Spring framework
  • Enterprise softwareLong-running projects with legacy codebases Easy maintenance is important Testing is critical
  • RefactoringWrite test first
  • RSpechttp://relishapp.com/rspecBehavior-driven development tool for Ruby
  • RSpec# bowling_spec.rbrequire bowlingdescribe Bowling, "#score" do it "returns 0 for all gutter game" do bowling = Bowling.new 20.times { bowling.hit(0) } bowling.score.should == 0 endend
  • Cucumberhttp://cukes.infoTesting framework for describing softwarebehavior in plain English
  • CucumberFeature: Pets Scenario: Edit Pet Given I am on the owners search page And I press "Find Owners" And I follow "George Franklin" And I follow "Edit Pet" When I fill in "Name" with "Leoni" And press "Update Pet" Then I should see "Leoni"
  • http://bit.ly/refactoring-to-rails
  • Small refactoringGive Ruby a foot in the door
  • Requests/ /vets Java/ Spring App
  • Requests JRuby/ /vets /rack/ Java/ Spring App
  • Cucumber testdiff --git a/src/main/webapp/WEB-INF/jsp/vets.jsp b/src/main/webapp/WEB-INF/jsp/vets.jspindex cff2154..0d99817 100644--- a/src/main/webapp/WEB-INF/jsp/vets.jsp+++ b/src/main/webapp/WEB-INF/jsp/vets.jsp@@ -23,7 +23,7 @@ <table class="table-buttons"> <tr> <td>- <a href="<spring:url value="/vets.xml" htmlEscape="true" />">View as XML</a>+ <a href="<spring:url value="/rack/vets.xml" htmlEscape="true" />">View as XML</a> </td> </tr> </table>
  • Cucumber testStand by while Tomcat finishes booting...Using the default profile...............................................................F-.............(::) failed steps (::)expected: /xml/, got: "text/html;charset=utf-8" (using =~)Diff:@@ -1,2 +1,2 @@-/xml/+text/html;charset=utf-8 (RSpec::Expectations::ExpectationNotMetError)org/jruby/RubyProc.java:268:in `call./features/step_definitions/xml_json_steps.rb:12:in `(root):in `/^I should seean XML document$/features/vets.feature:6:in `Then I should see an XML documentFailing Scenarios:cucumber features/vets.feature:3 # Scenario: View vets as XML13 scenarios (1 failed, 12 passed)75 steps (1 failed, 1 skipped, 73 passed)0m7.709srake aborted!Cucumber failed
  • diff --git a/pom.xml b/pom.xmlindex 9e22f83..0810701 100644--- a/pom.xml+++ b/pom.xml@@ -211,6 +211,18 @@ <scope>provided</scope> </dependency>+ <!-- JRuby and JRuby-Rack -->+ <dependency>+ <groupId>org.jruby</groupId>+ <artifactId>jruby-complete</artifactId>+ <version>1.6.0</version>+ </dependency>+ <dependency>+ <groupId>org.jruby.rack</groupId>+ <artifactId>jruby-rack</artifactId>+ <version>1.0.8</version>+ </dependency>+ <!-- Test dependencies --> <dependency> <groupId>org.junit</groupId>
  • diff --git a/src/main/webapp/WEB-INF/web.xml b/src/main/webapp/WEB-INF/web.xmlindex 8d02684..60ed6cb 100644--- a/src/main/webapp/WEB-INF/web.xml+++ b/src/main/webapp/WEB-INF/web.xml@@ -87,6 +87,21 @@ <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener>++ <listener>+ <listener-class>org.jruby.rack.RackServletContextListener</listener-class>+ </listener>++ <servlet>+ <servlet-name>rack</servlet-name>+ <servlet-class>org.jruby.rack.RackServlet</servlet-class>+ </servlet>++ <servlet-mapping>+ <servlet-name>rack</servlet-name>+ <url-pattern>/rack/*</url-pattern>+ </servlet-mapping>+ <!-- Defines the default servlet (usually for service static resources).
  • diff --git a/src/main/webapp/WEB-INF/lib/app.rb b/src/main/webapp/WEB-INF/lib/app.rbindex 6ab5b3c..4398fb4 100644--- a/src/main/webapp/WEB-INF/lib/app.rb+++ b/src/main/webapp/WEB-INF/lib/app.rb@@ -1,6 +1,33 @@ require builder require erb+require spring_helpers++helpers do+ include Spring+end get /rack/ do <h1>Sinatra</h1> end++get /rack/vets.xml do+ content_type application/vnd.petclinic+xml+ builder do |xml|+ xml.instruct!+ xml.vets do+ clinic.vets.each do |vet|+ xml.vetList do+ xml.id vet.id+ xml.firstName vet.firstName+ xml.lastName vet.lastName+ vet.specialties.each do |spec|+ xml.specialties do+ xml.id spec.id+ xml.name spec.name+ end+ end+ end+ end+ end+ end+end
  • Medium refactoringLayer on the Rails
  • Requests JRuby/ /vets /rack/ Java/ Spring App
  • Requests /owners Java// /vets /owners /1/pets Spring App
  • Requests/ /vets JRuby /owners Java// /vets /owners /1/pets Spring App
  • diff --git a/src/main/webapp/WEB-INF/web.xml b/src/main/webapp/WEB-INF/web.xmlindex 60ed6cb..f64b34d 100644--- a/src/main/webapp/WEB-INF/web.xml+++ b/src/main/webapp/WEB-INF/web.xml@@ -92,16 +92,6 @@ <listener-class>org.jruby.rack.RackServletContextListener</listener-class> </listener>- <servlet>- <servlet-name>rack</servlet-name>- <servlet-class>org.jruby.rack.RackServlet</servlet-class>- </servlet>-- <servlet-mapping>- <servlet-name>rack</servlet-name>- <url-pattern>/rack/*</url-pattern>- </servlet-mapping>- <!-- Defines the default servlet (usually for service static resources).@@ -162,6 +152,16 @@ <url-pattern>/</url-pattern> </servlet-mapping>+ <filter>+ <filter-name>RackFilter</filter-name>+ <filter-class>org.jruby.rack.RubyFirstRackFilter</filter-class>+ </filter>++ <filter-mapping>+ <filter-name>RackFilter</filter-name>+ <url-pattern>/*</url-pattern>+ </filter-mapping>+ <filter> <filter-name>httpMethodFilter</filter-name> <filter-class>org.springframework.web.filter.HiddenHttpMethodFilter</filter-class>
  • Large refactoringFull-on Rails development
  • Large refactoringChange directory structure; move files fromWEB-INF/* to Rails directory structureIn Rails initializer, create a Spring context to bootthe Spring appEnsure classpath includes Spring jars.
  • Large refactoringConfigure warbler to ensure necessary files areincluded in WAR file
  • TMTOWTDI <html> <head> </head> JSP <body> ### </body>Mixing JSP and </html>JRuby output + <div> Sinatra <h1>App</h1> </div>
  • Questions? Twitter @hiro_asari G+ http://gplus.to/hiroasariLinkedIn http://linkedin.com/in/hiroasari asari.ruby@gmail.com hasari@engineyard.com http://github.com/banzaiman http://www.flickr.com/photos/42033648@N00/372887164
  • Obligado! Twitter @hiro_asari G+ http://gplus.to/hiroasariLinkedIn http://linkedin.com/in/hiroasari asari.ruby@gmail.com hasari@engineyard.com http://github.com/banzaiman http://www.flickr.com/photos/42033648@N00/372887164