How can I specify my own Rhino context in Java?

I am trying to ensure that my Rhino scripts (running under Java 6) are strict so that if the script developer missed the expression, I want an exception thrown. What is currently happening is that the expression is simply evaluating to "undefined".

Now, according to Mozilla org https://developer.mozilla.org/en/New_in_Rhino_1.6R6 , there are functions to do strong validation in context. I cannot find a working example of this.

What I have done so far is 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 Main I set the value to default.

ContextFactory.initGlobal(new ScriptContextFactory());

      

and I am getting an illegal state exception .:(

Any ideas or examples on how this works?

TIA

+2


a source to share


1 answer


If you execute Context.enter()

before calling initGlobal()

, try reordering.



+2


a source







All Articles