+ All Categories
Home > Documents > Scripting for the JVM using Groovy - Cincinnati … · Scripting for the JVM using Groovy Adil Khan...

Scripting for the JVM using Groovy - Cincinnati … · Scripting for the JVM using Groovy Adil Khan...

Date post: 02-Sep-2018
Category:
Upload: truongkhanh
View: 223 times
Download: 0 times
Share this document with a friend
66
Scripting for the JVM using Groovy Adil Khan Sr. Application Developer /Java Group Biomedical Informatics
Transcript
Page 1: Scripting for the JVM using Groovy - Cincinnati … · Scripting for the JVM using Groovy Adil Khan ... (DSL). •Seamless ... • java groovy.lang.GroovyShell path_to/MyScript.groovy

Scripting for the JVM using Groovy

Adil Khan

Sr. Application Developer /Java Group

Biomedical Informatics

Page 2: Scripting for the JVM using Groovy - Cincinnati … · Scripting for the JVM using Groovy Adil Khan ... (DSL). •Seamless ... • java groovy.lang.GroovyShell path_to/MyScript.groovy

Outline

Page 3: Scripting for the JVM using Groovy - Cincinnati … · Scripting for the JVM using Groovy Adil Khan ... (DSL). •Seamless ... • java groovy.lang.GroovyShell path_to/MyScript.groovy

• What is Groovy?

Outline

Page 4: Scripting for the JVM using Groovy - Cincinnati … · Scripting for the JVM using Groovy Adil Khan ... (DSL). •Seamless ... • java groovy.lang.GroovyShell path_to/MyScript.groovy

• What is Groovy?

• Why would we want to use it?

Outline

Page 5: Scripting for the JVM using Groovy - Cincinnati … · Scripting for the JVM using Groovy Adil Khan ... (DSL). •Seamless ... • java groovy.lang.GroovyShell path_to/MyScript.groovy

• What is Groovy?

• Why would we want to use it?

• A taste of the features.

Outline

Page 6: Scripting for the JVM using Groovy - Cincinnati … · Scripting for the JVM using Groovy Adil Khan ... (DSL). •Seamless ... • java groovy.lang.GroovyShell path_to/MyScript.groovy

• What is Groovy?

• Why would we want to use it?

• A taste of the features.

• Some examples in the BMI world.

Outline

Page 7: Scripting for the JVM using Groovy - Cincinnati … · Scripting for the JVM using Groovy Adil Khan ... (DSL). •Seamless ... • java groovy.lang.GroovyShell path_to/MyScript.groovy

• What is Groovy?

• Why would we want to use it?

• A taste of the features.

• Some examples in the BMI world.

• Other Features

Outline

Page 8: Scripting for the JVM using Groovy - Cincinnati … · Scripting for the JVM using Groovy Adil Khan ... (DSL). •Seamless ... • java groovy.lang.GroovyShell path_to/MyScript.groovy

What is Groovy?

Page 9: Scripting for the JVM using Groovy - Cincinnati … · Scripting for the JVM using Groovy Adil Khan ... (DSL). •Seamless ... • java groovy.lang.GroovyShell path_to/MyScript.groovy

• Released in 2003 ~ Java 1.4

What is Groovy?

Page 10: Scripting for the JVM using Groovy - Cincinnati … · Scripting for the JVM using Groovy Adil Khan ... (DSL). •Seamless ... • java groovy.lang.GroovyShell path_to/MyScript.groovy

• Released in 2003 ~ Java 1.4

• Scripting language that is a Superset of Java.

What is Groovy?

Page 11: Scripting for the JVM using Groovy - Cincinnati … · Scripting for the JVM using Groovy Adil Khan ... (DSL). •Seamless ... • java groovy.lang.GroovyShell path_to/MyScript.groovy

• Released in 2003 ~ Java 1.4

• Scripting language that is a Superset of Java.

• Great for Designing Domain Specific Languages (DSL).

What is Groovy?

Page 12: Scripting for the JVM using Groovy - Cincinnati … · Scripting for the JVM using Groovy Adil Khan ... (DSL). •Seamless ... • java groovy.lang.GroovyShell path_to/MyScript.groovy

• Released in 2003 ~ Java 1.4

• Scripting language that is a Superset of Java.

• Great for Designing Domain Specific Languages (DSL).

• Seamless Integration with Java. (Groovy is Java)

What is Groovy?

Page 13: Scripting for the JVM using Groovy - Cincinnati … · Scripting for the JVM using Groovy Adil Khan ... (DSL). •Seamless ... • java groovy.lang.GroovyShell path_to/MyScript.groovy

