You are viewing a plain text version of this content. The canonical link for it is here.
Posted to scm@geronimo.apache.org by jd...@apache.org on 2008/03/27 11:00:19 UTC
svn commit: r641760 -
/geronimo/gshell/trunk/gshell-core/src/main/java/org/apache/geronimo/gshell/DefaultCommandExecutor.java
Author: jdillon
Date: Thu Mar 27 03:00:18 2008
New Revision: 641760
URL: http://svn.apache.org/viewvc?rev=641760&view=rev
Log:
Tidy up and note about throwing exceptions, also fix order thrown
Modified:
geronimo/gshell/trunk/gshell-core/src/main/java/org/apache/geronimo/gshell/DefaultCommandExecutor.java
Modified: geronimo/gshell/trunk/gshell-core/src/main/java/org/apache/geronimo/gshell/DefaultCommandExecutor.java
URL: http://svn.apache.org/viewvc/geronimo/gshell/trunk/gshell-core/src/main/java/org/apache/geronimo/gshell/DefaultCommandExecutor.java?rev=641760&r1=641759&r2=641760&view=diff
==============================================================================
--- geronimo/gshell/trunk/gshell-core/src/main/java/org/apache/geronimo/gshell/DefaultCommandExecutor.java (original)
+++ geronimo/gshell/trunk/gshell-core/src/main/java/org/apache/geronimo/gshell/DefaultCommandExecutor.java Thu Mar 27 03:00:18 2008
@@ -130,34 +130,46 @@
}
public Object execute(final Object[][] commands) throws Exception {
+ assert commands != null;
+
// Prepare IOs
final IO[] ios = new IO[commands.length];
PipedOutputStream pos = null;
+
for (int i = 0; i < ios.length; i++) {
InputStream is = (i == 0) ? env.getIO().inputStream : new PipedInputStream(pos);
OutputStream os;
+
if (i == ios.length - 1) {
os = env.getIO().outputStream;
- } else {
+ }
+ else {
os = pos = new PipedOutputStream();
}
+
ios[i] = new IO(is, os, env.getIO().errorStream);
}
+
Thread[] threads = new Thread[commands.length];
final List<Throwable> errors = new CopyOnWriteArrayList<Throwable>();
- final AtomicReference ref = new AtomicReference();
+ 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 {
Object o = execute(String.valueOf(commands[idx][0]), Arguments.shift(commands[idx]), ios[idx]);
+
if (idx == commands.length - 1) {
ref.set(o);
}
- } catch (Throwable t) {
+ }
+ catch (Throwable t) {
errors.add(t);
- } finally {
+ }
+ finally {
if (idx > 0) {
IOUtil.close(ios[idx].inputStream);
}
@@ -167,23 +179,35 @@
}
}
});
+
threads[i].start();
}
+
for (int i = 0; i < commands.length; i++) {
threads[i].join();
}
+
if (!errors.isEmpty()) {
Throwable t = errors.get(0);
+
+ //
+ // FIXME: Should not throw here, as that will cause the originating stack trace to be lost
+ //
+
+ if (t instanceof RuntimeException) {
+ throw (RuntimeException) t;
+ }
if (t instanceof Exception) {
throw (Exception) t;
- } else if (t instanceof RuntimeException) {
- throw (RuntimeException) t;
- } else if (t instanceof Error) {
+ }
+ else if (t instanceof Error) {
throw (Error) t;
- } else {
+ }
+ else {
throw new RuntimeException(t);
}
}
+
return ref.get();
}
@@ -297,5 +321,4 @@
throw new NotFoundException("Unable to get command id for: " + node);
}
-
}