Search in sources :

Example 1 with SimpleTable

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

the class PatchInspect method displayPatchXml.

private void displayPatchXml(CommandContext ctx, Patch patch) throws CommandException {
    final Identity identity = patch.getIdentity();
    SimpleTable table = new SimpleTable(2, ctx.getTerminalWidth());
    table.addLine(new String[] { "Patch ID:", patch.getPatchId() });
    table.addLine(new String[] { "Type:", identity.getPatchType().getName() });
    table.addLine(new String[] { "Identity name:", identity.getName() });
    table.addLine(new String[] { "Identity version:", identity.getVersion() });
    table.addLine(new String[] { "Description:", patch.getDescription() == null ? "n/a" : patch.getDescription() });
    if (patch.getLink() != null) {
        table.addLine(new String[] { "Link:", patch.getLink() });
    }
    ctx.printLine(table.toString(false));
    if (verbose) {
        ctx.printLine("");
        ctx.printLine("ELEMENTS");
        for (PatchElement e : patch.getElements()) {
            table = new SimpleTable(2, ctx.getTerminalWidth());
            table.addLine(new String[] { "Patch ID:", e.getId() });
            table.addLine(new String[] { "Name:", e.getProvider().getName() });
            table.addLine(new String[] { "Type:", e.getProvider().isAddOn() ? Constants.ADD_ON : Constants.LAYER });
            table.addLine(new String[] { "Description:", e.getDescription() });
            ctx.printLine("");
            ctx.printLine(table.toString(false));
        }
    }
}
Also used : SimpleTable(org.jboss.as.cli.util.SimpleTable) PatchElement(org.jboss.as.patching.metadata.PatchElement) Identity(org.jboss.as.patching.metadata.Identity)

Example 2 with SimpleTable

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

the class PatchHandler method doHandle.

