Search in sources :

Example 6 with SimpleTable

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

the class ReadOperationHandler method handleResponse.

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;
    }
    boolean opDescr;
    try {
        opDescr = name.isPresent(ctx.getParsedCommandLine());
    } catch (CommandFormatException e) {
        throw new CommandFormatException("Failed to read argument " + name.getFullName() + ": " + e.getLocalizedMessage());
    }
    if (opDescr) {
        final ModelNode result = response.get(Util.RESULT);
        if (result.has(Util.DESCRIPTION)) {
            ctx.printLine("\n\t" + result.get(Util.DESCRIPTION).asString());
        } else {
            ctx.printLine("Operation description is not available.");
        }
        final StringBuilder buf = new StringBuilder();
        buf.append("\n\nPARAMETERS\n");
        if (result.has(Util.REQUEST_PROPERTIES)) {
            final List<Property> props = result.get(Util.REQUEST_PROPERTIES).asPropertyList();
            if (props.isEmpty()) {
                buf.append("\n\tn/a\n");
            } else {
                for (Property prop : props) {
                    buf.append('\n');
                    buf.append(prop.getName()).append("\n\n");
                    final List<Property> propProps = prop.getValue().asPropertyList();
                    final SimpleTable table = new SimpleTable(2, ctx.getTerminalWidth());
                    for (Property propProp : propProps) {
                        if (propProp.getName().equals(Util.DESCRIPTION)) {
                            buf.append('\t').append(propProp.getValue().asString()).append("\n\n");
                        } else if (!propProp.getName().equals(Util.VALUE_TYPE)) {
                            // TODO not detailing the value-type here, it's readability/formatting issue
                            table.addLine(new String[] { '\t' + propProp.getName() + ':', propProp.getValue().asString() });
                        }
                    }
                    table.append(buf, false);
                    buf.append('\n');
                }
            }
        } else {
            buf.append("\n\tn/a\n");
        }
        ctx.printLine(buf.toString());
        buf.setLength(0);
        buf.append("\nRESPONSE\n");
        if (result.has(Util.REPLY_PROPERTIES)) {
            final List<Property> props = result.get(Util.REPLY_PROPERTIES).asPropertyList();
            if (props.isEmpty()) {
                buf.append("\n\tn/a\n");
            } else {
                buf.append('\n');
                final SimpleTable table = new SimpleTable(2, ctx.getTerminalWidth());
                StringBuilder vtBuf = null;
                for (Property prop : props) {
                    ModelType modelType = prop.getValue().getType();
                    if (prop.getName().equals(Util.DESCRIPTION)) {
                        buf.append('\t').append(prop.getValue().asString()).append("\n\n");
                    } else if (prop.getName().equals(Util.VALUE_TYPE) && (prop.getValue().getType() == ModelType.OBJECT || prop.getValue().getType() == ModelType.LIST)) {
                        final List<Property> vtProps = prop.getValue().asPropertyList();
                        if (!vtProps.isEmpty()) {
                            vtBuf = new StringBuilder();
                            for (Property vtProp : vtProps) {
                                vtBuf.append('\n').append(vtProp.getName()).append("\n\n");
                                final List<Property> vtPropProps = vtProp.getValue().asPropertyList();
                                final SimpleTable vtTable = new SimpleTable(2, ctx.getTerminalWidth());
                                for (Property vtPropProp : vtPropProps) {
                                    if (vtPropProp.getName().equals(Util.DESCRIPTION)) {
                                        vtBuf.append('\t').append(vtPropProp.getValue().asString()).append("\n\n");
                                    } else if (!vtPropProp.getName().equals(Util.VALUE_TYPE)) {
                                        // TODO not detailing the value-type here, it's readability/formatting issue
                                        vtTable.addLine(new String[] { '\t' + vtPropProp.getName() + ':', vtPropProp.getValue().asString() });
                                    }
                                }
                                vtTable.append(vtBuf, false);
                                vtBuf.append('\n');
                            }
                        }
                    } else {
                        table.addLine(new String[] { '\t' + prop.getName() + ':', prop.getValue().asString() });
                    }
                }
                table.append(buf, false);
                buf.append('\n');
                if (vtBuf != null) {
                    buf.append(vtBuf);
                }
            }
        } else {
            buf.append("\n\tn/a\n");
        }
        ctx.printLine(buf.toString());
    } else {
        ctx.printColumns(Util.getList(response));
    }
}
Also used : CommandFormatException(org.jboss.as.cli.CommandFormatException) SimpleTable(org.jboss.as.cli.util.SimpleTable) ModelType(org.jboss.dmr.ModelType) List(java.util.List) ModelNode(org.jboss.dmr.ModelNode) Property(org.jboss.dmr.Property)

Example 7 with SimpleTable

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

the class JDBCDriverInfoHandler method handleResponse.

