Wrapping POJO Classes with Mashups
You can use any Java class accessible to the EMML Reference Runtime Engine within the mashup <script> statement (for both JavaScript or JRuby). Because of this feature, you can easily use POJO classes as services by wrapping them in mashup scripts.
Note: based on the JDK version used with the application server that hosts the OMA runtime reference implementation, Java language classes are also available within mashup <script> statements.
Steps:
Include the POJO classes in the EMML Reference Runtime Engine classpath by copying the compiled class file or JAR file to web-apps-home/emml/WEB-INF/classes or web-apps-home/emml/WEB-INF/lib respectively.
Create a separate wrapper mashup script for each POJO method that you want to expose as a service.
In the wrapper mashup script:
Add <input> declarations, if needed, to accept input parameters for the Java method and an <output> declaration to receive the method return, if any.
Add a <script> statement to invoke the Java code. See Using Java Classes in a <script> Statement for instructions and examples.
Using Java Classes in a <script> Statement
For JRuby scripts, simply use the existing mechanisms in JRuby to use Java classes.
For JavaScript scripts, you must use the fully qualified class name starting with the keyword Packages. when you refer to a Java class. You create an instance of a Java object with new, just like any other JavaScript object. For example:
<script type="text/javascript">
<![CDATA[
var list = new Packages.java.util.ArrayList();
list.add("one");
list.add("six");
list.add("three");
var listLength = list.size();
print listLength;
]]>
</script>
You can assign properties and call methods on Java instances. See the Rhino Scripting topics for additional techniques and information.
You can also convert Java instances to an XML DOM tree and assign it to a mashup variable. For example:
<variable name="xmlDoc" type="document"/>
<script type="text/javascript" outputvariable="$xmlString">
<![CDATA[
var dt = new Packages.com.myCompany.test.Address();
// populating dummy values;
dt.street = 'disney avenue';
dt.city = 'toon town';
dt.zip.code = '91010';
// convert JavaObject to XML DOM tree;
xmlString=Packages.org.oma.emml.utils.ConversionUtils.javaToXml(dt);
]]>
</script>
<!-- use assign to convert from DOM string to document -->
<assign fromvariable="$xmlString" outputvariable="$xmlDoc"/>
Example
This example uses a JavaScript script to wrap a Java method with the following signature in a mashup:
public String getCategory(String item)
<mashup name="GetItemCategory"
xmlns="http://www.openemml.org/2009-04-15/EMMLSchema"
xsi:schemaLocation="http://www.openemml.org/2009-04-15/EMMLSchema
../schema/EMMLSpec.xsd"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<input name="itemId" type="string"/>
<output name="result" type="string"/>
<script type="text/javascript" inputvariables="$itemId"
outputvariable="$result">
<![CDATA[
var item = new Packages.com.myCompany.somePackage.Categories();
var result = item.getCategory(itemId);
]]>
</script>
</mashup>
Enterprise Mashup Markup Language (EMML) Documentation is licensed under a Creative Commons Attribution-Share Alike 3.0 United States License.
