Search in sources :

Example 1 with RequestWithAttachments

use of org.jboss.as.cli.RequestWithAttachments in project wildfly-core by wildfly.

the class OperationRequestHandler method handle.

/* (non-Javadoc)
     * @see org.jboss.as.cli.CommandHandler#handle(org.jboss.as.cli.CommandContext)
     */
@Override
public void handle(CommandContext ctx) throws CommandLineException {
    ModelControllerClient client = ctx.getModelControllerClient();
    if (client == null) {
        throw new CommandFormatException("You are disconnected at the moment." + " Type 'connect' to connect to the server" + " or 'help' for the list of supported commands.");
    }
    RequestWithAttachments reqWithAttachments = (RequestWithAttachments) ctx.get(Scope.REQUEST, "OP_REQ");
    if (reqWithAttachments == null) {
        throw new CommandLineException("Parsed request isn't available.");
    }
    ModelNode request = reqWithAttachments.getRequest();
    OperationBuilder opBuilder = new OperationBuilder(request, true);
    for (String path : reqWithAttachments.getAttachedFiles()) {
        opBuilder.addFileAsAttachment(new File(path));
    }
    Operation op = opBuilder.build();
    if (ctx.getConfig().isValidateOperationRequests()) {
        ModelNode opDescOutcome = Util.validateRequest(ctx, request);
        if (opDescOutcome != null) {
            // operation has params that might need to be replaced
            Util.replaceFilePathsWithBytes(request, opDescOutcome);
        }
    }
    try {
        final ModelNode result = ctx.execute(op, "Operation request");
        if (Util.isSuccess(result)) {
            ctx.printDMR(result);
        } else {
            if (!ctx.getConfig().isOutputJSON()) {
                throw new CommandLineException(result.toString());
            } else {
                throw new CommandLineException(result.toJSONString(false));
            }
        }
    } catch (NoSuchElementException e) {
        throw new CommandLineException("ModelNode request is incomplete", e);
    } catch (CancellationException e) {
        throw new CommandLineException("The result couldn't be retrieved (perhaps the task was cancelled", e);
    } catch (IOException e) {
        if (e.getCause() != null && !(e.getCause() instanceof InterruptedException)) {
            ctx.disconnectController();
        }
        throw new CommandLineException("Communication error", e);
    } catch (RuntimeException e) {
        throw new CommandLineException("Failed to execute operation.", e);
    }
}
Also used : OperationBuilder(org.jboss.as.controller.client.OperationBuilder) Operation(org.jboss.as.controller.client.Operation) IOException(java.io.IOException) CommandLineException(org.jboss.as.cli.CommandLineException) ModelControllerClient(org.jboss.as.controller.client.ModelControllerClient) CancellationException(java.util.concurrent.CancellationException) CommandFormatException(org.jboss.as.cli.CommandFormatException) RequestWithAttachments(org.jboss.as.cli.RequestWithAttachments) ModelNode(org.jboss.dmr.ModelNode) File(java.io.File) NoSuchElementException(java.util.NoSuchElementException)

Example 2 with RequestWithAttachments

use of org.jboss.as.cli.RequestWithAttachments in project wildfly-core by wildfly.

the class CommandContextImpl method handleOperation.

private void handleOperation(ParsedCommandLine parsedLine) throws CommandFormatException, CommandLineException {
    if (isBatchMode()) {
        String line = parsedLine.getOriginalLine();
        Batch batch = getBatchManager().getActiveBatch();
        final ModelNode request = Util.toOperationRequest(this, parsedLine, batch.getAttachments());
        StringBuilder op = new StringBuilder();
        op.append(getNodePathFormatter().format(parsedLine.getAddress()));
        op.append(line.substring(line.indexOf(':')));
        DefaultBatchedCommand batchedCmd = new DefaultBatchedCommand(this, op.toString(), request, null);
        batch.add(batchedCmd);
    } else {
        Attachments attachments = new Attachments();
        final ModelNode op = Util.toOperationRequest(CommandContextImpl.this, parsedLine, attachments);
        RequestWithAttachments req = new RequestWithAttachments(op, attachments);
        set(Scope.REQUEST, "OP_REQ", req);
        operationHandler.handle(this);
    }
}
Also used : Batch(org.jboss.as.cli.batch.Batch) RequestWithAttachments(org.jboss.as.cli.RequestWithAttachments) DefaultBatchedCommand(org.jboss.as.cli.batch.impl.DefaultBatchedCommand) ModelNode(org.jboss.dmr.ModelNode) RequestWithAttachments(org.jboss.as.cli.RequestWithAttachments) Attachments(org.jboss.as.cli.Attachments)

Aggregations

RequestWithAttachments (org.jboss.as.cli.RequestWithAttachments)2 ModelNode (org.jboss.dmr.ModelNode)2 File (java.io.File)1 IOException (java.io.IOException)1 NoSuchElementException (java.util.NoSuchElementException)1 CancellationException (java.util.concurrent.CancellationException)1 Attachments (org.jboss.as.cli.Attachments)1 CommandFormatException (org.jboss.as.cli.CommandFormatException)1 CommandLineException (org.jboss.as.cli.CommandLineException)1 Batch (org.jboss.as.cli.batch.Batch)1 DefaultBatchedCommand (org.jboss.as.cli.batch.impl.DefaultBatchedCommand)1 ModelControllerClient (org.jboss.as.controller.client.ModelControllerClient)1 Operation (org.jboss.as.controller.client.Operation)1 OperationBuilder (org.jboss.as.controller.client.OperationBuilder)1