use of dmg.util.CommandExitException 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.CommandExitException in project dcache by dCache.
the class ShellApplication method console.
/**
* Start an interactive session. The user is supplied a prompt and their input is executed as a
* command. This repeats until they indicate that they wish to exit the session.
*/
public void console() throws Throwable {
onInteractiveStart();
try {
while (true) {
String prompt = Ansi.ansi().bold().a(getPrompt()).boldOff().toString();
String str = console.readLine(prompt);
if (str == null) {
console.println();
break;
}
execute(new Args(str));
}
} catch (CommandExitException ignored) {
}
}
use of dmg.util.CommandExitException 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.CommandExitException in project dcache by dCache.
the class DCapDoorInterpreterV3 method com_hello.
// ////////////////////////////////////////////////////////////////
//
// the command functions String com_<commandName>(int,int,Args)
//
public String com_hello(int sessionId, int commandId, VspArgs args) throws CommandException {
if (args.argc() < 2) {
throw new CommandExitException("Command Syntax Exception", 2);
}
try {
int major = Integer.parseInt(args.argv(2));
int minor = Integer.parseInt(args.argv(3));
Integer bugfix = args.argc() > 4 ? Integer.parseInt(args.argv(4)) : null;
String patch = args.argv(5);
_version = new Version(major, minor, bugfix, patch);
} catch (NumberFormatException e) {
_log.error("Syntax error in client version number {} : {}", args, e.toString());
throw new CommandException("Invalid client version number", e);
}
/*
replace current values if alternatives are provided
*/
_pid = args.getOption("pid", _pid);
_uid = args.getIntOption("uid", _uid);
_gid = args.getIntOption("gid", _gid);
_log.debug("Client Version : {}", _version);
if (_settings.getMinClientVersion().matches(_version) > 0 || _settings.getMaxClientVersion().matches(_version) < 0) {
_log.error("Client {} (proc \"{}\" running with uid:{} gid:{}) rejected: bad client version: {}", InetAddresses.toAddrString(_clientAddress), _pid, _uid, _gid, _version);
throw new CommandExitException("Client version rejected : " + _version, 1);
}
String yourName = args.getName();
if (yourName.equals("server")) {
_ourName = "client";
}
return "0 0 " + _ourName + " welcome " + _version.getMajor() + " " + _version.getMinor();
}
use of dmg.util.CommandExitException in project dcache by dCache.
the class DcapLineBasedInterpreterAdapter method execute.
@Override
public void execute(String cmd) throws CommandExitException {
LOGGER.info("Executing command: {}", cmd);
commandCounter++;
lastCommand = cmd;
VspArgs args;
try {
args = new VspArgs(cmd);
} catch (IllegalArgumentException e) {
println("protocol violation: " + e.getMessage());
LOGGER.debug("protocol violation [{}] from {}", e.getMessage(), clientAddress);
throw new CommandExitException("Protocol violation");
}
try {
String answer = execute(args);
if (answer != null) {
LOGGER.info("Our answer : {}", answer);
println(answer);
}
} catch (CommandExitException e) {
hasExited = true;
LOGGER.info("ComThread : protocol ended");
throw e;
}
}
Aggregations