• Released in 2003 ~ Java 1.4

• Scripting language that is a Superset of Java.

• Great for Designing Domain Specific Languages (DSL).

• Seamless Integration with Java. (Groovy is Java)

• Natural for Java Developers /Access to all java libraries.

What is Groovy?

Page 14: Scripting for the JVM using Groovy - Cincinnati … · Scripting for the JVM using Groovy Adil Khan ... (DSL). •Seamless ... • java groovy.lang.GroovyShell path_to/MyScript.groovy

• Released in 2003 ~ Java 1.4

• Scripting language that is a Superset of Java.

• Great for Designing Domain Specific Languages (DSL).

• Seamless Integration with Java. (Groovy is Java)

• Natural for Java Developers /Access to all java libraries.

• Natural for Scripters who want to learn/use Java.

What is Groovy?

Page 15: Scripting for the JVM using Groovy - Cincinnati … · Scripting for the JVM using Groovy Adil Khan ... (DSL). •Seamless ... • java groovy.lang.GroovyShell path_to/MyScript.groovy

• Released in 2003 ~ Java 1.4

• Scripting language that is a Superset of Java.

• Great for Designing Domain Specific Languages (DSL).

• Seamless Integration with Java. (Groovy is Java)

• Natural for Java Developers /Access to all java libraries.

• Natural for Scripters who want to learn/use Java.

• Potential avenue for non-java developers who need access to java api/libraries

What is Groovy?

Page 16: Scripting for the JVM using Groovy - Cincinnati … · Scripting for the JVM using Groovy Adil Khan ... (DSL). •Seamless ... • java groovy.lang.GroovyShell path_to/MyScript.groovy

• Numerous ways to run groovy.

• Interactive console like Ruby, Python

• Shell Scripts: #!/usr/bin/env groovy

• GroovyShell Helper class

• java groovy.lang.GroovyShell path_to/MyScript.groovy

• Plugins for Eclipse/Netbeans etc.

• The designers goals is to have a language that gets out

of the way and allows you to be productive.

What is Groovy (Ctd.)

Page 17: Scripting for the JVM using Groovy - Cincinnati … · Scripting for the JVM using Groovy Adil Khan ... (DSL). •Seamless ... • java groovy.lang.GroovyShell path_to/MyScript.groovy

• Hello World Java:

• Hello World Groovy:

Note lack of parenthesis, semicolons, modifiers and

Typing.

A Taste

Page 18: Scripting for the JVM using Groovy - Cincinnati … · Scripting for the JVM using Groovy Adil Khan ... (DSL). •Seamless ... • java groovy.lang.GroovyShell path_to/MyScript.groovy

• Java (6) Print contents of a file:

• With Groovy:

More useful Example

Page 19: Scripting for the JVM using Groovy - Cincinnati … · Scripting for the JVM using Groovy Adil Khan ... (DSL). •Seamless ... • java groovy.lang.GroovyShell path_to/MyScript.groovy

• Java (6) Print contents of a file:

• With Groovy:

More useful Example

Page 20: Scripting for the JVM using Groovy - Cincinnati … · Scripting for the JVM using Groovy Adil Khan ... (DSL). •Seamless ... • java groovy.lang.GroovyShell path_to/MyScript.groovy

• java.io.File class with a Groovy Nature.

• Groovy ‘enhances’ existing java Objects.

• Use of closures /Functional programming.

• A more java like example:

More useful Example (Ctd)

Page 21: Scripting for the JVM using Groovy - Cincinnati … · Scripting for the JVM using Groovy Adil Khan ... (DSL). •Seamless ... • java groovy.lang.GroovyShell path_to/MyScript.groovy

Baked in Features

Page 22: Scripting for the JVM using Groovy - Cincinnati … · Scripting for the JVM using Groovy Adil Khan ... (DSL). •Seamless ... • java groovy.lang.GroovyShell path_to/MyScript.groovy

A quick overview of some basic built in features.

Baked in Features

Page 23: Scripting for the JVM using Groovy - Cincinnati … · Scripting for the JVM using Groovy Adil Khan ... (DSL). •Seamless ... • java groovy.lang.GroovyShell path_to/MyScript.groovy

A quick overview of some basic built in features.

• Collections

Baked in Features

Page 24: Scripting for the JVM using Groovy - Cincinnati … · Scripting for the JVM using Groovy Adil Khan ... (DSL). •Seamless ... • java groovy.lang.GroovyShell path_to/MyScript.groovy

A quick overview of some basic built in features.

• Collections

• {G}Strings

Baked in Features

