geronimo-scm mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From gno...@apache.org
Subject svn commit: r707633 - in /geronimo/gshell/trunk: gshell-api/src/main/java/org/apache/geronimo/gshell/commandline/ gshell-parser/src/main/java/org/apache/geronimo/gshell/parser/visitor/ gshell-wisdom/gshell-wisdom-core/src/main/java/org/apache/geronimo/...
Date Fri, 24 Oct 2008 13:52:59 GMT
Author: gnodet
Date: Fri Oct 24 06:52:59 2008
New Revision: 707633

URL: http://svn.apache.org/viewvc?rev=707633&view=rev
Log:
Move the pipe logic from CommandLineExecutorImpl to ExecutingVisitor and avoid creating threads
when running simple commands with no pipes.

Modified:
    geronimo/gshell/trunk/gshell-api/src/main/java/org/apache/geronimo/gshell/commandline/CommandLineExecutor.java
    geronimo/gshell/trunk/gshell-parser/src/main/java/org/apache/geronimo/gshell/parser/visitor/ExecutingVisitor.java
    geronimo/gshell/trunk/gshell-wisdom/gshell-wisdom-core/src/main/java/org/apache/geronimo/gshell/wisdom/shell/CommandLineExecutorImpl.java

Modified: geronimo/gshell/trunk/gshell-api/src/main/java/org/apache/geronimo/gshell/commandline/CommandLineExecutor.java
URL: http://svn.apache.org/viewvc/geronimo/gshell/trunk/gshell-api/src/main/java/org/apache/geronimo/gshell/commandline/CommandLineExecutor.java?rev=707633&r1=707632&r2=707633&view=diff
==============================================================================
--- geronimo/gshell/trunk/gshell-api/src/main/java/org/apache/geronimo/gshell/commandline/CommandLineExecutor.java
(original)
+++ geronimo/gshell/trunk/gshell-api/src/main/java/org/apache/geronimo/gshell/commandline/CommandLineExecutor.java
Fri Oct 24 06:52:59 2008
@@ -62,14 +62,4 @@
      */
     Object execute(ShellContext context, Object... args) throws Exception;
 
-    /**
-     * Execute a piped-command-line.
-     *
-     * @param context   The executing shell's context.
-     * @param commands  Command arguments.
-     * @return          Command execution result.
-     *
-     * @throws Exception    Command-line execution failed.
-     */
-    Object execute(ShellContext context, Object[][] commands) throws Exception;
 }
\ No newline at end of file

Modified: geronimo/gshell/trunk/gshell-parser/src/main/java/org/apache/geronimo/gshell/parser/visitor/ExecutingVisitor.java
URL: http://svn.apache.org/viewvc/geronimo/gshell/trunk/gshell-parser/src/main/java/org/apache/geronimo/gshell/parser/visitor/ExecutingVisitor.java?rev=707633&r1=707632&r2=707633&view=diff
==============================================================================
--- geronimo/gshell/trunk/gshell-parser/src/main/java/org/apache/geronimo/gshell/parser/visitor/ExecutingVisitor.java
(original)
+++ geronimo/gshell/trunk/gshell-parser/src/main/java/org/apache/geronimo/gshell/parser/visitor/ExecutingVisitor.java
Fri Oct 24 06:52:59 2008
@@ -20,7 +20,9 @@
 package org.apache.geronimo.gshell.parser.visitor;
 
 import org.apache.geronimo.gshell.commandline.CommandLineExecutor;
+import org.apache.geronimo.gshell.commandline.CommandLineExecutionFailed;
 import org.apache.geronimo.gshell.notification.ErrorNotification;
+import org.apache.geronimo.gshell.notification.Notification;
 import org.apache.geronimo.gshell.parser.ASTCommandLine;
 import org.apache.geronimo.gshell.parser.ASTExpression;
 import org.apache.geronimo.gshell.parser.ASTOpaqueString;
@@ -30,13 +32,26 @@
 import org.apache.geronimo.gshell.parser.CommandLineParserVisitor;
 import org.apache.geronimo.gshell.parser.SimpleNode;
 import org.apache.geronimo.gshell.command.Arguments;
+import org.apache.geronimo.gshell.command.Variables;
 import org.apache.geronimo.gshell.interpolation.VariableInterpolator;
 import org.apache.geronimo.gshell.shell.ShellContext;
+import org.apache.geronimo.gshell.shell.Shell;
+import org.apache.geronimo.gshell.io.IO;
+import org.apache.geronimo.gshell.io.Closer;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
 import java.util.ArrayList;
 import java.util.List;
