How can I specify my own Rhino context in Java?

Posted by Dan Howard on Stack Overflow See other posts from Stack Overflow or by Dan Howard
Published on 2010-04-29T19:04:43Z Indexed on 2010/04/29 19:07 UTC
Read the original article Hit count: 289

Filed under:
|
|
|

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

© Stack Overflow or respective owner

Related posts about javax.script

Related posts about java