javajavascriptrhinojavax.script

How can I specify my own Rhino context in Java?


I'm trying to ensure that my Rhino scripts (running under Java 6) are strict so that if a script developer misspells an expression I want an exception to be thrown. Currently what happens is the expression simply evaluates to "undefined".

Now according to Mozilla org https://developer.mozilla.org/en/New_in_Rhino_1.6R6 there are features to enable strict checking in the context. I cannot find a working example of this.

What I did so far was write a class to extend ContextFactory and then override the hasFeature method.

public class ScriptContextFactory extends ContextFactory {

    protected boolean hasFeature(Context context, int featureIndex) {

        switch (featureIndex) {
            case Context.FEATURE_STRICT_EVAL:
                return true;

            case Context.FEATURE_STRICT_VARS:
                return true;
        }

        return super.hasFeature(context, featureIndex);
    }
  }

Then in the Main I set mine to the default.

ContextFactory.initGlobal(new ScriptContextFactory());

and I get an illegal state exception. :(

Any ideas or samples on how this works?

TIA


Solution

  • If you are doing Context.enter() before calling initGlobal() try reversing the order.