@Override
protected void doHandle(CommandContext ctx) throws CommandLineException {
    final ParsedCommandLine parsedLine = ctx.getParsedCommandLine();
    if (host.isPresent(parsedLine) && !ctx.isDomainMode()) {
        throw new CommandFormatException("The --host option is not available in the current context. Connection to the controller might be unavailable or not running in domain mode.");
    }
    final String action = this.action.getValue(parsedLine);
    if (INSPECT.equals(action)) {
        doInspect(ctx);
        return;
    }
    final PatchOperationTarget target = createPatchOperationTarget(ctx);
    final PatchOperationBuilder builder = createPatchOperationBuilder(parsedLine);
    final ModelNode response;
    try {
        response = builder.execute(target);
    } catch (Exception e) {
        throw new CommandLineException(action + " failed", e);
    }
    if (!Util.isSuccess(response)) {
        final ModelNode fd = response.get(ModelDescriptionConstants.FAILURE_DESCRIPTION);
        if (!fd.isDefined()) {
            throw new CommandLineException("Failed to apply patch: " + response.asString());
        }
        if (fd.has(Constants.CONFLICTS)) {
            final StringBuilder buf = new StringBuilder();
            buf.append(fd.get(Constants.MESSAGE).asString()).append(": ");
            final ModelNode conflicts = fd.get(Constants.CONFLICTS);
            String title = "";
            if (conflicts.has(Constants.BUNDLES)) {
                formatConflictsList(buf, conflicts, "", Constants.BUNDLES);
                title = ", ";
            }
            if (conflicts.has(Constants.MODULES)) {
                formatConflictsList(buf, conflicts, title, Constants.MODULES);
                title = ", ";
            }
            if (conflicts.has(Constants.MISC)) {
                formatConflictsList(buf, conflicts, title, Constants.MISC);
            }
            buf.append(lineSeparator).append("Use the --override-all, --override=[] or --preserve=[] arguments in order to resolve the conflict.");
            throw new CommandLineException(buf.toString());
        } else {
            throw new CommandLineException(Util.getFailureDescription(response));
        }
    }
    if (INFO.equals(action)) {
        if (patchId.getValue(parsedLine) != null) {
            final ModelNode result = response.get(ModelDescriptionConstants.RESULT);
            if (!result.isDefined()) {
                return;
            }
            SimpleTable table = new SimpleTable(2, ctx.getTerminalWidth());
            table.addLine(new String[] { "Patch ID:", result.get(Constants.PATCH_ID).asString() });
            table.addLine(new String[] { "Type:", result.get(Constants.TYPE).asString() });
            table.addLine(new String[] { "Identity name:", result.get(Constants.IDENTITY_NAME).asString() });
            table.addLine(new String[] { "Identity version:", result.get(Constants.IDENTITY_VERSION).asString() });
            table.addLine(new String[] { "Description:", result.get(Constants.DESCRIPTION).asString() });
            if (result.hasDefined(Constants.LINK)) {
                table.addLine(new String[] { "Link:", result.get(Constants.LINK).asString() });
            }
            ctx.printLine(table.toString(false));
            final ModelNode elements = result.get(Constants.ELEMENTS);
            if (elements.isDefined()) {
                ctx.printLine("");
                ctx.printLine("ELEMENTS");
                for (ModelNode e : elements.asList()) {
                    table = new SimpleTable(2, ctx.getTerminalWidth());
                    table.addLine(new String[] { "Patch ID:", e.get(Constants.PATCH_ID).asString() });
                    table.addLine(new String[] { "Name:", e.get(Constants.NAME).asString() });
                    table.addLine(new String[] { "Type:", e.get(Constants.TYPE).asString() });
                    table.addLine(new String[] { "Description:", e.get(Constants.DESCRIPTION).asString() });
                    ctx.printLine("");
                    ctx.printLine(table.toString(false));
                }
            }
        } else if (jsonOutput.isPresent(parsedLine)) {
            ctx.printLine(response.toJSONString(false));
        } else if (streams.isPresent(parsedLine)) {
            final List<ModelNode> list = response.get(ModelDescriptionConstants.RESULT).asList();
            if (list.size() == 1) {
                ctx.printLine(list.get(0).asString());
            } else {
                final List<String> streams = new ArrayList<String>(list.size());
                for (ModelNode stream : list) {
                    streams.add(stream.asString());
                }
                ctx.printColumns(streams);
            }
        } else {
            final ModelNode result = response.get(ModelDescriptionConstants.RESULT);
            if (!result.isDefined()) {
                return;
            }
            SimpleTable table = new SimpleTable(2, ctx.getTerminalWidth());
            table.addLine(new String[] { "Version:", result.get(Constants.VERSION).asString() });
            addPatchesInfo(result, table);
            ctx.printLine(table.toString(false));
            if (verbose.isPresent(parsedLine)) {
                printLayerPatches(ctx, result, Constants.ADD_ON);
                printLayerPatches(ctx, result, Constants.LAYER);
            }
        }
    } else {
        ctx.printLine(response.toJSONString(false));
    }
}
Also used : SimpleTable(org.jboss.as.cli.util.SimpleTable) ArrayList(java.util.ArrayList) PatchOperationBuilder(org.jboss.as.patching.tool.PatchOperationBuilder) XMLStreamException(javax.xml.stream.XMLStreamException) ZipException(java.util.zip.ZipException) CommandFormatException(org.jboss.as.cli.CommandFormatException) PatchingException(org.jboss.as.patching.PatchingException) IOException(java.io.IOException) CommandLineException(org.jboss.as.cli.CommandLineException) CommandLineException(org.jboss.as.cli.CommandLineException) CommandFormatException(org.jboss.as.cli.CommandFormatException) ParsedCommandLine(org.jboss.as.cli.operation.ParsedCommandLine) ModelNode(org.jboss.dmr.ModelNode) PatchOperationTarget(org.jboss.as.patching.tool.PatchOperationTarget)

Example 3 with SimpleTable

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

the class PatchHandler method printLayerPatches.

protected void printLayerPatches(CommandContext ctx, final ModelNode result, final String type) {
    ModelNode layer = result.get(type);
    if (layer.isDefined()) {
        final String header = Character.toUpperCase(type.charAt(0)) + type.substring(1) + ':';
        for (String name : layer.keys()) {
            final ModelNode node = layer.get(name);
            final SimpleTable table = new SimpleTable(2, ctx.getTerminalWidth());
            table.addLine(new String[] { header, name });
            addPatchesInfo(node, table);
            ctx.printLine(lineSeparator + table.toString(false));
        }
    }
}
Also used : SimpleTable(org.jboss.as.cli.util.SimpleTable) ModelNode(org.jboss.dmr.ModelNode)

Example 4 with SimpleTable

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

the class PatchInfo method printLayerPatches.

private void printLayerPatches(CommandContext ctx, final ModelNode result, final String type) {
    ModelNode layer = result.get(type);
    if (layer.isDefined()) {
        final String header = Character.toUpperCase(type.charAt(0)) + type.substring(1) + ':';
        for (String name : layer.keys()) {
            final ModelNode node = layer.get(name);
            final SimpleTable table = new SimpleTable(2, ctx.getTerminalWidth());
            table.addLine(new String[] { header, name });
            addPatchesInfo(node, table);
            ctx.printLine(lineSeparator + table.toString(false));
        }
    }
}
Also used : SimpleTable(org.jboss.as.cli.util.SimpleTable) ModelNode(org.jboss.dmr.ModelNode)

