final TryBlock tryBlock = TryBlock.remove(ctx);
final BatchManager batchManager = ctx.getBatchManager();
if(!batchManager.isBatchActive()) {
if(tryBlock.isInCatch()) {
throw new CommandLineException("catch block did not activate batch mode.");
} else {
throw new CommandLineException("finally block did not activate batch mode.");
}
}
final Batch batch = batchManager.getActiveBatch();
batchManager.discardActiveBatch();
final ModelControllerClient client = ctx.getModelControllerClient();
if(client == null) {
throw new CommandLineException("The connection to the controller has not been established.");
}
final ModelNode tryRequest = tryBlock.getTryRequest();
if(tryRequest == null) {
throw new CommandLineException("The try request is not available.");
}
ModelNode response;
try {
response = client.execute(tryRequest);
} catch (IOException e) {
throw new CommandLineException("try request failed", e);
}
CommandLineException catchError = null;
if(!Util.isSuccess(response)) {
ctx.printLine("try block failed: " + Util.getFailureDescription(response));
ModelNode catchRequest = tryBlock.getCatchRequest();
if(catchRequest == null && tryBlock.isInCatch() && batch.size() > 0) {
catchRequest = batch.toRequest();
}
if(catchRequest != null) {
try {
response = client.execute(catchRequest);
} catch (IOException e) {
throw new CommandLineException("catch request failed", e);
}
if(!Util.isSuccess(response)) {
catchError = new CommandLineException("catch request failed: " + Util.getFailureDescription(response));
}
}
}
if(tryBlock.isInFinally() && batch.size() > 0) {
final ModelNode finallyRequest = batch.toRequest();
try {
response = client.execute(finallyRequest);
} catch (IOException e) {
throw new CommandLineException("finally request failed", e);
}
if(!Util.isSuccess(response)) {
throw new CommandLineException("finally request failed: " + Util.getFailureDescription(response));
}
}
if(catchError != null) {
throw catchError;