SlideShare a Scribd company logo
1 of 29
EMBEDDING GROOVY IN A
   JAVA APPLICATION
   paolo.predonzani@manydesigns.com
Java-Groovy integration


      Compiling                   Embedding
                             (loading at run-time)


   BSF and JSR 223            Native embedding




GroovyShell   GroovyClassLoader        GroovyScriptEngine
COMPILING
    MyClass.groovy                 MyClass.class

       Maven                           Ant




Joint compilation: Java references Groovy and vice versa
BEAN SCRIPTING
  FRAMEWORK AND JSR 223




http://commons.apache.org/bsf/
http://groovy.codehaus.org/Bean+Scripting+Framework
http://groovy.codehaus.org/JSR+223+Scripting+with+Groovy
NATIVE EMBEDDING
                                   GroovyScript
                                      Engine

                                    Scripts +
                  GroovyClass
                                   classpath +
                    Loader
                                    reload +
                  Introduces the   dependency
GroovyShell         concept of     management
                     classpath
Simple isolated
    scripts
BEHIND THE SCENES...
GroovyShell                 GroovyScriptEngine


                      ScriptClassLoader

                                          dependency
                                            mgmt.


              GroovyClassLoader
GROOVYSHELL

GroovyShell
              .evaluate(String)                              Object


              .run(File, String[])                           Object

                                  Script
              .parse(File)
                                           .run()

                                           .invokeMethod()   Object
BINDING
GROOVYCLASSLOADER

GroovyClass
  Loader    .addClassPath(“/home/groovy”)


            .loadClass(“com.manydesigns.Foo”)               Class


            .parseClass(“/home/groovy/com/manydesigns/Foo.groovy”)


                                                            Class
GROOVYSCRIPTENGINE

GroovyScript
   Engine    GroovyScriptEngine(String[] roots)


            .loadScriptByName(String)                      Class


            .run(String, Binding)                          Object

                                        Script
            .createScript(String)                 .run()
SCRIPT == CLASS
                                  Script


                         run()

def f() {}               main()

println “Hello world!”   f()
IT MAKES SENSE TO DEAL WITH
SCRIPTS AND CLASSES SEPARATELY


                        }   Classpath, packages, classes,
                            reuse, engineering


                        }   Scripts, files, “spot” usage,
                            hacking


Groovy is groovy because it supports both perspectives!
GROOVY IN PORTOFINO 4
           Classpath for
           common groovy
           classes



           Groovy actions
Groovy actions:
  URL structure same as directory
  structure
  One action.groovy per directory
  Packages are not required
  URL’s can be moved/renamed
  by moving/renaming the
  directories
  Nobody can depend on an
  action
  No refactoring is required
  Editable from browser or IDE

  Each action.groovy is self-
  contained. We treat it as a script.
Groovy classpath:
  A real classpath with packages
  and classes
  Contains beans, base classes for
  actions, utility classes invokable
  by actions
  Typically edited from an IDE
  Refactoring

  Groovy classpath: similar to a
  Java classpath but dynamically
  reloadable
CLASS LOADING

   ClassLoader        Loads Java classes only

           parent
        classloader
GroovyClassLoader     Loads Java & Groovy classes
This is the main problem of embedding.
If Groovy was compiled, Java and Groovy
    would live in the same class loader
CLASS LOADING: OGNL

OGNL: %{@org.example.MyGroovyClass@myMethod()}


... does not work because OGNL uses the Java classloader
             and cannot resolve Groovy classes
CLASS LOADING: HIBERNATE
In Groovy this works:




From Java it works only if we retrieve Book from the
GroovyClassLoader
PERFORMANCE:
CACHING & RELOADING

  Test strategy:
  1. “engine warm up”
  2. measure the first run
  3. measure the second run
  4. “touch” the script
  5. measure the third run
PERFORMANCE:
               CONCLUSIONS
                               Caching   Reloading

GroovyShell.evaluate(String)     No         --

   GroovyShell.run(File)         Yes       No

    GroovyClassLoader            Yes       No

    GroovyScriptEngine           Yes       Yes
DEPENDENCY RELOADING
       A
                    A depends on B
                    B is modified
                    B is reloaded
       B
                    Also A should be reloaded


In theory, but until Groovy 1.8.8/2.0.4 there was a bug:
http://jira.codehaus.org/browse/GROOVY-4975
REFERENCES
http://groovy.codehaus.org/Embedding+Groovy

http://groovy.codehaus.org/The+groovyc+Ant+Task

http://docs.codehaus.org/display/GMAVEN/Home

