use of org.mozilla.javascript.Scriptable in project cxf by apache.
the class RPCClientTest method testBean2ToJS.
public static Object testBean2ToJS(JavascriptTestUtilities testUtilities, Context context, TestBean2 beanTwoItem) {
if (beanTwoItem == null) {
return null;
}
Scriptable rv = context.newObject(testUtilities.getRhinoScope(), "org_apache_cxf_javascript_testns3_testBean2");
testUtilities.rhinoCallMethod(rv, "setStringItem", beanTwoItem.stringItem);
return rv;
}
use of org.mozilla.javascript.Scriptable in project cxf by apache.
the class RPCClientTest method testBean1ToJS.
public static Scriptable testBean1ToJS(JavascriptTestUtilities testUtilities, Context context, TestBean1 b1) {
if (b1 == null) {
// black is always in fashion. (Really, we can be called with a null).
return null;
}
Scriptable rv = context.newObject(testUtilities.getRhinoScope(), "org_apache_cxf_javascript_testns_testBean1");
testUtilities.rhinoCallMethod(rv, "setStringItem", testUtilities.javaToJS(b1.stringItem));
testUtilities.rhinoCallMethod(rv, "setIntItem", testUtilities.javaToJS(b1.intItem));
testUtilities.rhinoCallMethod(rv, "setLongItem", testUtilities.javaToJS(b1.longItem));
testUtilities.rhinoCallMethod(rv, "setBase64Item", testUtilities.javaToJS(b1.base64Item));
testUtilities.rhinoCallMethod(rv, "setOptionalIntItem", testUtilities.javaToJS(b1.optionalIntItem));
testUtilities.rhinoCallMethod(rv, "setOptionalIntArrayItem", testUtilities.javaToJS(b1.optionalIntArrayItem));
testUtilities.rhinoCallMethod(rv, "setDoubleItem", testUtilities.javaToJS(b1.doubleItem));
testUtilities.rhinoCallMethod(rv, "setBeanTwoItem", testBean2ToJS(testUtilities, context, b1.beanTwoItem));
testUtilities.rhinoCallMethod(rv, "setBeanTwoNotRequiredItem", testBean2ToJS(testUtilities, context, b1.beanTwoNotRequiredItem));
return rv;
}
use of org.mozilla.javascript.Scriptable in project pentaho-platform by pentaho.
the class JavascriptRule method executeAction.
/*
* (non-Javadoc)
*
* @see org.pentaho.component.ComponentBase#execute()
*/
@Override
protected boolean executeAction() {
Context cx = ContextFactory.getGlobal().enterContext();
StringBuffer buffer = new StringBuffer();
@SuppressWarnings("unchecked") Iterator<String> iter = getResourceNames().iterator();
while (iter.hasNext()) {
IActionSequenceResource resource = getResource(iter.next().toString());
// If this is a javascript resource then append it to the script string
if ("text/javascript".equalsIgnoreCase(resource.getMimeType())) {
// $NON-NLS-1$
buffer.append(getResourceAsString(resource));
}
}
List<String> outputNames = new ArrayList<String>();
JavascriptAction jscriptAction = (JavascriptAction) getActionDefinition();
IActionOutput[] actionOutputs = jscriptAction.getOutputs();
if (actionOutputs.length == 1) {
String outputName = actionOutputs[0].getName();
outputNames.add(outputName);
} else {
if (oldStyleOutputs) {
int i = 1;
while (true) {
if (jscriptAction.getInput("output" + i) != ActionInputConstant.NULL_INPUT) {
// $NON-NLS-1$
// $NON-NLS-1$
outputNames.add(jscriptAction.getInput("output" + i).getStringValue());
} else {
break;
}
i++;
}
} else {
for (IActionOutput element : actionOutputs) {
outputNames.add(element.getName());
}
}
}
boolean success = false;
try {
String script = jscriptAction.getScript().getStringValue();
if (script == null) {
// $NON-NLS-1$
error(Messages.getInstance().getErrorString("JSRULE.ERROR_0001_SCRIPT_NOT_DEFINED", getActionName()));
} else {
buffer.append(script);
script = buffer.toString();
if (ComponentBase.debug) {
// $NON-NLS-1$
debug("script=" + script);
}
try {
ScriptableObject scriptable = new RhinoScriptable();
// initialize the standard javascript objects
Scriptable scope = cx.initStandardObjects(scriptable);
Object resultObject = executeScript(scriptable, scope, script, cx);
if (oldStyleOutputs) {
if (resultObject instanceof org.mozilla.javascript.NativeArray) {
// we need to convert this to an ArrayList
NativeArray jsArray = (NativeArray) resultObject;
int length = (int) jsArray.getLength();
for (int i = 0; i < length; i++) {
Object value = jsArray.get(i, scriptable);
if (i < outputNames.size()) {
jscriptAction.getOutput(outputNames.get(i).toString()).setValue(convertWrappedJavaObject(value));
} else {
break;
}
}
} else {
jscriptAction.getOutput(outputNames.get(0).toString()).setValue(convertWrappedJavaObject(resultObject));
}
} else {
if ((outputNames.size() == 1) && (resultObject != null)) {
jscriptAction.getOutput(outputNames.get(0).toString()).setValue(convertWrappedJavaObject(resultObject));
} else {
List<String> setOutputs = new ArrayList<String>(outputNames.size());
Object[] ids = ScriptableObject.getPropertyIds(scope);
for (Object element : ids) {
int idx = outputNames.indexOf(element.toString());
if (idx >= 0) {
jscriptAction.getOutput(outputNames.get(idx).toString()).setValue(convertWrappedJavaObject(ScriptableObject.getProperty(scope, (String) element)));
setOutputs.add(outputNames.get(idx));
}
}
// So, set it to null.
if (setOutputs.size() != outputNames.size()) {
for (int i = 0; i < outputNames.size(); i++) {
if (setOutputs.indexOf(outputNames.get(i)) < 0) {
// An output that wasn't set in the
// javascript component
jscriptAction.getOutput(outputNames.get(i).toString()).setValue(null);
}
}
}
}
}
success = true;
} catch (Exception e) {
// $NON-NLS-1$
error(Messages.getInstance().getErrorString("JSRULE.ERROR_0003_EXECUTION_FAILED"), e);
}
}
} finally {
Context.exit();
}
return success;
}
use of org.mozilla.javascript.Scriptable in project pentaho-platform by pentaho.
the class ConditionalExecution method shouldExecute.
public boolean shouldExecute(final Map currentInputs, final Log logger) throws Exception {
boolean shouldExecute = true;
Context cx = ContextFactory.getGlobal().enterContext();
try {
ScriptableObject scriptable = new RhinoScriptable();
// initialize the standard javascript objects
Scriptable scope = cx.initStandardObjects(scriptable);
ScriptableObject.defineClass(scope, JavaScriptResultSet.class);
Object inputValue;
IActionParameter inputParameter;
String inputName;
Iterator inputs = currentInputs.entrySet().iterator();
Map.Entry mapEntry;
while (inputs.hasNext()) {
mapEntry = (Map.Entry) inputs.next();
inputName = (String) mapEntry.getKey();
if (inputName.indexOf('-') >= 0) {
// $NON-NLS-1$
logger.info("Ignoring Input: " + inputName);
continue;
}
inputParameter = (IActionParameter) mapEntry.getValue();
inputValue = inputParameter.getValue();
Object wrapper;
if (inputValue instanceof IPentahoResultSet) {
JavaScriptResultSet results = new JavaScriptResultSet();
// Required as of Rhino 1.7R1 to resolve caching, base object
// inheritance and property tree
results.setPrototype(scriptable);
results.setResultSet((IPentahoResultSet) inputValue);
wrapper = Context.javaToJS(inputValue, results);
} else {
wrapper = Context.javaToJS(inputValue, scope);
}
ScriptableObject.putProperty(scope, inputName, wrapper);
}
Object wrappedOut = Context.javaToJS(System.out, scope);
Object wrappedThis = Context.javaToJS(this, scope);
// $NON-NLS-1$
ScriptableObject.putProperty(scope, "out", wrappedOut);
// $NON-NLS-1$
ScriptableObject.putProperty(scope, "rule", wrappedThis);
// evaluate the script
// $NON-NLS-1$
Object resultObject = cx.evaluateString(scope, script, "<cmd>", 1, null);
Object actualObject = null;
if (resultObject instanceof org.mozilla.javascript.NativeJavaObject) {
actualObject = ((org.mozilla.javascript.NativeJavaObject) resultObject).unwrap();
} else {
actualObject = resultObject;
}
if (actualObject instanceof Boolean) {
return ((Boolean) actualObject).booleanValue();
} else if (actualObject instanceof String) {
return ("true".equalsIgnoreCase(actualObject.toString())) || ("yes".equalsIgnoreCase(// $NON-NLS-1$ //$NON-NLS-2$
actualObject.toString()));
} else if (actualObject instanceof Number) {
return ((Number) actualObject).intValue() > 0;
} else if (actualObject instanceof IPentahoResultSet) {
return ((IPentahoResultSet) actualObject).getRowCount() > 0;
}
// } catch (Exception e) {
// logger.error("Error executing conditional execution script.", e);
} finally {
Context.exit();
}
return shouldExecute;
}
use of org.mozilla.javascript.Scriptable in project wombat by PLOS.
the class AssetServiceTest method testCompiledJs.
@Test
public void testCompiledJs() throws Exception {
// First, we use a javascript runtime to execute some uncompiled javascript, to
// get the expected value.
Object expected;
{
Context jsContext = Context.enter();
jsContext.setOptimizationLevel(-1);
Scriptable jsScope = jsContext.initStandardObjects();
try (FileReader reader1 = new FileReader(new File(DATA_PATH + "test1.js"));
FileReader reader2 = new FileReader(new File(DATA_PATH + "test2.js"))) {
jsContext.evaluateReader(jsScope, reader1, "test1.js", 1, null);
expected = jsContext.evaluateReader(jsScope, reader2, "test2.js", 1, null);
assertEquals(expected, "magicValue");
}
}
// Now we compile the same javascript files, execute them, and check the value returned.
List<String> jsFiles = new ArrayList<>();
jsFiles.add("resource/js/test1.js");
jsFiles.add("resource/js/test2.js");
String compiledJsPath = assetService.getCompiledAssetLink(AssetService.AssetType.JS, jsFiles, siteSet.getSite("site1"));
String[] fields = compiledJsPath.split("/");
String basename = fields[fields.length - 1];
Context jsContext = Context.enter();
jsContext.setOptimizationLevel(-1);
Scriptable jsScope = jsContext.initStandardObjects();
File file = new File(runtimeConfiguration.getCompiledAssetDir() + File.separator + basename);
try (FileReader fr = new FileReader(file)) {
Object actual = jsContext.evaluateReader(jsScope, fr, basename, 1, null);
assertEquals(actual, expected);
}
}
Aggregations