+import java.util.concurrent.CopyOnWriteArrayList;
+import java.util.concurrent.CountDownLatch;
+import java.util.concurrent.atomic.AtomicReference;
+import java.io.PipedOutputStream;
+import java.io.InputStream;
+import java.io.PipedInputStream;
+import java.io.OutputStream;
+import java.io.PrintStream;
+import java.io.IOException;
 
 /**
  * Visitor which will execute command-lines as parsed.
@@ -46,7 +61,7 @@
 public class ExecutingVisitor
     implements CommandLineParserVisitor
 {
-    private final Logger log = LoggerFactory.getLogger(getClass());
+    private final Logger MetaDataRegistryConfigurerTestlog = LoggerFactory.getLogger(getClass());
 
     private final ShellContext context;
 
@@ -97,7 +112,7 @@
         }
 
         try {
-            return executor.execute(context, commands);
+            return executePiped(commands);
         }
         catch (Exception e) {
             String s = Arguments.asString(commands[0]);
@@ -146,4 +161,98 @@
 
         return appendString(node.getValue(), data);
     }
+
+    protected Thread createThread(final Runnable task) {
+        return new Thread(task);
+    }
+
+    private Object executePiped(final Object[][] commands) throws CommandLineExecutionFailed,
InterruptedException, IOException {
+        // Prepare IOs
+        final IO[] ios = new IO[commands.length];
+        PipedOutputStream pos = null;
+
+        IO io = this.context.getIo();
+
+        for (int i = 0; i < ios.length; i++) {
+            InputStream is = (i == 0) ? io.inputStream : new PipedInputStream(pos);
+            OutputStream os;
+
+            if (i == ios.length - 1) {
+                os = io.outputStream;
+            }
+            else {
+                os = pos = new PipedOutputStream();
+            }
+
+            ios[i] = new IO(is, new PrintStream(os), io.errorStream);
+        }
+
+        final List<Throwable> errors = new CopyOnWriteArrayList<Throwable>();
+        final AtomicReference<Object> ref = new AtomicReference<Object>();
+        final CountDownLatch latch = new CountDownLatch(commands.length);
+
+        for (int i = 0; i < commands.length; i++) {
+            final int idx = i;
+
+            Runnable r = new Runnable() {
+                public void run() {
+                    try {
+                        ShellContext pipedContext = new ShellContext() {
+                            public Shell getShell() {
+                                return ExecutingVisitor.this.context.getShell();
+                            }
+
+                            public IO getIo() {
+                                return ios[idx];
+                            }
+
+                            public Variables getVariables() {
+                                return ExecutingVisitor.this.context.getVariables();
+                            }
+                        };
+
+                        Object obj = executor.execute(pipedContext, String.valueOf(commands[idx][0]),
Arguments.shift(commands[idx]));
+
+                        if (idx == commands.length - 1) {
+                            ref.set(obj);
+                        }
+                    }
+                    catch (Throwable t) {
+                        errors.add(t);
+                    }
+                    finally {
+                        if (idx > 0) {
+                            Closer.close(ios[idx].inputStream);
+                        }
+                        if (idx < commands.length - 1) {
+                            Closer.close(ios[idx].outputStream);
+                        }
+                        latch.countDown();
+                    }
+                }
+            };
+            if (idx != commands.length - 1) {
+                createThread(r).start();
+            } else {
+                r.run();
+            }
+        }
+
+        latch.await();
+
+        if (!errors.isEmpty()) {
+            Throwable t = errors.get(0);
+
+            // Always preserve the type of notication throwables, reguardless of the trace
+            if (t instanceof Notification) {
+                throw (Notification)t;
+            }
+
+            // Otherwise wrap to preserve the trace
+            throw new CommandLineExecutionFailed(t);
+        }
+
+        return ref.get();
+    }
+
 }

Modified: geronimo/gshell/trunk/gshell-wisdom/gshell-wisdom-core/src/main/java/org/apache/geronimo/gshell/wisdom/shell/CommandLineExecutorImpl.java
URL: http://svn.apache.org/viewvc/geronimo/gshell/trunk/gshell-wisdom/gshell-wisdom-core/src/main/java/org/apache/geronimo/gshell/wisdom/shell/CommandLineExecutorImpl.java?rev=707633&r1=707632&r2=707633&view=diff
==============================================================================
--- geronimo/gshell/trunk/gshell-wisdom/gshell-wisdom-core/src/main/java/org/apache/geronimo/gshell/wisdom/shell/CommandLineExecutorImpl.java
(original)
+++ geronimo/gshell/trunk/gshell-wisdom/gshell-wisdom-core/src/main/java/org/apache/geronimo/gshell/wisdom/shell/CommandLineExecutorImpl.java
Fri Oct 24 06:52:59 2008
@@ -27,28 +27,15 @@
 import org.apache.geronimo.gshell.command.Variables;
 import org.apache.geronimo.gshell.commandline.CommandLine;
 import org.apache.geronimo.gshell.commandline.CommandLineBuilder;
-import org.apache.geronimo.gshell.commandline.CommandLineExecutionFailed;
 import org.apache.geronimo.gshell.commandline.CommandLineExecutor;
-import org.apache.geronimo.gshell.io.Closer;
 import org.apache.geronimo.gshell.io.IO;
 import org.apache.geronimo.gshell.io.SystemOutputHijacker;
 import org.apache.geronimo.gshell.notification.ErrorNotification;
-import org.apache.geronimo.gshell.notification.Notification;
 import org.apache.geronimo.gshell.registry.CommandResolver;
 import org.apache.geronimo.gshell.shell.ShellContext;
-import org.apache.geronimo.gshell.shell.Shell;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
-import java.io.InputStream;
-import java.io.OutputStream;
-import java.io.PipedInputStream;
-import java.io.PipedOutputStream;
-import java.io.PrintStream;
-import java.util.List;
-import java.util.concurrent.CopyOnWriteArrayList;
-import java.util.concurrent.atomic.AtomicReference;
-
 /**
  * The default {@link CommandLineExecutor} component.
  *
@@ -119,102 +106,6 @@
         return doExecute(context, path, args);
     }
 
-    public Object execute(final ShellContext context, final Object[][] commands) throws Exception
{
-        assert context != null;
-        assert commands != null;
-
-        log.info("Executing (Object[][]): {}", Arguments.asString(commands));
-
-        // Prepare IOs
-        final IO[] ios = new IO[commands.length];
-        PipedOutputStream pos = null;
-
-        IO io = context.getIo();
-
-        for (int i = 0; i < ios.length; i++) {
-            InputStream is = (i == 0) ? io.inputStream : new PipedInputStream(pos);
-            OutputStream os;
-
-            if (i == ios.length - 1) {
-                os = io.outputStream;
-            }
-            else {
-                os = pos = new PipedOutputStream();
-            }
-
-            ios[i] = new IO(is, new PrintStream(os), io.errorStream);
-        }
-
-        Thread[] threads = new Thread[commands.length];
-        final List<Throwable> errors = new CopyOnWriteArrayList<Throwable>();
-        final AtomicReference<Object> ref = new AtomicReference<Object>();
-
-        for (int i = 0; i < commands.length; i++) {
-            final int idx = i;
-
-            threads[i] = createThread(new Runnable() {
-                public void run() {
-                    try {
-                        ShellContext pipedContext = new ShellContext() {
-                            public Shell getShell() {
-                                return context.getShell();
-                            }
-
-                            public IO getIo() {
-                                return ios[idx];
-                            }
-
-                            public Variables getVariables() {
-                                return context.getVariables();
-                            }
-                        };
-
-                        Object obj = execute(pipedContext, String.valueOf(commands[idx][0]),
Arguments.shift(commands[idx]));
-
-                        if (idx == commands.length - 1) {
-                            ref.set(obj);
-                        }
-                    }
-                    catch (Throwable t) {
-                        errors.add(t);
-                    }
-                    finally {
-                        if (idx > 0) {
-                            Closer.close(ios[idx].inputStream);
-                        }
-                        if (idx < commands.length - 1) {
-                            Closer.close(ios[idx].outputStream);
-                        }
-                    }
-                }
-            });
-
-            threads[i].start();
-        }
-
-        for (int i = 0; i < commands.length; i++) {
-            threads[i].join();
-        }
-
-        if (!errors.isEmpty()) {
-            Throwable t = errors.get(0);
-
-            // Always preserve the type of notication throwables, reguardless of the trace
-            if (t instanceof Notification) {
-                throw (Notification)t;
-            }
-
-            // Otherwise wrap to preserve the trace
-            throw new CommandLineExecutionFailed(t);
-        }
-
-        return ref.get();
-    }
-
-    protected Thread createThread(final Runnable task) {
-        return new Thread(task);
-    }
-
     protected Object doExecute(final ShellContext context, final String path, final Object[]
args) throws Exception {
         assert context != null;
 



Mime
View raw message