47
Colorado Software Summit: October 21 – 26, 2007 Eric Schreiner – Groovy: Getting the Power Out of Groovy Slide 1 © Copyright 2007, Contecon Software GmbH Groovy Getting the Power Out of Groovy Eric Schreiner Managing Director Contecon Software GmbH

Groovy - Software Summitsoftwaresummit.com/2007/speakers/presentations/SchreinerGroovyAdvanced.pdfColorado Software Summit: October 21 – 26, 2007 Eric Schreiner – Groovy: Getting

  • Upload
    others

  • View
    1

  • Download
    0

Embed Size (px)

Citation preview

Colorado Software Summit: October 21 – 26, 2007

Eric Schreiner – Groovy: Getting the Power Out of Groovy Slide 1

© Copyright 2007, Contecon Software GmbH

GroovyGetting the Power Out of Groovy

Eric SchreinerManaging DirectorContecon Software GmbH

Colorado Software Summit: October 21 – 26, 2007

Eric Schreiner – Groovy: Getting the Power Out of Groovy Slide 2

© Copyright 2007, Contecon Software GmbH

Other Groovy related sessions

Eric Schreiner

Groovy

A Language Introduction for Java Programmers

Scott Davis

Introduction to Grails

During the presentation: If you have questions – please ask

Colorado Software Summit: October 21 – 26, 2007

Eric Schreiner – Groovy: Getting the Power Out of Groovy Slide 3

© Copyright 2007, Contecon Software GmbH

Prerequisites

Basic understanding of Groovy

or first attend the session

“Groovy – A language introduction for

Java Programmers,”

Understanding of Java would be

helpful.

Colorado Software Summit: October 21 – 26, 2007

Eric Schreiner – Groovy: Getting the Power Out of Groovy Slide 4

© Copyright 2007, Contecon Software GmbH

Conventions used in this presentation

Blue italic will be used for keywords and

operators in the text (not in example

code)

All class names for Java classes used in

my examples will start with a capital J to

differ them from Groovy

Additional example code is indicated in

green at the bottom of the slides. Grey

indicates that we will skip the sample

during the presentation (time is limited)

Colorado Software Summit: October 21 – 26, 2007

Eric Schreiner – Groovy: Getting the Power Out of Groovy Slide 5

© Copyright 2007, Contecon Software GmbH

Agenda

Embedding Groovy in a Java

Application

Working with builders

Groovy XML and Soap

Groovlets – The Groovy Servlet

Gui Support (Swing Builder)

Groovy SQL (GSQL)

Colorado Software Summit: October 21 – 26, 2007

Eric Schreiner – Groovy: Getting the Power Out of Groovy Slide 6

© Copyright 2007, Contecon Software GmbH

Definitions

WikipediaGroovy is an object-oriented programming language for

the Java Platform as an alternative to the Java

programming language. It can be viewed as a scripting

language for the Java Platform, as it has features similar

to those of Python, Ruby, Perl, and Smalltalk. In some

contexts, the name JSR 241 is used as an alternate

identifier for the Groovy language.

Codehaus.orgGroovy is like a super version of Java. It can leverage

Java's enterprise capabilities but also has cool

productivity features like closures, builders and dynamic

typing. If you are a developer, tester or script guru, you

have to love Groovy.

Colorado Software Summit: October 21 – 26, 2007

Eric Schreiner – Groovy: Getting the Power Out of Groovy Slide 7

© Copyright 2007, Contecon Software GmbH

Eclipse Setup for the examples

Plugin: org.codehaus.groovy_1.0.1

Compiler output location bin-groovy

Colorado Software Summit: October 21 – 26, 2007

Eric Schreiner – Groovy: Getting the Power Out of Groovy Slide 8

© Copyright 2007, Contecon Software GmbH

Embedding Groovy in a Java Application

Groovyc compiler(all sourcecode available before compilation)

GroovyShell

GroovyScriptEngine

JSR-223 Scripting for the Java(6) platform

Other options not covered here

Spring scripting support

Bean Scripting Framework (BSF)

GroovyClassLoader

