Hello,

I am new to Rhino, and I want to make sure I understand how it works in 
multithreaded environment. In particular, I have a java class that wraps a 
JavaScript expression, and I hope it is thread safe. I would really appreciate 
if someone with more experience can confirm it is thread safe, or point to 
things to change in order to make it thread safe.

I am using Rhino version that comes with Weblogic 10.0.1 (I think I read 
somewhere it is a modified 1.6R4).

Please note the following constraints which should help with thread safety:
-          objects put into sharedScope during initialization are all with 
thread safe methods;
-          each JS script must by just a function, without any variables 
declared aoutside of the function scope;
-          parameters passed to a JS function are all thread safe.

Thanks,
Aleksandar Likic

package rule;

import org.mozilla.javascript.Context;
import org.mozilla.javascript.Function;
import org.mozilla.javascript.NativeJavaObject;
import org.mozilla.javascript.Scriptable;

/**
 * Rule that evaluates a JavaScript expression.
 *
 * @author Aleksandar Likic
 *
 */
public class JavaScriptRule implements Rule {

            public static final String RULE_REPOSITORY = "__r";
            private Scriptable sharedScope;
            private Function f;
            private ExpressionCompiler compiler = 
JavaScriptExpressionCompiler.getInstance();

            private String ruleName = null;
            private String expression = null;

            public JavaScriptRule(RuleRepository ruleRepository, String 
ruleName, String expression) {
                        try {
                                    if (ruleName == null || 
ruleName.trim().length() == 0) {
                                                throw new 
IllegalArgumentException("Rule name must not be empty");
                                    }
                                    if (expression == null || 
expression.trim().length() == 0) {
                                                throw new 
IllegalArgumentException("Expression must not be empty");
                                    }
                                    this.ruleName = ruleName;
                                    this.expression = expression;
                                    this.expression = 
compiler.compile(expression);

                                    Context context = Context.enter();
                                    if (sharedScope == null) {
                                                sharedScope = 
context.initStandardObjects(null);
                                                
sharedScope.put(RULE_REPOSITORY, sharedScope, ruleRepository);
                                                sharedScope.put("out", 
sharedScope, System.out);
                                    }
                                    f = context.compileFunction(sharedScope, 
this.expression, ruleName, 1, null);
                        } finally {
                                    Context.exit();
                        }
            }

            public Object invoke(Object[] args) throws RuleException {
                        Context context = Context.enter();
                        try {
                                    Object result = f.call(context, 
sharedScope, sharedScope, args);
                                    if (result instanceof NativeJavaObject) {
                                                result = 
((NativeJavaObject)result).unwrap();
                                    }
                                    return result;
                        } catch (Throwable t) {
                                    throw new RuleException("Error executing 
rule: " + getName(), t);
                        } finally {
                                    Context.exit();
                        }
            }

            public String getName() {
                        return ruleName;
            }

            public String getExpression() {
                        return expression;
            }
}



The information contained in this e-mail message and any 
attachments may be privileged and confidential. If the reader of 
this message is not the intended recipient or an agent 
responsible for delivering it to the intended recipient, you are 
hereby notified that any review, dissemination, distribution or 
copying of this communication is strictly prohibited. If you have 
received this communication in error, please notify the sender 
immediately by replying to this e-mail and delete the message and 
any attachments from your computer.

_______________________________________________
dev-tech-js-engine-rhino mailing list
[email protected]
https://lists.mozilla.org/listinfo/dev-tech-js-engine-rhino

Reply via email to