Search in sources :

Example 1 with Result

use of org.jboss.as.cli.scriptsupport.CLI.Result in project wildfly by wildfly.

the class CommandLineMain method main.

/**
     * Creates a JBoss Diagnostic Reporter (JDR) Report. A JDR report response
     * is printed to <code>System.out</code>.
     *
     * @param args ignored
     */
public static void main(String[] args) {
    int port = 9990;
    String host = "localhost";
    String protocol = "http-remoting";
    String config = null;
    try {
        CommandLine line = parser.parse(options, args, false);
        if (line.hasOption("help")) {
            formatter.printHelp(usage, NEW_LINE + JdrLogger.ROOT_LOGGER.jdrDescriptionMessage(), options, null);
            return;
        }
        if (line.hasOption("host")) {
            host = line.getOptionValue("host");
        }
        if (line.hasOption("port")) {
            port = Integer.parseInt(line.getOptionValue("port"));
        }
        if (line.hasOption("protocol")) {
            protocol = line.getOptionValue("protocol");
        }
        if (line.hasOption("config")) {
            config = line.getOptionValue("config");
        }
    } catch (ParseException e) {
        System.out.println(e.getMessage());
        formatter.printHelp(usage, options);
        return;
    } catch (NumberFormatException nfe) {
        System.out.println(nfe.getMessage());
        formatter.printHelp(usage, options);
        return;
    }
    System.out.println("Initializing JBoss Diagnostic Reporter...");
    // Try to run JDR on the Wildfly JVM
    CLI cli = CLI.newInstance();
    boolean embedded = false;
    JdrReport report = null;
    try {
        System.out.println(String.format("Trying to connect to %s %s:%s", protocol, host, port));
        cli.connect(protocol, host, port, null, null);
    } catch (IllegalStateException ex) {
        System.out.println("Starting embedded server");
        String startEmbeddedServer = "embed-server --std-out=echo " + ((config != null && !config.isEmpty()) ? (" --server-config=" + config) : "");
        cli.getCommandContext().handleSafe(startEmbeddedServer);
        embedded = true;
    }
    try {
        Result cmdResult = cli.cmd("/subsystem=jdr:generate-jdr-report()");
        ModelNode response = cmdResult.getResponse();
        if (Operations.isSuccessfulOutcome(response) || !embedded) {
            reportFailure(response);
            ModelNode result = response.get(ClientConstants.RESULT);
            report = new JdrReport(result);
        } else {
            report = standaloneCollect(cli, protocol, host, port);
        }
    } catch (IllegalStateException ise) {
        System.out.println(ise.getMessage());
        report = standaloneCollect(cli, protocol, host, port);
    } finally {
        if (cli != null) {
            try {
                if (embedded)
                    cli.getCommandContext().handleSafe("stop-embedded-server");
                else
                    cli.disconnect();
            } catch (Exception e) {
                System.out.println("Caught exception while disconnecting: " + e.getMessage());
            }
        }
    }
    printJdrReportInfo(report);
    System.exit(0);
}
Also used : CLI(org.jboss.as.cli.scriptsupport.CLI) CommandLine(org.apache.commons.cli.CommandLine) ParseException(org.apache.commons.cli.ParseException) ModelNode(org.jboss.dmr.ModelNode) OperationFailedException(org.jboss.as.controller.OperationFailedException) ParseException(org.apache.commons.cli.ParseException) Result(org.jboss.as.cli.scriptsupport.CLI.Result)

Aggregations

CommandLine (org.apache.commons.cli.CommandLine)1 ParseException (org.apache.commons.cli.ParseException)1 CLI (org.jboss.as.cli.scriptsupport.CLI)1 Result (org.jboss.as.cli.scriptsupport.CLI.Result)1 OperationFailedException (org.jboss.as.controller.OperationFailedException)1 ModelNode (org.jboss.dmr.ModelNode)1