You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@tuscany.apache.org by an...@apache.org on 2006/10/24 10:17:50 UTC

svn commit: r467273 - /incubator/tuscany/sandbox/ant/container.script/src/main/java/org/apache/tuscany/container/script/jsr223/JSR223BSFEngine.java

Author: antelder
Date: Tue Oct 24 01:17:49 2006
New Revision: 467273

URL: http://svn.apache.org/viewvc?view=rev&rev=467273
Log:
Add a BSF engine for JSR-223

Added:
    incubator/tuscany/sandbox/ant/container.script/src/main/java/org/apache/tuscany/container/script/jsr223/JSR223BSFEngine.java

Added: incubator/tuscany/sandbox/ant/container.script/src/main/java/org/apache/tuscany/container/script/jsr223/JSR223BSFEngine.java
URL: http://svn.apache.org/viewvc/incubator/tuscany/sandbox/ant/container.script/src/main/java/org/apache/tuscany/container/script/jsr223/JSR223BSFEngine.java?view=auto&rev=467273
==============================================================================
--- incubator/tuscany/sandbox/ant/container.script/src/main/java/org/apache/tuscany/container/script/jsr223/JSR223BSFEngine.java (added)
+++ incubator/tuscany/sandbox/ant/container.script/src/main/java/org/apache/tuscany/container/script/jsr223/JSR223BSFEngine.java Tue Oct 24 01:17:49 2006
@@ -0,0 +1,118 @@
+/*
+ * Copyright 2004,2005 The Apache Software Foundation.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.tuscany.container.script.jsr223;
+
+import java.util.Iterator;
+import java.util.Vector;
+
+import javax.script.Invocable;
+import javax.script.ScriptContext;
+import javax.script.ScriptEngine;
+import javax.script.ScriptEngineFactory;
+import javax.script.ScriptEngineManager;
+import javax.script.ScriptException;
+
+import org.apache.bsf.BSFDeclaredBean;
+import org.apache.bsf.BSFException;
+import org.apache.bsf.BSFManager;
+import org.apache.bsf.util.BSFEngineImpl;
+
+/**
+ * JSR223BSFEngine enables using any available JSR-223 script engine with BSF
+ * 
+ * Use the static method JSR223BSFEngine.registerAllJSR223Engines to register all
+ * the available JSR-223 engines with BSF.
+ */
+public class JSR223BSFEngine extends BSFEngineImpl {
+
+    private ScriptEngine engine;
+
+    /*
+     * @see org.apache.bsf.BSFEngine.call(Object, String, Object[])
+     */
+    public Object call(Object object, String name, Object[] args) throws BSFException {
+        if (!(engine instanceof Invocable)) {
+            throw new BSFException(BSFException.REASON_OTHER_ERROR, "JSR223Bridge Error, call not supported by engine: "
+                    + engine.getContext().getAttribute(ScriptEngine.ENGINE));
+        }
+        try {
+
+            return ((Invocable) engine).call(name, object, args);
+
+        } catch (Exception e) {
+            throw new BSFException(BSFException.REASON_OTHER_ERROR, "JSR223Bridge Error: " + e.getLocalizedMessage(), e);
+        }
+    }
+
+    /*
+     * @see org.apache.bsf.BSFEngine.call(Object, String, Object[])
+     */
+    public Object eval(String source, int lineNo, int columnNo, Object expr) throws BSFException {
+        try {
+
+            return engine.eval(expr.toString());
+
+        } catch (ScriptException e) {
+            throw new BSFException(BSFException.REASON_OTHER_ERROR, "JSR223Bridge Error: " + e.getLocalizedMessage(), e);
+        }
+    }
+
+    /*
+     * @see org.apache.bsf.BSFEngine.declareBean(BSFDeclaredBean)
+     */
+    public void declareBean(BSFDeclaredBean bean) throws BSFException {
+        engine.getContext().setAttribute(bean.name, bean.bean, ScriptContext.ENGINE_SCOPE);
+    }
+
+    /*
+     * @see org.apache.bsf.BSFEngine.undeclareBean(BSFDeclaredBean)
+     */
+    public void undeclareBean(BSFDeclaredBean bean) throws BSFException {
+        engine.getContext().removeAttribute(bean.name, ScriptContext.ENGINE_SCOPE);
+    }
+
+    /*
+     * @see org.apache.bsf.BSFEngine.initialize(BSFManager, String, Vector)
+     */
+    public void initialize(BSFManager mgr, String lang, Vector declaredBeans) throws BSFException {
+
+        super.initialize(mgr, lang, declaredBeans);
+
+        this.engine = new ScriptEngineManager().getEngineByName(lang);
+
+        for (Iterator it = declaredBeans.iterator(); it.hasNext();) {
+            declareBean((BSFDeclaredBean) it.next());
+        }
+
+    }
+
+    /**
+     * Register all the available JSR-223 engines with BSF.
+     * 
+     * Any BSF engines for the same language will be replaced with the JSR-223 engine.
+     */
+    public static void registerAllJSR223Engines() {
+        ScriptEngineFactory[] engineFactories = new ScriptEngineManager().getEngineFactories();
+        for (int i = 0; i < engineFactories.length; i++) {
+            String[] names = engineFactories[i].getNames();
+            String[] extensions = engineFactories[i].getExtensions();
+            for (int j = 0; j < names.length; j ++) {
+                BSFManager.registerScriptingEngine(names[j], JSR223BSFEngine.class.getName(), extensions);
+            }
+        }
+    }
+
+}



---------------------------------------------------------------------
To unsubscribe, e-mail: tuscany-commits-unsubscribe@ws.apache.org
For additional commands, e-mail: tuscany-commits-help@ws.apache.org