Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

Groovy & Grails: Scripting for Modern Web Applications


Published on

Dynamic scripting languages are a powerful addition to a software designer’s toolbox. Rails/Ruby and Python have not gained much acceptance in the enterprise. Grails and Groovy are an attempt to bridge the gap between the modern scripting world and the Enterprise Java world.

This talk is an introduction towards building web applications in Grails. First we will go about creating a REST based webservice. We will also show how to replace the default database backend of Grails with MySQL.

We will then build a web application that consumes this webservice. The emphasis will be on the design patterns and idioms in Grails that address the web application development lifecycle.

Published in: Technology

Groovy & Grails: Scripting for Modern Web Applications

  1. 1. Groovy & Grails Scripting for Modern Web Applications Rohit Nayak Talentica Software
  2. 2. Agenda <ul><li>Demo: Quick intro to Grails </li></ul><ul><li>Scripting, Web Applications and Grails/Groovy </li></ul><ul><li>REST service in Grails </li></ul><ul><ul><li>Demo </li></ul></ul><ul><ul><li>Internals </li></ul></ul><ul><li>Web Client in Grails </li></ul><ul><ul><li>Demo </li></ul></ul><ul><ul><li>Internals </li></ul></ul><ul><li>Perspective </li></ul>
  3. 3. Demo Quick Intro to Grails
  4. 4. Maggi <ul><li>grails create-app Maggi </li></ul><ul><li>Domain class: Noodle, Packaging </li></ul><ul><li>grails generate-all Noodle, Packaging </li></ul><ul><li>grails run-app </li></ul>
  5. 5. Web Frameworks with Scripting <ul><li>Ruby on Rails (2004) </li></ul><ul><li>CakePHP (2005) </li></ul><ul><li>Django / Python (2005) </li></ul><ul><li>Groovy on Grails (2006) </li></ul>
  6. 6. Power of these frameworks <ul><li>Baked Experience </li></ul><ul><li>The Language </li></ul><ul><li>Agility / Productivity </li></ul>
  7. 7. Baked Experience <ul><li>Model View Controller </li></ul><ul><li>Object-Relational Mapping </li></ul><ul><li>Templates </li></ul><ul><li>Layout </li></ul><ul><li>URL rewriting </li></ul><ul><li>Ajax support </li></ul><ul><li>XML / JSON support </li></ul>
  8. 8. The Language <ul><li>Dynamic </li></ul><ul><li>More expressive code </li></ul><ul><li>Smaller code </li></ul><ul><li>Native support for Lists, Hashmaps </li></ul><ul><li>Lang. support for IO, Net, XML </li></ul><ul><li>Idioms for common usage </li></ul>
  9. 9. Agile <ul><li>Scaffolding </li></ul><ul><li>Unit tests </li></ul><ul><li>No build cycles </li></ul><ul><li>Built-in webservers </li></ul><ul><li>Fail faster! </li></ul>
  10. 10. (Nov 2nd)
  11. 11. (Nov 2nd)
  12. 12. <ul><li>public class HelloWorld { </li></ul><ul><li>String name; </li></ul><ul><li>public void setName(String name) { this .name = name; } </li></ul><ul><li>public String getName() { return name; } </li></ul><ul><li>public String hello() { return “Hello “ + name; } </li></ul><ul><li>public static void main(String args[]) { </li></ul><ul><li>HelloWorld helloWorld = new HelloWorld(); </li></ul><ul><li>helloWorld.setName( “Java” ); </li></ul><ul><li>System.out.println( helloWorld. hello() ); </li></ul><ul><li>} </li></ul><ul><li>} </li></ul>
  13. 13. HelloWorld.groovy <ul><li>class HelloWorld { </li></ul><ul><li>String name </li></ul><ul><li>def hello() { &quot;Hello $name&quot; } </li></ul><ul><li>} </li></ul><ul><li>def helloWorld = new HelloWorld(name : &quot; Groovy&quot; ) </li></ul><ul><li>println helloWorld.hello() </li></ul>
  14. 14. Key Groovy Features <ul><li>Java-like syntax </li></ul><ul><li>Complements Java </li></ul><ul><li>Object-oriented </li></ul><ul><li>Targets Java VM (JSR-241) </li></ul><ul><ul><li>Invoke Java class within Groovy </li></ul></ul><ul><ul><li>Invoke Groovy class within Java </li></ul></ul><ul><li>Dynamic </li></ul><ul><li>Scripting (JSR-223) </li></ul><ul><li>Brevity </li></ul>
  15. 15. Brevity <ul><li>Optional semicolons, package prefixes </li></ul><ul><li>Automatic imports (java.util.*,*,*, groovy…) </li></ul><ul><li>GroovyBeans (generated accessors) </li></ul><ul><li>Optional typing </li></ul><ul><li>Optional return </li></ul>
  16. 16. Groovy Gravy <ul><li>GStrings: ”$book.title: $ (${book.reviewers.length})” </li></ul><ul><li>Regular expressions: assert ‘12345’ =~ /d+/ </li></ul><ul><li>Only objects: primitives converted to Reference Types </li></ul><ul><li>Lists: def list = [1, 2, 'hello', new java.util.Date()] </li></ul><ul><li>Maps: def map = ['name':‘Indic Threads', 'location':‘Pune'] </li></ul><ul><li>Closures [1,2,3].collect {it*2} ===> [2, 4, 6] </li></ul><ul><li>String literals – single, double, triple quotes </li></ul>
  17. 17. Closures <ul><li>Anonymous block of statements </li></ul><ul><li>First class objects </li></ul><ul><li>Parameters including default values </li></ul><ul><li>Return value </li></ul><ul><li>Binds variables in definition scope </li></ul>
  18. 18. Pluggable Behavior - Groovy <ul><li>def sum(num, closure) </li></ul><ul><li>{ </li></ul><ul><li>def sum=0 </li></ul><ul><li>1.upto(num) { </li></ul><ul><li>if (closure(it)) sum+=it </li></ul><ul><li>} </li></ul><ul><li>} </li></ul><ul><li>total=sum(10){it%2==0} </li></ul><ul><li>println &quot;Total: ${total}&quot; </li></ul>
  19. 19. Pluggable Behavior - Java <ul><li>public static int sum(int num, Algo algo) </li></ul><ul><li>{ </li></ul><ul><li>int val=0; </li></ul><ul><li>for (int i=1; i<=n, i++) </li></ul><ul><li>{ </li></ul><ul><li>if (algo.eval(i)) val+=i; </li></ul><ul><li>} </li></ul><ul><li>return val; </li></ul><ul><li>} </li></ul><ul><li>public static void main (String[] args) </li></ul><ul><li>{ </li></ul><ul><li>int total; </li></ul><ul><li>total = sum(10, new Algo() { </li></ul><ul><ul><ul><li>public boolean eval(int i) </li></ul></ul></ul><ul><ul><ul><li>{ </li></ul></ul></ul><ul><ul><ul><li>return i%2==0; </li></ul></ul></ul><ul><ul><ul><li>} </li></ul></ul></ul><ul><ul><li>} </li></ul></ul><ul><li>}); </li></ul><ul><li>System.out.println(&quot;Total: &quot; + total); </li></ul><ul><li>} </li></ul>
  20. 20. Dynamic Programming <ul><li>Add methods, properties to classes/objects at run-time </li></ul><ul><li>Mixins to inject behaviour </li></ul><ul><ul><ul><li>class ListUtils { static foo(List list) { &quot;foo called&quot; } } List.metaClass.mixin ListUtils assert &quot;foo called&quot; == [1, 2, 3].foo() </li></ul></ul></ul><ul><li>Can extend class field-access mechanism </li></ul><ul><li>Dynamic method invocation </li></ul><ul><ul><li> myObj.”$action”() </li></ul></ul>
  21. 21. Poolster <ul><li>Online “football pools” application </li></ul><ul><li>Entities: Game, User </li></ul><ul><li>Game  Stake, Option, Ends </li></ul><ul><li>To join User chooses an Option </li></ul><ul><li>REST Webservice backend </li></ul><ul><li>Clients: iPhone, Grails, Android, Silverlight </li></ul>
  22. 22. Demo The Poolster Webservice
  23. 23. Grails – Philosophy <ul><li>Convention over Configuration </li></ul><ul><ul><li>Magic directories </li></ul></ul><ul><ul><li>Implicit table names, column names </li></ul></ul><ul><li>Don’t Repeat Yourself </li></ul><ul><ul><li>Database maps to model class </li></ul></ul><ul><ul><li>hasMany defines relationship & declares variable </li></ul></ul><ul><ul><li>Layout, form validations </li></ul></ul><ul><li>Lightweight </li></ul><ul><ul><li>Modify and F5 </li></ul></ul><ul><li>Strong shoulders </li></ul><ul><ul><li>Spring (Grails MVC, DI, Transactions) </li></ul></ul><ul><ul><li>Hibernate (GORM) </li></ul></ul><ul><ul><li>Ant, JUnit, SiteMesh, log4j, OSCache </li></ul></ul>
  24. 24. Grails – Key Features <ul><li>Database mapping, relations </li></ul><ul><li>MySQL integration </li></ul><ul><li>URL Mapping </li></ul><ul><li>Authentication / Filters </li></ul><ul><li>Logging </li></ul>
  25. 25. Demo Poolster Web Client
  26. 26. Grails Web Application <ul><li>Rest Service Proxy </li></ul><ul><li>Custom Tag libraries </li></ul><ul><li>Sitemesh layout </li></ul><ul><li>Templates </li></ul>
  27. 27. Unseen Gravy <ul><li>JUnit test cases, Mocking/Stubbing </li></ul><ul><li>Web testing with Canoo Webtest </li></ul><ul><li>Bootstrapping </li></ul><ul><li>Pagination </li></ul>
  28. 28. Cons <ul><li>Learning curve </li></ul><ul><li>Performance ?! </li></ul><ul><li>Early adopter issues </li></ul><ul><li>Dynamic/Functional programming </li></ul>
  29. 29. Scripted In Groovy <ul><li>Canoo WebTest </li></ul><ul><li>Tellurium </li></ul><ul><li>Ant / Maven config files </li></ul><ul><li>SoapUI script step </li></ul><ul><li>Spring beans </li></ul>
  30. 30. Invoking Groovy Scripts <ul><li>import; </li></ul><ul><li>import groovy.lang.Binding; </li></ul><ul><li>import groovy.util.GroovyScriptEngine; </li></ul><ul><li>public class ScriptEngineEmbedGroovy{ </li></ul><ul><li>public static void main(String args[]) throws Throwable{ </li></ul><ul><li>String[] paths = {&quot;C:roovy&quot;}; </li></ul><ul><li>GroovyScriptEngine gse = new GroovyScriptEngine(paths); </li></ul><ul><li>Binding binding = new Binding(); </li></ul><ul><li>Object[] var1 = 123; </li></ul><ul><li>binding.setVariable(&quot;args&quot;,var1); </li></ul><ul><li>String ret =“DoSomething.groovy&quot;, binding); </li></ul><ul><li>} </li></ul><ul><li>} </li></ul>
  31. 31. Groovy Script Eval <ul><li>import javax.script.ScriptEngine; </li></ul><ul><li>import javax.script.ScriptEngineManager; </li></ul><ul><li>public class CalcMain { </li></ul><ul><li>public static void main(String[] args) throws Exception { </li></ul><ul><li>ScriptEngineManager factory = new ScriptEngineManager(); </li></ul><ul><li>ScriptEngine engine = factory.getEngineByName(&quot;groovy&quot;); </li></ul><ul><li>// basic example </li></ul><ul><li>System.out.println(engine.eval(&quot;(1..10).sum()&quot;)); //  55 </li></ul><ul><li>// example showing scripting variables </li></ul><ul><li>engine.put(&quot;first&quot;, &quot;HELLO&quot;); </li></ul><ul><li>engine.put(&quot;second&quot;, &quot;world&quot;); </li></ul><ul><li>System.out.println(engine.eval(&quot;first.toLowerCase() + second.toUpperCase()&quot;)); //  helloWORLD </li></ul><ul><li>} </li></ul><ul><li>} </li></ul>
  32. 32. Using Grails <ul><li>Web UI Flow Prototyping </li></ul><ul><li>REST Service Prototyping </li></ul><ul><li>Supporting Web Apps </li></ul><ul><li>Internal Web Apps </li></ul>
  33. 33. Using Groovy <ul><li>Glue together existing components </li></ul><ul><li>Reengineering legacy components </li></ul><ul><li>Externalizing business logic, rules </li></ul><ul><li>Domain Specific Languages </li></ul><ul><li>Smart Configuration </li></ul><ul><li>Scripting language of choice </li></ul>
  34. 34. Getting Started <ul><li> </li></ul><ul><li> </li></ul><ul><li>Free e-books </li></ul><ul><ul><li>Beginning Groovy and Grails (Apress) </li></ul></ul><ul><ul><li>Getting started with Grails (InfoQ) </li></ul></ul><ul><li> cheat sheets </li></ul><ul><li> Mastering Grails, Practically Groovy </li></ul>
  35. 35. Thanks