demos.Main.java Source code

Java tutorial

Introduction

Here is the source code for demos.Main.java

Source

/*
 * Copyright 2015 Red Hat, Inc. and/or its affiliates
 * and other contributors as indicated by the @author tags.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package demos;

import org.apache.commons.cli.CommandLine;
import org.apache.commons.cli.CommandLineParser;
import org.apache.commons.cli.HelpFormatter;
import org.apache.commons.cli.Option;
import org.apache.commons.cli.Options;
import org.apache.commons.cli.ParseException;
import org.apache.commons.cli.PosixParser;

/**
 * The main class for running examples.
 *
 * @author jsanda
 */
public class Main {

    public static final int STATUS_SHOW_USAGE = 100;

    public static void main(String[] args) throws Exception {
        try {
            CommandLineParser parser = new PosixParser();
            CommandLine cmdLine = parser.parse(getOptions(), args);
            int status = run(cmdLine);
            System.exit(status);
        } catch (ParseException e) {
            printUsage();
            System.exit(STATUS_SHOW_USAGE);
        }
    }

    private static int run(CommandLine cmdLine) throws Exception {
        Runnable demo;
        if (cmdLine.hasOption("h")) {
            demo = Main::printUsage;
        } else if (cmdLine.hasOption("d")) {
            demo = new NodeDiscovery();
        } else if (cmdLine.hasOption("t")) {
            demo = new TopologyChange();
        } else if (cmdLine.hasOption("s")) {
            demo = new SynchronousInsert();
        } else if (cmdLine.hasOption("a")) {
            demo = new AsynchronousInsert();
        } else if (cmdLine.hasOption("b")) {
            demo = new BatchInsert();
        } else if (cmdLine.hasOption("r")) {
            demo = new SynchronousRead();
        } else if (cmdLine.hasOption("ar")) {
            demo = new AsynchronousRead();
        } else {
            demo = Main::printUsage;
        }

        try {
            demo.run();
        } catch (Exception e) {
            return 1;
        }
        return 0;
    }

    private static Options getOptions() {
        return new Options().addOption(new Option("h", "help", false, "Display this message"))
                .addOption(new Option("d", "discovery", false, "Discover and log cluster nodes"))
                .addOption(new Option("t", "topology", false, "Listen for cluster topology changes"))
                .addOption(new Option("s", "sync-insert", false, "Execute synchronous/blocking inserts"))
                .addOption(new Option("a", "async-insert", false, "Execute asynchronous/non-blocking inserts"))
                .addOption(new Option("b", "batch-insert", false, "Execute batch inserts (asynchronously)"))
                .addOption(new Option("r", "sync-read", false, "Execute synchronous/blocking reads"))
                .addOption(new Option("ar", "async-read", false, "Executes asynchronous/non-blocking reads"));
    }

    private static void printUsage() {
        HelpFormatter helpFormatter = new HelpFormatter();
        String syntax = "java -jar /path/to/java-driver-demos-1.0.0-SNAPSHOT.jar [options]";
        String header = "";

        helpFormatter.printHelp(syntax, header, getOptions(), null);
    }

}