summaryrefslogtreecommitdiffstats
path: root/sandbox/ant/container.script/src/main/java/org/apache/tuscany/container/script/jsr223/JSR223BSFEngine.java
diff options
context:
space:
mode:
Diffstat (limited to '')
-rw-r--r--sandbox/ant/container.script/src/main/java/org/apache/tuscany/container/script/jsr223/JSR223BSFEngine.java118
1 files changed, 118 insertions, 0 deletions
diff --git a/sandbox/ant/container.script/src/main/java/org/apache/tuscany/container/script/jsr223/JSR223BSFEngine.java b/sandbox/ant/container.script/src/main/java/org/apache/tuscany/container/script/jsr223/JSR223BSFEngine.java
new file mode 100644
index 0000000000..fab9813af4
--- /dev/null
+++ b/sandbox/ant/container.script/src/main/java/org/apache/tuscany/container/script/jsr223/JSR223BSFEngine.java
@@ -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);
+ }
+ }
+ }
+
+}