Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 25 additions & 16 deletions src/main/java/AeshExample.java
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;
import org.jboss.aesh.console.command.CommandException;

/**
* @author <a href="mailto:stale.pedersen@jboss.org">Ståle W. Pedersen</a>
Expand Down Expand Up @@ -170,7 +171,7 @@ public void handleInterrupt(Console console) {
public static class ExitCommand implements Command {

@Override
public CommandResult execute(CommandInvocation commandInvocation) throws IOException, InterruptedException {
public CommandResult execute(CommandInvocation commandInvocation) throws CommandException, InterruptedException {
commandInvocation.stop();
return CommandResult.SUCCESS;
}
Expand All @@ -183,15 +184,19 @@ public static class RunCommand implements Command {
private List<Resource> arguments;

@Override
public CommandResult execute(CommandInvocation commandInvocation) throws IOException, InterruptedException {
public CommandResult execute(CommandInvocation commandInvocation) throws CommandException, InterruptedException {

commandInvocation.putProcessInBackground();

if(arguments != null && arguments.size() > 0 && arguments.get(0).isLeaf()) {
List<String> script = readScriptFile(arguments.get(0));
if (arguments != null && arguments.size() > 0 && arguments.get(0).isLeaf()) {
try {
List<String> script = readScriptFile(arguments.get(0));

for (String line : script) {
commandInvocation.executeCommand(line + Config.getLineSeparator());
for (String line : script) {
commandInvocation.executeCommand(line + Config.getLineSeparator());
}
} catch (IOException ex) {
throw new CommandException(ex);
}
}

Expand Down Expand Up @@ -221,7 +226,7 @@ public static class FooCommand implements Command {
private String foo;

@Override
public CommandResult execute(CommandInvocation commandInvocation) throws IOException, InterruptedException {
public CommandResult execute(CommandInvocation commandInvocation) throws CommandException, InterruptedException {
if(bar == null)
commandInvocation.getShell().out().println("NO BAR!");
else {
Expand Down Expand Up @@ -251,14 +256,18 @@ public static class TestConsoleCommand implements Command {
private Shell shell;

@Override
public CommandResult execute(CommandInvocation commandInvocation) throws IOException, InterruptedException {
public CommandResult execute(CommandInvocation commandInvocation) throws CommandException, InterruptedException {
this.shell = commandInvocation.getShell();
if(help) {
shell.out().println(commandInvocation.getHelpInfo("test"));
}
else {
//display();
processOperation(commandInvocation);
try {
//display();
processOperation(commandInvocation);
} catch (IOException ex) {
throw new CommandException(ex);
}
}

return CommandResult.SUCCESS;
Expand Down Expand Up @@ -335,7 +344,7 @@ public static class LsCommand implements Command {
private List<Resource> arguments;

@Override
public CommandResult execute(CommandInvocation commandInvocation) throws IOException, InterruptedException {
public CommandResult execute(CommandInvocation commandInvocation) throws CommandException, InterruptedException {
if(help) {
commandInvocation.getShell().out().println(commandInvocation.getHelpInfo("ls"));
}
Expand Down Expand Up @@ -385,7 +394,7 @@ public static class PromptCommand implements Command {
private Shell shell;

@Override
public CommandResult execute(CommandInvocation commandInvocation) throws IOException, InterruptedException {
public CommandResult execute(CommandInvocation commandInvocation) throws CommandException, InterruptedException {
this.shell = commandInvocation.getShell();
if(bar) {
shell.out().print("are you sure you want bar? (y/n) ");
Expand All @@ -401,7 +410,7 @@ public CommandResult execute(CommandInvocation commandInvocation) throws IOExcep
return CommandResult.SUCCESS;
}

public void processOperation(CommandOperation operation) throws IOException {
public void processOperation(CommandOperation operation) {
if(operation.getInputKey() == Key.y) {
shell.out().println(Config.getLineSeparator()+"you wanted bar!");
}
Expand Down Expand Up @@ -524,7 +533,7 @@ public static class GroupCommand implements Command {
private boolean help;

@Override
public CommandResult execute(CommandInvocation commandInvocation) throws IOException, InterruptedException {
public CommandResult execute(CommandInvocation commandInvocation) throws CommandException, InterruptedException {
if(help)
commandInvocation.getShell().out().println(commandInvocation.getHelpInfo("group"));
else
Expand All @@ -542,7 +551,7 @@ public static class Child1 implements Command {
private boolean help;

@Override
public CommandResult execute(CommandInvocation commandInvocation) throws IOException, InterruptedException {
public CommandResult execute(CommandInvocation commandInvocation) throws CommandException, InterruptedException {
if(help)
commandInvocation.getShell().out().println(commandInvocation.getHelpInfo("group child1"));
else
Expand All @@ -557,7 +566,7 @@ public static class Child2 implements Command {
private boolean bar;

@Override
public CommandResult execute(CommandInvocation commandInvocation) throws IOException, InterruptedException {
public CommandResult execute(CommandInvocation commandInvocation) throws CommandException, InterruptedException {
commandInvocation.getShell().out().println("bar is set to: "+bar);
return CommandResult.SUCCESS;
}
Expand Down
6 changes: 3 additions & 3 deletions src/main/java/AeshGraphicsExample.java
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@
import org.jboss.aesh.terminal.Key;
import org.jboss.aesh.terminal.TerminalColor;

import java.io.IOException;
import org.jboss.aesh.console.command.CommandException;

/**
* @author <a href="mailto:stale.pedersen@jboss.org">Ståle W. Pedersen</a>
Expand Down Expand Up @@ -65,7 +65,7 @@ public static void main(String[] args) {
public static class ExitCommand implements Command {

@Override
public CommandResult execute(CommandInvocation commandInvocation) throws IOException, InterruptedException {
public CommandResult execute(CommandInvocation commandInvocation) throws CommandException, InterruptedException {
commandInvocation.stop();
return CommandResult.SUCCESS;
}
Expand All @@ -78,7 +78,7 @@ public static class GraphicsCommand implements Command {
private Graphics g;

@Override
public CommandResult execute(CommandInvocation commandInvocation) throws IOException, InterruptedException {
public CommandResult execute(CommandInvocation commandInvocation) throws CommandException, InterruptedException {
invocation = commandInvocation;
invocation.getShell().enableAlternateBuffer();
doGfx();
Expand Down
5 changes: 5 additions & 0 deletions src/main/java/org/jboss/aesh/cl/result/NullResultHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
*/
package org.jboss.aesh.cl.result;

