Groovymonkey
We have introduced Groovy in or product about a year ago as a scripting mechanism for power users. A year later it became a key component and we use it everywhere.
It is important to understand that our developers do not write Groovy code, it is only used as a way for end-users to extend the behavior of the application.
It would be too complicated to explain the product I am working on and it is also more interesting to think about what can be done rather than what we have done.
Imagine what you could do if you could write scripts in your email application. The application could be fully written in Java and you as an end-user could write scripts that act like filters on your inbox but could do much more than moving emails to folders.
Isn’t it exciting? Groovy enables in your application what Greasemonkey does for Firefox.
You can write Groovy code just like you write Java. It has classes and methods just like Java. In fact, you could rename a .java into .groovy and in many cases it will work without changing anything.
Groovy also works in scripted mode. It means that you can execute a piece of code out of a class/method context.
The scripted mode can be invoked from Java. That is the mode that you would use to implement end-user scripting.
new GroovyShell().evaluate("System.out.println(\"Hello World\");");
This very simple example shows how easy it is to run a script but also shows the seamless integration between the script and your application. The System.out referenced in the script is THE Sytem.out.
The script would not be very useful without input.
Here is how you you would pass variables:
GroovyShell shell = new GroovyShell();
shell.setVariable("message", "Hello World");
shell.evaluate("System.out.println(message);");
And you can get data back either by using getVariable() or using the returned value:
GroovyShell shell = new GroovyShell();
shell.setVariable("message", "Hello");
Object o = shell.evaluate("return message + \" World\";");
System.out.println(o);
There is much much more to write about Groovy. The language is a really nice extension to Java. I used Java-like syntax in the examples but Groovy extends Java and allows a lot of shortcuts that could have been used in the examples. Semicolons are optional, println() can replace System.out.println(), variables can be referenced inside strings ("Hello $name").
I have only written a dozen of scripts and I am already hooked to closures.
If you do not know Groovy, I would encourage you to read about the language and if you are doing Web development, you should also have a look at Grails.