MashZone NextGen 10.2 | Appendix | Legacy Presto components | Mashables and Mashups | Mashups in EMML | Advanced Mashup Techniques | Wrapping POJO Classes with Mashups | Using Java Classes in a <script> Statement
 
Using Java Classes in a <script> Statement
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.com.jackbe.jbp.transform.DataTransformer.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.openmashup.org/schemas/v1.0/EMML"
xsi:schemaLocation="http://www.openmashup.org/schemas/v1.0/EMML
../xsd/EMMLPrstoSpec.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>

Copyright © 2013-2018 | Software AG, Darmstadt, Germany and/or Software AG USA, Inc., Reston, VA, USA, and/or its subsidiaries and/or its affiliates and/or their licensors.
Innovation Release