@Override
protected void handleResponse(CommandContext ctx, ModelNode response, boolean composite) throws CommandLineException {
    final ModelNode result = response.get(Util.RESULT);
    if (!result.isDefined()) {
        throw new CommandLineException("The operation result is not defined: " + result);
    }
    final List<ModelNode> list = result.asList();
    if (!name.isPresent(ctx.getParsedCommandLine())) {
        final SimpleTable table = new SimpleTable(new String[] { "NAME", "SOURCE" }, ctx.getTerminalWidth());
        for (ModelNode node : list) {
            final ModelNode driverName = node.get(Util.DRIVER_NAME);
            if (!driverName.isDefined()) {
                throw new CommandLineException(Util.DRIVER_NAME + " is not available: " + node);
            }
            final String source;
            if (node.hasDefined(Util.DEPLOYMENT_NAME)) {
                source = node.get(Util.DEPLOYMENT_NAME).asString();
            } else if (node.hasDefined(Util.DRIVER_MODULE_NAME)) {
                final StringBuilder buf = new StringBuilder();
                buf.append(node.get(Util.DRIVER_MODULE_NAME).asString());
                if (node.hasDefined(Util.MODULE_SLOT)) {
                    buf.append('/').append(node.get(Util.MODULE_SLOT).asString());
                }
                source = buf.toString();
            } else {
                source = "n/a";
            }
            table.addLine(new String[] { driverName.asString(), source });
        }
        ctx.printLine(table.toString(true));
    } else {
        final String name = this.name.getValue(ctx.getParsedCommandLine());
        final SimpleTable table = new SimpleTable(2, ctx.getTerminalWidth());
        for (ModelNode node : list) {
            final ModelNode driverName = node.get(Util.DRIVER_NAME);
            if (!driverName.isDefined()) {
                throw new CommandLineException(Util.DRIVER_NAME + " is not available: " + node);
            }
            if (name.equals(driverName.asString())) {
                for (String propName : node.keys()) {
                    final ModelNode value = node.get(propName);
                    table.addLine(new String[] { propName, value.isDefined() ? value.asString() : "n/a" });
                }
            }
        }
        ctx.printLine(table.toString(false));
    }
}
Also used : SimpleTable(org.jboss.as.cli.util.SimpleTable) ModelNode(org.jboss.dmr.ModelNode) CommandLineException(org.jboss.as.cli.CommandLineException)

Example 8 with SimpleTable

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

the class InfoCommand method handleResponse.

