Need Help on Building Nashorn Equivalent code for Below Programme(From Rhino)
RAVI KIRAN
ravikirans1271 at gmail.com
Mon Nov 21 10:46:45 UTC 2016
Hi Sundar,
Thank you for responding. Unfortunately our existing Rhino Java code
doesn't use javax.script (JSR-223) API . Only Rhino specific code is used
which uses org.mozilla.javascript.*
Could you suggest the best way to Migrate Rhino to Nashorn. I understood
from our earlier conversation below could be possibility
1) Extend Existing Rhino Specif java code to use javax.script (JSR-223) API
2) Next Migrate Java Code from Point 1 to get compatible Use javax.script
(JSR-223) API in Nashorn
The other way i found is to Load Nashorn Compatibility scripts that
Rhino/Mozilla extensions support. But as i am aware that Rhino extensions
may get deprecated in future for any reason then my code will be need to re
modified.
Main challenge is i am unable to find one to one correspondence between
Rhino and Nashorn Internals.
Ex: 1) How to create Shared scope in Nashorn ?
2) How to extend my class using contextfactory ? etc...
Regards
Ravi kiran
On Thu, Nov 17, 2016 at 8:37 PM, Sundararajan Athijegannathan <
sundararajan.athijegannathan at oracle.com> wrote:
> Hi,
>
> The Rhino APIs you mention are all Rhino specific. There is no
> one-to-one correspondence between Rhino internals and Nashorn
> internals. If you had used javax.script (JSR-223) API to access Rhino
> from your Java code, it is lot simpler to migrate to Nashorn. Nashorn
> supports Nashorn specific API apart from javax.script (JSR-223):
>
>
> https://docs.oracle.com/javase/8/docs/jdk/api/nashorn/
> index.html?jdk/nashorn/api/scripting/ScriptObjectMirror.html
>
> We've a migration guide that deals with JS language & API differences
> between Rhino and Nashorn:
>
> https://wiki.openjdk.java.net/display/Nashorn/Rhino+Migration+Guide
>
> HTH,
>
> -Sundar
>
>
> On 11/17/2016 7:43 PM, RAVI KIRAN wrote:
> > 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