Message-ID: <1777979365.1727.1411272220509.JavaMail.firstname.lastname@example.org> Subject: Exported From Confluence MIME-Version: 1.0 Content-Type: multipart/related; boundary="----=_Part_1726_686886570.1411272220508" ------=_Part_1726_686886570.1411272220508 Content-Type: text/html; charset=UTF-8 Content-Transfer-Encoding: quoted-printable Content-Location: file:///C:/exported.html
Groovy integrates cleanly with BSF (the Bean Script=
ing Framework) which allows you to embed any scripting engine into your Jav=
a code while keeping your Java code decoupled from any particular scripting=
The BSF engine for Groovy is implemented by the GroovyEngine class; ho= wever, that fact is normally hidden away by the BSF APIs. You just treat Gr= oovy like any of the other scripting languages via the BSF API.
Note:= Groovy has its own native support for integration with Java. See Embedding Groovy for further deta= ils. So you only need to worry about BSF if you want to also be able to cal= l other languages from Java, e.g. JRuby or if you want to remain v= ery loosely coupled from your scripting language.
Provided you have Groovy and BSF jars in your classpath, you can use the= following Java code to run a sample Groovy script:=20 =20
BSF lets you pass beans between Java and your scripting language. You ca= n register/unregister beans which makes them known to BSF= . You can then use BSF methods to lookup beans as required. Altern= atively, you can declare/undeclare beans. This will regis= ter them but also make them available for use directly in your scripting la= nguage. This second approach is the normal approach used with Groovy. Here = is an example:=20 =20
The previous examples used the eval method. BSF makes multiple = methods available for your use (see the BSF documentati= on for more details). One of the other available methods is apply= em>. It allows you to define an anonymous function in your scripting langua= ge and apply that function to arguments. Groovy supports this function usin= g closures. Here is an example:=20 =20
Although you don't normally need it, BSF does provide a hook that lets y= ou get directly to the scripting engine. One of the functions which the eng= ine can perform is to invoke a single method call on an object. Here is an = example:=20 =20
If you must integrate with early version of BSF (i.e. prior to b= sf 2.3.0-rc2) then you'll need to manually register the Groovy lan= guage with BSF using the following snippet of code:=20