• Save
Embedding Groovy in a Java Application
Upcoming SlideShare
Loading in...5
×
 

Embedding Groovy in a Java Application

on

  • 3,994 views

 

Statistics

Views

Total Views
3,994
Views on SlideShare
3,973
Embed Views
21

Actions

Likes
1
Downloads
0
Comments
0

4 Embeds 21

http://www.linkedin.com 12
http://localhost 5
https://www.linkedin.com 3
https://twitter.com 1

Accessibility

Categories

Upload Details

Uploaded via as Adobe PDF

Usage Rights

© All Rights Reserved

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Processing…
Post Comment
Edit your comment

Embedding Groovy in a Java Application Embedding Groovy in a Java Application Presentation Transcript

  • EMBEDDING GROOVY IN A JAVA APPLICATION paolo.predonzani@manydesigns.com
  • Java-Groovy integration Compiling Embedding (loading at run-time) BSF and JSR 223 Native embeddingGroovyShell GroovyClassLoader GroovyScriptEngine
  • COMPILING MyClass.groovy MyClass.class Maven AntJoint compilation: Java references Groovy and vice versa View slide
  • BEAN SCRIPTING FRAMEWORK AND JSR 223http://commons.apache.org/bsf/http://groovy.codehaus.org/Bean+Scripting+Frameworkhttp://groovy.codehaus.org/JSR+223+Scripting+with+Groovy View slide
  • NATIVE EMBEDDING GroovyScript Engine Scripts + GroovyClass classpath + Loader reload + Introduces the dependencyGroovyShell concept of management classpathSimple isolated scripts
  • BEHIND THE SCENES...GroovyShell GroovyScriptEngine ScriptClassLoader dependency mgmt. GroovyClassLoader
  • GROOVYSHELLGroovyShell .evaluate(String) Object .run(File, String[]) Object Script .parse(File) .run() .invokeMethod() Object
  • BINDING
  • GROOVYCLASSLOADERGroovyClass Loader .addClassPath(“/home/groovy”) .loadClass(“com.manydesigns.Foo”) Class .parseClass(“/home/groovy/com/manydesigns/Foo.groovy”) Class
  • GROOVYSCRIPTENGINEGroovyScript 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 WITHSCRIPTS AND CLASSES SEPARATELY } Classpath, packages, classes, reuse, engineering } Scripts, files, “spot” usage, hackingGroovy 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 classloaderGroovyClassLoader 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: OGNLOGNL: %{@org.example.MyGroovyClass@myMethod()}... does not work because OGNL uses the Java classloader and cannot resolve Groovy classes
  • CLASS LOADING: HIBERNATEIn Groovy this works:From Java it works only if we retrieve Book from theGroovyClassLoader
  • 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 ReloadingGroovyShell.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 reloadedIn theory, but until Groovy 1.8.8/2.0.4 there was a bug:http://jira.codehaus.org/browse/GROOVY-4975
  • REFERENCEShttp://groovy.codehaus.org/Embedding+Groovyhttp://groovy.codehaus.org/The+groovyc+Ant+Taskhttp://docs.codehaus.org/display/GMAVEN/Homehttp://groovy.codehaus.org/api/groovy/lang/GroovyShell.htmlhttp://groovy.codehaus.org/api/groovy/lang/GroovyClassLoader.htmlhttp://groovy.codehaus.org/api/groovy/util/GroovyScriptEngine.html
  • THANK YOU!paolo.predonzani@manydesigns.com http://www.manydesigns.com/