import org.jboss.aesh.console.command.CommandException;
import org.jboss.aesh.console.command.CommandResult;

/**
Expand All @@ -37,4 +38,8 @@ public void onFailure(CommandResult result) {
@Override
public void onValidationFailure(CommandResult result, Exception exception) {
}

@Override
public void onExecutionFailure(CommandResult result, CommandException exception) {
}
}
3 changes: 3 additions & 0 deletions src/main/java/org/jboss/aesh/cl/result/ResultHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
*/
package org.jboss.aesh.cl.result;

import org.jboss.aesh.console.command.CommandException;
import org.jboss.aesh.console.command.CommandResult;

/**
Expand All @@ -31,4 +32,6 @@ public interface ResultHandler {
void onFailure(CommandResult result);

void onValidationFailure(CommandResult result, Exception exception);

void onExecutionFailure(CommandResult result, CommandException exception);
}
10 changes: 8 additions & 2 deletions src/main/java/org/jboss/aesh/console/AeshConsoleImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
import org.jboss.aesh.cl.validator.OptionValidatorException;
import org.jboss.aesh.complete.CompleteOperation;
import org.jboss.aesh.complete.Completion;
import org.jboss.aesh.console.command.CommandException;
import org.jboss.aesh.console.command.CommandNotFoundException;
import org.jboss.aesh.console.command.CommandResult;
import org.jboss.aesh.console.command.activator.AeshOptionActivatorProvider;
Expand Down Expand Up @@ -352,8 +353,13 @@ else if(resultHandler != null)
result = CommandResult.FAILURE;
if(commandNotFoundHandler != null)
commandNotFoundHandler.handleCommandNotFound(output.getBuffer(), getShell());
}
catch (Exception e) {
} catch (CommandException cmd) {
getShell().out().println(cmd.getMessage());
result = CommandResult.FAILURE;
if (resultHandler != null) {
resultHandler.onExecutionFailure(result, cmd);
}
} catch (Exception e) {
if(e instanceof InterruptedException)
throw (InterruptedException) e;
else {
Expand Down
7 changes: 4 additions & 3 deletions src/main/java/org/jboss/aesh/console/command/Command.java
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,9 @@ public interface Command<T extends CommandInvocation> {
*
* @param commandInvocation invocation
* @return success or failure depending on how the execution went.
* @throws IOException
* @throws InterruptedException
* @throws CommandException In case an exception occurs during execution
* @throws InterruptedException In case the current thread is being
* interrupted.
*/
CommandResult execute(T commandInvocation) throws IOException, InterruptedException;
CommandResult execute(T commandInvocation) throws CommandException, InterruptedException;
}
34 changes: 34 additions & 0 deletions src/main/java/org/jboss/aesh/console/command/CommandException.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package org.jboss.aesh.console.command;

