use of dmg.util.CommandThrowableException in project dcache by dCache.
the class ShellApplication method execute.
/**
* Executes a single command with the output being printed to the console.
*/
public void execute(Args args) throws Throwable {
if (args.argc() == 0) {
return;
}
String out;
try {
if (isAnsiSupported && args.argc() > 0) {
if (args.argv(0).equals("help")) {
args.shift();
args = new Args("help -format=" + HelpFormat.ANSI + " " + args.toString());
}
}
try {
out = Objects.toString(commandInterpreter.command(args), null);
} catch (CommandThrowableException e) {
throw e.getCause();
}
} catch (CommandSyntaxException e) {
Ansi sb = Ansi.ansi();
sb.fg(RED).a("Syntax error: " + e.getMessage() + "\n").reset();
String help = e.getHelpText();
if (help != null) {
sb.a(help);
}
out = sb.toString();
} catch (CommandExitException e) {
throw e;
} catch (CommandPanicException e) {
Ansi sb = Ansi.ansi();
sb.fg(RED).a("Bug detected! ").reset().a("Please email the following details to <support@dcache.org>:\n");
Throwable t = e.getCause() == null ? e : e.getCause();
StringWriter sw = new StringWriter();
t.printStackTrace(new PrintWriter(sw));
out = sb.a(sw.toString()).toString();
} catch (Exception e) {
out = Ansi.ansi().fg(RED).a(e.getMessage()).reset().toString();
}
if (!isNullOrEmpty(out)) {
console.print(out);
if (out.charAt(out.length() - 1) != '\n') {
console.println();
}
}
console.flush();
}
use of dmg.util.CommandThrowableException in project dcache by dCache.
the class UniversalSpringCell method executeSetup.
private void executeSetup(CommandInterpreter interpreter, String source, byte[] data) throws CommandException {
try {
BufferedReader in = new BufferedReader(new InputStreamReader(new ByteArrayInputStream(data), UTF_8));
int lineCount = 1;
for (String line = in.readLine(); line != null; line = in.readLine(), lineCount++) {
line = line.trim();
if (line.isEmpty() || line.charAt(0) == '#') {
continue;
}
try {
Serializable result = interpreter.command(new Args(line));
if (result instanceof DelayedReply) {
((DelayedReply) result).take();
}
} catch (InterruptedException e) {
throw new CommandExitException("Error at " + source + ":" + lineCount + ": command interrupted");
} catch (CommandPanicException e) {
throw new CommandPanicException("Error at " + source + ":" + lineCount + ": " + e.getMessage(), e);
} catch (CommandException e) {
throw new CommandThrowableException("Error at " + source + ":" + lineCount + ": " + e.getMessage(), e);
}
}
} catch (IOException e) {
// Should not be possible
throw new RuntimeException(e);
}
}
use of dmg.util.CommandThrowableException in project dcache by dCache.
the class UserAdminShell method sendObject.
private Serializable sendObject(CellPath cellPath, Serializable object) throws NoRouteToCellException, InterruptedException, CommandException, AclException {
CellAddressCore addr = cellPath.getCurrent();
checkCdPermission(addr.isLocalAddress() ? addr.getCellName() : addr.toString());
try {
return _cellStub.send(cellPath, object, Serializable.class, _timeout).get();
} catch (ExecutionException e) {
Throwable cause = e.getCause();
if (_fullException) {
return getStackTrace(cause);
}
Throwables.throwIfInstanceOf(cause, Error.class);
Throwables.throwIfInstanceOf(cause, NoRouteToCellException.class);
Throwables.throwIfInstanceOf(cause, CommandException.class);
throw new CommandThrowableException(cause.toString(), cause);
}
}
use of dmg.util.CommandThrowableException in project dcache by dCache.
the class BillingCell method appendHeaders.
protected void appendHeaders(Path path) throws CommandThrowableException {
try {
String headers = getFormatHeaders();
Files.write(path, headers.getBytes(StandardCharsets.UTF_8), StandardOpenOption.APPEND, StandardOpenOption.WRITE);
} catch (NoSuchFileException ignored) {
} catch (IOException e) {
throw new CommandThrowableException("Failed to write to billing file " + path + ": " + e, e);
}
}
use of dmg.util.CommandThrowableException in project dcache by dCache.
the class CellShell method objectCommand.
public Object objectCommand(String strin) throws CommandExitException {
String str;
if ((str = prepareCommand(strin)) == null) {
return "";
}
try {
Args args = new Args(strin);
if (args.argc() == 0) {
return "";
}
Object o;
if (_externalInterpreter != null) {
o = _externalInterpreter.command(args);
} else {
o = command(args);
}
_errorCode = 0;
_errorMsg = null;
if (o == null) {
return "";
}
return o;
} catch (CommandException ce) {
_errorCode = ce.getErrorCode();
_errorMsg = ce.getErrorMessage();
switch(_doOnError) {
case SHUTDOWN:
throw new CommandExitException(ce.toString(), 666);
case EXIT:
throw new CommandExitException(ce.getErrorMessage(), ce.getErrorCode());
}
if (ce instanceof CommandSyntaxException) {
CommandSyntaxException cse = (CommandSyntaxException) ce;
StringBuilder sb = new StringBuilder();
sb.append("Syntax Error : ").append(cse.getMessage());
if (_helpMode == 1) {
sb.append("\nUse 'help' for more information\n");
} else if (_helpMode == 2) {
String help = cse.getHelpText();
if (help != null) {
sb.append('\n').append(help).append('\n');
}
}
return sb.toString();
} else if (ce instanceof CommandExitException) {
if (_externalInterpreter != null) {
_externalInterpreter = null;
return "external shell exited ... ";
} else {
throw (CommandExitException) ce;
}
} else if (ce instanceof CommandThrowableException) {
CommandThrowableException cte = (CommandThrowableException) ce;
StringBuilder sb = new StringBuilder();
sb.append(cte.getMessage()).append(" -> ");
Throwable t = cte.getTargetException();
sb.append(t.getClass().getName()).append(" : ").append(t.getMessage()).append('\n');
return sb.toString();
} else if (ce instanceof CommandPanicException) {
CommandPanicException cpe = (CommandPanicException) ce;
StringBuilder sb = new StringBuilder();
sb.append("Panic : ").append(cpe.getMessage()).append('\n');
Throwable t = cpe.getTargetException();
sb.append(t.getClass().getName()).append(" : ").append(t.getMessage()).append('\n');
return sb.toString();
} else {
return "CommandException :" + ce.getMessage();
}
}
}
Aggregations