Publicidad

Embedding Groovy in a Java Application

Java developer
7 de Dec de 2012
Publicidad

Más contenido relacionado

Publicidad
Publicidad

Embedding Groovy in a Java Application

  1. EMBEDDING GROOVY IN A JAVA APPLICATION paolo.predonzani@manydesigns.com
  2. Java-Groovy integration Compiling Embedding (loading at run-time) BSF and JSR 223 Native embedding GroovyShell GroovyClassLoader GroovyScriptEngine
  3. COMPILING MyClass.groovy MyClass.class Maven Ant Joint compilation: Java references Groovy and vice versa
  4. 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
  5. NATIVE EMBEDDING GroovyScript Engine Scripts + GroovyClass classpath + Loader reload + Introduces the dependency GroovyShell concept of management classpath Simple isolated scripts
  6. BEHIND THE SCENES... GroovyShell GroovyScriptEngine ScriptClassLoader dependency mgmt. GroovyClassLoader
  7. GROOVYSHELL GroovyShell .evaluate(String) Object .run(File, String[]) Object Script .parse(File) .run() .invokeMethod() Object
  8. BINDING
  9. GROOVYCLASSLOADER GroovyClass Loader .addClassPath(“/home/groovy”) .loadClass(“com.manydesigns.Foo”) Class .parseClass(“/home/groovy/com/manydesigns/Foo.groovy”) Class
  10. GROOVYSCRIPTENGINE GroovyScript Engine GroovyScriptEngine(String[] roots) .loadScriptByName(String) Class .run(String, Binding) Object Script .createScript(String) .run()
  11. SCRIPT == CLASS Script run() def f() {} main() println “Hello world!” f()
  12. 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!
  13. GROOVY IN PORTOFINO 4 Classpath for common groovy classes Groovy actions
  14. 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.
  15. 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
  16. CLASS LOADING ClassLoader Loads Java classes only parent classloader GroovyClassLoader Loads Java & Groovy classes
  17. This is the main problem of embedding. If Groovy was compiled, Java and Groovy would live in the same class loader
  18. CLASS LOADING: OGNL OGNL: %{@org.example.MyGroovyClass@myMethod()} ... does not work because OGNL uses the Java classloader and cannot resolve Groovy classes
  19. CLASS LOADING: HIBERNATE In Groovy this works: From Java it works only if we retrieve Book from the GroovyClassLoader
  20. 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
  21. PERFORMANCE: CONCLUSIONS Caching Reloading GroovyShell.evaluate(String) No -- GroovyShell.run(File) Yes No GroovyClassLoader Yes No GroovyScriptEngine Yes Yes
  22. 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
  23. 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
  24. THANK YOU! paolo.predonzani@manydesigns.com http://www.manydesigns.com/
Publicidad