Need Help on Building Nashorn Equivalent code for Below Programme(From Rhino)

RAVI KIRAN ravikirans1271 at
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
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.util.Values;
import java.util.*;
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();
       try {
           //use the shared scope as a prototype when creating the local
           Scriptable scope = cx.newObject(sharedScope);
           scope.setParentScope(null); // We want "scope" to be a new
           // Evaluate the string received
             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 =, 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.
//the shared scope variable
private static ScriptableObject sharedScope=null;
private static class MyFactory extends ContextFactory
       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
       Context cx = Context.enter();
       try {
         sharedScope = cx.initStandardObjects(null, true);
       } finally {
private static String getFirstPipelineValue(IData doc, String key) {
IDataCursor idc = doc.getCursor();
String result = "";
while ( && 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;
return result;

* 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