Need Help on Building Nashorn Equivalent code for Below Programme(From Rhino)
RAVI KIRAN
ravikirans1271 at gmail.com
Thu Nov 17 14:13:09 UTC 2016
Hi Team,
I am new to Java Scripting and now i need to Migrate all Rhino Script java
service to Nashorn supported code.
I need help from experts who can share the equivalent
functions/methods/classes in Nashorn which Rhino was using earlier from
below code snippet. Request you all to help
1) Equivalent function for Context(Rhino) in Nashorn. I believe it will
be SimpleScriptContext.
2) Context.enter() replacement in Nashorn ?
3) how to set OptimizationLevel in Nashorn. In Rhino we used
cx.setOptimizationLevel(9);
4) How to create and use shared scope as a prototype when creating the
local .scope. Rhino way is Scriptable scope = cx.newObject(sharedScope);
5) Exit from the context in Nashorn? Rhino way is Context.exit();
6) How to extend ContextFactory ? Rhino way is private static class
MyFactory extends ContextFactory
7) How to Initialize the global scope to be reused by all service threads?
Changing Below code in Nashorn by any Expert is really Appreciated.
-----------------------------------------------------------------------------------------------------
package se.stubs.common.ruleEngine;
import com.wm.data.*;
import com.wm.util.Values;
import com.wm.app.b2b.server.Service;
import com.wm.app.b2b.server.ServiceException;
import java.util.*;
import com.wm.app.b2b.server.*;
import com.wm.lang.ns.*;
import org.mozilla.javascript.*;
public final class executeJavaScript_SVC
{
/**
* The primary method for the Java service
*
* @param pipeline
* The IData pipeline
* @throws ServiceException
*/
public static final void executeJavaScript(IData pipeline)
throws ServiceException {
// pipeline
IDataCursor pipelineCursor = pipeline.getCursor();
//
// Declare input variables
//
String script = IDataUtil.getString(pipelineCursor, "script");
String function = IDataUtil.getString(pipelineCursor, "functionToCall");
// Creates and enters a Context. The Context stores information
// about the execution environment of a script.
Context cx = Context.enter();
cx.setOptimizationLevel(9);
try {
//use the shared scope as a prototype when creating the local
"scope"
Scriptable scope = cx.newObject(sharedScope);
scope.setPrototype(sharedScope);
scope.setParentScope(null); // We want "scope" to be a new
top-level
// Evaluate the string received
try{
cx.evaluateString(scope, script, "<cmd>", 1, null);
} catch (Exception e) {
throw new ServiceException(
"Error evaluating script, original exception message: "
+ e.getMessage()); }
//call given function
Object result=null;
try {
Object fObj = scope.get(function, scope);
if (!(fObj instanceof Function)) {
throw new ServiceException(function+" is undefined or not
a function.");
} else {
Function f = (Function)fObj;
result = f.call(cx, scope, scope, (Object[]) null);
}
} catch (Exception e) {
throw new ServiceException("Error executing function (" + function
+ "), original exception message: " + e.getMessage()); }
// Null return means no match => error.
if (result == null) {
throw new ServiceException("Function returned null: " + function);
}
//Although an array could be returned represented as a string
// the stubs limit the results to these types.
if (!(result instanceof String) && !(result instanceof Double) &&
!(result instanceof Integer)) {
throw new ServiceException("Function returned nor string nor
double: " + function);
}
IDataUtil.put( pipelineCursor, "result",
Context.toString(result) );
}
catch (Exception ex) {
throw new ServiceException(ex);
} finally {
// Exit from the context.
Context.exit();
}
pipelineCursor.destroy();
}
// --- <<IS-BEGIN-SHARED-SOURCE-AREA>> ---
//the shared scope variable
private static ScriptableObject sharedScope=null;
private static class MyFactory extends ContextFactory
{
@Override
protected boolean hasFeature(Context cx, int featureIndex)
{
if (featureIndex == Context.FEATURE_DYNAMIC_SCOPE) {
return true;
}
return super.hasFeature(cx, featureIndex);
}
}
//Initialize the global scope to be reused by all service threads.
//the scope is sealed, so any changes to its objects throw an exception.
//each thread will have it own set of variables that do not colide.
static {
ContextFactory.initGlobal(new MyFactory());
// avoid init all the standard objects since this is a costly
operation
Context cx = Context.enter();
try {
sharedScope = cx.initStandardObjects(null, true);
sharedScope.sealObject();
} finally {
Context.exit();
}
}
private static String getFirstPipelineValue(IData doc, String key) {
IDataCursor idc = doc.getCursor();
String result = "";
while (idc.next() && result.equals("")) {
Object placeholder = idc.getValue();
if (placeholder instanceof IData) {
result = getFirstPipelineValue((IData)placeholder, key);
}
if (placeholder instanceof IData[]) {
result = getFirstPipelineValue(((IData[])placeholder)[0], key);
if ((result.equals("") || result.equals("SUCCESS")) &&
idc.getKey().equals("header:Status")) {
IDataUtil.remove(idc, idc.getKey());
}
}
if (idc.getKey().equals(key)) {
if (placeholder instanceof String[]) {
result = ((String[])placeholder)[0];
}
if (placeholder instanceof String) {
result = (String) placeholder;
}
}
}
idc.destroy();
return result;
}
// --- <<IS-END-SHARED-SOURCE-AREA>> ---
/**
* The service implementations given below are read-only and show only the
* method definitions and not the complete implementation.
*/
public static final void removeSuccessHeader(IData pipeline)
throws ServiceException {
}
final static executeJavaScript_SVC _instance = new executeJavaScript_SVC();
static executeJavaScript_SVC _newInstance() { return new
executeJavaScript_SVC(); }
static executeJavaScript_SVC _cast(Object o) { return
(executeJavaScript_SVC)o; }
}
More information about the nashorn-dev
mailing list