Example 5 with SimpleTable

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

the class ReadAttributeHandler method handleResponse.

@Override
protected void handleResponse(CommandContext ctx, ModelNode response, boolean composite) throws CommandFormatException {
    if (!Util.isSuccess(response)) {
        throw new CommandFormatException(Util.getFailureDescription(response));
    }
    if (!response.hasDefined(Util.RESULT)) {
        return;
    }
    final ModelNode result = response.get(Util.RESULT);
    if (composite) {
        final SimpleTable table = new SimpleTable(2, ctx.getTerminalWidth());
        final StringBuilder valueBuf = new StringBuilder();
        if (result.hasDefined(Util.STEP_1)) {
            final ModelNode stepOutcome = result.get(Util.STEP_1);
            if (Util.isSuccess(stepOutcome)) {
                if (stepOutcome.hasDefined(Util.RESULT)) {
                    final ModelNode valueResult = stepOutcome.get(Util.RESULT);
                    final ModelNodeFormatter formatter = ModelNodeFormatter.Factory.forType(valueResult.getType());
                    formatter.format(valueBuf, 0, valueResult);
                } else {
                    valueBuf.append("n/a");
                }
                table.addLine(new String[] { "value", valueBuf.toString() });
            } else {
                throw new CommandFormatException("Failed to get resource description: " + response);
            }
        }
        if (result.hasDefined(Util.STEP_2)) {
            final ModelNode stepOutcome = result.get(Util.STEP_2);
            if (Util.isSuccess(stepOutcome)) {
                if (stepOutcome.hasDefined(Util.RESULT)) {
                    final ModelNode descrResult = stepOutcome.get(Util.RESULT);
                    if (descrResult.hasDefined(Util.ATTRIBUTES)) {
                        ModelNode attributes = descrResult.get(Util.ATTRIBUTES);
                        final String name = this.name.getValue(ctx.getParsedCommandLine());
                        if (name == null) {
                            throw new CommandFormatException("Attribute name is not available in handleResponse.");
                        } else if (attributes.hasDefined(name)) {
                            final ModelNode descr = attributes.get(name);
                            for (String prop : descr.keys()) {
                                table.addLine(new String[] { prop, descr.get(prop).asString() });
                            }
                        } else {
                            throw new CommandFormatException("Attribute description is not available.");
                        }
                    } else {
                        throw new CommandFormatException("The resource doesn't provide attribute descriptions.");
                    }
                } else {
                    throw new CommandFormatException("Result is not available for read-resource-description request: " + response);
                }
            } else {
                throw new CommandFormatException("Failed to get resource description: " + response);
            }
        }
        ctx.printLine(table.toString(true));
    } else {
        final ModelNodeFormatter formatter = ModelNodeFormatter.Factory.forType(result.getType());
        final StringBuilder buf = new StringBuilder();
        formatter.format(buf, 0, result);
        ctx.printLine(buf.toString());
    }
}
Also used : CommandFormatException(org.jboss.as.cli.CommandFormatException) SimpleTable(org.jboss.as.cli.util.SimpleTable) ModelNode(org.jboss.dmr.ModelNode) ModelNodeFormatter(org.jboss.as.cli.ModelNodeFormatter)

Aggregations

SimpleTable (org.jboss.as.cli.util.SimpleTable)14 ModelNode (org.jboss.dmr.ModelNode)12 CommandFormatException (org.jboss.as.cli.CommandFormatException)7 CommandLineException (org.jboss.as.cli.CommandLineException)4 IOException (java.io.IOException)3 ArrayList (java.util.ArrayList)3 Property (org.jboss.dmr.Property)3 ModelNodeFormatter (org.jboss.as.cli.ModelNodeFormatter)2 ParsedCommandLine (org.jboss.as.cli.operation.ParsedCommandLine)2 Identity (org.jboss.as.patching.metadata.Identity)2 PatchElement (org.jboss.as.patching.metadata.PatchElement)2 X509Certificate (java.security.cert.X509Certificate)1 List (java.util.List)1 Pattern (java.util.regex.Pattern)1 ZipException (java.util.zip.ZipException)1 XMLStreamException (javax.xml.stream.XMLStreamException)1 ConnectionInfo (org.jboss.as.cli.ConnectionInfo)1 OperationFormatException (org.jboss.as.cli.operation.OperationFormatException)1 StrictSizeTable (org.jboss.as.cli.util.StrictSizeTable)1 ModelControllerClient (org.jboss.as.controller.client.ModelControllerClient)1