You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@tomee.apache.org by tv...@apache.org on 2012/06/15 12:34:47 UTC
svn commit: r1350565 - in /openejb/trunk/openejb:
container/openejb-core/src/main/java/org/apache/openejb/util/
tomee/tomee-webapp/src/main/java/org/apache/tomee/webapp/servlet/
tomee/tomee-webapp/src/main/webapp/application/js/
Author: tveronezi
Date: Fri Jun 15 10:34:47 2012
New Revision: 1350565
URL: http://svn.apache.org/viewvc?rev=1350565&view=rev
Log:
https://issues.apache.org/jira/browse/TOMEE-228
* setting execution global and local variables (OpenEJBScripter)
* create placeholder for the execution exception handler
Modified:
openejb/trunk/openejb/container/openejb-core/src/main/java/org/apache/openejb/util/OpenEJBScripter.java
openejb/trunk/openejb/tomee/tomee-webapp/src/main/java/org/apache/tomee/webapp/servlet/ConsoleServlet.java
openejb/trunk/openejb/tomee/tomee-webapp/src/main/webapp/application/js/ApplicationController.js
openejb/trunk/openejb/tomee/tomee-webapp/src/main/webapp/application/js/ApplicationModel.js
Modified: openejb/trunk/openejb/container/openejb-core/src/main/java/org/apache/openejb/util/OpenEJBScripter.java
URL: http://svn.apache.org/viewvc/openejb/trunk/openejb/container/openejb-core/src/main/java/org/apache/openejb/util/OpenEJBScripter.java?rev=1350565&r1=1350564&r2=1350565&view=diff
==============================================================================
--- openejb/trunk/openejb/container/openejb-core/src/main/java/org/apache/openejb/util/OpenEJBScripter.java (original)
+++ openejb/trunk/openejb/container/openejb-core/src/main/java/org/apache/openejb/util/OpenEJBScripter.java Fri Jun 15 10:34:47 2012
@@ -25,13 +25,7 @@ import org.apache.openejb.util.proxy.Pro
import javax.enterprise.inject.spi.Bean;
import javax.enterprise.inject.spi.BeanManager;
-import javax.script.Bindings;
-import javax.script.ScriptContext;
-import javax.script.ScriptEngine;
-import javax.script.ScriptEngineFactory;
-import javax.script.ScriptEngineManager;
-import javax.script.ScriptException;
-import javax.script.SimpleBindings;
+import javax.script.*;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;
@@ -62,27 +56,35 @@ public class OpenEJBScripter {
}
public Object evaluate(final String language, final String script) throws ScriptException {
- return evaluate(language, script, new SimpleBindings());
+ return evaluate(language, script, null);
}
- public Object evaluate(final String language, final String script, final Bindings bindings) throws ScriptException {
+ public Object evaluate(final String language, final String script, final ScriptContext context) throws ScriptException {
if (!ENGINE_FACTORIES.containsKey(language)) {
throw new IllegalArgumentException("can't find factory for language " + language + ". You probably need to add the jar to openejb libs.");
}
- Bindings usedBindings = bindings;
- if (usedBindings == null) {
- usedBindings = new SimpleBindings();
+ ScriptContext executionContext = context;
+ if (executionContext == null) {
+ executionContext = new SimpleScriptContext();
}
- return engine(language, usedBindings).eval(script);
+
+ //we bind local variables (per execution) every time we execute a script
+ bindLocal(executionContext);
+
+ final ScriptEngine engine = engine(language);
+ return engine.eval(script, executionContext);
}
- private static ScriptEngine engine(final String language, final Bindings bindings) {
+ private static ScriptEngine engine(final String language) {
ScriptEngine engine = ENGINES.get().get(language);
if (engine == null) {
final ScriptEngineFactory factory = ENGINE_FACTORIES.get(language);
engine = factory.getScriptEngine();
- engine.setBindings(binding(bindings), ScriptContext.ENGINE_SCOPE);
+
+ //we bind system global variables just once
+ bindGlobal(engine);
+
ENGINES.get().put(language, engine);
}
return engine;
@@ -92,8 +94,16 @@ public class OpenEJBScripter {
ENGINES.get().clear();
}
- private static Bindings binding(final Bindings bindings) {
- bindings.put("bm", new BeanManagerHelper());
+ private static void bindGlobal(final ScriptEngine engine) {
+ //"bm" is a global variable during the execution of any script
+ engine.put("bm", new BeanManagerHelper());
+ }
+
+ private static void bindLocal(final ScriptContext context) {
+ final Bindings bindings = context.getBindings(ScriptContext.ENGINE_SCOPE);
+
+ Map<String, Object> beans = new HashMap<String, Object>();
+ bindings.put("beans", beans);
final ContainerSystem cs = SystemInstance.get().getComponent(ContainerSystem.class);
for (BeanContext beanContext : cs.deployments()) {
@@ -115,7 +125,6 @@ public class OpenEJBScripter {
bindings.put(beanContext.getEjbName().replaceAll("[^a-zA-Z0-9]", "_"), service);
}
}
- return bindings;
}
public static class BeanManagerHelper {
Modified: openejb/trunk/openejb/tomee/tomee-webapp/src/main/java/org/apache/tomee/webapp/servlet/ConsoleServlet.java
URL: http://svn.apache.org/viewvc/openejb/trunk/openejb/tomee/tomee-webapp/src/main/java/org/apache/tomee/webapp/servlet/ConsoleServlet.java?rev=1350565&r1=1350564&r2=1350565&view=diff
==============================================================================
--- openejb/trunk/openejb/tomee/tomee-webapp/src/main/java/org/apache/tomee/webapp/servlet/ConsoleServlet.java (original)
+++ openejb/trunk/openejb/tomee/tomee-webapp/src/main/java/org/apache/tomee/webapp/servlet/ConsoleServlet.java Fri Jun 15 10:34:47 2012
@@ -76,11 +76,11 @@ public class ConsoleServlet extends Http
});
//note that "engine" does not know "bindings". It only knows the current context.
- final Object result = SCRIPTER.evaluate(engineName, req.getParameter("scriptCode"));
- // TODO: don't throw an exception here
- // doing it just to print the result but should be done in a 'normal' way
- // i like the idea of the error popup but in the "result popup" way
- throw new TomeeException(new RuntimeException(result.toString())); // todo: json
+ //Eventual exceptions are handled by the ErrorServlet
+ final Object result = SCRIPTER.evaluate(engineName, req.getParameter("scriptCode"), newContext);
+
+ //returning results
+ json.put("result", String.valueOf(result));
}
});
}
Modified: openejb/trunk/openejb/tomee/tomee-webapp/src/main/webapp/application/js/ApplicationController.js
URL: http://svn.apache.org/viewvc/openejb/trunk/openejb/tomee/tomee-webapp/src/main/webapp/application/js/ApplicationController.js?rev=1350565&r1=1350564&r2=1350565&view=diff
==============================================================================
--- openejb/trunk/openejb/tomee/tomee-webapp/src/main/webapp/application/js/ApplicationController.js (original)
+++ openejb/trunk/openejb/tomee/tomee-webapp/src/main/webapp/application/js/ApplicationController.js Fri Jun 15 10:34:47 2012
@@ -183,6 +183,12 @@ TOMEE.ApplicationController = function (
//TODO Implement me
throw "app.console.executed not implemented";
});
+
+ channel.bind('app.console.executed.error', function (params) {
+ //TODO Implement me
+ //Handle an eventual script execution error
+ throw "app.console.executed.error not implemented";
+ });
})();
Modified: openejb/trunk/openejb/tomee/tomee-webapp/src/main/webapp/application/js/ApplicationModel.js
URL: http://svn.apache.org/viewvc/openejb/trunk/openejb/tomee/tomee-webapp/src/main/webapp/application/js/ApplicationModel.js?rev=1350565&r1=1350564&r2=1350565&view=diff
==============================================================================
--- openejb/trunk/openejb/tomee/tomee-webapp/src/main/webapp/application/js/ApplicationModel.js (original)
+++ openejb/trunk/openejb/tomee/tomee-webapp/src/main/webapp/application/js/ApplicationModel.js Fri Jun 15 10:34:47 2012
@@ -124,6 +124,9 @@ TOMEE.ApplicationModel = function (cfg)
success:function (data) {
systemInfo = data;
channel.send('app.console.executed', data);
+ },
+ error:function (data) {
+ channel.send('app.console.executed.error', data);
}
});