use of org.pentaho.platform.plugin.condition.javascript.RhinoScriptable 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.pentaho.platform.plugin.condition.javascript.RhinoScriptable in project pentaho-platform by pentaho.
the class RhinoIT method testRhino.
public void testRhino() {
// Creates and enters a Context. The Context stores information
// about the execution environment of a script.
Context cx = ContextFactory.getGlobal().enterContext();
try {
// Initialize the standard objects (Object, Function, etc.)
// This must be done before scripts can be executed. Returns
// a scope object that we use in later calls.
Scriptable scope = new RhinoScriptable();
scope.getClassName();
// Collect the arguments into a single string.
// $NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$ //$NON-NLS-5$
String[] args = { "var s=5;", "s++;", "s+=2;", "s = \'" + scope.getClassName() + "\'" };
// $NON-NLS-1$
String s = "";
for (int i = 0; i < args.length; i++) {
s += args[i];
}
// Now evaluate the string we've colected.
// $NON-NLS-1$
Object result = cx.evaluateString(scope, s, "<cmd>", 1, null);
// Convert the result to a string and print it.
System.err.println(Context.toString(result));
} finally {
// Exit from the context.
Context.exit();
}
}
Aggregations