public void handleResponse(CommandContext ctx, ModelNode response) throws CommandFormatException {
    try {
        if (!response.hasDefined(Util.RESULT)) {
            throw new CommandFormatException("The operation response came back " + "w/o result: " + response);
        }
        ModelNode result = response.get(Util.RESULT);
        if (ctx.isDomainMode()) {
            // TODO it could be... could be not...
            if (result.hasDefined(Util.DOMAIN_RESULTS)) {
                result = result.get(Util.DOMAIN_RESULTS);
            }
            final Iterator<Property> steps = result.asPropertyList().iterator();
            if (!steps.hasNext()) {
                throw new CommandFormatException("Response for the main resource " + "info of the deployment is missing: " + result);
            }
            // /deployment=<name>
            ModelNode step = steps.next().getValue();
            if (step.has(Util.STEP_1)) {
                // TODO remove when the structure is consistent
                step = step.get(Util.STEP_1);
            }
            if (!step.has(Util.RESULT)) {
                throw new CommandFormatException("Failed to read the main resource " + "info of the deployment: " + Util.getFailureDescription(step));
            }
            ModelNode stepResponse = step.get(Util.RESULT);
            if (serverGroup != null) {
                step = result.get(Util.STEP_1);
                if (!step.isDefined()) {
                    throw new CommandFormatException("No step outcome for deployment " + "resources (step-1): " + result);
                }
                final ModelNode allDeployments = step.get(Util.RESULT);
                if (!allDeployments.isDefined()) {
                    throw new CommandFormatException("No result for deployment " + "resources (step-1): " + result);
                }
                step = result.get(Util.STEP_2);
                if (!step.isDefined()) {
                    throw new CommandFormatException("No step outcome for " + "server-group deployment resources (step-2): " + result);
                }
                final ModelNode sgDeployments = step.get(Util.RESULT);
                if (!sgDeployments.isDefined()) {
                    throw new CommandFormatException("No result for server-group " + "deployment resources (step-2): " + result);
                }
                final String deploymentName = getName();
                final Pattern pattern = Pattern.compile(Util.wildcardToJavaRegex(deploymentName == null ? "*" : deploymentName));
                final SimpleTable table = new SimpleTable(new String[] { NAME, RUNTIME_NAME, STATE }, ctx.getTerminalWidth());
                for (String name : allDeployments.keys()) {
                    if (!pattern.matcher(name).matches()) {
                        continue;
                    }
                    if (sgDeployments.hasDefined(name)) {
                        final ModelNode node = sgDeployments.get(name);
                        table.addLine(new String[] { node.get(Util.NAME).asString(), node.get(Util.RUNTIME_NAME).asString(), node.get(Util.ENABLED).asBoolean() ? Util.ENABLED : ADDED });
                    } else {
                        final ModelNode resource = allDeployments.get(name);
                        table.addLine(new String[] { resource.get(Util.NAME).asString(), resource.get(Util.RUNTIME_NAME).asString(), NOT_ADDED });
                    }
                }
                if (!table.isEmpty()) {
                    ctx.printLine(table.toString(true));
                }
            } else {
                final StrictSizeTable table = new StrictSizeTable(1);
                table.addCell(Util.NAME, stepResponse.get(Util.NAME).asString());
                table.addCell(Util.RUNTIME_NAME, stepResponse.get(Util.RUNTIME_NAME).asString());
                ctx.printLine(table.toString());
                final SimpleTable groups = new SimpleTable(new String[] { SERVER_GROUP, STATE }, ctx.getTerminalWidth());
                if (addedServerGroups == null) {
                    if (steps.hasNext()) {
                        throw new CommandFormatException("Didn't expect results " + "for server groups but received " + (result.asPropertyList().size() - 1) + " more steps.");
                    }
                } else {
                    for (String sg : addedServerGroups) {
                        final Property prop = steps.next();
                        stepResponse = prop.getValue();
                        if (stepResponse.has(prop.getName())) {
                            // TODO remove when the structure is consistent
                            stepResponse = stepResponse.get(prop.getName());
                        }
                        if (stepResponse.hasDefined(Util.RESULT)) {
                            final ModelNode stepResult = stepResponse.get(Util.RESULT);
                            if (stepResult.hasDefined(Util.ENABLED)) {
                                groups.addLine(new String[] { sg, stepResult.get(Util.ENABLED).asBoolean() ? Util.ENABLED : ADDED });
                            } else {
                                groups.addLine(new String[] { sg, N_A });
                            }
                        } else {
                            groups.addLine(new String[] { sg, "no response" });
                        }
                    }
                }
                if (otherServerGroups != null) {
                    for (String sg : otherServerGroups) {
                        groups.addLine(new String[] { sg, NOT_ADDED });
                    }
                }
                ctx.printLine(groups.toString(true));
            }
        } else {
            final SimpleTable table = new SimpleTable(new String[] { NAME, RUNTIME_NAME, PERSISTENT, ENABLED, STATUS }, ctx.getTerminalWidth());
            final String deploymentName = getName();
            if (deploymentName == null || deploymentName.indexOf('*') >= 0) {
                final List<Property> list = result.asPropertyList();
                if (!list.isEmpty()) {
                    final Pattern pattern = Pattern.compile(Util.wildcardToJavaRegex(deploymentName == null ? "*" : deploymentName));
                    for (Property p : list) {
                        final ModelNode node = p.getValue();
                        final String name = node.get(Util.NAME).asString();
                        if (pattern.matcher(name).matches()) {
                            table.addLine(new String[] { name, node.get(Util.RUNTIME_NAME).asString(), node.get(Util.PERSISTENT).asString(), node.get(Util.ENABLED).asString(), node.get(Util.STATUS).asString() });
                        }
                    }
                }
            } else {
                table.addLine(new String[] { result.get(Util.NAME).asString(), result.get(Util.RUNTIME_NAME).asString(), result.get(Util.PERSISTENT).asString(), result.get(Util.ENABLED).asString(), result.get(Util.STATUS).asString() });
            }
            if (!table.isEmpty()) {
                ctx.printLine(table.toString());
            }
        }
    } finally {
        addedServerGroups = null;
        otherServerGroups = null;
    }
}
Also used : Pattern(java.util.regex.Pattern) CommandFormatException(org.jboss.as.cli.CommandFormatException) SimpleTable(org.jboss.as.cli.util.SimpleTable) ModelNode(org.jboss.dmr.ModelNode) Property(org.jboss.dmr.Property) StrictSizeTable(org.jboss.as.cli.util.StrictSizeTable)

Example 9 with SimpleTable

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

the class PatchHandler method displayPatchXml.

private void displayPatchXml(CommandContext ctx, Patch patch) throws CommandLineException {
    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.isPresent(ctx.getParsedCommandLine())) {
        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 10 with SimpleTable

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

the class PatchInfo method handleResponse.

@Override
protected void handleResponse(CommandContext ctx, ModelNode response) throws CommandException {
    if (getPatchId() != 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) {
        ctx.printLine(response.toJSONString(false));
    } else if (streams) {
        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) {
            printLayerPatches(ctx, result, Constants.ADD_ON);
            printLayerPatches(ctx, result, Constants.LAYER);
        }
    }
}
Also used : SimpleTable(org.jboss.as.cli.util.SimpleTable) ArrayList(java.util.ArrayList) ModelNode(org.jboss.dmr.ModelNode)

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