Page 25: Scripting for the JVM using Groovy - Cincinnati … · Scripting for the JVM using Groovy Adil Khan ... (DSL). •Seamless ... • java groovy.lang.GroovyShell path_to/MyScript.groovy

A quick overview of some basic built in features.

• Collections

• {G}Strings

• Templating

Baked in Features

Page 26: Scripting for the JVM using Groovy - Cincinnati … · Scripting for the JVM using Groovy Adil Khan ... (DSL). •Seamless ... • java groovy.lang.GroovyShell path_to/MyScript.groovy

A quick overview of some basic built in features.

• Collections

• {G}Strings

• Templating

• Switch

Baked in Features

Page 27: Scripting for the JVM using Groovy - Cincinnati … · Scripting for the JVM using Groovy Adil Khan ... (DSL). •Seamless ... • java groovy.lang.GroovyShell path_to/MyScript.groovy

A quick overview of some basic built in features.

• Collections

• {G}Strings

• Templating

• Switch

• Builders / Slurpers

Baked in Features

Page 28: Scripting for the JVM using Groovy - Cincinnati … · Scripting for the JVM using Groovy Adil Khan ... (DSL). •Seamless ... • java groovy.lang.GroovyShell path_to/MyScript.groovy

• Lists :

Collections

Page 29: Scripting for the JVM using Groovy - Cincinnati … · Scripting for the JVM using Groovy Adil Khan ... (DSL). •Seamless ... • java groovy.lang.GroovyShell path_to/MyScript.groovy

• Lists :

Collections

Page 30: Scripting for the JVM using Groovy - Cincinnati … · Scripting for the JVM using Groovy Adil Khan ... (DSL). •Seamless ... • java groovy.lang.GroovyShell path_to/MyScript.groovy

• Maps :

Collections (Ctd)

Page 31: Scripting for the JVM using Groovy - Cincinnati … · Scripting for the JVM using Groovy Adil Khan ... (DSL). •Seamless ... • java groovy.lang.GroovyShell path_to/MyScript.groovy

• Maps :

Collections (Ctd)

Page 32: Scripting for the JVM using Groovy - Cincinnati … · Scripting for the JVM using Groovy Adil Khan ... (DSL). •Seamless ... • java groovy.lang.GroovyShell path_to/MyScript.groovy

• Range – Using the ‘..’ operator

Collections (Ctd)

Page 33: Scripting for the JVM using Groovy - Cincinnati … · Scripting for the JVM using Groovy Adil Khan ... (DSL). •Seamless ... • java groovy.lang.GroovyShell path_to/MyScript.groovy

• Range – Using the ‘..’ operator

Collections (Ctd)

Page 34: Scripting for the JVM using Groovy - Cincinnati … · Scripting for the JVM using Groovy Adil Khan ... (DSL). •Seamless ... • java groovy.lang.GroovyShell path_to/MyScript.groovy

• Powerful extensions to the java.lang.String class.

• Evaluate expressions and here documents (“””).

{G}Strings

Page 35: Scripting for the JVM using Groovy - Cincinnati … · Scripting for the JVM using Groovy Adil Khan ... (DSL). •Seamless ... • java groovy.lang.GroovyShell path_to/MyScript.groovy

• Powerful extensions to the java.lang.String class.

• Evaluate expressions and here documents (“””).

{G}Strings

Page 36: Scripting for the JVM using Groovy - Cincinnati … · Scripting for the JVM using Groovy Adil Khan ... (DSL). •Seamless ... • java groovy.lang.GroovyShell path_to/MyScript.groovy

• Templating engine allows for quick building of text. Good use case

of building sql statements, or any dynamic text.

Templating

Page 37: Scripting for the JVM using Groovy - Cincinnati … · Scripting for the JVM using Groovy Adil Khan ... (DSL). •Seamless ... • java groovy.lang.GroovyShell path_to/MyScript.groovy

• Templating engine allows for quick building of text. Good use case

of building sql statements, or any dynamic text.

Templating

Page 38: Scripting for the JVM using Groovy - Cincinnati … · Scripting for the JVM using Groovy Adil Khan ... (DSL). •Seamless ... • java groovy.lang.GroovyShell path_to/MyScript.groovy

• Templating engine allows for quick building of text. Good use case

of building sql statements, or any dynamic text.

Templating

Page 39: Scripting for the JVM using Groovy - Cincinnati … · Scripting for the JVM using Groovy Adil Khan ... (DSL). •Seamless ... • java groovy.lang.GroovyShell path_to/MyScript.groovy

• Can use any object in the condition

Switch

