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/10/06 18:40:02 UTC

svn commit: r1395108 - in /openejb/trunk/openejb/tomee/tomee-webapp/src/main/java/org/apache/tomee/webapp: application/ command/ command/impl/ command/impl/script/ listener/

Author: tveronezi
Date: Sat Oct  6 16:40:01 2012
New Revision: 1395108

URL: http://svn.apache.org/viewvc?rev=1395108&view=rev
Log:
https://issues.apache.org/jira/browse/TOMEE-447

Added:
    openejb/trunk/openejb/tomee/tomee-webapp/src/main/java/org/apache/tomee/webapp/command/CommandSession.java
Removed:
    openejb/trunk/openejb/tomee/tomee-webapp/src/main/java/org/apache/tomee/webapp/application/ApplicationData.java
    openejb/trunk/openejb/tomee/tomee-webapp/src/main/java/org/apache/tomee/webapp/application/SessionData.java
    openejb/trunk/openejb/tomee/tomee-webapp/src/main/java/org/apache/tomee/webapp/command/Params.java
    openejb/trunk/openejb/tomee/tomee-webapp/src/main/java/org/apache/tomee/webapp/command/impl/DeployApplication.java
    openejb/trunk/openejb/tomee/tomee-webapp/src/main/java/org/apache/tomee/webapp/command/impl/GetDeployedApplications.java
    openejb/trunk/openejb/tomee/tomee-webapp/src/main/java/org/apache/tomee/webapp/command/impl/GetJndiTree.java
    openejb/trunk/openejb/tomee/tomee-webapp/src/main/java/org/apache/tomee/webapp/command/impl/GetSessionData.java
    openejb/trunk/openejb/tomee/tomee-webapp/src/main/java/org/apache/tomee/webapp/command/impl/GetSystemInfo.java
    openejb/trunk/openejb/tomee/tomee-webapp/src/main/java/org/apache/tomee/webapp/command/impl/JndiLookup.java
    openejb/trunk/openejb/tomee/tomee-webapp/src/main/java/org/apache/tomee/webapp/command/impl/Logout.java
    openejb/trunk/openejb/tomee/tomee-webapp/src/main/java/org/apache/tomee/webapp/command/impl/script/MethodParam.java
    openejb/trunk/openejb/tomee/tomee-webapp/src/main/java/org/apache/tomee/webapp/command/impl/script/Utility.java
    openejb/trunk/openejb/tomee/tomee-webapp/src/main/java/org/apache/tomee/webapp/listener/ApplicationListener.java
    openejb/trunk/openejb/tomee/tomee-webapp/src/main/java/org/apache/tomee/webapp/listener/UserSessionListener.java
Modified:
    openejb/trunk/openejb/tomee/tomee-webapp/src/main/java/org/apache/tomee/webapp/command/Command.java
    openejb/trunk/openejb/tomee/tomee-webapp/src/main/java/org/apache/tomee/webapp/command/CommandExecutor.java
    openejb/trunk/openejb/tomee/tomee-webapp/src/main/java/org/apache/tomee/webapp/command/impl/GetLog.java
    openejb/trunk/openejb/tomee/tomee-webapp/src/main/java/org/apache/tomee/webapp/command/impl/RunScript.java

Modified: openejb/trunk/openejb/tomee/tomee-webapp/src/main/java/org/apache/tomee/webapp/command/Command.java
URL: http://svn.apache.org/viewvc/openejb/trunk/openejb/tomee/tomee-webapp/src/main/java/org/apache/tomee/webapp/command/Command.java?rev=1395108&r1=1395107&r2=1395108&view=diff
==============================================================================
--- openejb/trunk/openejb/tomee/tomee-webapp/src/main/java/org/apache/tomee/webapp/command/Command.java (original)
+++ openejb/trunk/openejb/tomee/tomee-webapp/src/main/java/org/apache/tomee/webapp/command/Command.java Sat Oct  6 16:40:01 2012
@@ -17,8 +17,10 @@
 
 package org.apache.tomee.webapp.command;
 
+import java.util.Map;
+
 public interface Command {
 
-    Object execute(Params params) throws Exception;
+    Object execute(CommandSession session, Map<String, Object> params) throws Exception;
 
 }