/**
*
* An exception thrown by Commands when exception occurs
*
* @author jdenise@redhat.com
*/
public final class CommandException extends Exception {

private static final long serialVersionUID = -1098155769714455108L;

public CommandException() {
super();
}

public CommandException(String message) {
super(message);
}

public CommandException(String message, Throwable cause) {
super(message, cause);
}

public CommandException(Throwable cause) {
super(cause);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
import org.jboss.aesh.parser.AeshLine;

import java.io.IOException;
import org.jboss.aesh.console.command.CommandException;

/**
* A CommandContainer hold reference to the Command and
Expand Down Expand Up @@ -65,5 +66,7 @@ public interface CommandContainer<T extends Command> extends AutoCloseable {

CommandContainerResult executeCommand(AeshLine line, InvocationProviders invocationProviders,
AeshContext aeshContext,
CommandInvocation commandInvocation) throws CommandLineParserException, OptionValidatorException, CommandValidatorException, IOException, InterruptedException;
CommandInvocation commandInvocation)
throws CommandLineParserException, OptionValidatorException,
CommandValidatorException, CommandException, InterruptedException;
}
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
import org.jboss.aesh.parser.AeshLine;

import java.io.IOException;
import org.jboss.aesh.console.command.CommandException;

/**
* @author <a href="mailto:stale.pedersen@jboss.org">Ståle W. Pedersen</a>
Expand All @@ -42,7 +43,7 @@ public abstract class DefaultCommandContainer<C extends Command> implements Comm
public CommandContainerResult executeCommand(AeshLine line, InvocationProviders invocationProviders,
AeshContext aeshContext,
CommandInvocation commandInvocation)
throws CommandLineParserException, OptionValidatorException, CommandValidatorException, IOException, InterruptedException {
throws CommandLineParserException, OptionValidatorException, CommandValidatorException, CommandException, InterruptedException {

CommandLine commandLine = getParser().parse(line, false);
commandLine.getParser().getCommandPopulator().populateObject(commandLine, invocationProviders, aeshContext, true);
Expand Down
11 changes: 8 additions & 3 deletions src/main/java/org/jboss/aesh/console/man/Man.java
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;
import org.jboss.aesh.console.command.CommandException;

/**
* A Man implementation for Aesh. ref: http://en.wikipedia.org/wiki/Man_page
Expand Down Expand Up @@ -86,7 +87,7 @@ else if(getSearchStatus() == TerminalPage.Search.NO_SEARCH ||
}

@Override
public CommandResult execute(CommandInvocation commandInvocation) throws IOException, InterruptedException {
public CommandResult execute(CommandInvocation commandInvocation) throws CommandException, InterruptedException {
if(manPages == null || manPages.size() == 0) {
commandInvocation.getShell().out().println("What manual page do you want?");
return CommandResult.SUCCESS;
Expand All @@ -100,8 +101,12 @@ public CommandResult execute(CommandInvocation commandInvocation) throws IOExcep
InputStream inputStream = manProvider.getManualDocument(manPages.get(0));
if(inputStream != null) {
setCommandInvocation(commandInvocation);
fileParser.setInput(inputStream);
afterAttach();
try {
fileParser.setInput(inputStream);
afterAttach();
} catch (IOException ex) {
throw new CommandException(ex);
}
}

return CommandResult.SUCCESS;
Expand Down
6 changes: 3 additions & 3 deletions src/test/java/org/jboss/aesh/cl/CommandLineParserTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,9 @@
import org.jboss.aesh.console.command.invocation.CommandInvocation;
import org.junit.Test;

import java.io.IOException;
import java.util.List;
import java.util.Map;
import org.jboss.aesh.console.command.CommandException;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
Expand Down Expand Up @@ -355,7 +355,7 @@ public class GroupCommandTest extends TestingCommand {

public class TestingCommand implements Command {
@Override
public CommandResult execute(CommandInvocation commandInvocation) throws IOException, InterruptedException {
public CommandResult execute(CommandInvocation commandInvocation) throws CommandException, InterruptedException {
return CommandResult.SUCCESS;
}
}
Expand All @@ -374,7 +374,7 @@ public class SubHelp extends HelpClass implements Command {
private String foo;

@Override
public CommandResult execute(CommandInvocation commandInvocation) throws IOException, InterruptedException {
public CommandResult execute(CommandInvocation commandInvocation) throws CommandException, InterruptedException {
return CommandResult.SUCCESS;
}
}
Expand Down
4 changes: 2 additions & 2 deletions src/test/java/org/jboss/aesh/cl/ParseCompleteObjectTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,8 @@
import org.jboss.aesh.console.command.invocation.CommandInvocation;
import org.junit.Test;

import java.io.IOException;
import java.util.List;
import org.jboss.aesh.console.command.CommandException;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
Expand Down Expand Up @@ -357,7 +357,7 @@ public class ParseCompleteGroupChild2 extends TestCommand {

public class TestCommand implements Command {
@Override
public CommandResult execute(CommandInvocation commandInvocation) throws IOException, InterruptedException {
public CommandResult execute(CommandInvocation commandInvocation) throws CommandException, InterruptedException {
return CommandResult.SUCCESS;
}
}
Expand Down
Loading