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() th...
public class FooTest {    static Foo foo;    public FooTest() {    }    @BeforeClass    public static void setUpClass() th...
public class FooTest {    static Foo foo;    public FooTest() {    }    @BeforeClass    public static void setUpClass() th...
What if…You didn’t have towrite in Java?
JRubyA Ruby languageimplementation on JVM10 years (+ 1 day) sincethe first recorded commitSupporting Rails sinceMay, 2006La...
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 andGla...
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)U...
Rackhttp://rack.rubyforge.org/Specifications for how Ruby-based webapplications should behavejruby-rack 1.0.10 (based on ra...
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 = Bow...
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 fo...
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..0d9981...
Cucumber testStand by while Tomcat finishes booting...Using the default profile..............................................
diff --git a/pom.xml b/pom.xmlindex 9e22f83..0810701 100644--- a/pom.xml+++ b/pom.xml@@ -211,6 +211,18 @@                 ...
diff --git a/src/main/webapp/WEB-INF/web.xml b/src/main/webapp/WEB-INF/web.xmlindex 8d02684..60ed6cb 100644--- a/src/main/...
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...
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   Sprin...
diff --git a/src/main/webapp/WEB-INF/web.xml b/src/main/webapp/WEB-INF/web.xmlindex 60ed6cb..f64b34d 100644--- a/src/main/...
Large refactoringFull-on Rails development
Large refactoringChange directory structure; move files fromWEB-INF/* to Rails directory structureIn Rails initializer, cre...
Large refactoringConfigure warbler to ensure necessary files areincluded in WAR file
TMTOWTDI                           <html>                             <head>                             </head>          ...
Questions?          Twitter @hiro_asari      G+ http://gplus.to/hiroasariLinkedIn http://linkedin.com/in/hiroasari        ...
Obligado!          Twitter @hiro_asari      G+ http://gplus.to/hiroasariLinkedIn http://linkedin.com/in/hiroasari         ...
Upcoming SlideShare
Loading in...5
×

Spring into rails

1,565

Published on

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

Published in: Technology
0 Comments
1 Like
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total Views
1,565
On Slideshare
0
From Embeds
0
Number of Embeds
2
Actions
Shares
0
Downloads
0
Comments
0
Likes
1
Embeds 0
No embeds

No notes for slide

Transcript of "Spring into rails"

  1. 1. Spring into RailsHiro AsariEngine Yard, Inc.
  2. 2. Imagine…Maintaining enterpriseweb application writtenin Java…
  3. 3. Imagine…
  4. 4. 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(…, …); }}
  5. 5. 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()); }}
  6. 6. 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) ? }}
  7. 7. What if…You didn’t have towrite in Java?
  8. 8. JRubyA Ruby languageimplementation on JVM10 years (+ 1 day) sincethe first recorded commitSupporting Rails sinceMay, 2006Latest release is 1.6.4 (22August, 2011)
  9. 9. JRuby Jam Session 15:00 BST 15 Setembro, 2011 http://pages.engineyard.com/JRubyInDepth.html
  10. 10. 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
  11. 11. Application ServersTrinidad (Tomcat-based)TorqueBox (JBoss-based)Kirk (Jetty-based)
  12. 12. $ 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
  13. 13. Rackhttp://rack.rubyforge.org/Specifications for how Ruby-based webapplications should behavejruby-rack 1.0.10 (based on rack 1.3.2)
  14. 14. Pet Clinic Example database- backed web app for Spring framework
  15. 15. Enterprise softwareLong-running projects with legacy codebases Easy maintenance is important Testing is critical
  16. 16. RefactoringWrite test first
  17. 17. RSpechttp://relishapp.com/rspecBehavior-driven development tool for Ruby
  18. 18. 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
  19. 19. Cucumberhttp://cukes.infoTesting framework for describing softwarebehavior in plain English
  20. 20. 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"
  21. 21. http://bit.ly/refactoring-to-rails
  22. 22. Small refactoringGive Ruby a foot in the door
  23. 23. Requests/ /vets Java/ Spring App
  24. 24. Requests JRuby/ /vets /rack/ Java/ Spring App
  25. 25. 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>
  26. 26. 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
  27. 27. 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>
  28. 28. 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).
  29. 29. 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
  30. 30. Medium refactoringLayer on the Rails
  31. 31. Requests JRuby/ /vets /rack/ Java/ Spring App
  32. 32. Requests /owners Java// /vets /owners /1/pets Spring App
  33. 33. Requests/ /vets JRuby /owners Java// /vets /owners /1/pets Spring App
  34. 34. 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>
  35. 35. Large refactoringFull-on Rails development
  36. 36. 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.
  37. 37. Large refactoringConfigure warbler to ensure necessary files areincluded in WAR file
  38. 38. TMTOWTDI <html> <head> </head> JSP <body> ### </body>Mixing JSP and </html>JRuby output + <div> Sinatra <h1>App</h1> </div>
  39. 39. 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
  40. 40. 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

×