Modified: openejb/trunk/openejb/tomee/tomee-webapp/src/main/java/org/apache/tomee/webapp/command/CommandExecutor.java
URL: http://svn.apache.org/viewvc/openejb/trunk/openejb/tomee/tomee-webapp/src/main/java/org/apache/tomee/webapp/command/CommandExecutor.java?rev=1395108&r1=1395107&r2=1395108&view=diff
==============================================================================
--- openejb/trunk/openejb/tomee/tomee-webapp/src/main/java/org/apache/tomee/webapp/command/CommandExecutor.java (original)
+++ openejb/trunk/openejb/tomee/tomee-webapp/src/main/java/org/apache/tomee/webapp/command/CommandExecutor.java Sat Oct  6 16:40:01 2012
@@ -18,108 +18,52 @@
 package org.apache.tomee.webapp.command;
 
 import com.google.gson.Gson;
-import com.google.gson.GsonBuilder;
-import org.apache.tomee.webapp.TomeeException;
+import com.google.gson.reflect.TypeToken;
+import org.quartz.SimpleTrigger;
 
-import javax.servlet.ServletException;
-import javax.servlet.http.HttpServlet;
-import javax.servlet.http.HttpServletRequest;
-import javax.servlet.http.HttpServletResponse;
-import java.io.IOException;
-import java.util.*;
-import java.util.concurrent.Callable;
-import java.util.concurrent.ExecutorService;
-import java.util.concurrent.Executors;
-import java.util.concurrent.TimeUnit;
+import java.io.PrintWriter;
+import java.io.StringWriter;
+import java.io.Writer;
+import java.lang.reflect.Type;
+import java.util.HashMap;
+import java.util.Map;
+
+public class CommandExecutor {
+    private Gson gson = new Gson();
+    private final Type mapType = new TypeToken<Map<String, Object>>() {}.getType();
 
-public class CommandExecutor extends HttpServlet {
+    private static final String PATH = "org.apache.tomee.webapp.command.impl.";
 
-    private static final int TIMEOUT = 20;
+    public Map<String, Object> execute(final CommandSession session, final String raw) {
+        final Map<String, Object> result = new HashMap<String, Object>();
 
-    public interface Executor {
-        void call(Map<String, Object> json) throws Exception;
-    }
+        final long start = System.currentTimeMillis();
+        result.put("start", start);
 
-    private List<Command> getCommands(final HttpServletRequest req, String key) throws ClassNotFoundException, IllegalAccessException, InstantiationException {
-        final String strCmds = req.getParameter(key);
-        if (strCmds == null || "".equals(strCmds.trim())) {
-            return Collections.emptyList();
-        }
+        try {
+            final Map<String, Object> params = gson.fromJson(raw, mapType);
+            result.put("params", params);
 
-        final List<Command> result = new ArrayList<Command>();
-        final String[] arrCmds = strCmds.split(",");
-        for (String strCmd : arrCmds) {
-            final Class<?> cls = Class.forName("org.apache.tomee.webapp.command.impl." + strCmd);
+            // Remove the cmdName from this list.
+            final String cmdName = (String) params.remove("cmdName");
+            final Class<?> cls = Class.forName(PATH + cmdName);
             final Command cmd = (Command) cls.newInstance();
-            result.add(cmd);
-        }
-
-        return result;
-    }
 
-    public void execute(final HttpServletRequest req, final HttpServletResponse resp) {
-        try {
-            resp.setContentType("application/json");
-            resp.setCharacterEncoding("UTF-8");
+            result.put("cmdName", cmdName);
+            result.put("output", cmd.execute(session, params));
+            result.put("success", Boolean.TRUE);
 
-            final Gson gson;
-            if (Boolean.valueOf(req.getParameter("pretty"))) {
-                gson = new GsonBuilder().setPrettyPrinting().create();
-            } else {
-                gson = new Gson();
-            }
-
-
-            final Params params = new Params(req, resp);
-            final Map<String, Object> result = Collections.synchronizedMap(new HashMap<String, Object>());
-
-            //execute the commands
-            {
-                final List<Command> commands = getCommands(req, "cmd");
-                for (Command command : commands) {
-                    result.put(command.getClass().getSimpleName(), command.execute(params));
-                }
-            }
-
-            //execute the async commands
-            {
-                final List<Command> commands = getCommands(req, "asyncCmd");
-                final ExecutorService executor = Executors.newCachedThreadPool();
-                final List<Callable<Void>> commandsToRun = new ArrayList<Callable<Void>>();
-                for (final Command command : commands) {
-                    final Callable<Void> callMe = new Callable<Void>() {
-                        @Override
-                        public Void call() throws Exception {
-                            result.put(command.getClass().getSimpleName(), command.execute(params));
-                            return null;
-                        }
-                    };
-                    commandsToRun.add(callMe);
-                }
-
-                if (params.getInteger("timeout") != null) {
-                    executor.invokeAll(commandsToRun, params.getLong("timeout"), TimeUnit.SECONDS);
-                } else {
-                    executor.invokeAll(commandsToRun, TIMEOUT, TimeUnit.SECONDS);
-                }
-                executor.shutdown();
-            }
+        } catch (Throwable e) {
+            result.put("success", Boolean.FALSE);
 
-            resp.getWriter().write(gson.toJson(result));
+            final Writer writer = new StringWriter();
+            final PrintWriter printWriter = new PrintWriter(writer);
+            e.printStackTrace(printWriter);
 
-        } catch (Throwable e) {
-            //this will redirect the result to the ErrorServlet
-            throw new TomeeException(e);
+            result.put("output", writer.toString());
         }
-    }
 
-    @Override
-    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
-        execute(req, resp);
-    }
-
-    @Override
-    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
-        execute(req, resp);
+        result.put("timeSpent", (System.currentTimeMillis() - start));
+        return result;
     }
 }

Added: openejb/trunk/openejb/tomee/tomee-webapp/src/main/java/org/apache/tomee/webapp/command/CommandSession.java
URL: http://svn.apache.org/viewvc/openejb/trunk/openejb/tomee/tomee-webapp/src/main/java/org/apache/tomee/webapp/command/CommandSession.java?rev=1395108&view=auto
==============================================================================
--- openejb/trunk/openejb/tomee/tomee-webapp/src/main/java/org/apache/tomee/webapp/command/CommandSession.java (added)
+++ openejb/trunk/openejb/tomee/tomee-webapp/src/main/java/org/apache/tomee/webapp/command/CommandSession.java Sat Oct  6 16:40:01 2012
@@ -0,0 +1,25 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You 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.tomee.webapp.command;
+
+public interface CommandSession {
+
+    Object get(String key);
+    void set(String key, Object value);
+
+}

Modified: openejb/trunk/openejb/tomee/tomee-webapp/src/main/java/org/apache/tomee/webapp/command/impl/GetLog.java
URL: http://svn.apache.org/viewvc/openejb/trunk/openejb/tomee/tomee-webapp/src/main/java/org/apache/tomee/webapp/command/impl/GetLog.java?rev=1395108&r1=1395107&r2=1395108&view=diff
==============================================================================
--- openejb/trunk/openejb/tomee/tomee-webapp/src/main/java/org/apache/tomee/webapp/command/impl/GetLog.java (original)
+++ openejb/trunk/openejb/tomee/tomee-webapp/src/main/java/org/apache/tomee/webapp/command/impl/GetLog.java Sat Oct  6 16:40:01 2012
@@ -19,7 +19,7 @@ package org.apache.tomee.webapp.command.
 
 import org.apache.commons.lang3.StringEscapeUtils;
 import org.apache.tomee.webapp.command.Command;
-import org.apache.tomee.webapp.command.Params;
+import org.apache.tomee.webapp.command.CommandSession;
 
 import java.io.BufferedReader;
 import java.io.File;
@@ -27,14 +27,14 @@ import java.io.FileReader;
 import java.io.IOException;
 import java.util.*;
 
-
 public class GetLog implements Command {
 
     @Override
-    public Object execute(Params params) throws Exception {
+    public Object execute(final CommandSession session, final Map<String, Object> params) throws Exception {
         final Map<String, Object> json = new HashMap<String, Object>();
 
-        final File logFolder = new File(System.getProperty("catalina.base"), "logs");
+        final File logFolder = new File(System.getProperty("catalina.base"),
+                "logs");
 
         final File[] files = logFolder.listFiles();
         final Set<String> names = new TreeSet<String>();
@@ -48,15 +48,15 @@ public class GetLog implements Command {
 
         json.put("files", names);
 
-        final String loadFileName = params.getString("file");
+        final String loadFileName = (String) params.get("file");
         if (loadFileName != null) {
             Map<String, Object> log = new HashMap<String, Object>();
             log.put("name", loadFileName);
 
             log.put("lines", read(
-                    Boolean.valueOf(params.getString("escapeHtml")),
+                    Boolean.valueOf((String) params.get("escapeHtml")),
                     new File(logFolder, loadFileName),
-                    params.getInteger("tail")
+                    Integer.getInteger((String) params.get("tail"))
             ));
 
             json.put("log", log);
@@ -66,7 +66,8 @@ public class GetLog implements Command {
     }
 
 
-    private Collection<String> read(final boolean escapeHtml, final File file, final Integer tail) throws IOException {
+    private Collection<String> read(final boolean escapeHtml, final File file,
+                                    final Integer tail) throws IOException {
         final Queue<String> lines = new LinkedList<String>();
 
         BufferedReader br = null;

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=1395108&r1=1395107&r2=1395108&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 Sat Oct  6 16:40:01 2012
@@ -22,32 +22,35 @@ import org.apache.openejb.loader.SystemI
 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 org.apache.tomee.webapp.command.CommandSession;
 
 import javax.script.*;
+import java.util.Map;
 import java.util.concurrent.CountDownLatch;
 
 public class RunScript implements Command {
 
     @Override
-    public Object execute(final Params params) throws Exception {
-        final String scriptCode = params.getString("scriptCode");
+    public Object execute(final CommandSession session, final Map<String, Object> params) throws Exception {
+        final String scriptCode = (String) params.get("scriptCode");
         if (scriptCode == null) {
             return null; //nothing to do
         }
 
-        String engineName = params.getString("engineName");
+        String engineName = (String) params.get("engineName");
         if (engineName == null) {
             engineName = "js";
         }
 
         final CountDownLatch latch = new CountDownLatch(1);
 
-        //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);
+        // 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, session,
+                engineName, scriptCode);
         final Thread thread = new Thread(execution);
-        thread.setContextClassLoader(getClassLoader(params.getString("appName")));
+        thread.setContextClassLoader(
+                getClassLoader((String) params.get("appName")));
         thread.start();
 
         //wait until it is done
@@ -66,7 +69,8 @@ public class RunScript implements Comman
             return Thread.currentThread().getContextClassLoader();
         }
 
-        final ContainerSystem cs = SystemInstance.get().getComponent(ContainerSystem.class);
+        final ContainerSystem cs = SystemInstance.get().getComponent(
+                ContainerSystem.class);
         final AppContext ctx = cs.getAppContext(appName);
         if (ctx == null) {
             return Thread.currentThread().getContextClassLoader();
@@ -77,7 +81,7 @@ public class RunScript implements Comman
 
     private class ExecutionThread implements Runnable {
         private final CountDownLatch latch;
-        private final Params params;
+        private final CommandSession session;
         private final String engineName;
         private final String scriptCode;
 
@@ -92,9 +96,12 @@ public class RunScript implements Comman
             return exception;
         }
 
-        private ExecutionThread(final CountDownLatch latch, final Params params, final String engineName, final String scriptCode) {
+        private ExecutionThread(final CountDownLatch latch,
+                                final CommandSession session,
+                                final String engineName,
+                                final String scriptCode) {
             this.latch = latch;
-            this.params = params;
+            this.session = session;
             this.engineName = engineName;
             this.scriptCode = scriptCode;
         }
@@ -102,15 +109,16 @@ public class RunScript implements Comman
         @Override
         public void run() {
             final ScriptEngineManager manager = new ScriptEngineManager();
-            final ScriptEngine engine = manager.getEngineByName(this.engineName);
+            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));
+            final Bindings bindings = newContext.getBindings(
+                    ScriptContext.ENGINE_SCOPE);
+            bindings.put("session", session);
 
             try {
                 this.result = engine.eval(this.scriptCode, newContext);