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/29 23:51:33 UTC
svn commit: r1355572 - in /openejb/trunk/openejb/tomee/tomee-webapp/src:
main/java/org/apache/tomee/webapp/command/impl/ main/webapp/application/js/
main/webapp/application/js/view/panels/
test/java/org/apache/tomee/webapp/test/
Author: tveronezi
Date: Fri Jun 29 21:51:31 2012
New Revision: 1355572
URL: http://svn.apache.org/viewvc?rev=1355572&view=rev
Log:
https://issues.apache.org/jira/browse/TOMEE-256
* use the application classloader when running scripts
Modified:
openejb/trunk/openejb/tomee/tomee-webapp/src/main/java/org/apache/tomee/webapp/command/impl/RunScript.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
openejb/trunk/openejb/tomee/tomee-webapp/src/main/webapp/application/js/view/panels/Console.js
openejb/trunk/openejb/tomee/tomee-webapp/src/test/java/org/apache/tomee/webapp/test/RunScriptTest.java
Modified: openejb/trunk/openejb/tomee/tomee-webapp/src/main/java/org/apache/tomee/webapp/command/impl/RunScript.java
URL: http://svn.apache.org/viewvc/openejb/trunk/openejb/tomee/tomee-webapp/src/main/java/org/apache/tomee/webapp/command/impl/RunScript.java?rev=1355572&r1=1355571&r2=1355572&view=diff
==============================================================================
--- openejb/trunk/openejb/tomee/tomee-webapp/src/main/java/org/apache/tomee/webapp/command/impl/RunScript.java (original)
+++ openejb/trunk/openejb/tomee/tomee-webapp/src/main/java/org/apache/tomee/webapp/command/impl/RunScript.java Fri Jun 29 21:51:31 2012
@@ -17,17 +17,18 @@
package org.apache.tomee.webapp.command.impl;
-import org.apache.openejb.util.OpenEJBScripter;
+import org.apache.openejb.AppContext;
+import org.apache.openejb.loader.SystemInstance;
+import org.apache.openejb.spi.ContainerSystem;
+import org.apache.tomee.webapp.TomeeException;
import org.apache.tomee.webapp.command.Command;
import org.apache.tomee.webapp.command.Params;
import org.apache.tomee.webapp.command.impl.script.Utility;
-import javax.script.Bindings;
-import javax.script.ScriptContext;
-import javax.script.SimpleScriptContext;
+import javax.script.*;
+import java.util.concurrent.CountDownLatch;
public class RunScript implements Command {
- public static final OpenEJBScripter SCRIPTER = new OpenEJBScripter();
@Override
public Object execute(final Params params) throws Exception {
@@ -41,17 +42,83 @@ public class RunScript implements Comman
engineName = "js";
}
- //new context for the execution of this script
- final ScriptContext newContext = new SimpleScriptContext();
+ final CountDownLatch latch = new CountDownLatch(1);
- //creating the bidings object for the current execution
- final Bindings bindings = newContext.getBindings(ScriptContext.ENGINE_SCOPE);
+ //everything should be created inside the new classloader, so run it inside another thread and set the proper classloader
+ final ExecutionThread execution = new ExecutionThread(latch, params, engineName, scriptCode);
+ final Thread thread = new Thread(execution);
+ thread.setContextClassLoader(getClassLoader(params.getString("appName")));
+ thread.start();
+
+ //wait until it is done
+ latch.await();
+
+ //any exception?
+ if (execution.getException() != null) {
+ //just throw it
+ throw new TomeeException(execution.getException());
+ }
+ return execution.getResult();
+ }
+
+ private ClassLoader getClassLoader(final String appName) {
+ if (appName == null) {
+ return Thread.currentThread().getContextClassLoader();
+ }
+
+ final ContainerSystem cs = SystemInstance.get().getComponent(ContainerSystem.class);
+ final AppContext ctx = cs.getAppContext(appName);
+ if (ctx == null) {
+ return Thread.currentThread().getContextClassLoader();
+ }
+
+ return ctx.getClassLoader();
+ }
- bindings.put("util", new Utility(params));
+ private class ExecutionThread implements Runnable {
+ private final CountDownLatch latch;
+ private final Params params;
+ private final String engineName;
+ private final String scriptCode;
- //note that "engine" does not know "bindings". It only knows the current context.
- //Eventual exceptions are handled by the ErrorServlet
- final Object result = SCRIPTER.evaluate(engineName, scriptCode, newContext);
- return result;
+ private Object result;
+ private Exception exception;
+
+ public Object getResult() {
+ return result;
+ }
+
+ public Exception getException() {
+ return exception;
+ }
+
+ private ExecutionThread(final CountDownLatch latch, final Params params, final String engineName, final String scriptCode) {
+ this.latch = latch;
+ this.params = params;
+ this.engineName = engineName;
+ this.scriptCode = scriptCode;
+ }
+
+ @Override
+ public void run() {
+ final ScriptEngineManager manager = new ScriptEngineManager();
+ final ScriptEngine engine = manager.getEngineByName(this.engineName);
+
+ //new context for the execution of this script
+ final ScriptContext newContext = new SimpleScriptContext();
+
+ //creating the bidings object for the current execution
+ final Bindings bindings = newContext.getBindings(ScriptContext.ENGINE_SCOPE);
+
+ bindings.put("util", new Utility(this.params));
+
+ try {
+ this.result = engine.eval(this.scriptCode, newContext);
+ } catch (ScriptException e) {
+ this.exception = e;
+ } finally {
+ latch.countDown();
+ }
+ }
}
}
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=1355572&r1=1355571&r2=1355572&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 29 21:51:31 2012
@@ -84,6 +84,7 @@ TOMEE.ApplicationController = function (
if (params['GetDeployedApplications']) {
deployments.loadDeployeApps(params['GetDeployedApplications']);
+ consolePanel.loadAppsField(params['GetDeployedApplications'].uids);
}
if (params['GetSessionData']) {
@@ -217,7 +218,7 @@ TOMEE.ApplicationController = function (
(function () {
channel.bind('trigger.console.exec', function (params) {
- model.executeCommands(model.execute(params.codeType, params.codeText));
+ model.executeCommands(model.execute(params));
});
channel.bind('app.console.executed', function (params) {
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=1355572&r1=1355571&r2=1355572&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 29 21:51:31 2012
@@ -113,7 +113,7 @@ TOMEE.ApplicationModel = function (cfg)
}
var myFinalCommand = {
- method:'GET',
+ method:'POST',
url:TOMEE.baseURL('command'),
data:{},
success:function (data) {
@@ -209,10 +209,9 @@ TOMEE.ApplicationModel = function (cfg)
}
};
},
- execute:function (codeType, codeText) {
+ execute:function (params) {
var executionBean = {
- codeType:codeType,
- codeText:codeText,
+ params:params,
start:(new Date())
};
executions.push(executionBean);
@@ -220,12 +219,9 @@ TOMEE.ApplicationModel = function (cfg)
return [
{
cmd:'RunScript',
- data:{
- engineName:codeType,
- scriptCode:codeText
- },
+ data:params,
success:function (data) {
- setLastScript(codeText);
+ setLastScript(params.scriptCode);
executionBean.success = true;
executionBean.data = data['GetSystemInfo'];
Modified: openejb/trunk/openejb/tomee/tomee-webapp/src/main/webapp/application/js/view/panels/Console.js
URL: http://svn.apache.org/viewvc/openejb/trunk/openejb/tomee/tomee-webapp/src/main/webapp/application/js/view/panels/Console.js?rev=1355572&r1=1355571&r2=1355572&view=diff
==============================================================================
--- openejb/trunk/openejb/tomee/tomee-webapp/src/main/webapp/application/js/view/panels/Console.js (original)
+++ openejb/trunk/openejb/tomee/tomee-webapp/src/main/webapp/application/js/view/panels/Console.js Fri Jun 29 21:51:31 2012
@@ -34,11 +34,15 @@ TOMEE.Console = function (cfg) {
extraStyles:{
height:'500px'
},
- onResize: function(height) {
+ onResize:function (height) {
elText.main.height(height);
},
bbar:[
{
+ elName:'appSelector',
+ tag:'select'
+ },
+ {
elName:'scriptSelector',
tag:'select'
},
@@ -46,13 +50,15 @@ TOMEE.Console = function (cfg) {
tag:'a',
cls:'btn',
html:TOMEE.I18N.get('application.console.execute'),
- listeners: {
- 'click': function() {
+ listeners:{
+ 'click':function () {
var text = elText.main.val();
var script = console.getElement('scriptSelector').val();
+ var app = console.getElement('appSelector').val();
channel.send('trigger.console.exec', {
- codeType:script,
- codeText:text
+ engineName:script,
+ scriptCode:text,
+ appName:app
});
}
}
@@ -63,34 +69,52 @@ TOMEE.Console = function (cfg) {
var el = console.getContentEl();
el.append(elText.main);
- var loadScriptsField = function (languages) {
- var getOption = function (lang) {
+ var loadSelector = function (values, getValueBean, selector) {
+ var getOption = function (valueBean) {
var option = $('<option></option>');
- option.attr('value', lang);
- option.append(lang);
+ option.attr('value', valueBean.value);
+ option.append(valueBean.text);
return option;
};
- var selector = console.getElement('scriptSelector');
selector.empty();
- if (!languages) {
+ if (!values) {
return;
}
- for (var i = 0; i < languages.length; i++) {
- selector.append(getOption(languages[i]));
+ for (var i = 0; i < values.length; i++) {
+ selector.append(getOption(getValueBean(values[i])));
}
};
+ var loadScriptsField = function (languages) {
+ loadSelector(languages, function (bean) {
+ return {
+ value:bean,
+ text:bean
+ };
+ }, console.getElement('scriptSelector'));
+ };
+
+ var loadAppsField = function (apps) {
+ loadSelector(apps, function (bean) {
+ return {
+ value:bean,
+ text:bean
+ };
+ }, console.getElement('appSelector'));
+ };
+
return {
- setScript: function(script) {
+ setScript:function (script) {
elText.main.html(script);
},
- setHeight:function(height) {
+ setHeight:function (height) {
console.setHeight(height);
},
getEl:function () {
return console.getEl();
},
- loadScriptsField:loadScriptsField
+ loadScriptsField:loadScriptsField,
+ loadAppsField:loadAppsField
};
};
\ No newline at end of file
Modified: openejb/trunk/openejb/tomee/tomee-webapp/src/test/java/org/apache/tomee/webapp/test/RunScriptTest.java
URL: http://svn.apache.org/viewvc/openejb/trunk/openejb/tomee/tomee-webapp/src/test/java/org/apache/tomee/webapp/test/RunScriptTest.java?rev=1355572&r1=1355571&r2=1355572&view=diff
==============================================================================
--- openejb/trunk/openejb/tomee/tomee-webapp/src/test/java/org/apache/tomee/webapp/test/RunScriptTest.java (original)
+++ openejb/trunk/openejb/tomee/tomee-webapp/src/test/java/org/apache/tomee/webapp/test/RunScriptTest.java Fri Jun 29 21:51:31 2012
@@ -34,7 +34,7 @@ public class RunScriptTest {
@Test
public void getInstanceTest() throws Exception {
this.myParameterMap.put("scriptCode", new String[]{
- readJsFile()
+ readJsFile("/Test.js")
});
this.myParameterMap.put("engineName", new String[]{"js"});
@@ -44,10 +44,10 @@ public class RunScriptTest {
assertEquals("myValue", result);
}
- private String readJsFile() throws IOException {
+ private String readJsFile(String fileName) throws IOException {
StringBuilder contents = new StringBuilder();
- DataInputStream in = new DataInputStream(RunScriptTest.class.getResourceAsStream("/Test.js"));
+ DataInputStream in = new DataInputStream(RunScriptTest.class.getResourceAsStream(fileName));
BufferedReader br = new BufferedReader(new InputStreamReader(in));
String strLine;
while ((strLine = br.readLine()) != null) {