Adding User-Defined Scripting Code to Mashups
You can also add your own scripting code to a mashup script using the <script> statement. Your user-defined code is executed within mashup processing wherever <script> appears.
Specific Scripting Languages are supported in mashups. You can write script libraries to use with mashup. For JavaScript, you include these libraries in your application. For JRuby, you must deploy script libraries in the EMML Reference Runtime Engine (see Deploying JRuby Scripts in Presto).
You can Import Script Libraries or Include Scripts Directly. Scripts also have defined Access to Mashup Variables and Access to Java Classes. See also Adding User-Defined or Third-Party Java Classes for JavaScript Access.
Scripting Languages
You identify the scripting language in the type attribute using its MIME type. Currently EMML supports JavaScript and JRuby 1.0 as scripting languages within mashup scripts.
Note: JRuby scripting is not supported for deployments in WebSphere 6.1.
JavaScript support is included in EMML using Rhino. No changes are needed for your development or production environments. For more information on JavaScript support, see http://www.mozilla.org/rhino/.
For JRuby support, you must install and configure JRuby in your development and production environments. See Installing and Configuring JRuby for Mashups.
Import Script Libraries
You can include JavaScript or JRuby files from the local server using the src attribute.
Note: to import JRuby libraries, you must also deploy them in the EMML Engine (see Deploying JRuby Scripts in Presto).
For example:
<script type="text/javascript" src="myFunctions.js" inputvariables="$order, $customer" outputvariable="$scriptResult"/> ... <script type="ruby" src="myRubyScript.rb" inputvariables="$order, $customer" outputvariable="$scriptResult"/>
The script has access to any variables that you pass in inputvariables. Order is not important. The result of the script, if any, is placed in outputvariable.
Include Scripts Directly
For JavaScript, you can also simply include scripting code directly in the body of <script>. This feature is not available for JRuby scripts.
You must enclose your JavaScript code in a CDATA section. This ensures that any characters in the JavaScript code that are delimiters in XML, such as <, are not misinterpreted. For example:
<script type="text/javascript" inputvariables="$reviewers"
outputvariable="$newPayload">
<![CDATA[
var newPayload = TopReviewers;
if ( reviewers.reviewer.rating > 3 ) {
newPayload.TopReviewers += <name>{i.name}</name>;
}
]]>
</script>
Access to Mashup Variables
Scripting code, whether JRuby or JavaScript, can only add to or update the mashup variable returned as the output variable of <script>.
JRuby scripts can only access those mashup variables that you pass to <script> using the inputvariables attribute. JavaScript code can access any mashup variable. However, this can impact performance. It is a best practice to pass mashup variables to JavaScript code using the inputvariables attribute in <script>.
You can pass any number of variables in inputvariables, separated by commas. For example:
<script type="text/javascript" inputvariables="$Orders">
<![CDATA[
var myName = Orders.order.customer.firstname;
var orderID = Orders.order.@id;
var items = Orders..item \\contains all items in Orders
for each (i in items) {
totalprice += i.price * i.quantity;
}
Orders.order.item += <item><description>Catapult</description><price>139.95</price></item>;
]]>
</script>
...
<script type="ruby" src="myRubyScript.rb"
inputvariables="$feedData,$myDoc" outputvariable="$rubyResult"/>
Access to Java Classes
For JRuby scripts, access to Java classes is implicit in the language. See JRuby documentation for more information.
For JavaScript scripts, the following classes are accessible in a <script> command
Java language classes.
Classes in any JAR files deployed in the path identified by the application attribute. See Adding User-Defined or Third-Party Java Classes for JavaScript Access for more information.
Any class in the classpath for the EMML Reference Runtime Engine.
Use fully-qualified class names, such as in this example:
<script type="text/javascript">
<![CDATA[
var p = java.util.regex.Pattern.compile("a*b");
var m = p.matcher("aaaaab");
var b = m.matches();
print(b);
]]>
</script>
Important: For Java language classes, access is dependent on the version of the JDK used by the application server that hosts the EMML Engine.
Adding User-Defined or Third-Party Java Classes for JavaScript Access
You can add user-defined Java classes or third party Java libraries directly to the classpath for the EMML Engine. Simply copy them to the web-apps-home/emml/WEB-INF/classes folder or add them in a JAR to the web-apps-home/emml/WEB-INF/lib folder. You also need to restart the OMA.
Directly adding classes is simple, but can make upgrades more complicated and does not allow you to use different versions of third party libraries than the OMA. To simplify migration or use specific versions of third party libraries, you add your user-define or third party classes and JARs to an application-scope folder. To deploy classes in an application-scope:
Steps:
If needed, define a root folder for application scopes for the EMML Reference Runtime Engine.
The root folder for application scoping must reside in the same host as the EMML Engine. The default root folder for application scoping is web-apps-home/apps but this can be any folder. To change this default:
Open the web-apps-home/emml/WEB-INF/classes/emml.config file for the EMML Engine in any text editor.
Add the property emml.ext.applications.dir=path-to-folder with the absolute path to the folder you want to use as the root folder for all application scopes.
Save your changes.
Create a subfolder for your application directly under the application-scope root folder (see previous steps for more information).
Add your classes or JARs in this subfolder and restart the EMML Engine.
Add application=your-application-subfolder to the <script> statement.
Enterprise Mashup Markup Language (EMML) Documentation is licensed under a Creative Commons Attribution-Share Alike 3.0 United States License.