Page 40: Scripting for the JVM using Groovy - Cincinnati … · Scripting for the JVM using Groovy Adil Khan ... (DSL). •Seamless ... • java groovy.lang.GroovyShell path_to/MyScript.groovy

• Can use any object in the condition

Switch

Page 41: Scripting for the JVM using Groovy - Cincinnati … · Scripting for the JVM using Groovy Adil Khan ... (DSL). •Seamless ... • java groovy.lang.GroovyShell path_to/MyScript.groovy

• Can use any object in the condition

Switch

Page 42: Scripting for the JVM using Groovy - Cincinnati … · Scripting for the JVM using Groovy Adil Khan ... (DSL). •Seamless ... • java groovy.lang.GroovyShell path_to/MyScript.groovy

• Built in ability to generate/parse JSON/XML

• Declarative Nature of groovy simplifies the task.

Builders

Page 43: Scripting for the JVM using Groovy - Cincinnati … · Scripting for the JVM using Groovy Adil Khan ... (DSL). •Seamless ... • java groovy.lang.GroovyShell path_to/MyScript.groovy

• Built in ability to generate/parse JSON/XML

• Declarative Nature of groovy simplifies the task.

Builders

Page 44: Scripting for the JVM using Groovy - Cincinnati … · Scripting for the JVM using Groovy Adil Khan ... (DSL). •Seamless ... • java groovy.lang.GroovyShell path_to/MyScript.groovy

• Built in ability to generate/parse JSON/XML

• Declarative Nature of groovy simplifies the task.

Builders

Page 45: Scripting for the JVM using Groovy - Cincinnati … · Scripting for the JVM using Groovy Adil Khan ... (DSL). •Seamless ... • java groovy.lang.GroovyShell path_to/MyScript.groovy

Using MarkupBuilder to generate XML

Builders (Ctd)

Page 46: Scripting for the JVM using Groovy - Cincinnati … · Scripting for the JVM using Groovy Adil Khan ... (DSL). •Seamless ... • java groovy.lang.GroovyShell path_to/MyScript.groovy

Using MarkupBuilder to generate XML

Builders (Ctd)

Page 47: Scripting for the JVM using Groovy - Cincinnati … · Scripting for the JVM using Groovy Adil Khan ... (DSL). •Seamless ... • java groovy.lang.GroovyShell path_to/MyScript.groovy

Using MarkupBuilder to generate XML

Builders (Ctd)

Page 48: Scripting for the JVM using Groovy - Cincinnati … · Scripting for the JVM using Groovy Adil Khan ... (DSL). •Seamless ... • java groovy.lang.GroovyShell path_to/MyScript.groovy

• Using the ‘Slurpers’ to build an object graph from

Json/XML

Slurpers

Page 49: Scripting for the JVM using Groovy - Cincinnati … · Scripting for the JVM using Groovy Adil Khan ... (DSL). •Seamless ... • java groovy.lang.GroovyShell path_to/MyScript.groovy

• Xml example.

Slurpers (Ctd)

Page 50: Scripting for the JVM using Groovy - Cincinnati … · Scripting for the JVM using Groovy Adil Khan ... (DSL). •Seamless ... • java groovy.lang.GroovyShell path_to/MyScript.groovy

Potential Uses for Java Group

Page 51: Scripting for the JVM using Groovy - Cincinnati … · Scripting for the JVM using Groovy Adil Khan ... (DSL). •Seamless ... • java groovy.lang.GroovyShell path_to/MyScript.groovy

• Testing : unit/integration

• Ease of parsing/generating xml/json is very useful in

integration tests.

Potential Uses for Java Group

Page 52: Scripting for the JVM using Groovy - Cincinnati … · Scripting for the JVM using Groovy Adil Khan ... (DSL). •Seamless ... • java groovy.lang.GroovyShell path_to/MyScript.groovy

• Testing : unit/integration

• Ease of parsing/generating xml/json is very useful in

integration tests.

• Simplifying ‘Utils’ classes.

Potential Uses for Java Group

Page 53: Scripting for the JVM using Groovy - Cincinnati … · Scripting for the JVM using Groovy Adil Khan ... (DSL). •Seamless ... • java groovy.lang.GroovyShell path_to/MyScript.groovy

• Testing : unit/integration

• Ease of parsing/generating xml/json is very useful in

integration tests.

• Simplifying ‘Utils’ classes.

• Build/Deployment scripts.

Potential Uses for Java Group

Page 54: Scripting for the JVM using Groovy - Cincinnati … · Scripting for the JVM using Groovy Adil Khan ... (DSL). •Seamless ... • java groovy.lang.GroovyShell path_to/MyScript.groovy

