Tuesday, July 15, 2008

JavaScript and Groovy service functions in AltioLive

As discussed in the Using AltioLive with JavaScript whitepaper (http://developers.altio.com/developer/library/JavaScriptwithAltio.pdf), Java 6.0 has built in support for dynamic scripting languages, such as JavaScript and Groovy, within the JVM (referred to as JSR 223).

That document discussed integrating that functionality with the AltioLive client as a custom control, but you can also utilize the same feature to add support for implementing AltioLive Service Functions with scripting languages. We'll take a quick look at how to implement this in Java 6.0 and earlier JVMs by using the Apache Bean Scripting Framework (BSF).

All that is required is a simple Java service function proxy to handle calling a script and returning the results. There are 2 service functions, one for Java 6 and one for BSF, and the source code is included with the Altio script service function JAR file (http://developers.altio.com/developer/downloads/scriptsvcfunc.jar). Each service function takes the same parameters:
  • Engine: Name of the script engine to use. e.g. javascript, js, groovy
  • Script: Name of the script to execute
  • Function: Name of the function in the script to execute
The examples here are in JavaScript and Groovy, but other scripting languages are supported in both approaches, such as Ruby and Python.

The advantages to using a scripting language for custom Altio Service Functions instead of Java are:
  • Quicker development time (no IDE or compiler required)
  • Take advantage of particular langauge features to perform tasks. Some languages are better at string handling, XML parsing etc and are better suited for particular tasks than Java.
The examples here are in JavaScript and Groovy, but other scripting languages are supported in both approaches, such as Ruby and Python.

Setup:

1 - Either:
  • Run the AltioLive Presentation Server using a Java 6 JDK and use the Java 6 examples.(You should remove 'eics-client.jar' from Altio\WEB-INF\lib before starting otherwise you will have JSP compilation problems)
  • Run AltioLive Presentation Server using an older JDK and use the BSF examples
2 - Add scripting engines and supporting JARs into the AltioLive Presentation Server CLASSPATH (Altio\WEB-INF\lib).
Java6
BSF

3 - Install the Altio Script Service Function by copying the JAR file (http://developers.altio.com/developer/downloads/scriptsvcfunc.jar) into (Altio\WEB-INF\lib)
4 - Create an Altio\WEB-INF\classes\scripts folder and copy the example scripts (http://developers.altio.com/developer/downloads/scripts.zip) in there.

Running

1 - You can reuse the Java Scripting service function proxy to execute any function in any script in any supported scripting engine, as in the examples below. The screenshots below are for the Java 6 implementation - if you are using the BSF then replace the classname with
com.altio.services.scripting.BSFScriptServiceFunction
Execute JavaScript Service Function


Example JavaScript:

function returnSomeXml() {
var result = "<javascript msg="'hello">"
return result;
}

Results:

<RESULT AL_ID="RESULT">
<javascript msg="hello again from javascript, the time is Tue Jul 15 2008 12:17:02 GMT+0100 (BST)" AL_ID="RESULT-javascript"/>
</RESULT>

Execute Groovy Service Function


Example Groovy:

def returnSomeXml2() {
String result = "<groovy>"
xml = {result+="<groovyitem id='${it}'/>"}
val = (1..10).collect(xml)
result += "</groovy>"
return result
}

Results:

<RESULT AL_ID="RESULT">
<groovy AL_ID="RESULT-groovy">
<groovyitem id="1" AL_ID="gi1"/>
<groovyitem id="2" AL_ID="gi2"/>
<groovyitem id="3" AL_ID="gi3"/>
<groovyitem id="4" AL_ID="gi4"/>
<groovyitem id="5" AL_ID="gi5"/>
<groovyitem id="6" AL_ID="gi6"/>
<groovyitem id="7" AL_ID="gi7"/>
<groovyitem id="8" AL_ID="gi8"/>
<groovyitem id="9" AL_ID="gi9"/>
<groovyitem id="10" AL_ID="gi10"/>
</groovy>
</RESULT>

These are trivial examples, but they demonstrate how you can implement Altio Service Functions in dynamic scripting languages. Their advantages of ease of development and deployment should make it simple to start using them for more complicated tasks. Further enhancements to the proxy could include passing parameters to the named script's service function, which would make it possible to send values all the way from the AltioLive client through to a server side script function.

No comments: