rhino-在JAVA中调用javascript脚本

阅读更多

Rhino可以实现JAVA与javascript的互相调用,有时候我们会希望用javascript解释执行自定义的JAVA语句,如

$console.print("hello world");

这时,可以使用ScriptableObject.defineClass实现注册。具体代码实现可参考如下:

package com.zjyang.scriptable;

import org.mozilla.javascript.Context;
import org.mozilla.javascript.Function;
import org.mozilla.javascript.Scriptable;
import org.mozilla.javascript.annotations.JSFunction;

import java.io.PrintStream;

public class ConsoleScriptable extends org.mozilla.javascript.ScriptableObject {

    private static final long serialVersionUID = 1L;
    private java.io.PrintStream printStream;

    public void setPrintStream(java.io.PrintStream printStream) {
        this.printStream = printStream;
    }

    public ConsoleScriptable() {}

    @JSFunction
    public static void print(Context cx, Scriptable obj, Object[] args, Function funObj) {
        print0(obj, args, false);
    }

    @JSFunction
    public static void println(Context cx, Scriptable obj, Object[] args, Function funObj) {
        print0(obj, args, true);
    }

    private static void print0(Scriptable obj, Object[] args, boolean eol) {
        PrintStream printStream = checkInstance(obj).printStream;
        for (Object arg : args) {
            printStream.print(Context.toString(arg));
        }
        if (eol) {
            printStream.println();
        }
    }

    private static ConsoleScriptable checkInstance(Scriptable obj) {
        if (null == obj || !(obj instanceof ConsoleScriptable)) {
            throw Context.reportRuntimeError("call ed on incompatible object");
        }
        return (ConsoleScriptable) obj;
    }
    @Override
    public String getClassName() {
        return ConsoleScriptable.class.getSimpleName();
    }
}

 

package com.zjyang.handler;

import com.zjyang.scriptable.ConsoleScriptable;
import org.mozilla.javascript.*;

import java.io.Console;
import java.lang.reflect.InvocationTargetException;

public class JavaScriptHandler {

    private Scriptable global;
    private int optimizationLevel = -1;
    private Script script;

    public JavaScriptHandler() {
        Context ctx = Context.enter();
        global = ctx.initStandardObjects();
        try {
            ScriptableObject.defineClass(global, ConsoleScriptable.class);
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (InstantiationException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        } finally {
            Context.exit();
        }
    }

    public void handle() {
        Context ctx = Context.enter();
        try {
            ctx.setOptimizationLevel(optimizationLevel);
            if (script == null) {
                script = ctx.compileString("$console.println('xx');", "firstRhino", 0, null);
            }
            Scriptable scope = new NativeObject();
            scope.setParentScope(global);
            ConsoleScriptable $console = (ConsoleScriptable) ctx.newObject(scope, ConsoleScriptable.class.getSimpleName());
            $console.setPrintStream(System.out);
            ScriptableObject.putProperty(scope, "$console", $console);
            script.exec(ctx, scope);
        } finally {
            Context.exit();
        }
    }

    public static void main(String args[]) {
        new JavaScriptHandler().handle();
        System.out.println("finish");
    }
}

  

个人感觉,在做一些应用时,不用改后台,就可以直接做开发,而且比较容易上手,因为是弱语言类型的了。

 

参考:http://my.oschina.net/yybear/blog/102018

 

你可能感兴趣的:(rhino)