• Testing : unit/integration

• Ease of parsing/generating xml/json is very useful in

integration tests.

• Simplifying ‘Utils’ classes.

• Build/Deployment scripts.

• System Administration

Potential Uses for Java Group

Page 55: Scripting for the JVM using Groovy - Cincinnati … · Scripting for the JVM using Groovy Adil Khan ... (DSL). •Seamless ... • java groovy.lang.GroovyShell path_to/MyScript.groovy

• Testing : unit/integration

• Ease of parsing/generating xml/json is very useful in

integration tests.

• Simplifying ‘Utils’ classes.

• Build/Deployment scripts.

• System Administration

• DSLs

Potential Uses for Java Group

Page 56: Scripting for the JVM using Groovy - Cincinnati … · Scripting for the JVM using Groovy Adil Khan ... (DSL). •Seamless ... • java groovy.lang.GroovyShell path_to/MyScript.groovy

• Creating Mock/Stub objects that simulate

request/response for unit testing.

• Requirements specific testing using frameworks such

as Spock (BDD)

• Unit Testing baked in groovy.util.GroovyTestCase

• Use case of using groovy constructs to simplify

loops/parameters to java code.

Testing Java with Groovy

Page 57: Scripting for the JVM using Groovy - Cincinnati … · Scripting for the JVM using Groovy Adil Khan ... (DSL). •Seamless ... • java groovy.lang.GroovyShell path_to/MyScript.groovy

Simplifying ‘Utils’ Classes

Page 58: Scripting for the JVM using Groovy - Cincinnati … · Scripting for the JVM using Groovy Adil Khan ... (DSL). •Seamless ... • java groovy.lang.GroovyShell path_to/MyScript.groovy

Simplifying ‘Utils’ Classes

Page 59: Scripting for the JVM using Groovy - Cincinnati … · Scripting for the JVM using Groovy Adil Khan ... (DSL). •Seamless ... • java groovy.lang.GroovyShell path_to/MyScript.groovy

Simplifying ‘Utils’ Classes

Page 60: Scripting for the JVM using Groovy - Cincinnati … · Scripting for the JVM using Groovy Adil Khan ... (DSL). •Seamless ... • java groovy.lang.GroovyShell path_to/MyScript.groovy

Simplifying ‘Utils’ Classes

Page 61: Scripting for the JVM using Groovy - Cincinnati … · Scripting for the JVM using Groovy Adil Khan ... (DSL). •Seamless ... • java groovy.lang.GroovyShell path_to/MyScript.groovy

Simplifying ‘Utils’ Classes

Page 62: Scripting for the JVM using Groovy - Cincinnati … · Scripting for the JVM using Groovy Adil Khan ... (DSL). •Seamless ... • java groovy.lang.GroovyShell path_to/MyScript.groovy

• Invoke Groovy Scripts in Ant using the <groovy> tag.

• Use Groovy to monitor tomcat/application status *.

Build/Monitor Scripts

Page 63: Scripting for the JVM using Groovy - Cincinnati … · Scripting for the JVM using Groovy Adil Khan ... (DSL). •Seamless ... • java groovy.lang.GroovyShell path_to/MyScript.groovy

• Invoke Groovy Scripts in Ant using the <groovy> tag.

• Use Groovy to monitor tomcat/application status *.

Build/Monitor Scripts

Page 64: Scripting for the JVM using Groovy - Cincinnati … · Scripting for the JVM using Groovy Adil Khan ... (DSL). •Seamless ... • java groovy.lang.GroovyShell path_to/MyScript.groovy

• Invoke Groovy Scripts in Ant using the <groovy> tag.

• Use Groovy to monitor tomcat/application status *.

Build/Monitor Scripts

* http://blog.techstacks.com/2009/05/tomcat-management-use-groovy-to.html

Page 65: Scripting for the JVM using Groovy - Cincinnati … · Scripting for the JVM using Groovy Adil Khan ... (DSL). •Seamless ... • java groovy.lang.GroovyShell path_to/MyScript.groovy

• DSLs-Powerful feature of Groovy to build specific

languages for specific tasks.

• Web Application Frameworks like Grails could be useful

for rapid prototyping of ideas.

• Developers with scripting/non-java background can get

easier access to existing java code base.

Other Topics to Research

Page 66: Scripting for the JVM using Groovy - Cincinnati … · Scripting for the JVM using Groovy Adil Khan ... (DSL). •Seamless ... • java groovy.lang.GroovyShell path_to/MyScript.groovy

Q/A


Recommended