groovy usage patterns

Upload: iamandicip

Post on 07-Apr-2018

232 views

Category:

Documents


0 download

TRANSCRIPT

  • 8/3/2019 Groovy Usage Patterns

    1/16

    www.canoo.com

    your provider for business web solutions

    1

    Dierk Knig

    your provider for business web solutions

    [email protected]@mittie

    Groovy

    Seven Usage Patterns

    Donnerstag, 28. April 2011

    http://www.canoo.com/http://www.canoo.com/
  • 8/3/2019 Groovy Usage Patterns

    2/16

    www.canoo.com

    your provider for business web solutions

    Dierk Knig

    Fellow @ Canoo Engineering AG, Basel (CH)

    Canoo RIA Suite

    Projects, Consulting

    www.canoo.com

    Open-source committer Groovy, Grails, GPars, Grasp

    Donnerstag, 28. April 2011

    http://www.canoo.com/http://www.canoo.com/
  • 8/3/2019 Groovy Usage Patterns

    3/16

    www.canoo.com

    your provider for business web solutions

    The seven usage patterns

    Super Glue

    Liquid Heart

    Lipstick

    Keyhole Surgery

    Unlimited Openness

    Ghost Writer

    House Elf

    Donnerstag, 28. April 2011

    http://www.canoo.com/http://www.canoo.com/
  • 8/3/2019 Groovy Usage Patterns

    4/16

    www.canoo.com

    your provider for business web solutions

    #1 Super Glue

    Build application from existing building blocks

    Java makes perfect infrastructure:

    middleware, frameworks, widget sets, services

    Scripting makes a flexible (agile) application layer:

    views und controller

    Grails, Gaelyk, Griffon, Ratpack, JBoss Seam,

    WebWork, Struts 2 Actions, Swing, ... Example: combination of XML parser, Java networking

    and Swing widget set in order to build a standard RSS

    feed reader

    Donnerstag, 28. April 2011

    http://www.canoo.com/http://www.canoo.com/
  • 8/3/2019 Groovy Usage Patterns

    5/16

    www.canoo.com

    your provider for business web solutions

    defurl ='http://www.groovyblogs.org/feed/rss'

    defitems = new XmlParser().parse(url).channel.item

    defcols = 'pubDate title description'.tokenize()

    groovy.swing.SwingBuilder.build {

    frame(id:'f', title: 'Groovy Blogs', visible:true) {scrollPane {

    table {

    tableModel(list: items) {

    cols.each { col ->

    closureColumn header: col, read: { it[col].text() }} } } } }

    f.pack()

    }

    Donnerstag, 28. April 2011

    http://www.canoo.com/http://www.canoo.com/
  • 8/3/2019 Groovy Usage Patterns

    6/16

    www.canoo.com

    your provider for business web solutions

    #2 Liquid Heart

    Externalize business models

    Given application structure in Java

    Allow to learn about the business:keep entities, relations, and behaviour flexible through

    scripting Usages: Spring beans, JBoss components,rule

    engines (groovyrules.dev.java.net, JSR-94), Grails,enterprise risk management, smart configuration

    Example: calculation rules for bonus allocation

    Donnerstag, 28. April 2011

    http://www.canoo.com/http://www.canoo.com/
  • 8/3/2019 Groovy Usage Patterns

    7/16

    revenue = employee.revenue

    switch(revenue / 1000) {

    case 0..100 : return revenue * 0.04

    case 100..200 : return revenue * 0.05

    case {it > 200} : bonusClub.add(employee)

    return revenue * 0.06

    }

    Liquid Heart example: bonus allocation

    Binding binding = new Binding();

    binding.setVariable("employee", employee);

    binding.setVariable("bonusClub", bonusClub);

    GroovyShell shell = new GroovyShell(binding);

    File script = new File(filename);

    float bonus = (float) shell.evaluate(script);

    Donnerstag, 28. April 2011

  • 8/3/2019 Groovy Usage Patterns

    8/16

    www.canoo.com

    your provider for business web solutions

    #3 Lipstick

    Make code read nicely

    Fluent transition from APIs to DSLs

    Usages: GDK, Groovy Beans, GPath, Lists, Maps,

    Callable, XML, SQL, Command chains, GPars,

    Scriptom, and pretty much all of Groovy...

    Example: calculation rules for bonus allocation (again)

    Donnerstag, 28. April 2011

    http://www.canoo.com/http://www.canoo.com/
  • 8/3/2019 Groovy Usage Patterns

    9/16

    www.canoo.com

    your provider for business web solutions

    definvite(subject) {

    [to : { target -> target.add(subject) } ]

    }

    invite employee to bonusClub

    Donnerstag, 28. April 2011

    http://www.canoo.com/http://www.canoo.com/http://www.canoo.com/http://www.canoo.com/
  • 8/3/2019 Groovy Usage Patterns

    10/16

    www.canoo.com

    your provider for business web solutions

    #4 Keyhole Surgery

    Minimal-invasive surgery "in vivo"

    Runtime inspectionand modifications

    Ad-hoc queries are not foreseeable "Backdoor" for the live execution of scripts Particularly useful for

    product support, failure analysis, hot fixes,

    emergencies Usages: Oracle JMX Beans, XWiki, SnipSnap, Ant,Canoo WebTest, Grails Console, GAE, ULC AdminConsole

    Example: a live Groovy Servlet

    Donnerstag, 28. April 2011

    http://www.canoo.com/http://www.canoo.com/
  • 8/3/2019 Groovy Usage Patterns

    11/16

    www.canoo.com

    your provider for business web solutions

    users = servletContext.getAttribute('users')

    bad = users.findAll { user -> user.cart.items.any { it.price < 0 } }

    servletContext.setAttribute('users', users - bad)

    Remove malicious users from the servlet context

    Donnerstag, 28. April 2011

    http://www.canoo.com/http://www.canoo.com/http://www.canoo.com/http://www.canoo.com/
  • 8/3/2019 Groovy Usage Patterns

    12/16

    www.canoo.com

    your provider for business web solutions

    #5 Unlimited Openness

    Every line of code may be changed

    It's impossible (and not desirable!)

    to design for every possible futurerequirement

    Allow for easily changing the code

    without tedious setup for compilation

    and deployment Follow the lessons of PERL, PHP, Python,...

    Example: groovyblogs.org, miki

    Donnerstag, 28. April 2011

    http://www.canoo.com/http://www.canoo.com/
  • 8/3/2019 Groovy Usage Patterns

    13/16

    www.canoo.com

    your provider for business web solutions

    #6 Ghost Writer

    Code generation without source code generation

    Avoid repeating code constructs but still make them

    available in the bytecode

    Mark the code with annotations and

    provide an AST transformation or use existing ones

    Examples: @Immutable, @Log, @Delegate, @Lazy,

    @Singleton, @Bindable, @AutoClone,

    @TimedInterrupt, ...

    Donnerstag, 28. April 2011

    http://www.canoo.com/http://www.canoo.com/http://www.canoo.com/
  • 8/3/2019 Groovy Usage Patterns

    14/16

    www.canoo.com

    your provider for business web solutions

    #7 House Elf

    Delegate the housework

    Build automation, continuous integration, deployment,

    installer, service monitoring, reports, statistics,automated documentation, functional tests, HTML

    scraping, Web remote control, XML-RPC,

    WebServices

    Usages with Ant, Maven, AntBuilder, Gant, Gradle,Canoo WebTest, Grails scaffolding, ...

    Examples: senv, groovy install

    Donnerstag, 28. April 2011

    http://www.canoo.com/http://www.canoo.com/
  • 8/3/2019 Groovy Usage Patterns

    15/16

    www.canoo.com

    your provider for business web solutions

    The seven usage patterns

    Super Glue

    Liquid Heart

    Lipstick

    Keyhole Surgery

    Unlimited Openness

    Ghost Writer

    House Elf

    Donnerstag, 28. April 2011

    http://www.canoo.com/http://www.canoo.com/
  • 8/3/2019 Groovy Usage Patterns

    16/16

    www.canoo.com

    your provider for business web solutions

    Discussion

    credits:

    Paul King

    [email protected]

    @mittie

    http://www.canoo.com/http://www.canoo.com/