http://groovy.codehaus.org/api/groovy/lang/GroovyShell.html

http://groovy.codehaus.org/api/groovy/lang/
GroovyClassLoader.html

http://groovy.codehaus.org/api/groovy/util/
GroovyScriptEngine.html
THANK YOU!
paolo.predonzani@manydesigns.com

  http://www.manydesigns.com/

More Related Content

What's hot

vmware_cloud_foundation_on_vxrail_technical_customer_presentation.pptx
vmware_cloud_foundation_on_vxrail_technical_customer_presentation.pptxvmware_cloud_foundation_on_vxrail_technical_customer_presentation.pptx
vmware_cloud_foundation_on_vxrail_technical_customer_presentation.pptx
VitNguyn252054
 
Amazon ECS를 통한 도커 기반 콘테이너 서비스 구축하기 - AWS Summit Seoul 2017
Amazon ECS를 통한 도커 기반 콘테이너 서비스 구축하기 - AWS Summit Seoul 2017Amazon ECS를 통한 도커 기반 콘테이너 서비스 구축하기 - AWS Summit Seoul 2017
Amazon ECS를 통한 도커 기반 콘테이너 서비스 구축하기 - AWS Summit Seoul 2017
Amazon Web Services Korea
 
Test Automation Strategy
Test Automation StrategyTest Automation Strategy
Test Automation Strategy
Martin Ruddy
 

What's hot (20)

Using gcov and lcov
Using gcov and lcovUsing gcov and lcov
Using gcov and lcov
 
쿠알못이 Amazon EKS로 안정적인 서비스 운영하기 - 최용호(넥슨코리아) :: AWS Community Day 2020
쿠알못이 Amazon EKS로 안정적인 서비스 운영하기 - 최용호(넥슨코리아) :: AWS Community Day 2020쿠알못이 Amazon EKS로 안정적인 서비스 운영하기 - 최용호(넥슨코리아) :: AWS Community Day 2020
쿠알못이 Amazon EKS로 안정적인 서비스 운영하기 - 최용호(넥슨코리아) :: AWS Community Day 2020
 
快速上手 Windows Containers 容器技術 (Docker Taipei)
快速上手 Windows Containers 容器技術 (Docker Taipei)快速上手 Windows Containers 容器技術 (Docker Taipei)
快速上手 Windows Containers 容器技術 (Docker Taipei)
 
vmware_cloud_foundation_on_vxrail_technical_customer_presentation.pptx
vmware_cloud_foundation_on_vxrail_technical_customer_presentation.pptxvmware_cloud_foundation_on_vxrail_technical_customer_presentation.pptx
vmware_cloud_foundation_on_vxrail_technical_customer_presentation.pptx
 
이스티오 (Istio) 자습서 v0.5.0
이스티오 (Istio) 자습서 v0.5.0이스티오 (Istio) 자습서 v0.5.0
이스티오 (Istio) 자습서 v0.5.0
 
Docker and Go: why did we decide to write Docker in Go?
Docker and Go: why did we decide to write Docker in Go?Docker and Go: why did we decide to write Docker in Go?
Docker and Go: why did we decide to write Docker in Go?
 
Securing enterprise-grade serverless applications - SDD401 - AWS re:Inforce 2...
Securing enterprise-grade serverless applications - SDD401 - AWS re:Inforce 2...Securing enterprise-grade serverless applications - SDD401 - AWS re:Inforce 2...
Securing enterprise-grade serverless applications - SDD401 - AWS re:Inforce 2...
 
Using Mockito
Using MockitoUsing Mockito
Using Mockito
 
Softwaretesting
SoftwaretestingSoftwaretesting
Softwaretesting
 
[Infographic] Challenges in Test Automation
[Infographic] Challenges in Test Automation [Infographic] Challenges in Test Automation
[Infographic] Challenges in Test Automation
 
Amazon ECS를 통한 도커 기반 콘테이너 서비스 구축하기 - AWS Summit Seoul 2017
Amazon ECS를 통한 도커 기반 콘테이너 서비스 구축하기 - AWS Summit Seoul 2017Amazon ECS를 통한 도커 기반 콘테이너 서비스 구축하기 - AWS Summit Seoul 2017
Amazon ECS를 통한 도커 기반 콘테이너 서비스 구축하기 - AWS Summit Seoul 2017
 
Test Automation Strategy
Test Automation StrategyTest Automation Strategy
Test Automation Strategy
 
Advanced networking on AWS | AWS Floor28
Advanced networking on AWS | AWS Floor28Advanced networking on AWS | AWS Floor28
Advanced networking on AWS | AWS Floor28
 
