Presentation is loading. Please wait.

Presentation is loading. Please wait.

Groovy in 15 minutes… Johannes Carlén Callista Enterprise AB

Similar presentations


Presentation on theme: "Groovy in 15 minutes… Johannes Carlén Callista Enterprise AB"— Presentation transcript:

1 Groovy in 15 minutes… Johannes Carlén Callista Enterprise AB johannes.carlen@callista.se

2 CADEC2006, Groovy, Slide 2 Copyright 2006, Callista Enterprise AB Today… Building systems requires broad and deep skills Complex applications requires a complex platform But… same mechanisms used for building simpler applications as for more complex.

3 CADEC2006, Groovy, Slide 3 Copyright 2006, Callista Enterprise AB PHP with MySQL

4 CADEC2006, Groovy, Slide 4 Copyright 2006, Callista Enterprise AB Java keeping up… Integrating existing script languages Beanshell Building a new script lanugage - Groovy

5 CADEC2006, Groovy, Slide 5 Copyright 2006, Callista Enterprise AB What´s Groovy? -Standard - JSR #241 -A scripting language that is tightly integrated into the Java platform. -A dynamic, agile OO language for the JVM -Full access to the Java API -Groovy Scripts can be compiled into Java bytecode -Static or dynamic typing

6 CADEC2006, Groovy, Slide 6 Copyright 2006, Callista Enterprise AB Java public class Utils { public static void main(String[] args) { List list = new ArrayList (); list.add("Dupond"); list.add("Dupont"); list.add("Tintin"); List filteredList = Utils.findAll("Dup", list); for (String name : filteredList) { System.out.println(name); } public static List findAll(String filter, List items) { List result = new ArrayList (); for (String item : items) { if (item.contains(filter)) { result.add(item); } return result; }

7 CADEC2006, Groovy, Slide 7 Copyright 2006, Callista Enterprise AB Groovy list = ["Dupond","Dupont","Tintin"] duponts = list.findAll { it.contains("Dup") } duponts.each { println it }

8 CADEC2006, Groovy, Slide 8 Copyright 2006, Callista Enterprise AB Strings GStrings name = ”Tintin" message = ”Hello ${name}” Multiline someXML = ””” Tintin ”””

9 CADEC2006, Groovy, Slide 9 Copyright 2006, Callista Enterprise AB Collections - list list = [1,2,3] longerlist = list + [”four”,”five”] longerlist now equals [1,2,3,”four”,”five”] indexing : list[2..3] equals [3,”four”] list[-1] equals ”five”

10 CADEC2006, Groovy, Slide 10 Copyright 2006, Callista Enterprise AB Collections - Map niceMap = ["tintin":true,"Rastapopulous":false] println niceMap[”tintin”] > True niceMap.calculus = true Println niceMap > ["tintin":true,"Rastapopulous":false,”calculus”:true]

11 CADEC2006, Groovy, Slide 11 Copyright 2006, Callista Enterprise AB Closures def list = [1,2,3] def square = { numberToSquare -> numberToSquare*numberToSquare } def squaredlist = list.collect (square) squaredlist equals [1,4,9] newlist.each { println it } 1 4 9

12 CADEC2006, Groovy, Slide 12 Copyright 2006, Callista Enterprise AB Example Extract data from a database table into XML

13 CADEC2006, Groovy, Slide 13 Copyright 2006, Callista Enterprise AB def sql = groovy.sql.Sql.newInstance( "jdbc:hsqldb:hsql://localhost/groovy", "sa","","org.hsqldb.jdbcDriver") def xml = new groovy.xml.MarkupBuilder() xml.product_catalogue() { sql.eachRow("select * from product") { row -> xml.product() { name(row.name) description(row.description) } From database into XML

14 CADEC2006, Groovy, Slide 14 Copyright 2006, Callista Enterprise AB The XML iPod mp3 player with video MacBook Pro Intel based Apple

15 CADEC2006, Groovy, Slide 15 Copyright 2006, Callista Enterprise AB What about the other way? def file = new java.io.File("/groovy/products.xml") def products = new groovy.util.XmlParser().parseText(file.getText()) def sql = groovy.sql.Sql.newInstance( "jdbc:hsqldb:hsql://localhost/groovy", "sa","", "org.hsqldb.jdbcDriver") products.each { product -> sql.execute("insert into product values (?,?)”, [product.name.text(),product.description.text()]) }

16 CADEC2006, Groovy, Slide 16 Copyright 2006, Callista Enterprise AB Unit testing JUnit built into runtime => script your tests for Groovy and Java classes with Groovy syntax Groovy provides several new assertions Easily scripted with Ant or Maven Integrate Groovy unit tests with your existing suite

17 CADEC2006, Groovy, Slide 17 Copyright 2006, Callista Enterprise AB Unit testing class StringSplitTest extends GroovyTestCase { void testFullSplit() { splitArray = StringSplitter.split( "groovy.util.GroovyTestCase", ".") expect = ["groovy", "util", ”GroovyTestCase"].toArray() assertArrayEquals(expect, splitAr) }

18 CADEC2006, Groovy, Slide 18 Copyright 2006, Callista Enterprise AB Other features Ant Scripting, Templates Groovlets, GSP, Swing, SWT, XMLRPC GroovyBeans class Product { @Property String name @Property String description }

19 CADEC2006, Groovy, Slide 19 Copyright 2006, Callista Enterprise AB Grails ”Coding by convention”-paradigm A toolkit of Spring, Hibernate, SiteMesh… Smaller applications –forums, blogs etc.

20 CADEC2006, Groovy, Slide 20 Copyright 2006, Callista Enterprise AB What else can you do? Configuration Simple tasks Prototypes Building and testing Agile development Rules for rules engines ESB transformations …

21 CADEC2006, Groovy, Slide 21 Copyright 2006, Callista Enterprise AB Q&A johannes.carlen@callista.se http://www.callista.se/enterprise


Download ppt "Groovy in 15 minutes… Johannes Carlén Callista Enterprise AB"

Similar presentations


Ads by Google