Sunday, May 17, 2009

Extending Java apps using Groovy

Using groovy it's possible to create class that implements interface written in Java. Groovy syntax also inherited from Java syntax thus you can move your existing java code into groovy with minimal changes.

Here is how to produce class from Groovy script.


File scriptFile = new File("implementation.groovy");
GroovyClassLoader gcl = new GroovyClassLoader();
Class clazz = gcl.parseClass(scriptFile);
Object object = clazz.newInstance();


It is possible to put class object (clazz instance in above example) into cache system to enhance performace.

Warn! You need to watch for your app performance on high trafic system and extend your application cautiously.