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/06/01 10:34:35 UTC
svn commit: r662168 -
/geronimo/gshell/trunk/gshell-core/src/main/java/org/apache/geronimo/gshell/CommandExecutionFailied.java
Author: jdillon
Date: Sun Jun 1 01:34:35 2008
New Revision: 662168
URL: http://svn.apache.org/viewvc?rev=662168&view=rev
Log:
Add new exception type when commands fail
Added:
geronimo/gshell/trunk/gshell-core/src/main/java/org/apache/geronimo/gshell/CommandExecutionFailied.java
- copied, changed from r662160, geronimo/gshell/trunk/gshell-core/src/main/java/org/apache/geronimo/gshell/DefaultCommandExecutor.java
Copied: geronimo/gshell/trunk/gshell-core/src/main/java/org/apache/geronimo/gshell/CommandExecutionFailied.java (from r662160, 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/CommandExecutionFailied.java?p2=geronimo/gshell/trunk/gshell-core/src/main/java/org/apache/geronimo/gshell/CommandExecutionFailied.java&p1=geronimo/gshell/trunk/gshell-core/src/main/java/org/apache/geronimo/gshell/DefaultCommandExecutor.java&r1=662160&r2=662168&rev=662168&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/CommandExecutionFailied.java Sun Jun 1 01:34:35 2008
@@ -19,322 +19,15 @@
package org.apache.geronimo.gshell;
-import org.apache.geronimo.gshell.command.Command;
-import org.apache.geronimo.gshell.command.CommandContext;
-import org.apache.geronimo.gshell.command.CommandExecutor;
-import org.apache.geronimo.gshell.command.CommandInfo;
-import org.apache.geronimo.gshell.command.Variables;
-import org.apache.geronimo.gshell.common.Arguments;
-import org.apache.geronimo.gshell.common.StopWatch;
-import org.apache.geronimo.gshell.io.IO;
-import org.apache.geronimo.gshell.layout.LayoutManager;
-import org.apache.geronimo.gshell.layout.NotFoundException;
-import org.apache.geronimo.gshell.model.layout.AliasNode;
-import org.apache.geronimo.gshell.model.layout.CommandNode;
-import org.apache.geronimo.gshell.model.layout.Node;
-import org.apache.geronimo.gshell.registry.CommandRegistry;
-import org.apache.geronimo.gshell.registry.NotRegisteredException;
-import org.apache.geronimo.gshell.shell.Environment;
-import org.apache.geronimo.gshell.application.ApplicationManager;
-import org.apache.geronimo.gshell.application.DefaultVariables;
-import org.codehaus.plexus.component.annotations.Component;
-import org.codehaus.plexus.component.annotations.Requirement;
-import org.codehaus.plexus.util.IOUtil;
-import org.codehaus.plexus.personality.plexus.lifecycle.phase.Initializable;
-import org.codehaus.plexus.personality.plexus.lifecycle.phase.InitializationException;
-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.util.List;
-import java.util.concurrent.CopyOnWriteArrayList;
-import java.util.concurrent.atomic.AtomicReference;
-
/**
- * The default command executor.
+ * Thrown when a command execution fails.
*
* @version $Rev$ $Date$
*/
-@Component(role=CommandExecutor.class, hint="default")
-public class DefaultCommandExecutor
- implements CommandExecutor, Initializable
+public class CommandExecutionFailied
+ extends Exception
{
- private final Logger log = LoggerFactory.getLogger(getClass());
-
- @Requirement
- private ApplicationManager applicationManager;
-
- @Requirement
- private LayoutManager layoutManager;
-
- @Requirement
- private CommandRegistry commandRegistry;
-
- @Requirement
- private CommandLineBuilder commandLineBuilder;
-
- private Environment env;
-
- public DefaultCommandExecutor() {}
-
- public DefaultCommandExecutor(final ApplicationManager applicationManager, final LayoutManager layoutManager, final CommandRegistry commandRegistry, final CommandLineBuilder commandLineBuilder) {
- assert applicationManager != null;
- assert layoutManager != null;
- assert commandRegistry != null;
- assert commandLineBuilder != null;
-
- this.applicationManager = applicationManager;
- this.layoutManager = layoutManager;
- this.commandRegistry = commandRegistry;
- this.commandLineBuilder = commandLineBuilder;
- }
-
- public void initialize() throws InitializationException {
- assert applicationManager != null;
-
- this.env = applicationManager.getContext().getEnvironment();
- }
-
- public Object execute(final String line) throws Exception {
- assert line != null;
-
- log.info("Executing (String): {}", line);
-
- try {
- CommandLine cl = commandLineBuilder.create(line);
-
- return cl.execute();
- }
- catch (ErrorNotification n) {
- // Decode the error notifiation
- Throwable cause = n.getCause();
-
- if (cause instanceof Exception) {
- throw (Exception)cause;
- }
- else if (cause instanceof Error) {
- throw (Error)cause;
- }
- else {
- // Um, if we get this far, which we probably never will, then just re-toss the notifcation
- throw n;
- }
- }
- }
-
- public Object execute(final Object... args) throws Exception {
- assert args != null;
- assert args.length > 1;
-
- log.info("Executing (Object...): [{}]", Arguments.asString(args));
-
- return execute(String.valueOf(args[0]), Arguments.shift(args), env.getIO());
- }
-
- public Object execute(final String path, final Object[] args) throws Exception {
- assert path != null;
- assert args != null;
-
- log.info("Executing ({}): [{}]", path, Arguments.asString(args));
-
- return execute(path, args, env.getIO());
- }
-
- 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 {
- 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<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) {
- errors.add(t);
- }
- finally {
- if (idx > 0) {
- IOUtil.close(ios[idx].inputStream);
- }
- if (idx < commands.length - 1) {
- IOUtil.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);
-
- //
- // FIXME: Should not throw here, as that will cause the originating stack trace to be lost
- //
-
- if (t instanceof Exception) {
- throw (Exception) t;
- }
- else if (t instanceof Error) {
- throw (Error) t;
- }
- else {
- throw new RuntimeException(t);
- }
- }
-
- return ref.get();
- }
-
- protected Thread createThread(Runnable run) {
- return new Thread(run);
- }
-
- protected Object execute(final String path, final Object[] args, final IO io) throws Exception {
- log.debug("Executing");
-
- final String searchPath = (String) env.getVariables().get(LayoutManager.COMMAND_PATH);
- log.debug("Search path: {}", searchPath);
-
- final Node node = layoutManager.findNode(path, searchPath);
- log.debug("Layout node: {}", node);
-
- final String id = findCommandId(node);
- log.debug("Command ID: {}", id);
-
- final Command command;
- try {
- command = commandRegistry.lookup(id);
- }
- catch (NotRegisteredException e) {
- throw new NotFoundException(e.getMessage());
- }
-
- // Setup the command context and pass it to the command instance
- CommandContext context = new CommandContext() {
- // Command instances get their own namespace with defaults from the current
- final Variables vars = new DefaultVariables(env.getVariables());
-
- CommandInfo info;
-
- public IO getIO() {
- return io;
- }
-
- public Variables getVariables() {
- return vars;
- }
-
- public CommandInfo getInfo() {
- if (info == null) {
- info = new CommandInfo()
- {
- public String getId() {
- return id;
- }
-
- public String getName() {
- if (node instanceof AliasNode) {
- return ((AliasNode)node).getCommand();
- }
-
- return node.getName();
- }
-
- public String getAlias() {
- if (node instanceof AliasNode) {
- return node.getName();
- }
-
- return null;
- }
-
- public String getPath() {
- //
- // TODO:
- //
-
- return null;
- }
- };
- }
-
- return info;
- }
- };
-
- // Setup command timings
- StopWatch watch = new StopWatch(true);
-
- Object result;
- try {
- result = command.execute(context, args);
-
- log.debug("Command completed with result: {}, after: {}", result, watch);
- }
- finally {
- // Make sure that the commands output has been flushed
- try {
- env.getIO().flush();
- }
- catch (Exception ignore) {}
- }
-
- return result;
- }
-
- protected String findCommandId(final Node node) throws NotFoundException {
- assert node != null;
-
- if (node instanceof AliasNode) {
- AliasNode aliasNode = (AliasNode) node;
- String targetPath = aliasNode.getCommand();
- Node target = layoutManager.findNode(layoutManager.getLayout(), targetPath);
-
- return findCommandId(target);
- }
- else if (node instanceof CommandNode) {
- CommandNode commandNode = (CommandNode) node;
-
- return commandNode.getId();
- }
-
- throw new NotFoundException("Unable to get command id for: " + node);
+ public CommandExecutionFailied(final Throwable cause) {
+ super(cause);
}
}
\ No newline at end of file