Meta programming

Colorado Software Summit: October 21 – 26, 2007

Eric Schreiner – Groovy: Getting the Power Out of Groovy Slide 9

© Copyright 2007, Contecon Software GmbH

Classpath

Just embed groovy-all-major.minor.jar

e.g. groovy-all-1.0.jar

Easiest way to integrate

Found in the embeddable directory of

distribution

Or use .jar files from lib directory

More complex

May cause problems if your application is

using different versions of the same .jar files

At least a JRE 1.4 required

Colorado Software Summit: October 21 – 26, 2007

Eric Schreiner – Groovy: Getting the Power Out of Groovy Slide 10

© Copyright 2007, Contecon Software GmbH

Groovyc Example

Compile HelloWorld.groovy

Examine with jad

Call it from Java

HelloWorld.groovy, JCallHelloWorld.java

Colorado Software Summit: October 21 – 26, 2007

Eric Schreiner – Groovy: Getting the Power Out of Groovy Slide 11

© Copyright 2007, Contecon Software GmbH

Class GroovyShell

Simplest integration technique

Uses Binding for passing parameters

Just call the evaluate Method

evaluate(File file)

evaluate(InputStream in)

evaluate(InputStream in, String fileName)

evaluate(String script)

evaluate(String script, String fileName)

JGroovyShellExample.java, JGroovyShellBinding.java, JGroovyShellSwing.java

Colorado Software Summit: October 21 – 26, 2007

Eric Schreiner – Groovy: Getting the Power Out of Groovy Slide 12

© Copyright 2007, Contecon Software GmbH

Class GroovyShell 2

Generating dynamic classes

Can be used to generate classes (e.g. after

parsing an XML file)

The parse method of GroovyShell

Returns an instance of Script

Is more efficient because recompile is not

required

JGroovyShellDynamic.java, JGroovyShellParseExample.java

Colorado Software Summit: October 21 – 26, 2007

Eric Schreiner – Groovy: Getting the Power Out of Groovy Slide 13

© Copyright 2007, Contecon Software GmbH

Class GroovyShell 3

The run method

Unlike evaluate, it executes Scripts and

classes

if a main(Object[] args) or main(String[]

args) exists, it's executed

If the class extends GroovyTestCase, a

JUnit test runner executes it

If the class implements Runnable, it will be

constructed with a String[] or default

constructor. Then run() will be called.

Colorado Software Summit: October 21 – 26, 2007

Eric Schreiner – Groovy: Getting the Power Out of Groovy Slide 14

© Copyright 2007, Contecon Software GmbH

GroovyShell configuration

The class CompilerConfiguration

Optionally passed to the constructor of

GroovyShell

setScriptBaseClass()

see example

setClasspath()

used to customize the classpath

setSourceEncoding()

And many more

It's also possible to define a custom classloader

MyDebugScript.groovy, JGroovyShellConfigExample.java

Colorado Software Summit: October 21 – 26, 2007

Eric Schreiner – Groovy: Getting the Power Out of Groovy Slide 15

© Copyright 2007, Contecon Software GmbH

GroovyScriptEngine (GSE)

Most complete solution to embed

scripts in an application

The GSE automatically tracks

dependencies

If a Script has been modified, all

dependent scripts will be recompiled

and reloaded

MyEvaluator.groovy, JGroovyScriptEngineSwingExample.java

Colorado Software Summit: October 21 – 26, 2007

Eric Schreiner – Groovy: Getting the Power Out of Groovy Slide 16

© Copyright 2007, Contecon Software GmbH

JSR-223 integration

See http://java.sun.com/developer/technicalArticles/J2SE/Desktop/scripting/

javax.script.ScriptEngineManager()

getEngineByExtension(String extension)

getEngineByMineType(String mimeType)

getEngineByName(String shortName)

For Groovy it's “groovy”

Use the eval methods to run scriptObject eval(Reader reader)

Object eval(Reader reader, Bindings b)

Object eval(String scriptReader)

Object eval(String scriptReader, Bindings b)

See JSR-223 documentationfor more infos

