21 Nov, 2009
Different Flavors of Embedded Groovy in Java Apps or “How To Make your Java Groovier!”
Posted by: TheKaptain In: Development
Lately I’ve been thinking about all the different ways to bring Groovy into a pure Java or command line environment, and ended up diving into some code to explore the various options. Turns out there’s definitely a good variety of options for running Groovy dynamically inside and out of a Java application. I started out on this page from the Groovy site.
In particular for the environments I’ve been working in lately it’s been important to be able to run the same code both from within a Java application and from the command line. It’s also been a ‘nice to have’ to be able to package a jar with a bunch of the same scripts compiled together. Using maven as a harness also has the benefit of allowing for testing compiled scripts directly through instantiation even though the intended usage is from within a Java app using one of these methods. Source code is available here on github.
Groovy on the command line
The quickest and simplest way to run a Groovy Script or Class, command line arguments are automatically marshalled into an ‘args’ String array. Please note that due to a problem I’m having with my syntax highlighter plugin the process execution is shown here in single quotes; the actual code requires a GString(double quoted) in order to do the replacement for the inline variable. "
THAT WordPress!
[groovy]
//the script
myArgs = args
result = args.join(‘ ‘)
println result
println myArgs
//…and the test
void testGroovyCall()
{
def proc = ‘groovy $groovyScriptOne Hello World’.execute()
proc.waitFor()
def result = proc.text.split()
assert result[0] == ‘Hello’
assert result[1] == ‘World’
}
[/groovy]
GroovyShell
This is the basis of Groovy script execution. The GroovyShell allows for executing scripts, passing in a particular Binding context that allows for bi-directional communication between the script and the calling code. Parameters can be passed into the executing script in the Binding and results can be stored there to be returned to the calling context. GroovyShell also allows for running a class from the ‘main’ method, passing in String arguments. It will also execute implementers of Runnable and test files for JUnit or TestNG. Script text can also be declared inline and executed in the same way as files on disk. All in all, pretty bloody handy. Here’s a straightforward example of running a dirt simple Groovy script and inspecting the results. Note that this isn’t executable as shown, but I’ll provide the full source code on github for anyone who wants a closer look. Note that I’m also passing in an ‘out’ variable in the Binding, which effectively redirect System.out to a specified Writer implementation – a nice touch for inspecting output.
[groovy language=”true”]
//the script
myArgs = args
result = args.join(‘ ‘)
println result
println myArgs
//…and the test
void testGroovyShell()
{
Binding binding = helper.createBinding()
def shell = new GroovyShell(binding)
shell.evaluate(new File(groovyScriptOne))
helper.assertBinding(binding)
}
//…and the Binding creation/assertion
def static args = [‘Hello’, ‘World’].asImmutable()
/**
* Create a Binding with a single parameter to be passed to scripts and an ‘out’ Writer to redirect console output.
*/
private Binding createBinding()
{
Binding binding = new Binding()
def sWriter = new StringWriter()
def pWriter = new PrintWriter(sWriter)
binding.setVariable (‘args’, new ArrayList(args))
binding.setVariable (‘out’, pWriter)
return binding
}
/**
* Assert that the expected ‘common’ actions are done with the Binding by each of the use cases.
* The original ‘args’ should be as expected.
* A copy of ‘args’ should have been placed in the Binding during execution.
* The ‘result’ should be the concatentation of ‘args’ separated by spaces.
*/
private def assertBinding(Binding binding)
{
assert binding.variables.size() == 4
assert binding.variables.args.value[0].toString() == args[0]
assert binding.variables.args.value[1].toString() == args[1]
assert binding.variables.result.value.toString() == args.join(‘ ‘)
assert binding.variables.myArgs.value[0].toString() == args[0]
assert binding.variables.myArgs.value[1].toString() == args[1]
}
[/groovy]
GroovyScriptEngine
The GroovyScriptEngine enables dynamically running Groovy sources located in a fixed set of content roots, complete with reloading modified scripts in between executions. Running a Groovy script this way is essentially the same as using GroovyShell.
[groovy language=”true”]
void testGroovyScriptEngine()
{
Binding binding = helper.createBinding()
def gse = new GroovyScriptEngine(new File(‘.’).toURL())
gse.run(groovyScriptOne, binding)
helper.assertBinding(binding)
}
[/groovy]
GroovyClassLoader
An extension to URLClassLoader that enables parsing Groovy sources into Class representations. Once a Class object is created, instances of the class can be created easily and either cast to a known type or manipulated through convention by use of the standard Groovy ‘invokeMethod’. This works equally well on Groovy and Java btw. Here’s an example of running a Java class using GroovyClassLoader. In this case the Java file has a field called ‘binding’ and implements a ‘run’ method.
[groovy language=”true”]
/**
* Dynamically compile, instantiate, inspect and call methods on a POJO.
*/
void testGroovyClassLoaderOnJava()
{
GroovyClassLoader loader = new GroovyClassLoader();
Class javaClass = loader.parseClass(new File(javaFileOne));
def groovyObject = javaClass.newInstance();
def binding = helper.createBinding()
groovyObject.binding = binding
if(groovyObject.metaClass.respondsTo(groovyObject, ‘run’))
{
groovyObject.invokeMethod(‘run’, null);
helper.assertBinding(binding)
}
if(groovyObject.metaClass.respondsTo(groovyObject, ‘main’))
{
groovyObject.invokeMethod(‘main’, new ArrayList(helper.args) as String[]);
}
}
[/groovy]
(Groovy)Console
The Console can be embedded in Java or Groovy code to provide a dynamic interactive Swing environment. This is the same UI spawned from the command line invocation of ‘groovyConsole’. Internally it uses GroovyShell for actual execution, and so can do everything that GroovyShell can do – plus a couple of additions. For one, you can add jars and/or directories to the classpath used when executing your scripts.
The Best of Both Worlds – at Least for my use case
In actual practice these patterns can be used a lot more successfully by observing standard Java practices, like casting classes parsed using GroovyClassLoader to a known interface before interacting with them, or by using Classes to organize business logic inside of a Script that essentially functions as a ‘main’ method. This example defines two dependent internal classes, marshals parameters to them and then returns the results attached to the originally passed in Binding.
[groovy language=”true”]
/**
* Classes inside of a Script.
*/
class TestableClass
{
Binding binding
def run()
{
binding.with
{
setVariable(‘myArgs’, getVariable(‘args’))
setVariable(‘result’, getVariable(‘args’)?.join(‘ ‘))
}
return binding
}
}
class TestableClass2
{
Binding binding
public TestableClass2(Binding binding)
{
this.binding = binding;
}
def run()
{
return new TestableClass(binding: binding).run()
}
}
if (args)
{
def internalBinding = new Binding()
internalBinding.setVariable(‘args’, new ArrayList(args))
internalBinding = new TestableClass2(internalBinding).run()
args = internalBinding.args
myArgs = internalBinding.myArgs
result = internalBinding.result //return value from script
}
else
{
println ‘no args!!’
}
[/groovy]
[table id=1]