Groovy Support has been edited by Felix Meschberger (Dec 10, 2008).

(View changes)

Content:

After meeting Paul King of the Groovy Team at Apache Con US 08 in New Orleans, I set out to take a stab at SLING-315 again to add Groovy support to Sling. It turned out, that the current Groovy 1.6 branch already contains the required setup to build the groovy-all.jar as an OSGi Bundle, which is directly usable with Sling by just installing that bundle.

As of this writing (9. Dec. 2008), there is no downloadable build of the groovy-all.jar containing the correct headers. So here is how to build it yourself and use it with Sling. I have tested groovy-all-1.6-beta-2-20081008.214907-8.jar.

The current builds have a slight issue with the bundle definition, which causes the Groovy bundle to not see classes required by the Groovy scripts. The solution to the problem is adding DynamicImport-Package: * to the bundle manifest. Refer to the section Get and Build Groovy below to how to apply such a patch to the Groovy 1.6 branch for your own build.
Issue SLING-315 will be updated with the newest developments around this issue.

Groovy 1.7 SNAPSHOT Builds

Paul King noted in SLING-315, that precanned builds may also be retrieved from Groovy's CI systems:

You can get a built version of Groovy (1.7 snapshot) from the Cruise CI server. Go to: http://build.canoo.com/groovy/ then 'Build Artifacts' then 'Deliverables'.

There are also TeamCity, Hudson (I think) and Bamboo CI servers. Some of those might also keep build jars.

Alternatively, point to the one from the maven snapshots repo mentioned in an earlier comment.
If you need I can push a 1.6 snapshot into the maven snapshot repo but 1.7 isn't much different to 1.6 at the moment.

In fact the 1.7 Beta 1 SNAPSHOT available from
http://snapshots.repository.codehaus.org/org/codehaus/groovy/groovy-all/1.7-beta-1-SNAPSHOT also work great. I tested with groovy-all-1.7-beta-1-20081118.115206-1.jar.

Get and Build Groovy

Checkout the Groovy 1.6 branch into a separate directory (we use groovy16 here):

$ svn checkout http://svn.codehaus.org/groovy/branches/GROOVY_1_6_X groovy16
$ cd groovy16

Apply the following patch to the build.xml file in the root folder of the checkout:

Index: build.xml
===================================================================
--- build.xml   (Revision 14334)
+++ build.xml   (Arbeitskopie)
@@ -496,6 +496,7 @@
                 <attribute name="Bundle-RequiredExecutionEnvironment" value="@{bundleEnvironment}" />
                 <attribute name="Eclipse-BuddyPolicy" value="dependent"/>
                 <attribute name="Eclipse-LazyStart" value="true"/>
+                <attribute name="DynamicImport-Package" value="*"/>
             </manifest>
         </sequential>
     </macrodef>

Next build groovy using Apache Ant (Ant 1.7 or higher is required)

$ ant install

If you don't want to run the tests, which may take quite some time, build Groovy using the -DskipTests=true option:

$ ant install -DskipTests=true

Wait for the process to complete and you find the groovy-all-1.6-RC-1-SNAPSHOT.jar file in the target/install/embeddable folder.

Deployment

To deploy the bundle go to the Bundles page, for example at http\://localhost:8888/system/console/bundles of the Apache Felix Web Console select the bundle file to upload, check the Start check box and click Install or Update button.

You may check, whether the Groovy ScriptEngine has been "accepted" by Sling, by going to the Script Engines page of the Apache Felix Web Console. You should see the entry for Groovy there, somthing like this:

Groovy Scripting Engine, 2.0
  Language      Groovy,
  Extensions    groovy
  MIME Types    application/x-groovy
  Names         groovy, Groovy

Testing

To test create a simple Groovy script, for example

response.setContentType("text/plain");
response.setCharacterEncoding("UTF-8");

println "Hello World !"
println "This is Groovy Speaking"
println "You requested the Resource ${resource} (yes, this is a GString)"

and upload it to the repository as /apps/nt/folder/GET.groovy using your favourite WebDAV client or use curl to upload the file (assuming Sling is running on localhost:8888) :

$ curl -u admin:admin [EMAIL PROTECTED] -F../../nt/jcr:primaryType=sling:Folder http:host:8888/apps/nt/folder

To test it create a /sample nt:Folder node using your favourite WebDAV client or use curl again:

$ curl -u admin:admin -Fjcr:primaryType=nt:folder http://localhost:8888/

Finally, request the /sample node using your favourite Browser or use curl again:

$ curl http://localhost:8888/sample
Hello World !
This is Groovy Speaking
You requested Resource JcrNodeResource, type=nt:folder, path=/sample (yes, this is a GString)

References

  • SLING-315 – The initial Sling issue proposing the addition of a Groovy ScriptEngine to Sling.
  • Groovy Support in Apache Sling – A short thread on turning the Groovy groovy-all.jar into an OSGi Bundle.
  • Groovy in Apache Sling – Thread on adding the DynamicImport-Package header to the Groovy bundle manifest.

Reply via email to