Risk-based Testing
Risk-based TestingRisk-based Testing
Risk-based Testing
 
Aws auto scalingによるwebapサーバbatchサーバの構成例
Aws auto scalingによるwebapサーバbatchサーバの構成例Aws auto scalingによるwebapサーバbatchサーバの構成例
Aws auto scalingによるwebapサーバbatchサーバの構成例
 
ISTQB PROJELERDE HATA YÖNETİMİ
ISTQB PROJELERDE HATA YÖNETİMİISTQB PROJELERDE HATA YÖNETİMİ
ISTQB PROJELERDE HATA YÖNETİMİ
 
Introducing QA Into an Agile Environment
Introducing QA Into an Agile EnvironmentIntroducing QA Into an Agile Environment
Introducing QA Into an Agile Environment
 
GraalVM
GraalVMGraalVM
GraalVM
 
Container Security
Container SecurityContainer Security
Container Security
 
Head first docker
Head first dockerHead first docker
Head first docker
 

Similar to Embedding Groovy in a Java Application

Groovy Online 100
Groovy Online 100Groovy Online 100
Groovy Online 100
reynolds
 
JavaOne 2008 - TS-5793 - Groovy and Grails, changing the landscape of Java EE...
JavaOne 2008 - TS-5793 - Groovy and Grails, changing the landscape of Java EE...JavaOne 2008 - TS-5793 - Groovy and Grails, changing the landscape of Java EE...
JavaOne 2008 - TS-5793 - Groovy and Grails, changing the landscape of Java EE...
Guillaume Laforge
 
Grails Internals Ggug Dec 2009
Grails Internals Ggug Dec 2009Grails Internals Ggug Dec 2009
Grails Internals Ggug Dec 2009
Skills Matter
 
cdac@parag.gajbhiye@groovy metaprogrammning
cdac@parag.gajbhiye@groovy metaprogrammningcdac@parag.gajbhiye@groovy metaprogrammning
cdac@parag.gajbhiye@groovy metaprogrammning
Parag Gajbhiye
 

Similar to Embedding Groovy in a Java Application (20)

Groovy & Grails: Scripting for Modern Web Applications
Groovy & Grails: Scripting for Modern Web ApplicationsGroovy & Grails: Scripting for Modern Web Applications
Groovy & Grails: Scripting for Modern Web Applications
 
Groovy Finesse
Groovy FinesseGroovy Finesse
Groovy Finesse
 
Groovy Finesse
Groovy FinesseGroovy Finesse
Groovy Finesse
 
Test Driven In Groovy
Test Driven In GroovyTest Driven In Groovy
Test Driven In Groovy
 
Groovy - Grails as a modern scripting language for Web applications
Groovy - Grails as a modern scripting language for Web applicationsGroovy - Grails as a modern scripting language for Web applications
Groovy - Grails as a modern scripting language for Web applications
 
Groovy Online 100
Groovy Online 100Groovy Online 100
Groovy Online 100
 
JavaOne 2008 - TS-5793 - Groovy and Grails, changing the landscape of Java EE...
JavaOne 2008 - TS-5793 - Groovy and Grails, changing the landscape of Java EE...JavaOne 2008 - TS-5793 - Groovy and Grails, changing the landscape of Java EE...
JavaOne 2008 - TS-5793 - Groovy and Grails, changing the landscape of Java EE...
 
What's New in Groovy 1.6?
What's New in Groovy 1.6?What's New in Groovy 1.6?
What's New in Groovy 1.6?
 
Grails Internals Ggug Dec 2009
Grails Internals Ggug Dec 2009Grails Internals Ggug Dec 2009
Grails Internals Ggug Dec 2009
 
Android Bootstrap
Android BootstrapAndroid Bootstrap
Android Bootstrap
 
An Introduction to Gradle for Java Developers
An Introduction to Gradle for Java DevelopersAn Introduction to Gradle for Java Developers
An Introduction to Gradle for Java Developers
 
Groovy and Grails in Action - Devoxx 2008 - University - Guillaume Laforge
Groovy and Grails in Action - Devoxx 2008 - University - Guillaume LaforgeGroovy and Grails in Action - Devoxx 2008 - University - Guillaume Laforge
Groovy and Grails in Action - Devoxx 2008 - University - Guillaume Laforge
 
Startup groovysession1
Startup groovysession1Startup groovysession1
Startup groovysession1
 
Dsl로 만나는 groovy
Dsl로 만나는 groovyDsl로 만나는 groovy
Dsl로 만나는 groovy
 
