use of org.mozilla.javascript.Context in project cxf by apache.
the class ProviderFactory method publishImpl.
private void publishImpl(File f, String epAddr, boolean isBase) throws Exception {
if (!f.exists()) {
throw new Exception(f.getPath() + NO_SUCH_FILE);
}
boolean isE4X = f.getName().endsWith(".jsx");
StringBuilder sb = new StringBuilder();
try (BufferedReader bufrd = new BufferedReader(new FileReader(f))) {
String line = null;
for (; ; ) {
line = bufrd.readLine();
if (line == null) {
break;
}
sb.append(line).append("\n");
}
}
String scriptStr = sb.toString();
Context cx = ContextFactory.getGlobal().enterContext();
boolean providerFound = false;
try {
Scriptable scriptScope = cx.initStandardObjects(null, true);
Object[] ids = compileScript(cx, scriptStr, scriptScope, f);
if (ids.length > 0) {
Service.Mode mode = Service.Mode.PAYLOAD;
for (Object idObj : ids) {
if (!(idObj instanceof String)) {
continue;
}
String id = (String) idObj;
if (!id.startsWith("WebServiceProvider")) {
continue;
}
Object obj = scriptScope.get(id, scriptScope);
if (!(obj instanceof Scriptable)) {
continue;
}
Scriptable wspVar = (Scriptable) obj;
providerFound = true;
obj = wspVar.get("ServiceMode", wspVar);
if (obj != Scriptable.NOT_FOUND) {
if (obj instanceof String) {
String value = (String) obj;
if ("PAYLOAD".equalsIgnoreCase(value)) {
mode = Service.Mode.PAYLOAD;
} else if ("MESSAGE".equalsIgnoreCase(value)) {
mode = Service.Mode.MESSAGE;
} else {
throw new Exception(f.getPath() + ILLEGAL_SVCMD_MODE + value);
}
} else {
throw new Exception(f.getPath() + ILLEGAL_SVCMD_TYPE);
}
}
AbstractDOMProvider provider = createProvider(mode, scriptScope, wspVar, epAddr, isBase, isE4X);
try {
provider.publish();
providers.add(provider);
} catch (AbstractDOMProvider.JSDOMProviderException ex) {
StringBuilder msg = new StringBuilder(f.getPath());
msg.append(": ").append(ex.getMessage());
throw new Exception(msg.toString());
}
}
}
} finally {
Context.exit();
}
if (!providerFound) {
throw new Exception(f.getPath() + NO_PROVIDER);
}
}
use of org.mozilla.javascript.Context in project yamcs-studio by yamcs.
the class ScriptStoreFactory method initRhinoJSEngine.
/**
* Must be called in UI Thread.
*
* @throws Exception
*/
private static void initRhinoJSEngine() throws Exception {
Context scriptContext = Context.enter();
final Display display = Display.getCurrent();
displayContextMap.put(display, scriptContext);
}
use of org.mozilla.javascript.Context 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.Context in project wso2-synapse by wso2.
the class ScriptMediator method invokeScript.
private boolean invokeScript(MessageContext synCtx) {
boolean returnValue;
try {
// if the engine is Rhino then needs to set the class loader specifically
if (language.equals("js")) {
Context cx = Context.enter();
cx.setApplicationClassLoader(this.loader);
}
Object returnObject;
if (key != null) {
returnObject = mediateWithExternalScript(synCtx);
} else {
returnObject = mediateForInlineScript(synCtx);
}
returnValue = !(returnObject != null && returnObject instanceof Boolean) || (Boolean) returnObject;
} catch (ScriptException e) {
handleException("The script engine returned an error executing the " + (key == null ? "inlined " : "external ") + language + " script" + (key != null ? " : " + key : "") + (function != null ? " function " + function : ""), e, synCtx);
returnValue = false;
} catch (NoSuchMethodException e) {
handleException("The script engine returned a NoSuchMethodException executing the " + "external " + language + " script" + " : " + key + (function != null ? " function " + function : ""), e, synCtx);
returnValue = false;
} catch (Exception e) {
handleException("The script engine returned an Exception executing the " + "external " + language + " script" + " : " + key + (function != null ? " function " + function : ""), e, synCtx);
returnValue = false;
} finally {
if (language.equals("js")) {
Context.exit();
}
}
return returnValue;
}
use of org.mozilla.javascript.Context 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;
}
Aggregations