Search in sources :

Example 1 with DisconnectedException

use of io.cdap.cdap.client.exception.DisconnectedException in project cdap by caskdata.

the class CLIMain method main.

public static void main(String[] args) {
    // disable logback logging
    Logger root = (Logger) LoggerFactory.getLogger(Logger.ROOT_LOGGER_NAME);
    root.setLevel(Level.OFF);
    final PrintStream output = System.out;
    Options options = getOptions();
    CLIMainArgs cliMainArgs = CLIMainArgs.parse(args, options);
    CommandLineParser parser = new BasicParser();
    try {
        CommandLine command = parser.parse(options, cliMainArgs.getOptionTokens());
        if (command.hasOption(HELP_OPTION.getOpt())) {
            usage();
            System.exit(0);
        }
        LaunchOptions launchOptions = LaunchOptions.builder().setUri(command.getOptionValue(URI_OPTION.getOpt())).setInstanceURI(command.getOptionValue(LINK_OPTION.getOpt(), getDefaultURI().toString())).setNamespace(command.getOptionValue(NAMESPACE_OPTION.getOpt(), NamespaceId.DEFAULT.getNamespace())).setDebug(command.hasOption(DEBUG_OPTION.getOpt())).setVerifySSL(parseBooleanOption(command, VERIFY_SSL_OPTION, DEFAULT_VERIFY_SSL)).setAutoconnect(parseBooleanOption(command, AUTOCONNECT_OPTION, DEFAULT_AUTOCONNECT)).build();
        String scriptFile = command.getOptionValue(SCRIPT_OPTION.getOpt(), "");
        boolean hasScriptFile = command.hasOption(SCRIPT_OPTION.getOpt());
        String[] commandArgs = cliMainArgs.getCommandTokens();
        try {
            ClientConfig clientConfig = ClientConfig.builder().setConnectionConfig(null).setDefaultReadTimeout(parseIntegerOption(command, TIMEOUT_OPTION, 60) * 1000).setUnavailableRetryLimit(parseIntegerOption(command, RETRIES_OPTION, 0)).build();
            final CLIConfig cliConfig = new CLIConfig(clientConfig, output, new AltStyleTableRenderer());
            CLIMain cliMain = new CLIMain(launchOptions, cliConfig);
            CLI cli = cliMain.getCLI();
            if (!cliMain.tryAutoconnect(command)) {
                System.exit(0);
            }
            CLIConnectionConfig connectionConfig = new CLIConnectionConfig(cliConfig.getClientConfig().getConnectionConfig(), cliConfig.getCurrentNamespace(), null);
            cliMain.updateCLIPrompt(connectionConfig);
            if (hasScriptFile) {
                File script = cliMain.getFilePathResolver().resolvePathToFile(scriptFile);
                if (!script.exists()) {
                    output.println("ERROR: Script file '" + script.getAbsolutePath() + "' does not exist");
                    System.exit(1);
                }
                List<String> scriptLines = Files.readLines(script, Charsets.UTF_8);
                for (String scriptLine : scriptLines) {
                    output.print(cliMain.getPrompt(connectionConfig));
                    output.println(scriptLine);
                    cli.execute(scriptLine, output);
                    output.println();
                }
            } else if (commandArgs.length == 0) {
                cli.startInteractiveMode(output);
            } else {
                cli.execute(Joiner.on(" ").join(commandArgs), output);
            }
        } catch (DisconnectedException e) {
            output.printf("Couldn't reach the CDAP instance at '%s'.", e.getConnectionConfig().getURI().toString());
        } catch (Exception e) {
            e.printStackTrace(output);
        }
    } catch (ParseException e) {
        output.println(e.getMessage());
        usage();
    }
}
Also used : PrintStream(java.io.PrintStream) Options(org.apache.commons.cli.Options) CLI(io.cdap.common.cli.CLI) AltStyleTableRenderer(io.cdap.cdap.cli.util.table.AltStyleTableRenderer) Logger(ch.qos.logback.classic.Logger) URISyntaxException(java.net.URISyntaxException) SSLHandshakeException(javax.net.ssl.SSLHandshakeException) ParseException(org.apache.commons.cli.ParseException) InvalidCommandException(io.cdap.common.cli.exception.InvalidCommandException) DisconnectedException(io.cdap.cdap.client.exception.DisconnectedException) ConnectException(java.net.ConnectException) JsonSyntaxException(com.google.gson.JsonSyntaxException) IOException(java.io.IOException) BasicParser(org.apache.commons.cli.BasicParser) CommandLine(org.apache.commons.cli.CommandLine) CommandLineParser(org.apache.commons.cli.CommandLineParser) ParseException(org.apache.commons.cli.ParseException) ClientConfig(io.cdap.cdap.client.config.ClientConfig) File(java.io.File) DisconnectedException(io.cdap.cdap.client.exception.DisconnectedException)

Aggregations

Logger (ch.qos.logback.classic.Logger)1 JsonSyntaxException (com.google.gson.JsonSyntaxException)1 AltStyleTableRenderer (io.cdap.cdap.cli.util.table.AltStyleTableRenderer)1 ClientConfig (io.cdap.cdap.client.config.ClientConfig)1 DisconnectedException (io.cdap.cdap.client.exception.DisconnectedException)1 CLI (io.cdap.common.cli.CLI)1 InvalidCommandException (io.cdap.common.cli.exception.InvalidCommandException)1 File (java.io.File)1 IOException (java.io.IOException)1 PrintStream (java.io.PrintStream)1 ConnectException (java.net.ConnectException)1 URISyntaxException (java.net.URISyntaxException)1 SSLHandshakeException (javax.net.ssl.SSLHandshakeException)1 BasicParser (org.apache.commons.cli.BasicParser)1 CommandLine (org.apache.commons.cli.CommandLine)1 CommandLineParser (org.apache.commons.cli.CommandLineParser)1 Options (org.apache.commons.cli.Options)1 ParseException (org.apache.commons.cli.ParseException)1