Grooscript gr8conf 2015
Grooscript gr8conf 2015Grooscript gr8conf 2015
Grooscript gr8conf 2015
 
Taming Functional Web Testing with Spock and Geb
Taming Functional Web Testing with Spock and GebTaming Functional Web Testing with Spock and Geb
Taming Functional Web Testing with Spock and Geb
 
Grooscript gr8conf
Grooscript gr8confGrooscript gr8conf
Grooscript gr8conf
 
Gradle in 45min
Gradle in 45minGradle in 45min
Gradle in 45min
 
Svcc Groovy Testing
Svcc Groovy TestingSvcc Groovy Testing
Svcc Groovy Testing
 
cdac@parag.gajbhiye@groovy metaprogrammning
cdac@parag.gajbhiye@groovy metaprogrammningcdac@parag.gajbhiye@groovy metaprogrammning
cdac@parag.gajbhiye@groovy metaprogrammning
 

More from Paolo Predonzani

Come Incorporare un Interprete Groovy in un Applicativo Java
Come Incorporare un Interprete Groovy in un Applicativo JavaCome Incorporare un Interprete Groovy in un Applicativo Java
Come Incorporare un Interprete Groovy in un Applicativo Java
Paolo Predonzani
 
Come Incorporare un Interprete Groovy in Java
Come Incorporare un Interprete Groovy in JavaCome Incorporare un Interprete Groovy in Java
Come Incorporare un Interprete Groovy in Java
Paolo Predonzani
 

More from Paolo Predonzani (8)

Wiki-like collaborative development for seamless customer involvement
Wiki-like collaborative development for seamless customer involvementWiki-like collaborative development for seamless customer involvement
Wiki-like collaborative development for seamless customer involvement
 
How to put 100k lines of code into the (Google) cloud: storms and rainbows
How to put 100k lines of code into the (Google) cloud: storms and rainbowsHow to put 100k lines of code into the (Google) cloud: storms and rainbows
How to put 100k lines of code into the (Google) cloud: storms and rainbows
 
Portofino 4: Creare Webapp da Database Esistenti in 30 Secondi
Portofino 4: Creare Webapp da Database Esistenti in 30 SecondiPortofino 4: Creare Webapp da Database Esistenti in 30 Secondi
Portofino 4: Creare Webapp da Database Esistenti in 30 Secondi
 
Come Incorporare un Interprete Groovy in un Applicativo Java
Come Incorporare un Interprete Groovy in un Applicativo JavaCome Incorporare un Interprete Groovy in un Applicativo Java
Come Incorporare un Interprete Groovy in un Applicativo Java
 
Come Incorporare un Interprete Groovy in Java
Come Incorporare un Interprete Groovy in JavaCome Incorporare un Interprete Groovy in Java
Come Incorporare un Interprete Groovy in Java
 
Logging
LoggingLogging
Logging
 
70k linee di codice, tangle architetturali e le sfide del refactoring
70k linee di codice, tangle architetturali e le sfide del refactoring70k linee di codice, tangle architetturali e le sfide del refactoring
70k linee di codice, tangle architetturali e le sfide del refactoring
 
Model Driven Engineering - ManyDesigns Portofino
Model Driven Engineering - ManyDesigns PortofinoModel Driven Engineering - ManyDesigns Portofino
Model Driven Engineering - ManyDesigns Portofino
 

Recently uploaded

IAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI SolutionsIAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI Solutions
Enterprise Knowledge
 

Recently uploaded (20)

Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
 
Scaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationScaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organization
 
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationFrom Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
 
2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...
 
What Are The Drone Anti-jamming Systems Technology?
What Are The Drone Anti-jamming Systems Technology?What Are The Drone Anti-jamming Systems Technology?
What Are The Drone Anti-jamming Systems Technology?
 
How to convert PDF to text with Nanonets
How to convert PDF to text with NanonetsHow to convert PDF to text with Nanonets
How to convert PDF to text with Nanonets
 
Boost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivityBoost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivity
 
A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?
 
A Call to Action for Generative AI in 2024
A Call to Action for Generative AI in 2024A Call to Action for Generative AI in 2024
A Call to Action for Generative AI in 2024
 
Automating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps ScriptAutomating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps Script
 
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
 
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected Worker
 
Handwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed textsHandwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed texts
 
Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)
 
08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men
 
Boost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfBoost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdf
 
IAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI SolutionsIAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI Solutions
 
Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024
 
Presentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreterPresentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreter
 

Embedding Groovy in a Java Application