Groovy scripts with Groovy

Download Groovy scripts with Groovy

Post on 14-Jul-2015

485 views

Category:

Software

3 download

Embed Size (px)

TRANSCRIPT

  • by Andrs Viedma

    scripts with

  • Restless Java lover

    Software dinosaurmore than 20 years nagging

    Working at

    Usual suspect of MadridGUG and MadridJUG

  • Arquitecture at

    Service DB

    TService

    TuentiNgDBs

    tuenti-ng

    Micro serviceTService

    Micro serviceTService

    Micro serviceTService

  • Arquitecture at

    Service DB

    TService

    TuentiNgDBs

    tuenti-ng

    Micro serviceTService

    Micro serviceTService

    Micro serviceTService

    PHP Script

  • About Groovy Dynamic Language, prepared for scripting Runs in the JVM

    o Lots of Java librarieso Can use the code of our Java services

    Source code almost compatible with Javao Main difference: == operator means equals()o If you make your script in Java, itll WORK

    Other cool features, mainly inspired by Python

  • Arquitecture at

    Service DB

    TService

    TuentiNgDBs

    tuenti-ng

    Micro serviceTService

    Micro serviceTService

    Micro serviceTService

  • Arquitecture at

    Service DB

    TService

    TuentiNgDBs

    tuenti-ng

    Micro serviceTService

    Micro serviceTService

    Micro serviceTService

    Groovy Script

  • Arquitecture at

    Service DB

    TService

    TuentiNgDBs

    tuenti-ng

    Micro serviceTService

    Micro serviceTService

    Micro serviceTService

    Groovy Script

  • Mix your Production code with direct accesses to the

    DB, config, TServices...

  • Hello World (Java, but works also as Groovy)

    import utils.*;World world = new World();world.setId("mongo");world.setName("Mongo");

    System.out.println("Hello " + world.getName() + "!");

    package utils;public class World { private String id; private String name; public String getId() { return id; } public void setId(String id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; }}

    $GRSCRIPTS/helloworld.groovy $GRSCRIPTS/utils/World.groovy

  • Hello World (Groovy style)

    def world = new World(id: "mongo", name: "Mongo")println "Hello ${world.name}!"

    class World { String id String name}

    $GRSCRIPTS/helloworld.groovy

    $ cd $GRSCRIPTS$ groovy helloworld.groovyHello Mongo!$

  • Hello World (Groovy style)

    def world = new World(id: "mongo", name: "Mongo")println "Hello ${world.name}!"

    class World { String id String name}

    $GRSCRIPTS/helloworld.groovy

    $ cd $GRSCRIPTS$ groovy helloworld.groovyHello Mongo!$

  • Hello World (Groovy style)

    def world = new World(id: "mongo", name: "Mongo")println "Hello ${world.name}!"

    class World { String id String name}

    $GRSCRIPTS/helloworld.groovy

    $ cd $GRSCRIPTS$ groovy helloworld.groovyHello Mongo!$

  • Hello World (Groovy style)

    def world = new World(id: "mongo", name: "Mongo")println "Hello ${world.name}!"

    class World { String id String name}

    $GRSCRIPTS/helloworld.groovy

    $ cd $GRSCRIPTS$ groovy helloworld.groovyHello Mongo!$

  • Closures / collectionsdef worlds = [ new World(id: "mongo", name: "Mongo"), new World(id: "mars", name: "Mars"), new World(id: "wonderland", name: "Wonderland")] as ArrayList

    def heroesByWorld = [ mongo: ["Flash Gordon", "Prince Barin"], mars: ["John Carter"], wonderland: ["Alice", "Queen of Hearts"]]

    heroesByWorld.each { worldEntry -> def worldId = worldEntry.key def world = worlds.find { it.id == worldId } println "*** World: ${world.name}"

    def heroList = worldEntry.value heroList.each { hero -> println hero }}

  • Closures / collectionsdef worlds = [ new World(id: "mongo", name: "Mongo"), new World(id: "mars", name: "Mars"), new World(id: "wonderland", name: "Wonderland")] as ArrayList

    def heroesByWorld = [ mongo: ["Flash Gordon", "Prince Barin"], mars: ["John Carter"], wonderland: ["Alice", "Queen of Hearts"]]

    heroesByWorld.each { worldEntry -> def worldId = worldEntry.key def world = worlds.find { it.id == worldId } println "*** World: ${world.name}"

    def heroList = worldEntry.value heroList.each { hero -> println hero }}

  • Closures / collectionsdef worlds = [ new World(id: "mongo", name: "Mongo"), new World(id: "mars", name: "Mars"), new World(id: "wonderland", name: "Wonderland")] as ArrayList

    def heroesByWorld = [ mongo: ["Flash Gordon", "Prince Barin"], mars: ["John Carter"], wonderland: ["Alice", "Queen of Hearts"]]

    heroesByWorld.each { worldEntry -> def worldId = worldEntry.key def world = worlds.find { it.id == worldId } println "*** World: ${world.name}"

    def heroList = worldEntry.value heroList.each { hero -> println hero }}

  • Closures / collectionsdef worlds = [ new World(id: "mongo", name: "Mongo"), new World(id: "mars", name: "Mars"), new World(id: "wonderland", name: "Wonderland")] as ArrayList

    def heroesByWorld = [ mongo: ["Flash Gordon", "Prince Barin"], mars: ["John Carter"], wonderland: ["Alice", "Queen of Hearts"]]

    heroesByWorld.each { worldEntry -> def worldId = worldEntry.key def world = worlds.find { it.id == worldId } println "*** World: ${world.name}"

    def heroList = worldEntry.value heroList.each { hero -> println hero }}

  • Closures / collectionsdef worlds = [ new World(id: "mongo", name: "Mongo"), new World(id: "mars", name: "Mars"), new World(id: "wonderland", name: "Wonderland")] as ArrayList

    def heroesByWorld = [ mongo: ["Flash Gordon", "Prince Barin"], mars: ["John Carter"], wonderland: ["Alice", "Queen of Hearts"]]

    heroesByWorld.each { worldEntry -> def worldId = worldEntry.key def world = worlds.find { it.id == worldId } println "*** World: ${world.name}"

    def heroList = worldEntry.value heroList.each { hero -> println hero }}

  • Functions and scope of variablesdef worlds = [ ]def heroesByWorld = [ ]eachWorld { world, heroes -> println "*** World: ${world.name}" heroes.each { hero -> println hero }}

    void eachWorld(Closure closure) { heroesByWorld.each { worldEntry -> def worldId = worldEntry.key def world = worlds.find { it.id == worldId } def heroList = worldEntry.value closure(world, heroList) }}

  • Functions and scope of variablesdef worlds = [ ]def heroesByWorld = [ ]eachWorld { world, heroes -> println "*** World: ${world.name}" heroes.each { hero -> println hero }}

    void eachWorld(Closure closure) { heroesByWorld.each { worldEntry -> def worldId = worldEntry.key def world = worlds.find { it.id == worldId } def heroList = worldEntry.value closure(world, heroList) }}

  • Functions and scope of variablesdef worlds = [ ]def heroesByWorld = [ ]eachWorld { world, heroes -> println "*** World: ${world.name}" heroes.each { hero -> println hero }}

    void eachWorld(Closure closure) { heroesByWorld.each { worldEntry -> def worldId = worldEntry.key def world = worlds.find { it.id == worldId } def heroList = worldEntry.value closure(world, heroList) }}

  • Functions and scope of variablesdef worlds = [ ]def heroesByWorld = [ ]eachWorld { world, heroes -> println "*** World: ${world.name}" heroes.each { hero -> println hero }}

    void eachWorld(Closure closure) { heroesByWorld.each { worldEntry -> def worldId = worldEntry.key def world = worlds.find { it.id == worldId } def heroList = worldEntry.value closure(world, heroList) }}

  • Functions and scope of variablesdef worlds = [ ]def heroesByWorld = [ ]eachWorld { world, heroes -> println "*** World: ${world.name}" heroes.each { hero -> println hero }}

    void eachWorld(Closure closure) { heroesByWorld.each { worldEntry -> def worldId = worldEntry.key def world = worlds.find { it.id == worldId } def heroList = worldEntry.value closure(world, heroList) }}

    worlds = [ ] @Field def worlds = [ ] @Field List worlds = [ ]

  • Grape: libraries from Maven repos@Grab(group='org.apache.commons', module='commons-email', version='1.3.3')import org.apache.commons.mail.*def usr, pwd, toAddress = [ ]println "Creating email object"Email email = new SimpleEmail( hostName: "smtp.googlemail.com", smtpPort: 465, authenticator: new DefaultAuthenticator(usr, pwd), SSLOnConnect: true, from: usr, subject: "Groovy mail!", msg: "You're hot and you know it ... :-)" )

    email.addTo(toAddress);

    println "Sending email..."email.send()println "OK"

  • Grape: default config

  • Grape: default config

  • Grape: default config

  • Grape: add your repository

    (...)

  • Grape: add your repository

    (...)

  • Grape: updatable snapshots

  • Grape: updatable snapshots

  • Databases: Sql object@Grab('mysql:mysql-connector-java')@GrabConfig(systemClassLoader=true)import groovy.sql.Sqldef sql = Sql.newInstance("jdbc:mysql://localhost:3306/simfonics_charging", "tuenti_developer", "lalala", "com.mysql.jdbc.Driver")sql.resultSetConcurrency = java.sql.ResultSet.CONCUR_UPDATABLE

    def version = 2def newVersion = 3def type = "BUNDLEDEF"sql.eachRow("""select * from charging_element_definition where version =