Colorado Software Summit: October 21 – 26, 2007

Eric Schreiner – Groovy: Getting the Power Out of Groovy Slide 17

© Copyright 2007, Contecon Software GmbH

Meta programming or how the magic works

In Groovy every class implements the GroovyObject

interface

public Object invokeMethod(String name, Object args);

public Object getProperty(String property);

public void setProperty(String property, Object newValue);

public MetaClass getMetaClass();

public void setMetaClass(MetaClass metaClass);

If an ordinary Java class should be recognized as a

Groovy class, it has to implement the GroovyObject interface.

There is also the abstract class GroovyObjectSupport that can

be extended

Colorado Software Summit: October 21 – 26, 2007

Eric Schreiner – Groovy: Getting the Power Out of Groovy Slide 18

© Copyright 2007, Contecon Software GmbH

The MetaClass

Every GroovyObject has an association with MetaClass

MetaClass provides all meta-information about a Groovy

class

Methods

Fields

Properties

MetaClass also provides the Methods that do the real work

of method invocation

public abstract Object invokeConstructor(Object[] args);

public abstract Object invokeMethod(Object o, String

methodName, Object[] arguments);

public abstract Object invokeStaticMethod(Object object, String

methodName, Object[] arguments);

Colorado Software Summit: October 21 – 26, 2007

Eric Schreiner – Groovy: Getting the Power Out of Groovy Slide 19

© Copyright 2007, Contecon Software GmbH

The MetaClassRegistry

The MetaClass is stored and received from a

central Store – the MetaClassRegistry

MetaClassRegistry

MetaClass<<interface>>

GroovyObject

+ getMetaClass (class): MetaClass

+ setMetaClass (class, metaClass)

+ invokeMethod(...): Objekt

+ getMetaClass(...): MetaClass

+ invokeMethod(...): Objekt+ invokeStaticMethod(...): Objekt+ invokeConstructor(...): Objekt

*

Colorado Software Summit: October 21 – 26, 2007

Eric Schreiner – Groovy: Getting the Power Out of Groovy Slide 20

© Copyright 2007, Contecon Software GmbH

Method invocation and interception

Use GroovyInterceptable as a marker Interface

to indicate that we override invokeMethod

Overriding the invokeMethod method

Only works on Groovy Objects

Not really reusable when implemented as an

abstract class

Intercepting method calls with ProxyMetaClass

Also works on non Groovy Objects

