Message-ID: <2014362932.796184.1386783921827.JavaMail.email@example.com> Subject: Exported From Confluence MIME-Version: 1.0 Content-Type: multipart/related; boundary="----=_Part_796183_496390840.1386783921826" ------=_Part_796183_496390840.1386783921826 Content-Type: text/html; charset=UTF-8 Content-Transfer-Encoding: quoted-printable Content-Location: file:///C:/exported.html
Groovy is a great language just on its own in various scenarios.= It is also extremely useful in mixed Groovy/Java environments. With this i= n mind, Groovy has been designed to be very lightweight and easy to embed i= nto any Java application system.
There are three main approaches for natively integrating Groovy with Jav= a. Each of these is discussed in more detail below.
Alternatively, you can use the Bean Scripting Framework to embed any scripting language in= to your Java code, giving you other language options if you needed them (th= ough we can't imagine why ). Using BSF allows you to be more loosely c= oupled to your scripting language; however, native integration is more ligh= t weight and offers closer integration.
It is often useful to make your groovy scripts extend a base class of yo= ur choosing so that the script can access common methods. This can be achie= ved by setting the script base class property on the compile configuration = and passing this new compiler configuration to the shell.
You can use the GroovyClassL= oader to load classes dynamically into a Java program and execute them = (or use them) directly. The following Java code shows an example:
If you have an interface you wish to use which you implement in the Groo= vy script you can use it as follows:
This works fine if the Groovy class implements the inferface MyInterface= . myObject can from then on be used as every other Java object implementing= MyInterface.
One thing to remember is that the parseClass will try to create an objec= t from your String fileName. Another way to do the gcl.parseClass is:=
Note that all of the error handling has been removed -- you won't be abl= e to do this in a java class. I actually use the Interface invocation for G= roovy inside of a Utility Class.
The most complete solution for people who want to embed groovy scripts i= nto their servers and have them reloaded on modification is the GroovyScrip= tEngine. You initialize the GroovyScriptEngine with a set of CLASSPATH like= roots that can be URLs or directory names. You can then execute any Groovy= script within those roots. The GSE will also track dependencies between sc= ripts so that if any dependent script is modified the whole tree will be re= compiled and reloaded.
Additionally, each time you run a script you can pass in a Binding that = contains properties that the script can access. Any properties set in the s= cript will also be available in that binding after the script has run. Here= is a simple example:
This will print "Hello, world!".
An interactive Groovy interpreter can be embedded in a running applicati= on for experimentation and testing. For a tutorial on how to do, including = example code, see the cookbook example Embedding a Groovy Console in a Java Server Applic= ation.
An example for the integration of Groovy as scripting language into an a= pplication can be found at Integrating Groovy in an application - a= success story
As well as Java 1.4 and the Groovy jar we also depend at runtime on the = ASM library constituted of five jars (asm-2.2.jar, asm-attrs-2.2.jar, asm-a= nalysis-2.2, asm-tree-2.2.jar, and asm-util-2.2.jar) plus the ANTLR library= (antlr-2.7.5.jar). That's it. So just add these 7 jars to your classpath a= nd away you go, you can happily embed Groovy into your application.
Alternatively, instead of several jars, you can use groovy-all-1.0-beta-= x.jar included in the GROOVY_HOME/embeddable directory of your distribution= : this jar contains both Groovy and ASM combined in a single and convenient= archive, with the ASM classes in a different namespace, so conflicts with = other libraries also using ASM will be avoided