Groovy Categories (http://groovy.codehaus.org/Groovy+Categories)

OverrideInvokeMethodExample.groovy, MethodProxyExample.groovy

JFileGetter.java, TypeFileContent.groovy

Colorado Software Summit: October 21 – 26, 2007

Eric Schreiner – Groovy: Getting the Power Out of Groovy Slide 21

© Copyright 2007, Contecon Software GmbH

BuildersThe Builder Pattern is a software design pattern. The intention is to separate the construction of a complex object from its representation

so that the same construction process can create different representations. See http://en.wikipedia.org/wiki/Builder_pattern

Used to create arbitrary nested tree

structures.

XML (MarkupBuilder, DomBuilder)

HTML (MarkupBuilder)

Swing (SwingBuilder)

Ant Task (AntBuilder)

Any hierarchical data (NodeBuilder)

Colorado Software Summit: October 21 – 26, 2007

Eric Schreiner – Groovy: Getting the Power Out of Groovy Slide 22

© Copyright 2007, Contecon Software GmbH

Builders - SVG Example<svg xmlns="http://www.w3.org/2000/svg" > <text x="20" y="30" style="font-size: 24px">Pet Shop sales</text> <g style="font-size: 12px; fill: #304080"> <rect x="100" y="80" width="139" height="15"/> <text x="20" y="90">Dogs</text> <rect x="100" y="110" width="80" height="15"/> <text x="20" y="120">Cats</text> <rect x="100" y="140" width="150" height="15"/> <text x="20" y="150">Mice</text> <rect x="100" y="170" width="30" height="15"/> <text x="20" y="180">Parrots</text> <rect x="100" y="200" width="190" height="15"/> <text x="20" y="210">Hamsters</text> </g> </svg>

Colorado Software Summit: October 21 – 26, 2007

Eric Schreiner – Groovy: Getting the Power Out of Groovy Slide 23

© Copyright 2007, Contecon Software GmbH

Builders - SVG Example

SVGxmlns="http://www.w3.org/2000/svg"

x="20" y="30" style="font-size: 24px"

Text – Petshop sales

style="font-size: 12px; fill: #304080"G

rect

text - dogs

text - cats

rect

text - mice

rect

text - parrots

rect

text - hamsters

rect

x="100" y="80" width="139" height="15"

x="20" y="90"

x="100" y="110" width="80" height="15"

x="20" y="120"

x="100" y="140" width="150" height="15"

x="20" y="150"

x="100" y="170" width="30" height="15"

x="20" y="180"

x="100" y="200" width="190" height="15"

x="20" y="210"

Colorado Software Summit: October 21 – 26, 2007

Eric Schreiner – Groovy: Getting the Power Out of Groovy Slide 24

© Copyright 2007, Contecon Software GmbH

Builders - SVG Example

import groovy.xml.MarkupBuilder

def values = [['Dogs',30],['Cats',50],['Mice',70],

['Parrots',10],['Hamsters',40] ]

def builder = new MarkupBuilder()

builder.svg(xmlns:"http://www.w3.org/2000/svg") {

text(x:20,y:30,style:"font-size: 24px", 'Pet shop sales')

g(style:"font-size: 12px; fill: #304080") {

def y=80

values.each{name, sales ->

rect(x:100, y:y, width:sales *3, height:15)

text(x:20, y:y+10, name)

y+=30

}

}

}

Nodes are created from 'pretended'method calls' on the builder.Method name determines node name

PetShopBuilderExample.groovy

Colorado Software Summit: October 21 – 26, 2007

Eric Schreiner – Groovy: Getting the Power Out of Groovy Slide 25

© Copyright 2007, Contecon Software GmbH

Builders - SVG Example

import groovy.xml.MarkupBuilder

def values = [['Dogs',30],['Cats',50],['Mice',70],

['Parrots',10],['Hamsters',40] ]

def builder = new MarkupBuilder()

builder.svg(xmlns:"http://www.w3.org/2000/svg") {

text(x:20,y:30,style:"font-size: 24px", 'Pet shop sales')

g(style:"font-size: 12px; fill: #304080") {

def y=80

values.each{name, sales ->

rect(x:100, y:y, width:sales *3, height:15)

text(x:20, y:y+10, name)

y+=30

}

}

}

Maps are passed as method argumentsand are used to create the node attributes

Colorado Software Summit: October 21 – 26, 2007

Eric Schreiner – Groovy: Getting the Power Out of Groovy Slide 26

© Copyright 2007, Contecon Software GmbH

Builders - SVG Example

import groovy.xml.MarkupBuilder

def values = [['Dogs',30],['Cats',50],['Mice',70],

['Parrots',10],['Hamsters',40] ]

def builder = new MarkupBuilder()

builder.svg(xmlns:"http://www.w3.org/2000/svg") {

text(x:20,y:30,style:"font-size: 24px", 'Pet shop sales')

g(style:"font-size: 12px; fill: #304080") {

def y=80

values.each{name, sales ->

rect(x:100, y:y, width:sales *3, height:15)

text(x:20, y:y+10, name)

y+=30

}

}

} Nesting of nodes is done with closuresClosures relay method calls to the builder

Colorado Software Summit: October 21 – 26, 2007

Eric Schreiner – Groovy: Getting the Power Out of Groovy Slide 27

© Copyright 2007, Contecon Software GmbH

NodeBuilder

Used to create nested object

hierarchies in memory

groovy.util.Node Methods

PetShopBuilderExample2.groovy

Type Method Purpose Shortcut

Object name() name of the node

Object value() node itself

Map attributes() The attributes as a map

Node parent() The parent node ..

List children() A list of children *

Iterator iterator() An iterator over all children

List depthFirst List of all nodes using depth-first traversal **

List breadthFirst() List of all nodes using breadth-first traversal

void print() pretty print as nested structure

more ....

Colorado Software Summit: October 21 – 26, 2007

Eric Schreiner – Groovy: Getting the Power Out of Groovy Slide 28

© Copyright 2007, Contecon Software GmbH

XML

Processing XML Documents

groovy.xml.DOMBuilder

returns org.w3.dom.Document

XmlParser

returns Node Object

XmlSlurper

returns GPathResult Object

better memory efficiency

Also other java technologies like SAX,

STAX, etc. can be integrated

Colorado Software Summit: October 21 – 26, 2007

Eric Schreiner – Groovy: Getting the Power Out of Groovy Slide 29

© Copyright 2007, Contecon Software GmbH

Comparison Node / GPathResult

groovy.util.Node (XmlParser) groovy.util.GPathResult (XmlSlurper)

Type Method Type Method Shortcut

Object name() String name()

String text() String text()

Object value()

void setValue(Object value)

Map attributes() Map attributes()

Object attribute(Object Key)

Node parent() GPathResult parent() ..

List children() GPathResult children() *

Iterator iterator() Iterator iterator()

List depthFirst Iterator depthFirst **

List breadthFirst() Iterator breadthFirst()

void print(PrintWriter out)

GPathResult parents()

List

int size

GPathResult find(Closure c)

GPathResult findAll(Closure c)

listgroovy.util.slurpersupport.Node

Colorado Software Summit: October 21 – 26, 2007

Eric Schreiner – Groovy: Getting the Power Out of Groovy Slide 30

© Copyright 2007, Contecon Software GmbH

XML Example<staff>

<location name="Frankfurt"><employee name="Klein, Henry" position="Manager" salary="10000"/><employee name="Rosenberg, Anna" position="Assistant" salary="5000"/><employee name="Schreiner, Karl" position="Sales" salary="7000"/><employee name="Koch, Robert" position="Sales" salary="8000"/>

</location><location name="Denver">

<employee name="Bush, Ronald" position="Manager" salary="11000"/><employee name="Fields, Henry" position="Assistant" salary="5000"/><employee name="Getty, Paul" position="Sales" salary="4000"/>

</location><location name="London">

<employee name="Poppins, Mary" position="Sales" salary="9000"/></location>

</staff>

GroovyXmlExample.groovy

Colorado Software Summit: October 21 – 26, 2007

Eric Schreiner – Groovy: Getting the Power Out of Groovy Slide 31

© Copyright 2007, Contecon Software GmbH

SwingBuilder

SwingExample1.groovy, SwingExample2.groovy

JFrametitle="CSS2007"

layout="borderLayout”

ContentPane

button

label

text="click me" action="count++"

text="clicked 5 times"

def frame =

swing.frame(title:'CSS2007') {

textlabel = label(text:"Clicked ${count} time(s).", constraints: BorderLayout.NORTH)

button(text:'Click Me',

actionPerformed: {clickCount++; textlabel.text = "Clicked ${clickCount} time(s).";},

constraints:BorderLayout.SOUTH)

}

Colorado Software Summit: October 21 – 26, 2007

Eric Schreiner – Groovy: Getting the Power Out of Groovy Slide 32

© Copyright 2007, Contecon Software GmbH

SwingBuilder

SwingBuilder uses Widgets to create

the component Tree

see documentation for details:

http://groovy.codehaus.org/Alphabetical+Widgets+List

can be extended with custom widgets

a lot of new functions since 1.1 Beta2

see: http://groovy.codehaus.org/Extending+Swing+Builder

Also more complex controls like Lists and

their models and views are supported

Colorado Software Summit: October 21 – 26, 2007

Eric Schreiner – Groovy: Getting the Power Out of Groovy Slide 33

© Copyright 2007, Contecon Software GmbH

Groovy Soap

GroovySoapSampleClient.groovy, GroovySoapSampleCalcServer.groovy,

GroovySoapSampleCalcClient.groovy (SoapUi client)

“SOAP is a lightweight protocol intended for exchanging

structured information in a decentralized, distributed

environment”

based on the Codehaus XFire framework

supports both server and client

additional jar file reqiredgroovysoap-all-1.0-0.3-snapshot_jdk1.5.0.jarmust be in classpath

still some limitationssee: http://groovy.codehaus.org/Groovy+SOAP

Colorado Software Summit: October 21 – 26, 2007

Eric Schreiner – Groovy: Getting the Power Out of Groovy Slide 34

© Copyright 2007, Contecon Software GmbH

Groovlets

Wikipedia: http://en.wikipedia.org/wiki/Servlet

The Java Servlet API allows a software developer to add dynamic content to a Web server using the Java platform...

You can write normal Java servlets in

Groovy

There is also a default implementation

with groovy.servlet.GroovyServlet

Supports

Groovlets (*.groovy)

Templates (*.gsp)

Colorado Software Summit: October 21 – 26, 2007

Eric Schreiner – Groovy: Getting the Power Out of Groovy Slide 35

© Copyright 2007, Contecon Software GmbH

Groovlets

webapps

myGroovlets

YourGroovlet.groovy

WEB-INF

web.xml

classes

groovy

lib

groovy-all-1.x.jar

<servlet>

<servlet-name>Groovy</servlet-name>

<servlet-class>groovy.servlet.GroovyServlet</servlet-class>

</servlet>

<servlet-mapping>

<servlet-name>Groovy</servlet-name>

<url-pattern>*.groovy</url-pattern>

</servlet-mapping>

Directory for the *.groovy files

<Context path ="/css2007"

reloadable="true"

docBase="D:\java\source\Groovy\Groovy Advanced CSS 2007\src"

workDir="D:\java\source\Groovy\Groovy Advanced CSS 2007\work" />

Tomcat Server.xml

HelloWorldGroovlet.groovy

Colorado Software Summit: October 21 – 26, 2007

Eric Schreiner – Groovy: Getting the Power Out of Groovy Slide 36

© Copyright 2007, Contecon Software GmbH

Information available to Groovlets

(the Groovlet binding)

Name Note Example usage

headers Map of HTTP request headers headers.host

params Map of HTTP request parameters params.myParam

sessions ServletSession, can be null session?.myParam

request HttpServletRequest request.remoteHost

response HttpServletResponse response.contentType='text/xml'

context ServletContext context.myParam

application application.myParam

out response.writer Lazy init, not in binding

sout response.outputStream Lazy init, not in binding

html Lazy init, not in binding

ServletContext (same as context)

Builder initialized as new MarkupBuilder

GroovyGroovletAnalyser.groovy,GroovyBingo.groovy

Colorado Software Summit: October 21 – 26, 2007

Eric Schreiner – Groovy: Getting the Power Out of Groovy Slide 37

© Copyright 2007, Contecon Software GmbH

Groovy SQL (GSQL)

Groovy/Java Application

Groovy SQL

JDBC API

JDBC DriverManager or DataSource

DatabaseServer

see: http://groovy.codehaus.org/GSQL

Based on JDBC

not a replacement for JDO or Hibernate

package groovy.sql

3 major Classes: Sql, GroovyResultSet, DataSet

Colorado Software Summit: October 21 – 26, 2007

Eric Schreiner – Groovy: Getting the Power Out of Groovy Slide 38

© Copyright 2007, Contecon Software GmbH

Connect to a Database

A connection to a Database is established

by providing a Connection through the

DriverManager (Sql.newInstance(...) methods )

Or a Datasoure (Sql() constructor Methods)

since 1.4

support of connection pooling

support: JNDI (java naming and Directory Interface)

// Make sure that derby.jar is in classpath

// see http://db.apache.org/derby/docs/dev/ref

db = Sql.newInstance('jdbc:derby:D:\\derby\\Css2007Groovy;create=true',

'', // userid

'', // password

'org.apache.derby.jdbc.EmbeddedDriver')

Colorado Software Summit: October 21 – 26, 2007

Eric Schreiner – Groovy: Getting the Power Out of Groovy Slide 39

© Copyright 2007, Contecon Software GmbH

Database model

Colorado Software Summit: October 21 – 26, 2007

Eric Schreiner – Groovy: Getting the Power Out of Groovy Slide 40

© Copyright 2007, Contecon Software GmbH

Execute SQL

use Sql.execute(...) methods

Sql.execute returns boolean

Sql.executeUpdate returns num rows updated

Sql.executeInsert returns generated Keys

DbCreateExample.groovy, DbPopulateExample.groovy

Colorado Software Summit: October 21 – 26, 2007

Eric Schreiner – Groovy: Getting the Power Out of Groovy Slide 41

© Copyright 2007, Contecon Software GmbH

Fetching data from a Database

Return Method Parameters Closure

void eachRow String statement {GroovyResultSet row -> your code}

void eachRow String preparedStatement, List values {GroovyResultSet row -> your code}

void eachRow GString preparedStatement {GroovyResultSet row -> your code}

void query String statement {ResultSet resultSet -> your code}

void query String preparedStatement, List values {ResultSet resultSet -> your code}

void query GString preparedStatement {ResultSet resultSet -> your code}

List rows String statement

List rows String preparedStatement, List values

Object firstRow String statement

Object firstRow String preparedStatement, List values

GroovyResultSet implements java.sql.ResultSet and is zero based

DbFetchExample.groovy

Colorado Software Summit: October 21 – 26, 2007

Eric Schreiner – Groovy: Getting the Power Out of Groovy Slide 42

© Copyright 2007, Contecon Software GmbH

Working with groovy.sql.DataSet

adding Rows to a Table

reading rows from a Table or View

does not support updates or deletes

make sure that the directory with the

groovy source files is in the classpath

DbDatasetExample.groovy

Colorado Software Summit: October 21 – 26, 2007

Eric Schreiner – Groovy: Getting the Power Out of Groovy Slide 43

© Copyright 2007, Contecon Software GmbH

How the findAll works in DataSets

The Closure passed as a paramter to the

findAll() method is never executed!

Instead Groovy's internal representation

of the closures code, the Abstract Syntax

Tree (AST) is parsed and a where clause

is generatedAST node SQL mapping

&& and

|| or

== =

Other operators Themselves, literally

it.propertyname propertyname

Constant expression ? (and expression is added to parameterlist)

Colorado Software Summit: October 21 – 26, 2007

Eric Schreiner – Groovy: Getting the Power Out of Groovy Slide 44

© Copyright 2007, Contecon Software GmbH

Other Modules

Grails (http://grails.codehaus.org/)

Web development

GORM (http://grails.codehaus.org/GORM)

Grails Object Relational Mapping based on Hibernate 3

Native launcher(http://groovy.codehaus.org/Native+Launcher)

Compiles Groovy scripts to platform executables

Groovy SWT(http://groovy.codehaus.org/GroovySWT)Creation of Eclipse SWT applications by using Groovy's builder mechanism.

and more ....

and All existing Java Frameworks can be used

Colorado Software Summit: October 21 – 26, 2007

Eric Schreiner – Groovy: Getting the Power Out of Groovy Slide 45

© Copyright 2007, Contecon Software GmbH

References

Groovy

Codehaus

http://groovy.codehaus.org/

JSR241

http://www.jcp.org/en/jsr/detail?id=241

developerWorkshttp://www-128.ibm.com/developerworks/java/library/j-alj08034.h

Books

Groovy in Action (very good)

Groovy Programming

Colorado Software Summit: October 21 – 26, 2007

Eric Schreiner – Groovy: Getting the Power Out of Groovy Slide 46

© Copyright 2007, Contecon Software GmbH

Contact me

Eric Schreiner

[email protected]

http://www.contecon.de

Sources: http://www.contecon.de/groovy/

Please fill out the evaluations

Colorado Software Summit: October 21 – 26, 2007

Eric Schreiner – Groovy: Getting the Power Out of Groovy Slide 47

© Copyright 2007, Contecon Software GmbH

Thank you

....if you have questions

ask now