Example usage for java.lang String isEmpty

List of usage examples for java.lang String isEmpty

Introduction

In this page you can find the example usage for java.lang String isEmpty.

Prototype

public boolean isEmpty() 

Source Link

Document

Returns true if, and only if, #length() is 0 .

Usage

From source file:com.oltpbenchmark.DBWorkload.java

/**
 * @param args/*from  ww  w . j  av  a  2  s .c o m*/
 * @throws Exception 
 */
public static void main(String[] args) throws Exception {
    // Initialize log4j
    String log4jPath = System.getProperty("log4j.configuration");
    if (log4jPath != null) {
        org.apache.log4j.PropertyConfigurator.configure(log4jPath);
    } else {
        throw new RuntimeException("Missing log4j.properties file");
    }

    // create the command line parser
    CommandLineParser parser = new PosixParser();
    XMLConfiguration pluginConfig = null;
    try {
        pluginConfig = new XMLConfiguration("config/plugin.xml");
    } catch (ConfigurationException e1) {
        LOG.info("Plugin configuration file config/plugin.xml is missing");
        e1.printStackTrace();
    }
    pluginConfig.setExpressionEngine(new XPathExpressionEngine());
    Options options = new Options();
    options.addOption("b", "bench", true,
            "[required] Benchmark class. Currently supported: " + pluginConfig.getList("/plugin//@name"));
    options.addOption("c", "config", true, "[required] Workload configuration file");
    options.addOption(null, "create", true, "Initialize the database for this benchmark");
    options.addOption(null, "clear", true, "Clear all records in the database for this benchmark");
    options.addOption(null, "load", true, "Load data using the benchmark's data loader");
    options.addOption(null, "execute", true, "Execute the benchmark workload");
    options.addOption(null, "runscript", true, "Run an SQL script");
    options.addOption(null, "upload", true, "Upload the result");

    options.addOption("v", "verbose", false, "Display Messages");
    options.addOption("h", "help", false, "Print this help");
    options.addOption("s", "sample", true, "Sampling window");
    options.addOption("ss", false, "Verbose Sampling per Transaction");
    options.addOption("o", "output", true, "Output file (default System.out)");
    options.addOption("d", "directory", true,
            "Base directory for the result files, default is current directory");
    options.addOption("t", "timestamp", false,
            "Each result file is prepended with a timestamp for the beginning of the experiment");
    options.addOption(null, "histograms", false, "Print txn histograms");
    options.addOption(null, "dialects-export", true, "Export benchmark SQL to a dialects file");

    // parse the command line arguments
    CommandLine argsLine = parser.parse(options, args);
    if (argsLine.hasOption("h")) {
        printUsage(options);
        return;
    } else if (argsLine.hasOption("c") == false) {
        LOG.error("Missing Configuration file");
        printUsage(options);
        return;
    } else if (argsLine.hasOption("b") == false) {
        LOG.fatal("Missing Benchmark Class to load");
        printUsage(options);
        return;
    }

    // If an output directory is used, store the information
    String outputDirectory = "";
    if (argsLine.hasOption("d")) {
        outputDirectory = argsLine.getOptionValue("d");
    }

    String timestampValue = "";
    if (argsLine.hasOption("t")) {
        timestampValue = String.valueOf(TimeUtil.getCurrentTime().getTime()) + "_";
    }

    //       -------------------------------------------------------------------
    //        GET PLUGIN LIST
    //       -------------------------------------------------------------------

    String plugins = argsLine.getOptionValue("b");

    String[] pluginList = plugins.split(",");
    List<BenchmarkModule> benchList = new ArrayList<BenchmarkModule>();

    // Use this list for filtering of the output
    List<TransactionType> activeTXTypes = new ArrayList<TransactionType>();

    String configFile = argsLine.getOptionValue("c");
    XMLConfiguration xmlConfig = new XMLConfiguration(configFile);
    xmlConfig.setExpressionEngine(new XPathExpressionEngine());
    int lastTxnId = 0;

    for (String plugin : pluginList) {

        // ----------------------------------------------------------------
        // WORKLOAD CONFIGURATION
        // ----------------------------------------------------------------

        String pluginTest = "";

        pluginTest = "[@bench='" + plugin + "']";

        WorkloadConfiguration wrkld = new WorkloadConfiguration();
        wrkld.setBenchmarkName(plugin);
        wrkld.setXmlConfig(xmlConfig);
        wrkld.setDBType(DatabaseType.get(xmlConfig.getString("dbtype")));
        wrkld.setDBDriver(xmlConfig.getString("driver"));
        wrkld.setDBConnection(xmlConfig.getString("DBUrl"));
        wrkld.setDBName(xmlConfig.getString("DBName"));
        wrkld.setDBUsername(xmlConfig.getString("username"));
        wrkld.setDBPassword(xmlConfig.getString("password"));
        int terminals = xmlConfig.getInt("terminals[not(@bench)]", 0);
        terminals = xmlConfig.getInt("terminals" + pluginTest, terminals);
        wrkld.setTerminals(terminals);
        wrkld.setIsolationMode(xmlConfig.getString("isolation", "TRANSACTION_SERIALIZABLE"));
        wrkld.setScaleFactor(xmlConfig.getDouble("scalefactor", 1.0));
        wrkld.setRecordAbortMessages(xmlConfig.getBoolean("recordabortmessages", false));

        int size = xmlConfig.configurationsAt("/works/work").size();
        for (int i = 1; i < size + 1; i++) {
            SubnodeConfiguration work = xmlConfig.configurationAt("works/work[" + i + "]");
            List<String> weight_strings;

            // use a workaround if there multiple workloads or single
            // attributed workload
            if (pluginList.length > 1 || work.containsKey("weights[@bench]")) {
                weight_strings = get_weights(plugin, work);
            } else {
                weight_strings = work.getList("weights[not(@bench)]");
            }
            int rate = 1;
            boolean rateLimited = true;
            boolean disabled = false;

            // can be "disabled", "unlimited" or a number
            String rate_string;
            rate_string = work.getString("rate[not(@bench)]", "");
            rate_string = work.getString("rate" + pluginTest, rate_string);
            if (rate_string.equals(RATE_DISABLED)) {
                disabled = true;
            } else if (rate_string.equals(RATE_UNLIMITED)) {
                rateLimited = false;
            } else if (rate_string.isEmpty()) {
                LOG.fatal(String.format("Please specify the rate for phase %d and workload %s", i, plugin));
                System.exit(-1);
            } else {
                try {
                    rate = Integer.parseInt(rate_string);
                    if (rate < 1) {
                        LOG.fatal("Rate limit must be at least 1. Use unlimited or disabled values instead.");
                        System.exit(-1);
                    }
                } catch (NumberFormatException e) {
                    LOG.fatal(String.format("Rate string must be '%s', '%s' or a number", RATE_DISABLED,
                            RATE_UNLIMITED));
                    System.exit(-1);
                }
            }
            Phase.Arrival arrival = Phase.Arrival.REGULAR;
            String arrive = work.getString("@arrival", "regular");
            if (arrive.toUpperCase().equals("POISSON"))
                arrival = Phase.Arrival.POISSON;

            int activeTerminals;
            activeTerminals = work.getInt("active_terminals[not(@bench)]", terminals);
            activeTerminals = work.getInt("active_terminals" + pluginTest, activeTerminals);
            if (activeTerminals > terminals) {
                System.out.println("Configuration error in work " + i + ": number of active terminals" + ""
                        + "is bigger than the total number of terminals");
                System.exit(-1);
            }
            wrkld.addWork(work.getInt("/time"), rate, weight_strings, rateLimited, disabled, activeTerminals,
                    arrival);
        } // FOR

        int numTxnTypes = xmlConfig.configurationsAt("transactiontypes" + pluginTest + "/transactiontype")
                .size();
        if (numTxnTypes == 0 && pluginList.length == 1) {
            //if it is a single workload run, <transactiontypes /> w/o attribute is used
            pluginTest = "[not(@bench)]";
            numTxnTypes = xmlConfig.configurationsAt("transactiontypes" + pluginTest + "/transactiontype")
                    .size();
        }
        wrkld.setNumTxnTypes(numTxnTypes);

        // CHECKING INPUT PHASES
        int j = 0;
        for (Phase p : wrkld.getAllPhases()) {
            j++;
            if (p.getWeightCount() != wrkld.getNumTxnTypes()) {
                LOG.fatal(String.format(
                        "Configuration files is inconsistent, phase %d contains %d weights but you defined %d transaction types",
                        j, p.getWeightCount(), wrkld.getNumTxnTypes()));
                System.exit(-1);
            }
        } // FOR

        // Generate the dialect map
        wrkld.init();

        assert (wrkld.getNumTxnTypes() >= 0);
        assert (xmlConfig != null);

        // ----------------------------------------------------------------
        // BENCHMARK MODULE
        // ----------------------------------------------------------------

        String classname = pluginConfig.getString("/plugin[@name='" + plugin + "']");

        if (classname == null) {
            throw new ParseException("Plugin " + plugin + " is undefined in config/plugin.xml");
        }
        BenchmarkModule bench = ClassUtil.newInstance(classname, new Object[] { wrkld },
                new Class<?>[] { WorkloadConfiguration.class });
        assert (benchList.get(0) != null);

        Map<String, Object> initDebug = new ListOrderedMap<String, Object>();
        initDebug.put("Benchmark", String.format("%s {%s}", plugin.toUpperCase(), classname));
        initDebug.put("Configuration", configFile);
        initDebug.put("Type", wrkld.getDBType());
        initDebug.put("Driver", wrkld.getDBDriver());
        initDebug.put("URL", wrkld.getDBConnection());
        initDebug.put("Isolation", xmlConfig.getString("isolation", "TRANSACTION_SERIALIZABLE [DEFAULT]"));
        initDebug.put("Scale Factor", wrkld.getScaleFactor());
        INIT_LOG.info(SINGLE_LINE + "\n\n" + StringUtil.formatMaps(initDebug));
        INIT_LOG.info(SINGLE_LINE);

        // Load TransactionTypes
        List<TransactionType> ttypes = new ArrayList<TransactionType>();

        // Always add an INVALID type for Carlo
        ttypes.add(TransactionType.INVALID);
        int txnIdOffset = lastTxnId;
        for (int i = 1; i < wrkld.getNumTxnTypes() + 1; i++) {
            String key = "transactiontypes" + pluginTest + "/transactiontype[" + i + "]";
            String txnName = xmlConfig.getString(key + "/name");
            int txnId = i + 1;
            if (xmlConfig.containsKey(key + "/id")) {
                txnId = xmlConfig.getInt(key + "/id");
            }
            TransactionType tmpType = bench.initTransactionType(txnName, txnId + txnIdOffset);
            // Keep a reference for filtering
            activeTXTypes.add(tmpType);
            // Add a reference for the active TTypes in this benchmark
            ttypes.add(tmpType);
            lastTxnId = i;
        } // FOR
        TransactionTypes tt = new TransactionTypes(ttypes);
        wrkld.setTransTypes(tt);
        LOG.debug("Using the following transaction types: " + tt);

        benchList.add(bench);
    }

    // Export StatementDialects
    if (isBooleanOptionSet(argsLine, "dialects-export")) {
        BenchmarkModule bench = benchList.get(0);
        if (bench.getStatementDialects() != null) {
            LOG.info("Exporting StatementDialects for " + bench);
            String xml = bench.getStatementDialects().export(bench.getWorkloadConfiguration().getDBType(),
                    bench.getProcedures().values());
            System.out.println(xml);
            System.exit(0);
        }
        throw new RuntimeException("No StatementDialects is available for " + bench);
    }

    @Deprecated
    boolean verbose = argsLine.hasOption("v");

    // Create the Benchmark's Database
    if (isBooleanOptionSet(argsLine, "create")) {
        for (BenchmarkModule benchmark : benchList) {
            CREATE_LOG.info("Creating new " + benchmark.getBenchmarkName().toUpperCase() + " database...");
            runCreator(benchmark, verbose);
            CREATE_LOG.info("Finished!");
            CREATE_LOG.info(SINGLE_LINE);
        }
    } else if (CREATE_LOG.isDebugEnabled()) {
        CREATE_LOG.debug("Skipping creating benchmark database tables");
        CREATE_LOG.info(SINGLE_LINE);
    }

    // Clear the Benchmark's Database
    if (isBooleanOptionSet(argsLine, "clear")) {
        for (BenchmarkModule benchmark : benchList) {
            CREATE_LOG.info("Resetting " + benchmark.getBenchmarkName().toUpperCase() + " database...");
            benchmark.clearDatabase();
            CREATE_LOG.info("Finished!");
            CREATE_LOG.info(SINGLE_LINE);
        }
    } else if (CREATE_LOG.isDebugEnabled()) {
        CREATE_LOG.debug("Skipping creating benchmark database tables");
        CREATE_LOG.info(SINGLE_LINE);
    }

    // Execute Loader
    if (isBooleanOptionSet(argsLine, "load")) {
        for (BenchmarkModule benchmark : benchList) {
            LOAD_LOG.info("Loading data into " + benchmark.getBenchmarkName().toUpperCase() + " database...");
            runLoader(benchmark, verbose);
            LOAD_LOG.info("Finished!");
            LOAD_LOG.info(SINGLE_LINE);
        }
    } else if (LOAD_LOG.isDebugEnabled()) {
        LOAD_LOG.debug("Skipping loading benchmark database records");
        LOAD_LOG.info(SINGLE_LINE);
    }

    // Execute a Script
    if (argsLine.hasOption("runscript")) {
        for (BenchmarkModule benchmark : benchList) {
            String script = argsLine.getOptionValue("runscript");
            SCRIPT_LOG.info("Running a SQL script: " + script);
            runScript(benchmark, script);
            SCRIPT_LOG.info("Finished!");
            SCRIPT_LOG.info(SINGLE_LINE);
        }
    }

    // Execute Workload
    if (isBooleanOptionSet(argsLine, "execute")) {
        // Bombs away!
        Results r = null;
        try {
            r = runWorkload(benchList, verbose);
        } catch (Throwable ex) {
            LOG.error("Unexpected error when running benchmarks.", ex);
            System.exit(1);
        }
        assert (r != null);

        PrintStream ps = System.out;
        PrintStream rs = System.out;
        ResultUploader ru = new ResultUploader(r, xmlConfig, argsLine);

        if (argsLine.hasOption("o")) {
            // Check if directory needs to be created
            if (outputDirectory.length() > 0) {
                FileUtil.makeDirIfNotExists(outputDirectory.split("/"));
            }

            // Build the complex path
            String baseFile = timestampValue + argsLine.getOptionValue("o");

            // Increment the filename for new results
            String nextName = FileUtil.getNextFilename(FileUtil.joinPath(outputDirectory, baseFile + ".res"));
            ps = new PrintStream(new File(nextName));
            EXEC_LOG.info("Output into file: " + nextName);

            nextName = FileUtil.getNextFilename(FileUtil.joinPath(outputDirectory, baseFile + ".raw"));
            rs = new PrintStream(new File(nextName));
            EXEC_LOG.info("Output Raw data into file: " + nextName);

            nextName = FileUtil.getNextFilename(FileUtil.joinPath(outputDirectory, baseFile + ".summary"));
            PrintStream ss = new PrintStream(new File(nextName));
            EXEC_LOG.info("Output summary data into file: " + nextName);
            ru.writeSummary(ss);
            ss.close();

            nextName = FileUtil.getNextFilename(FileUtil.joinPath(outputDirectory, baseFile + ".db.cnf"));
            ss = new PrintStream(new File(nextName));
            EXEC_LOG.info("Output db config into file: " + nextName);
            ru.writeDBParameters(ss);
            ss.close();

            nextName = FileUtil.getNextFilename(FileUtil.joinPath(outputDirectory, baseFile + ".ben.cnf"));
            ss = new PrintStream(new File(nextName));
            EXEC_LOG.info("Output benchmark config into file: " + nextName);
            ru.writeBenchmarkConf(ss);
            ss.close();
        } else if (EXEC_LOG.isDebugEnabled()) {
            EXEC_LOG.debug("No output file specified");
        }

        if (argsLine.hasOption("s")) {
            int windowSize = Integer.parseInt(argsLine.getOptionValue("s"));
            EXEC_LOG.info("Grouped into Buckets of " + windowSize + " seconds");
            r.writeCSV(windowSize, ps);

            if (isBooleanOptionSet(argsLine, "upload")) {
                ru.uploadResult();
            }

            // Allow more detailed reporting by transaction to make it easier to check
            if (argsLine.hasOption("ss")) {

                for (TransactionType t : activeTXTypes) {
                    PrintStream ts = ps;

                    if (ts != System.out) {
                        // Get the actual filename for the output
                        String baseFile = timestampValue + argsLine.getOptionValue("o") + "_" + t.getName();
                        String prepended = outputDirectory + timestampValue;
                        String nextName = FileUtil
                                .getNextFilename(FileUtil.joinPath(outputDirectory, baseFile + ".res"));
                        ts = new PrintStream(new File(nextName));
                        r.writeCSV(windowSize, ts, t);
                        ts.close();
                    }
                }
            }
        } else if (EXEC_LOG.isDebugEnabled()) {
            EXEC_LOG.warn("No bucket size specified");
        }
        if (argsLine.hasOption("histograms")) {
            EXEC_LOG.info(SINGLE_LINE);
            EXEC_LOG.info("Completed Transactions:\n" + r.getTransactionSuccessHistogram() + "\n");
            EXEC_LOG.info("Aborted Transactions:\n" + r.getTransactionAbortHistogram() + "\n");
            EXEC_LOG.info("Rejected Transactions:\n" + r.getTransactionRetryHistogram());
            EXEC_LOG.info("Unexpected Errors:\n" + r.getTransactionErrorHistogram());
            if (r.getTransactionAbortMessageHistogram().isEmpty() == false)
                EXEC_LOG.info(
                        "User Aborts:\n" + StringUtil.formatMaps(r.getTransactionAbortMessageHistogram()));
        } else if (EXEC_LOG.isDebugEnabled()) {
            EXEC_LOG.warn("No bucket size specified");
        }

        r.writeAllCSVAbsoluteTiming(rs);

        ps.close();
        rs.close();
    } else {
        EXEC_LOG.info("Skipping benchmark workload execution");
    }
}

From source file:RoucairolCarvahloBasicVersion.java

public static void main(String[] args) throws FileNotFoundException, IOException, InterruptedException {
    //For parsing the file and storing the information
    String line;
    String configurationFile = "configuration.txt";
    int lineCountInFile = 0;
    myProcessId = Integer.parseInt(args[0]);
    FileReader fileReader = new FileReader(configurationFile);
    BufferedReader bufferedReader = new BufferedReader(fileReader);
    while ((line = bufferedReader.readLine()) != null) {
        if ((!(line.startsWith("#"))) && (!(line.isEmpty()))) {
            lineCountInFile = lineCountInFile + 1;
            String[] splitLine = line.split(" ");
            switch (lineCountInFile) {
            case 1:
                numberOfProcesses = Integer.parseInt(splitLine[0]);
                interRequestDelay = Integer.parseInt(splitLine[1]);
                csExecutionTime = Integer.parseInt(splitLine[2]);
                maxNumberOfRequest = Integer.parseInt(splitLine[3]);
                machineNames = new String[Integer.parseInt(splitLine[0])];
                portNumbers = new int[Integer.parseInt(splitLine[0])];
                break;
            default:
                machineNames[lineCountInFile - 2] = splitLine[1];
                portNumbers[lineCountInFile - 2] = Integer.parseInt(splitLine[2]);
                break;
            }// w  ww  . ja  v a  2s. com
        }
    }
    //Initializing finish array
    finishFlagArray = new int[numberOfProcesses];
    //Initializing vector class
    VectorClass.initialize(numberOfProcesses);
    //Fill the arrays with zero false value
    for (int o = 0; o < numberOfProcesses; o++) {
        finishFlagArray[o] = 0;
    }
    //Initializing key array and inserting values
    keyArray = new int[numberOfProcesses];
    for (int q = 0; q < numberOfProcesses; q++) {
        if (q >= myProcessId) {
            keyArray[q] = 1;
        }
    }
    filename = filename + Integer.toString(myProcessId) + ".out";
    file = new File(filename);
    file.createNewFile();
    writer = new FileWriter(file);
    // Write clocks to file
    filenameClock = filenameClock + Integer.toString(myProcessId) + ".out";
    fileClock = new File(filenameClock);
    fileClock.createNewFile();
    //writerClock = new FileWriter(fileClock);
    fw = new FileWriter(fileClock);
    bw = new BufferedWriter(fw);
    //
    // Expo mean insert
    csExecutionExpoDelay = new ExponentialDistribution(csExecutionTime);
    interRequestExpoDelay = new ExponentialDistribution(interRequestDelay);
    //
    System.out.println("********************************************************");
    System.out.println("My process id : " + myProcessId);
    System.out.println("Number of processes : " + numberOfProcesses);
    System.out.println("Inter-request delay : " + interRequestDelay);
    System.out.println("Critical section execution time : " + csExecutionTime);
    System.out.println("Maximum number of request : " + maxNumberOfRequest);
    System.out.println(
            "My process name : " + machineNames[myProcessId] + " My port number : " + portNumbers[myProcessId]);
    for (int i = 0; i < numberOfProcesses; i++) {
        System.out.println("Process name : " + machineNames[i] + " Port number : " + portNumbers[i]);
    }
    System.out.println("********************************************************");
    for (int q = 0; q < numberOfProcesses; q++) {
        System.out.println("KeyArray" + q + " - " + keyArray[q]);
    }
    System.out.println("********************************************************");
    //For hosting server localhost
    SctpServerChannel sctpServerChannel = SctpServerChannel.open();
    InetSocketAddress serverAddr = new InetSocketAddress(portNumbers[myProcessId]);
    sctpServerChannel.bind(serverAddr);
    System.out.println("********************************************************");
    System.out.println("Local server hosted");
    System.out.println("********************************************************");
    //For creating neighbor SCTP channels
    Thread.sleep(30000);
    socketAddress = new SocketAddress[numberOfProcesses];
    sctpChannel = new SctpChannel[numberOfProcesses];
    System.out.println("********************************************************");
    System.out.println("Neighbor channels created");
    System.out.println("********************************************************");
    //Thread spanned for generating critical section request
    new Thread(new RoucairolCarvahloBasicVersion()).start();
    while (true) {
        try (SctpChannel sctpChannelFromClient = sctpServerChannel.accept()) {
            mutex.acquire();
            byteBufferFromNeighbor.clear();
            String receiveMessage;
            MessageInfo messageInfoFromNeighbor = sctpChannelFromClient.receive(byteBufferFromNeighbor, null,
                    null);
            //System.out.println("Raw Message : " + messageInfoFromNeighbor);
            receiveMessage = byteToString(byteBufferFromNeighbor, messageInfoFromNeighbor);
            System.out.println("Received Message : " + receiveMessage);
            if (receiveMessage.contains("Request")) {
                String[] parseMessage = receiveMessage.split("-");
                lamportClock = Math.max(lamportClock, Integer.parseInt(parseMessage[3])) + 1;
                //vector clock update
                String[] stringNumericalTimestamp = parseMessage[4].split(",");
                int[] numericalTimestamp = new int[stringNumericalTimestamp.length];
                for (int d = 0; d < stringNumericalTimestamp.length; d++) {
                    numericalTimestamp[d] = Integer.parseInt(stringNumericalTimestamp[d]);
                }
                VectorClass.update(myProcessId, numericalTimestamp);
                //
                int requestMade = Integer.parseInt(parseMessage[3] + parseMessage[1]);
                if (outstandingRequest == 1) {
                    if (requestMade < currentRequestBeingServed) {
                        lamportClock++;
                        //Newly inserted for vector timesatmp
                        int[] vector = VectorClass.increment(myProcessId);
                        String vectorClockConstruction = "";
                        for (int g = 0; g < vector.length; g++) {
                            if (g == 0) {
                                vectorClockConstruction = vectorClockConstruction + Integer.toString(vector[g]);
                            } else {
                                vectorClockConstruction = vectorClockConstruction + ","
                                        + Integer.toString(vector[g]);
                            }
                        }
                        //
                        keyArray[Integer.parseInt(parseMessage[1])] = 0;
                        try {
                            byteBufferToNeighbor.clear();
                            initializeChannels();
                            sctpChannel[Integer.parseInt(parseMessage[1])]
                                    .connect(socketAddress[Integer.parseInt(parseMessage[1])]);
                            String sendMessage = "Key from Process-" + myProcessId + "-" + requestMade + "-"
                                    + lamportClock + "-" + vectorClockConstruction;
                            System.out.println("Message sent is : " + sendMessage);
                            MessageInfo messageInfoToNeighbor = MessageInfo.createOutgoing(null, 0);
                            byteBufferToNeighbor.put(sendMessage.getBytes());
                            byteBufferToNeighbor.flip();
                            sctpChannel[Integer.parseInt(parseMessage[1])].send(byteBufferToNeighbor,
                                    messageInfoToNeighbor);
                            totalMessageCount++;
                            sctpChannel[Integer.parseInt(parseMessage[1])].close();
                        } catch (IOException ex) {
                            Logger.getLogger(RoucairolCarvahloBasicVersion.class.getName()).log(Level.SEVERE,
                                    null, ex);
                        }
                        //Include block for reverse request
                        lamportClock++;
                        //Newly inserted for vector timesatmp
                        int[] vector1 = VectorClass.increment(myProcessId);
                        String vectorClockConstruction1 = "";
                        for (int g = 0; g < vector1.length; g++) {
                            if (g == 0) {
                                vectorClockConstruction1 = vectorClockConstruction1
                                        + Integer.toString(vector1[g]);
                            } else {
                                vectorClockConstruction1 = vectorClockConstruction1 + ","
                                        + Integer.toString(vector1[g]);
                            }
                        }
                        //
                        try {
                            byteBufferToNeighbor.clear();
                            initializeChannels();
                            sctpChannel[Integer.parseInt(parseMessage[1])]
                                    .connect(socketAddress[Integer.parseInt(parseMessage[1])]);
                            String sendMessage = "ReverseSend from Process-" + myProcessId + "-"
                                    + currentRequestBeingServed + "-" + lamportClock + "-"
                                    + vectorClockConstruction1;
                            System.out.println("Message sent is : " + sendMessage);
                            MessageInfo messageInfoToNeighbor = MessageInfo.createOutgoing(null, 0);
                            byteBufferToNeighbor.put(sendMessage.getBytes());
                            byteBufferToNeighbor.flip();
                            sctpChannel[Integer.parseInt(parseMessage[1])].send(byteBufferToNeighbor,
                                    messageInfoToNeighbor);
                            totalMessageCount++;
                            sctpChannel[Integer.parseInt(parseMessage[1])].close();
                        } catch (IOException ex) {
                            Logger.getLogger(RoucairolCarvahloBasicVersion.class.getName()).log(Level.SEVERE,
                                    null, ex);
                        }
                    } else if (requestMade == currentRequestBeingServed) {
                        if (Integer.parseInt(parseMessage[1]) < myProcessId) {
                            lamportClock++;
                            //Newly inserted for vector timesatmp
                            int[] vector = VectorClass.increment(myProcessId);
                            String vectorClockConstruction = "";
                            for (int g = 0; g < vector.length; g++) {
                                if (g == 0) {
                                    vectorClockConstruction = vectorClockConstruction
                                            + Integer.toString(vector[g]);
                                } else {
                                    vectorClockConstruction = vectorClockConstruction + ","
                                            + Integer.toString(vector[g]);
                                }
                            }
                            //
                            keyArray[Integer.parseInt(parseMessage[1])] = 0;
                            try {
                                byteBufferToNeighbor.clear();
                                initializeChannels();
                                sctpChannel[Integer.parseInt(parseMessage[1])]
                                        .connect(socketAddress[Integer.parseInt(parseMessage[1])]);
                                String sendMessage = "Key from Process-" + myProcessId + "-" + requestMade + "-"
                                        + lamportClock + "-" + vectorClockConstruction;
                                System.out.println("Message sent is : " + sendMessage);
                                MessageInfo messageInfoToNeighbor = MessageInfo.createOutgoing(null, 0);
                                byteBufferToNeighbor.put(sendMessage.getBytes());
                                byteBufferToNeighbor.flip();
                                sctpChannel[Integer.parseInt(parseMessage[1])].send(byteBufferToNeighbor,
                                        messageInfoToNeighbor);
                                totalMessageCount++;
                                sctpChannel[Integer.parseInt(parseMessage[1])].close();
                            } catch (IOException ex) {
                                Logger.getLogger(RoucairolCarvahloBasicVersion.class.getName())
                                        .log(Level.SEVERE, null, ex);
                            }
                            //Include block for reverse request
                            lamportClock++;
                            //Newly inserted for vector timesatmp
                            int[] vector1 = VectorClass.increment(myProcessId);
                            String vectorClockConstruction1 = "";
                            for (int g = 0; g < vector1.length; g++) {
                                if (g == 0) {
                                    vectorClockConstruction1 = vectorClockConstruction1
                                            + Integer.toString(vector1[g]);
                                } else {
                                    vectorClockConstruction1 = vectorClockConstruction1 + ","
                                            + Integer.toString(vector1[g]);
                                }
                            }
                            //
                            try {
                                byteBufferToNeighbor.clear();
                                initializeChannels();
                                sctpChannel[Integer.parseInt(parseMessage[1])]
                                        .connect(socketAddress[Integer.parseInt(parseMessage[1])]);
                                String sendMessage = "ReverseSend from Process-" + myProcessId + "-"
                                        + currentRequestBeingServed + "-" + lamportClock + "-"
                                        + vectorClockConstruction1;
                                System.out.println("Message sent is : " + sendMessage);
                                MessageInfo messageInfoToNeighbor = MessageInfo.createOutgoing(null, 0);
                                byteBufferToNeighbor.put(sendMessage.getBytes());
                                byteBufferToNeighbor.flip();
                                sctpChannel[Integer.parseInt(parseMessage[1])].send(byteBufferToNeighbor,
                                        messageInfoToNeighbor);
                                totalMessageCount++;
                                sctpChannel[Integer.parseInt(parseMessage[1])].close();
                            } catch (IOException ex) {
                                Logger.getLogger(RoucairolCarvahloBasicVersion.class.getName())
                                        .log(Level.SEVERE, null, ex);
                            }
                        } else if (myProcessId < Integer.parseInt(parseMessage[1])) {
                            queue.add(requestMade);
                        }
                    } else if (requestMade > currentRequestBeingServed) {
                        queue.add(requestMade);
                    }
                } else if (outstandingRequest == 0) {
                    lamportClock++;
                    //Newly inserted for vector timesatmp
                    int[] vector = VectorClass.increment(myProcessId);
                    String vectorClockConstruction = "";
                    for (int g = 0; g < vector.length; g++) {
                        if (g == 0) {
                            vectorClockConstruction = vectorClockConstruction + Integer.toString(vector[g]);
                        } else {
                            vectorClockConstruction = vectorClockConstruction + ","
                                    + Integer.toString(vector[g]);
                        }
                    }
                    //
                    keyArray[Integer.parseInt(parseMessage[1])] = 0;
                    try {
                        byteBufferToNeighbor.clear();
                        initializeChannels();
                        sctpChannel[Integer.parseInt(parseMessage[1])]
                                .connect(socketAddress[Integer.parseInt(parseMessage[1])]);
                        String sendMessage = "Key from Process-" + myProcessId + "-" + requestMade + "-"
                                + lamportClock + "-" + vectorClockConstruction;
                        System.out.println("Message sent is : " + sendMessage);
                        MessageInfo messageInfoToNeighbor = MessageInfo.createOutgoing(null, 0);
                        byteBufferToNeighbor.put(sendMessage.getBytes());
                        byteBufferToNeighbor.flip();
                        sctpChannel[Integer.parseInt(parseMessage[1])].send(byteBufferToNeighbor,
                                messageInfoToNeighbor);
                        totalMessageCount++;
                        sctpChannel[Integer.parseInt(parseMessage[1])].close();
                    } catch (IOException ex) {
                        Logger.getLogger(RoucairolCarvahloBasicVersion.class.getName()).log(Level.SEVERE, null,
                                ex);
                    }
                }
            } else if (receiveMessage.contains("Key")) {
                //receive check condition execute critical section block
                String[] parseMessage = receiveMessage.split("-");
                lamportClock = Math.max(lamportClock, Integer.parseInt(parseMessage[3])) + 1;
                //vector clock update
                String[] stringNumericalTimestamp = parseMessage[4].split(",");
                int[] numericalTimestamp = new int[stringNumericalTimestamp.length];
                for (int d = 0; d < stringNumericalTimestamp.length; d++) {
                    numericalTimestamp[d] = Integer.parseInt(stringNumericalTimestamp[d]);
                }
                VectorClass.update(myProcessId, numericalTimestamp);
                //
                keyArray[Integer.parseInt(parseMessage[1])] = 1;
                int countOnes = 0;
                for (int y = 0; y < numberOfProcesses; y++) {
                    if (keyArray[y] == 1) {
                        countOnes = countOnes + 1;
                    }
                }
                if (countOnes == numberOfProcesses) {
                    outstandingRequest = 0;
                    currentRequestBeingServed = 0;
                    enterCriticalSectionExecution();
                    timestamp2 = new Timestamp(System.currentTimeMillis());
                    csExit();
                }
            } else if (receiveMessage.contains("ReverseSend")) {
                String[] parseMessage = receiveMessage.split("-");
                lamportClock = Math.max(lamportClock, Integer.parseInt(parseMessage[3])) + 1;
                //vector clock update
                String[] stringNumericalTimestamp = parseMessage[4].split(",");
                int[] numericalTimestamp = new int[stringNumericalTimestamp.length];
                for (int d = 0; d < stringNumericalTimestamp.length; d++) {
                    numericalTimestamp[d] = Integer.parseInt(stringNumericalTimestamp[d]);
                }
                VectorClass.update(myProcessId, numericalTimestamp);
                //
                int requestMade = Integer.parseInt(parseMessage[2]);
                if (outstandingRequest == 1) {
                    if (requestMade < currentRequestBeingServed) {
                        lamportClock++;
                        //Newly inserted for vector timesatmp
                        int[] vector = VectorClass.increment(myProcessId);
                        String vectorClockConstruction = "";
                        for (int g = 0; g < vector.length; g++) {
                            if (g == 0) {
                                vectorClockConstruction = vectorClockConstruction + Integer.toString(vector[g]);
                            } else {
                                vectorClockConstruction = vectorClockConstruction + ","
                                        + Integer.toString(vector[g]);
                            }
                        }
                        //
                        keyArray[Integer.parseInt(parseMessage[1])] = 0;
                        try {
                            byteBufferToNeighbor.clear();
                            initializeChannels();
                            sctpChannel[Integer.parseInt(parseMessage[1])]
                                    .connect(socketAddress[Integer.parseInt(parseMessage[1])]);
                            String sendMessage = "Key from Process-" + myProcessId + "-" + requestMade + "-"
                                    + lamportClock + "-" + vectorClockConstruction;
                            System.out.println("Message sent is : " + sendMessage);
                            MessageInfo messageInfoToNeighbor = MessageInfo.createOutgoing(null, 0);
                            byteBufferToNeighbor.put(sendMessage.getBytes());
                            byteBufferToNeighbor.flip();
                            sctpChannel[Integer.parseInt(parseMessage[1])].send(byteBufferToNeighbor,
                                    messageInfoToNeighbor);
                            totalMessageCount++;
                            sctpChannel[Integer.parseInt(parseMessage[1])].close();
                        } catch (IOException ex) {
                            Logger.getLogger(RoucairolCarvahloBasicVersion.class.getName()).log(Level.SEVERE,
                                    null, ex);
                        }
                        //Include block for reverse request
                        lamportClock++;
                        //Newly inserted for vector timesatmp
                        int[] vector1 = VectorClass.increment(myProcessId);
                        String vectorClockConstruction1 = "";
                        for (int g = 0; g < vector1.length; g++) {
                            if (g == 0) {
                                vectorClockConstruction1 = vectorClockConstruction1
                                        + Integer.toString(vector1[g]);
                            } else {
                                vectorClockConstruction1 = vectorClockConstruction1 + ","
                                        + Integer.toString(vector1[g]);
                            }
                        }
                        //
                        try {
                            byteBufferToNeighbor.clear();
                            initializeChannels();
                            sctpChannel[Integer.parseInt(parseMessage[1])]
                                    .connect(socketAddress[Integer.parseInt(parseMessage[1])]);
                            String sendMessage = "ReverseSend from Process-" + myProcessId + "-"
                                    + currentRequestBeingServed + "-" + lamportClock + "-"
                                    + vectorClockConstruction1;
                            System.out.println("Message sent is : " + sendMessage);
                            MessageInfo messageInfoToNeighbor = MessageInfo.createOutgoing(null, 0);
                            byteBufferToNeighbor.put(sendMessage.getBytes());
                            byteBufferToNeighbor.flip();
                            sctpChannel[Integer.parseInt(parseMessage[1])].send(byteBufferToNeighbor,
                                    messageInfoToNeighbor);
                            totalMessageCount++;
                            sctpChannel[Integer.parseInt(parseMessage[1])].close();
                        } catch (IOException ex) {
                            Logger.getLogger(RoucairolCarvahloBasicVersion.class.getName()).log(Level.SEVERE,
                                    null, ex);
                        }
                    } else if (requestMade == currentRequestBeingServed) {
                        if (Integer.parseInt(parseMessage[1]) < myProcessId) {
                            lamportClock++;
                            //Newly inserted for vector timesatmp
                            int[] vector = VectorClass.increment(myProcessId);
                            String vectorClockConstruction = "";
                            for (int g = 0; g < vector.length; g++) {
                                if (g == 0) {
                                    vectorClockConstruction = vectorClockConstruction
                                            + Integer.toString(vector[g]);
                                } else {
                                    vectorClockConstruction = vectorClockConstruction + ","
                                            + Integer.toString(vector[g]);
                                }
                            }
                            //
                            keyArray[Integer.parseInt(parseMessage[1])] = 0;
                            try {
                                byteBufferToNeighbor.clear();
                                initializeChannels();
                                sctpChannel[Integer.parseInt(parseMessage[1])]
                                        .connect(socketAddress[Integer.parseInt(parseMessage[1])]);
                                String sendMessage = "Key from Process-" + myProcessId + "-" + requestMade + "-"
                                        + lamportClock + "-" + vectorClockConstruction;
                                System.out.println("Message sent is : " + sendMessage);
                                MessageInfo messageInfoToNeighbor = MessageInfo.createOutgoing(null, 0);
                                byteBufferToNeighbor.put(sendMessage.getBytes());
                                byteBufferToNeighbor.flip();
                                sctpChannel[Integer.parseInt(parseMessage[1])].send(byteBufferToNeighbor,
                                        messageInfoToNeighbor);
                                totalMessageCount++;
                                sctpChannel[Integer.parseInt(parseMessage[1])].close();
                            } catch (IOException ex) {
                                Logger.getLogger(RoucairolCarvahloBasicVersion.class.getName())
                                        .log(Level.SEVERE, null, ex);
                            }
                            //Include block for reverse request
                            lamportClock++;
                            //Newly inserted for vector timesatmp
                            int[] vector1 = VectorClass.increment(myProcessId);
                            String vectorClockConstruction1 = "";
                            for (int g = 0; g < vector1.length; g++) {
                                if (g == 0) {
                                    vectorClockConstruction1 = vectorClockConstruction1
                                            + Integer.toString(vector1[g]);
                                } else {
                                    vectorClockConstruction1 = vectorClockConstruction1 + ","
                                            + Integer.toString(vector1[g]);
                                }
                            }
                            //
                            try {
                                byteBufferToNeighbor.clear();
                                initializeChannels();
                                sctpChannel[Integer.parseInt(parseMessage[1])]
                                        .connect(socketAddress[Integer.parseInt(parseMessage[1])]);
                                String sendMessage = "ReverseSend from Process-" + myProcessId + "-"
                                        + currentRequestBeingServed + "-" + lamportClock + "-"
                                        + vectorClockConstruction1;
                                System.out.println("Message sent is : " + sendMessage);
                                MessageInfo messageInfoToNeighbor = MessageInfo.createOutgoing(null, 0);
                                byteBufferToNeighbor.put(sendMessage.getBytes());
                                byteBufferToNeighbor.flip();
                                sctpChannel[Integer.parseInt(parseMessage[1])].send(byteBufferToNeighbor,
                                        messageInfoToNeighbor);
                                totalMessageCount++;
                                sctpChannel[Integer.parseInt(parseMessage[1])].close();
                            } catch (IOException ex) {
                                Logger.getLogger(RoucairolCarvahloBasicVersion.class.getName())
                                        .log(Level.SEVERE, null, ex);
                            }
                        } else if (myProcessId < Integer.parseInt(parseMessage[1])) {
                            queue.add(requestMade);
                        }
                    } else if (requestMade > currentRequestBeingServed) {
                        queue.add(requestMade);
                    }
                } else if (outstandingRequest == 0) {
                    lamportClock++;
                    //Newly inserted for vector timesatmp
                    int[] vector = VectorClass.increment(myProcessId);
                    String vectorClockConstruction = "";
                    for (int g = 0; g < vector.length; g++) {
                        if (g == 0) {
                            vectorClockConstruction = vectorClockConstruction + Integer.toString(vector[g]);
                        } else {
                            vectorClockConstruction = vectorClockConstruction + ","
                                    + Integer.toString(vector[g]);
                        }
                    }
                    //
                    keyArray[Integer.parseInt(parseMessage[1])] = 0;
                    try {
                        byteBufferToNeighbor.clear();
                        initializeChannels();
                        sctpChannel[Integer.parseInt(parseMessage[1])]
                                .connect(socketAddress[Integer.parseInt(parseMessage[1])]);
                        String sendMessage = "Key from Process-" + myProcessId + "-" + requestMade + "-"
                                + lamportClock + "-" + vectorClockConstruction;
                        System.out.println("Message sent is : " + sendMessage);
                        MessageInfo messageInfoToNeighbor = MessageInfo.createOutgoing(null, 0);
                        byteBufferToNeighbor.put(sendMessage.getBytes());
                        byteBufferToNeighbor.flip();
                        sctpChannel[Integer.parseInt(parseMessage[1])].send(byteBufferToNeighbor,
                                messageInfoToNeighbor);
                        totalMessageCount++;
                        sctpChannel[Integer.parseInt(parseMessage[1])].close();
                    } catch (IOException ex) {
                        Logger.getLogger(RoucairolCarvahloBasicVersion.class.getName()).log(Level.SEVERE, null,
                                ex);
                    }
                }
            } else if (receiveMessage.contains("Finish")) {
                String[] parseMessage = receiveMessage.split("-");
                lamportClock = Math.max(lamportClock, Integer.parseInt(parseMessage[3])) + 1;
                //vector clock update
                String[] stringNumericalTimestamp = parseMessage[4].split(",");
                int[] numericalTimestamp = new int[stringNumericalTimestamp.length];
                for (int d = 0; d < stringNumericalTimestamp.length; d++) {
                    numericalTimestamp[d] = Integer.parseInt(stringNumericalTimestamp[d]);
                }
                VectorClass.update(myProcessId, numericalTimestamp);
                //
                finishFlagArray[Integer.parseInt(parseMessage[1])] = 1;
                int count = 0;
                for (int v = 0; v < numberOfProcesses; v++) {
                    if (finishFlagArray[v] == 1) {
                        count = count + 1;
                    }
                }
                if (count == numberOfProcesses) {
                    break;
                }
            }
        }
        mutex.release();
    }
}

From source file:herddb.cli.HerdDBCLI.java

public static void main(String... args) throws IOException {
    try {//ww  w.  j a  v  a2s .  c o  m
        DefaultParser parser = new DefaultParser();
        Options options = new Options();
        options.addOption("x", "url", true, "JDBC URL");
        options.addOption("u", "username", true, "JDBC Username");
        options.addOption("pwd", "password", true, "JDBC Password");
        options.addOption("q", "query", true, "Execute inline query");
        options.addOption("v", "verbose", false, "Verbose output");
        options.addOption("a", "async", false, "Use (experimental) executeBatchAsync for sending DML");
        options.addOption("s", "schema", true, "Default tablespace (SQL schema)");
        options.addOption("fi", "filter", true, "SQL filter mode: all|ddl|dml");
        options.addOption("f", "file", true, "SQL Script to execute (statement separated by 'GO' lines)");
        options.addOption("at", "autotransaction", false,
                "Execute scripts in autocommit=false mode and commit automatically");
        options.addOption("atbs", "autotransactionbatchsize", true, "Batch size for 'autotransaction' mode");
        options.addOption("g", "script", true, "Groovy Script to execute");
        options.addOption("i", "ignoreerrors", false, "Ignore SQL Errors during file execution");
        options.addOption("sc", "sqlconsole", false, "Execute SQL console in interactive mode");
        options.addOption("fmd", "mysql", false,
                "Intruct the parser that the script is coming from a MySQL Dump");
        options.addOption("rwst", "rewritestatements", false, "Rewrite all statements to use JDBC parameters");
        options.addOption("b", "backup", false, "Backup one or more tablespaces (selected with --schema)");
        options.addOption("r", "restore", false, "Restore tablespace");
        options.addOption("nl", "newleader", true, "Leader for new restored tablespace");
        options.addOption("ns", "newschema", true, "Name for new restored tablespace");
        options.addOption("tsm", "tablespacemapper", true,
                "Path to groovy script with a custom functin to map table names to tablespaces");
        options.addOption("dfs", "dumpfetchsize", true,
                "Fetch size for dump operations. Defaults to chunks of 100000 records");
        options.addOption("n", "nodeid", true, "Node id");
        options.addOption("t", "table", true, "Table name");
        options.addOption("p", "param", true, "Parameter name");
        options.addOption("val", "values", true, "Parameter values");
        options.addOption("lts", "list-tablespaces", false, "List available tablespaces");
        options.addOption("ln", "list-nodes", false, "List available nodes");
        options.addOption("sts", "show-tablespace", false,
                "Show full informations about a tablespace (needs -s option)");
        options.addOption("lt", "list-tables", false, "List tablespace tables (needs -s option)");
        options.addOption("st", "show-table", false,
                "Show full informations about a table (needs -s and -t options)");

        options.addOption("ar", "add-replica", false,
                "Add a replica to the tablespace (needs -s and -r options)");
        options.addOption("rr", "remove-replica", false,
                "Remove a replica from the tablespace (needs -s and -r options)");
        options.addOption("adt", "create-tablespace", false, "Create a tablespace (needs -ns and -nl options)");
        options.addOption("at", "alter-tablespace", false,
                "Alter a tablespace (needs -s, -param and --values options)");

        options.addOption("d", "describe", false, "Checks and describes a raw file");
        options.addOption("ft", "filetype", true,
                "Checks and describes a raw file (valid options are txlog, datapage, tablecheckpoint, indexcheckpoint, tablesmetadata");
        options.addOption("mdf", "metadatafile", true,
                "Tables metadata file, required for 'datapage' filetype");
        options.addOption("tsui", "tablespaceuuid", true, "Tablespace UUID, used for describing raw files");

        org.apache.commons.cli.CommandLine commandLine;
        try {
            commandLine = parser.parse(options, args);
        } catch (ParseException error) {
            println("Syntax error: " + error);
            failAndPrintHelp(options);
            return;
        }
        if (args.length == 0) {
            failAndPrintHelp(options);
            return;
        }

        String schema = commandLine.getOptionValue("schema", TableSpace.DEFAULT);
        String tablespaceuuid = commandLine.getOptionValue("tablespaceuuid", "");
        final boolean verbose = commandLine.hasOption("verbose");
        final boolean async = commandLine.hasOption("async");
        final String filter = commandLine.getOptionValue("filter", "all");
        if (!verbose) {
            LogManager.getLogManager().reset();
        }
        String file = commandLine.getOptionValue("file", "");
        String tablesmetadatafile = commandLine.getOptionValue("metadatafile", "");
        String table = commandLine.getOptionValue("table", "");
        boolean describe = commandLine.hasOption("describe");
        String filetype = commandLine.getOptionValue("filetype", "");
        if (describe) {
            try {
                if (file.isEmpty()) {
                    throw new IllegalArgumentException("file option is required");
                }
                describeRawFile(tablespaceuuid, table, tablesmetadatafile, file, filetype);
            } catch (Exception error) {
                if (verbose) {
                    error.printStackTrace();
                } else {
                    println("error:" + error);
                }
                exitCode = 1;
            }
            return;
        }
        String url = commandLine.getOptionValue("url", "jdbc:herddb:server:localhost:7000");
        String username = commandLine.getOptionValue("username",
                ClientConfiguration.PROPERTY_CLIENT_USERNAME_DEFAULT);
        String password = commandLine.getOptionValue("password",
                ClientConfiguration.PROPERTY_CLIENT_PASSWORD_DEFAULT);
        String query = commandLine.getOptionValue("query", "");

        boolean backup = commandLine.hasOption("backup");
        boolean restore = commandLine.hasOption("restore");
        String newschema = commandLine.getOptionValue("newschema", "");
        String leader = commandLine.getOptionValue("newleader", "");
        String script = commandLine.getOptionValue("script", "");
        String tablespacemapperfile = commandLine.getOptionValue("tablespacemapper", "");
        int dumpfetchsize = Integer.parseInt(commandLine.getOptionValue("dumpfetchsize", 100000 + ""));
        final boolean ignoreerrors = commandLine.hasOption("ignoreerrors");
        boolean sqlconsole = commandLine.hasOption("sqlconsole");
        final boolean frommysqldump = commandLine.hasOption("mysql");
        final boolean rewritestatements = commandLine.hasOption("rewritestatements")
                || !tablespacemapperfile.isEmpty() || frommysqldump;
        boolean autotransaction = commandLine.hasOption("autotransaction") || frommysqldump;
        int autotransactionbatchsize = Integer
                .parseInt(commandLine.getOptionValue("autotransactionbatchsize", 100000 + ""));
        if (!autotransaction) {
            autotransactionbatchsize = 0;
        }

        String nodeId = commandLine.getOptionValue("nodeid", "");
        String param = commandLine.getOptionValue("param", "");
        String values = commandLine.getOptionValue("values", "");

        boolean listTablespaces = commandLine.hasOption("list-tablespaces");
        boolean listNodes = commandLine.hasOption("list-nodes");
        boolean showTablespace = commandLine.hasOption("show-tablespace");
        boolean listTables = commandLine.hasOption("list-tables");
        boolean showTable = commandLine.hasOption("show-table");
        if (showTable) {
            if (table.equals("")) {
                println("Specify the table (-t <table>)");
                exitCode = 1;
                System.exit(exitCode);
            }
        }

        boolean createTablespace = commandLine.hasOption("create-tablespace");
        if (createTablespace) {
            if (newschema.equals("")) {
                println("Specify the tablespace name (--newschema <schema>)");
                exitCode = 1;
                System.exit(exitCode);
            }
            if (leader.equals("")) {
                println("Specify the leader node (--newleader <nodeid>)");
                exitCode = 1;
                System.exit(exitCode);
            }
        }
        boolean alterTablespace = commandLine.hasOption("alter-tablespace");
        if (alterTablespace) {
            if (commandLine.getOptionValue("schema", null) == null) {
                println("Cowardly refusing to assume the default schema in an alter command. Explicitly use \"-s "
                        + TableSpace.DEFAULT + "\" instead");
                exitCode = 1;
                System.exit(exitCode);
            }
            if (param.equals("")) {
                println("Specify the parameter (--param <par>)");
                exitCode = 1;
                System.exit(exitCode);
            }
            if (values.equals("")) {
                println("Specify values (--values <vals>)");
                exitCode = 1;
                System.exit(exitCode);
            }

        }
        boolean addReplica = commandLine.hasOption("add-replica");
        if (addReplica) {
            if (commandLine.getOptionValue("schema", null) == null) {
                println("Cowardly refusing to assume the default schema in an alter command. Explicitly use \"-s "
                        + TableSpace.DEFAULT + "\" instead");
                exitCode = 1;
                System.exit(exitCode);
            }
            if (nodeId.equals("")) {
                println("Specify the node (-n <nodeid>)");
                exitCode = 1;
                System.exit(exitCode);
            }
        }
        boolean removeReplica = commandLine.hasOption("remove-replica");
        if (removeReplica) {
            if (commandLine.getOptionValue("schema", null) == null) {
                println("Cowardly refusing to assume the default schema in an alter command. Explicitly use \"-s "
                        + TableSpace.DEFAULT + "\" instead");
                exitCode = 1;
                System.exit(exitCode);
            }
            if (nodeId.equals("")) {
                println("Specify the node (-n <nodeid>)");
                exitCode = 1;
                System.exit(exitCode);
            }
        }

        TableSpaceMapper tableSpaceMapper = buildTableSpaceMapper(tablespacemapperfile);
        try (HerdDBDataSource datasource = new HerdDBDataSource()) {
            datasource.setUrl(url);
            datasource.setUsername(username);
            datasource.setPassword(password);

            try (Connection connection = datasource.getConnection();
                    Statement statement = connection.createStatement()) {
                connection.setSchema(schema);
                if (sqlconsole) {
                    runSqlConsole(connection, statement, PRETTY_PRINT);
                } else if (backup) {
                    performBackup(statement, schema, file, options, connection, dumpfetchsize);
                } else if (restore) {
                    performRestore(file, leader, newschema, options, statement, connection);
                } else if (!query.isEmpty()) {
                    executeStatement(verbose, ignoreerrors, false, false, query, statement, tableSpaceMapper,
                            false, PRETTY_PRINT);
                } else if (!file.isEmpty()) {
                    executeSqlFile(autotransactionbatchsize, connection, file, verbose, async, ignoreerrors,
                            frommysqldump, rewritestatements, statement, tableSpaceMapper, PRETTY_PRINT, filter,
                            datasource);
                } else if (!script.isEmpty()) {
                    executeScript(connection, datasource, statement, script);
                } else if (listTablespaces) {
                    printTableSpaces(verbose, ignoreerrors, statement, tableSpaceMapper);
                } else if (listNodes) {
                    printNodes(verbose, ignoreerrors, statement, tableSpaceMapper);
                } else if (showTablespace) {
                    printTableSpaceInfos(verbose, ignoreerrors, statement, tableSpaceMapper, schema);
                } else if (listTables) {
                    listTables(verbose, ignoreerrors, statement, tableSpaceMapper, schema);
                } else if (showTable) {
                    printTableInfos(verbose, ignoreerrors, statement, tableSpaceMapper, schema, table);
                } else if (addReplica) {
                    changeReplica(verbose, ignoreerrors, statement, tableSpaceMapper, schema, nodeId,
                            ChangeReplicaAction.ADD);
                } else if (removeReplica) {
                    changeReplica(verbose, ignoreerrors, statement, tableSpaceMapper, schema, nodeId,
                            ChangeReplicaAction.REMOVE);
                } else if (createTablespace) {
                    createTablespace(verbose, ignoreerrors, statement, tableSpaceMapper, newschema, leader);
                } else if (alterTablespace) {
                    alterTablespace(verbose, ignoreerrors, statement, tableSpaceMapper, schema, param, values);
                } else {
                    failAndPrintHelp(options);
                    return;
                }
            }
            exitCode = 0;
        } catch (Exception error) {
            if (verbose) {
                error.printStackTrace();
            } else {
                println("error:" + error);
            }
            exitCode = 1;
        }
    } finally {
        System.exit(exitCode);
    }
}

From source file:edu.cmu.lti.oaqa.apps.Client.java

public static void main(String args[]) {
    BufferedReader inp = new BufferedReader(new InputStreamReader(System.in));

    Options opt = new Options();

    Option o = new Option(PORT_SHORT_PARAM, PORT_LONG_PARAM, true, PORT_DESC);
    o.setRequired(true);/*from   w w w .  ja v  a2 s. co m*/
    opt.addOption(o);
    o = new Option(HOST_SHORT_PARAM, HOST_LONG_PARAM, true, HOST_DESC);
    o.setRequired(true);
    opt.addOption(o);
    opt.addOption(K_SHORT_PARAM, K_LONG_PARAM, true, K_DESC);
    opt.addOption(R_SHORT_PARAM, R_LONG_PARAM, true, R_DESC);
    opt.addOption(QUERY_TIME_SHORT_PARAM, QUERY_TIME_LONG_PARAM, true, QUERY_TIME_DESC);
    opt.addOption(RET_OBJ_SHORT_PARAM, RET_OBJ_LONG_PARAM, false, RET_OBJ_DESC);
    opt.addOption(RET_EXTERN_ID_SHORT_PARAM, RET_EXTERN_ID_LONG_PARAM, false, RET_EXTERN_ID_DESC);

    CommandLineParser parser = new org.apache.commons.cli.GnuParser();

    try {
        CommandLine cmd = parser.parse(opt, args);

        String host = cmd.getOptionValue(HOST_SHORT_PARAM);

        String tmp = null;

        tmp = cmd.getOptionValue(PORT_SHORT_PARAM);

        int port = -1;

        try {
            port = Integer.parseInt(tmp);
        } catch (NumberFormatException e) {
            Usage("Port should be integer!");
        }

        boolean retObj = cmd.hasOption(RET_OBJ_SHORT_PARAM);
        boolean retExternId = cmd.hasOption(RET_EXTERN_ID_SHORT_PARAM);

        String queryTimeParams = cmd.getOptionValue(QUERY_TIME_SHORT_PARAM);
        if (null == queryTimeParams)
            queryTimeParams = "";

        SearchType searchType = SearchType.kKNNSearch;
        int k = 0;
        double r = 0;

        if (cmd.hasOption(K_SHORT_PARAM)) {
            if (cmd.hasOption(R_SHORT_PARAM)) {
                Usage("Range search is not allowed if the KNN search is specified!");
            }
            tmp = cmd.getOptionValue(K_SHORT_PARAM);
            try {
                k = Integer.parseInt(tmp);
            } catch (NumberFormatException e) {
                Usage("K should be integer!");
            }
            searchType = SearchType.kKNNSearch;
        } else if (cmd.hasOption(R_SHORT_PARAM)) {
            if (cmd.hasOption(K_SHORT_PARAM)) {
                Usage("KNN search is not allowed if the range search is specified!");
            }
            searchType = SearchType.kRangeSearch;
            tmp = cmd.getOptionValue(R_SHORT_PARAM);
            try {
                r = Double.parseDouble(tmp);
            } catch (NumberFormatException e) {
                Usage("The range value should be numeric!");
            }
        } else {
            Usage("One has to specify either range or KNN-search parameter");
        }

        String separator = System.getProperty("line.separator");

        StringBuffer sb = new StringBuffer();
        String s;

        while ((s = inp.readLine()) != null) {
            sb.append(s);
            sb.append(separator);
        }

        String queryObj = sb.toString();

        try {
            TTransport transport = new TSocket(host, port);
            transport.open();

            TProtocol protocol = new TBinaryProtocol(transport);
            QueryService.Client client = new QueryService.Client(protocol);

            if (!queryTimeParams.isEmpty())
                client.setQueryTimeParams(queryTimeParams);

            List<ReplyEntry> res = null;

            long t1 = System.nanoTime();

            if (searchType == SearchType.kKNNSearch) {
                System.out.println(String.format("Running a %d-NN search", k));
                res = client.knnQuery(k, queryObj, retExternId, retObj);
            } else {
                System.out.println(String.format("Running a range search (r=%g)", r));
                res = client.rangeQuery(r, queryObj, retExternId, retObj);
            }

            long t2 = System.nanoTime();

            System.out.println(String.format("Finished in %g ms", (t2 - t1) / 1e6));

            for (ReplyEntry e : res) {
                System.out.println(String.format("id=%d dist=%g %s", e.getId(), e.getDist(),
                        retExternId ? "externId=" + e.getExternId() : ""));
                if (retObj)
                    System.out.println(e.getObj());
            }

            transport.close(); // Close transport/socket !
        } catch (TException te) {
            System.err.println("Apache Thrift exception: " + te);
            te.printStackTrace();
        }

    } catch (ParseException e) {
        Usage("Cannot parse arguments");
    } catch (Exception e) {
        e.printStackTrace();
        System.exit(1);
    }
}

From source file:IMAPService.java

public static void main(String[] args) {
    // Arguments//  w  w w.  j ava 2  s .  co m
    String server = "";
    int port = -1;
    String login = "";
    String password = "";
    boolean deleteAfterDownload = false;
    boolean downloadAll = false;
    String[] foldersToDownload = null;

    // Set up apache cli
    Options options = new Options();

    Option S = new Option("S", true, "Server Name");
    S.setRequired(true);
    options.addOption(S);

    Option P = new Option("P", true, "Port Number");
    P.setRequired(true);
    options.addOption(P);

    Option l = new Option("l", true, "Login");
    l.setRequired(true);
    options.addOption(l);

    Option p = new Option("p", true, "Password if not on stdin");
    options.addOption(p);

    Option d = new Option("d", false, "Delete after downloading");
    d.setRequired(false);
    options.addOption(d);

    Option a = new Option("a", false, "Download from all folders");
    a.setRequired(false);
    options.addOption(a);

    Option f = new Option("f", true, "Download messages from specified folder");
    options.addOption(f);

    CommandLineParser clp = new DefaultParser();

    try {
        CommandLine cl = clp.parse(options, args);

        if (cl.hasOption("S"))
            server = cl.getOptionValue("S");

        if (cl.hasOption("P"))
            port = Integer.parseInt(cl.getOptionValue("P"));

        if (cl.hasOption("l"))
            login = cl.getOptionValue("l");

        if (cl.hasOption("p"))
            password = cl.getOptionValue("p");

        if (cl.hasOption("d"))
            deleteAfterDownload = true;

        if (cl.hasOption("a"))
            downloadAll = true;
        else if (cl.hasOption("f"))
            foldersToDownload = cl.getOptionValues("f");
        else {
            showArgMenu(options);
            return;
        }

    } catch (Exception e) {
        showArgMenu(options);
        return;
    }

    if (password.isEmpty()) {
        // Grab p/w of stdin if it's there
        Scanner sc = new Scanner(System.in);

        password = sc.nextLine();

        sc.close();
    }

    if (!server.isEmpty() && port != -1 && !login.isEmpty() && !password.isEmpty()
            && (downloadAll || foldersToDownload != null)) {
        //IMAPService imapService = new IMAPService("imap.gmail.com", 993);
        IMAPService imapService = new IMAPService(server, port);
        //imapService.login("kinglibingli@gmail.com", "kingli1bingli");
        imapService.login(login, password);
        imapService.buildMailbox();
        // Check if service should delete emails
        if (deleteAfterDownload)
            imapService.deleteEmailsAfterDownload();
        // Proceed with downloads
        if (downloadAll)
            imapService.downloadAllFolders();
        else {
            for (String folderNames : foldersToDownload) {
                imapService.downloadFoldersEmails(folderNames);
            }
        }

        imapService.logout();
    }

}

From source file:ca.uqac.info.monitor.BeepBeepMonitor.java

public static void main(String[] args) {
    int verbosity = 1, slowdown = 0, tcp_port = 0;
    boolean show_stats = false, to_stdout = false;
    String trace_filename = "", pipe_filename = "", event_name = "message";
    final MonitorFactory mf = new MonitorFactory();

    // In case we open a socket
    ServerSocket m_serverSocket = null;
    Socket m_connection = null;/*from  w  ww .  ja va 2s. co m*/

    // Parse command line arguments
    Options options = setupOptions();
    CommandLine c_line = setupCommandLine(args, options);
    assert c_line != null;
    if (c_line.hasOption("verbosity")) {
        verbosity = Integer.parseInt(c_line.getOptionValue("verbosity"));
    }
    if (verbosity > 0) {
        showHeader();
    }
    if (c_line.hasOption("version")) {
        System.err.println("(C) 2008-2013 Sylvain Hall et al., Universit du Qubec  Chicoutimi");
        System.err.println("This program comes with ABSOLUTELY NO WARRANTY.");
        System.err.println("This is a free software, and you are welcome to redistribute it");
        System.err.println("under certain conditions. See the file COPYING for details.\n");
        System.exit(ERR_OK);
    }
    if (c_line.hasOption("h")) {
        showUsage(options);
        System.exit(ERR_OK);
    }
    if (c_line.hasOption("version")) {
        System.exit(ERR_OK);
    }
    if (c_line.hasOption("slowdown")) {
        slowdown = Integer.parseInt(c_line.getOptionValue("slowdown"));
        if (verbosity > 0)
            System.err.println("Slowdown factor: " + slowdown + " ms");
    }
    if (c_line.hasOption("stats")) {
        show_stats = true;
    }
    if (c_line.hasOption("csv")) {
        // Will output data in CSV format to stdout
        to_stdout = true;
    }
    if (c_line.hasOption("eventname")) {
        // Set event name
        event_name = c_line.getOptionValue("eventname");
    }
    if (c_line.hasOption("t")) {
        // Read events from a trace
        trace_filename = c_line.getOptionValue("t");
    }
    if (c_line.hasOption("p")) {
        // Read events from a pipe
        pipe_filename = c_line.getOptionValue("p");
    }
    if (c_line.hasOption("k")) {
        // Read events from a TCP port
        tcp_port = Integer.parseInt(c_line.getOptionValue("k"));
    }
    if (!trace_filename.isEmpty() && !pipe_filename.isEmpty()) {
        System.err.println("ERROR: you must specify at most one of trace file or named pipe");
        showUsage(options);
        System.exit(ERR_ARGUMENTS);
    }
    @SuppressWarnings("unchecked")
    List<String> remaining_args = c_line.getArgList();
    if (remaining_args.isEmpty()) {
        System.err.println("ERROR: no input formula specified");
        showUsage(options);
        System.exit(ERR_ARGUMENTS);
    }
    // Instantiate the event notifier
    boolean notify = (verbosity > 0);
    EventNotifier en = new EventNotifier(notify);
    en.m_slowdown = slowdown;
    en.m_csvToStdout = to_stdout;
    // Create one monitor for each input file and add it to the notifier 
    for (String formula_filename : remaining_args) {
        try {
            String formula_contents = FileReadWrite.readFile(formula_filename);
            Operator op = Operator.parseFromString(formula_contents);
            op.accept(mf);
            Monitor mon = mf.getMonitor();
            Map<String, String> metadata = getMetadata(formula_contents);
            metadata.put("Filename", formula_filename);
            en.addMonitor(mon, metadata);
        } catch (IOException e) {
            e.printStackTrace();
            System.exit(ERR_IO);
        } catch (Operator.ParseException e) {
            System.err.println("Error parsing input formula");
            System.exit(ERR_PARSE);
        }
    }

    // Read trace and iterate
    // Opens file
    PipeReader pr = null;
    try {
        if (!pipe_filename.isEmpty()) {
            // We tell the pipe reader we read a pipe
            File f = new File(pipe_filename);
            if (verbosity > 0)
                System.err.println("Reading from pipe named " + f.getName());
            pr = new PipeReader(new FileInputStream(f), en, false);
        } else if (!trace_filename.isEmpty()) {
            // We tell the pipe reader we read a regular file
            File f = new File(trace_filename);
            if (verbosity > 0)
                System.err.println("Reading from file " + f.getName());
            pr = new PipeReader(new FileInputStream(f), en, true);
        } else if (tcp_port > 0) {
            // We tell the pipe reader we read from a socket
            if (verbosity > 0)
                System.err.println("Reading from TCP port " + tcp_port);
            m_serverSocket = new ServerSocket(tcp_port);
            m_connection = m_serverSocket.accept();
            pr = new PipeReader(m_connection.getInputStream(), en, false);
        } else {
            // We tell the pipe reader we read from standard input
            if (verbosity > 0)
                System.err.println("Reading from standard input");
            pr = new PipeReader(System.in, en, false);
        }
    } catch (FileNotFoundException ex) {
        // We print both trace and pipe since one of them must be empty
        System.err.println("ERROR: file not found " + trace_filename + pipe_filename);
        System.exit(ERR_IO);
    } catch (IOException e) {
        // Caused by socket error
        e.printStackTrace();
        System.exit(ERR_IO);
    }
    pr.setSeparator("<" + event_name + ">", "</" + event_name + ">");

    // Check parameters for the event notifier
    if (c_line.hasOption("no-trigger")) {
        en.m_notifyOnVerdict = false;
    } else {
        en.m_notifyOnVerdict = true;
    }
    if (c_line.hasOption("mirror")) {
        en.m_mirrorEventsOnStdout = true;
    }

    // Start event notifier
    en.reset();
    Thread th = new Thread(pr);
    long clock_start = System.nanoTime();
    th.start();
    try {
        th.join(); // Wait for thread to finish
    } catch (InterruptedException e1) {
        // Thread is finished
    }
    if (tcp_port > 0 && m_serverSocket != null) {
        // We opened a socket; now we close it
        try {
            m_serverSocket.close();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
    long clock_end = System.nanoTime();
    int ret_code = pr.getReturnCode();
    switch (ret_code) {
    case PipeReader.ERR_EOF:
        if (verbosity > 0)
            System.err.println("\nEnd of file reached");
        break;
    case PipeReader.ERR_EOT:
        if (verbosity > 0)
            System.err.println("\nEOT received on pipe: closing");
        break;
    case PipeReader.ERR_OK:
        // Do nothing
        break;
    default:
        // An error
        System.err.println("Runtime error");
        System.exit(ERR_RUNTIME);
        break;
    }
    if (show_stats) {
        if (verbosity > 0) {
            System.out.println("Messages:   " + en.m_numEvents);
            System.out.println("Time:       " + (int) (en.m_totalTime / 1000000f) + " ms");
            System.out.println("Clock time: " + (int) ((clock_end - clock_start) / 1000000f) + " ms");
            System.out.println("Max heap:   " + (int) (en.heapSize / 1048576f) + " MB");
        } else {
            // If stats are asked but verbosity = 0, only show time value
            // (both monitor and wall clock) 
            System.out.print((int) (en.m_totalTime / 1000000f));
            System.out.print(",");
            System.out.print((int) ((clock_end - clock_start) / 1000000f));
        }
    }
    System.exit(ERR_OK);
}

From source file:main.DOORS_Service.java

/**
 * Login to the DWA server and perform some OSLC actions
 * @param args/*w w  w . j a va 2 s  . com*/
 * @throws ParseException 
 */
public static void main(String[] args) throws ParseException {

    Options options = new Options();

    options.addOption("url", true, "url");
    options.addOption("user", true, "user ID");
    options.addOption("password", true, "password");
    options.addOption("project", true, "project area");

    CommandLineParser cliParser = new GnuParser();

    //Parse the command line
    CommandLine cmd = cliParser.parse(options, args);

    if (!validateOptions(cmd)) {
        logger.severe(
                "Syntax:  java <class_name> -url https://<server>:port/<context>/ -user <user> -password <password> -project \"<project_area>\"");
        logger.severe(
                "Example: java DoorsOauthSample -url https://exmple.com:9443/dwa -user ADMIN -password ADMIN -project \"JKE Banking (Requirements Management)\"");
        return;
    }

    String webContextUrl = cmd.getOptionValue("url");
    String user = cmd.getOptionValue("user");
    String passwd = cmd.getOptionValue("password");
    String projectArea = cmd.getOptionValue("project");

    try {
        //STEP 1: Initialize a Jazz rootservices helper and indicate we're looking for the RequirementManagement catalog
        // The root services for DOORs is found at /public level
        JazzRootServicesHelper helper = new JazzRootServicesHelper(webContextUrl + "/public",
                OSLCConstants.OSLC_RM);

        //STEP 2: Create a new OSLC OAuth capable client
        OslcOAuthClient client = helper.initOAuthClient("JIRA", "JIRA");

        if (client != null) {
            //STEP 3: Try to access the context URL to trigger the OAuth dance and login
            try {
                client.getResource(webContextUrl, OSLCConstants.CT_RDF);
            } catch (OAuthRedirectException oauthE) {
                validateTokens(client,
                        oauthE.getRedirectURL() + "?oauth_token=" + oauthE.getAccessor().requestToken, user,
                        passwd, webContextUrl + "/j_acegi_security_check");
                // Try to access again
                ClientResponse response = client.getResource(webContextUrl, OSLCConstants.CT_RDF);
                response.getEntity(InputStream.class).close();
            }

            //STEP 4: Get our requirements collection that we want
            //TODO: Replace with option from startup
            String serviceProviderUrl = "http://usnx47:8080/dwa/rm/urn:rational::1-4d2b67b464226e12-M-0000048a";
            ClientResponse response = client.getResource(serviceProviderUrl,
                    "application/x-oslc-rm-requirement-collection-1.0+xml");
            //build the rdf
            Model rdfModel = ModelFactory.createDefaultModel();
            rdfModel.read(response.getEntity(InputStream.class), serviceProviderUrl);
            response.consumeContent();

            //get the statements
            List<Statement> reqs = rdfModel.getResource(serviceProviderUrl).listProperties().toList();
            HashMap<String, String> requirements = new HashMap<String, String>();
            for (Statement s : reqs) {

                String reqURI = s.getObject().toString();
                if (reqURI.contains("http")) {
                    response = client.getResource(reqURI, "application/x-oslc-rm-requirement-1.0+xml");
                    if (response.getStatusCode() == 200) {
                        InputStream in = response.getEntity(InputStream.class);
                        Model model = ModelFactory.createDefaultModel();

                        try {
                            model.read(in, reqURI);
                        } catch (Exception sa) {
                            System.out.println(reqURI);
                        }

                        //Properties to traverse on
                        Property attrDef = model
                                .createProperty("http://jazz.net/doors/xmlns/prod/jazz/doors/1.0/attrDef");
                        Property name = model
                                .createProperty("http://jazz.net/doors/xmlns/prod/jazz/doors/1.0/name");

                        //Flags we use for parsing
                        int count = 0;
                        boolean isText = false;
                        boolean isID = false;
                        boolean done = false;
                        //Text of the DOORS Object and its ID are what we are going to extract
                        String text = "";
                        String id = "";

                        //Look through all of the possible fields
                        StmtIterator statementIter = model.listStatements();
                        while (statementIter.hasNext() && done != true) {
                            Statement field = statementIter.next();
                            //Get the attrDef property to find out what kind of value we have
                            StmtIterator props = field.getSubject().listProperties(attrDef);
                            while (props.hasNext() && done != true) {
                                Statement kind = props.next();
                                RDFNode propertyNode = kind.getObject();
                                StmtIterator propIt = propertyNode.asResource().listProperties(name);
                                //Check all of the properties for our desired fields
                                while (propIt.hasNext()) {
                                    Statement node = propIt.next();
                                    if (node.getObject().isLiteral()) {
                                        if (node.getObject().toString().contains("Object+Text")
                                                && field.getObject().isLiteral()) {
                                            text = field.getLiteral().toString();
                                            text = text.substring(0, text.indexOf("^"));
                                            count++;

                                        }
                                        if (node.getObject().toString().contains("Absolute+Number")
                                                && field.getObject().isLiteral()) {
                                            id = field.getLiteral().toString();
                                            id = id.substring(0, id.indexOf("^"));
                                            count++;
                                        }

                                    }
                                }
                                if (count == 2) {
                                    if (!text.isEmpty()) {
                                        //System.out.println( "Req: " + id );
                                        //System.out.println( text );
                                        requirements.put(id, text);
                                        count = 0;
                                        done = true;
                                        break;
                                    }

                                }
                            }

                        }

                    }

                }
                response.consumeContent();
            }
            //check if already in JIRA
            //post to jira
            for (Entry<String, String> e : requirements.entrySet()) {

            }
        }

    } catch (Exception e) {
        logger.log(Level.SEVERE, e.getMessage(), e);
    }

}

From source file:com.mijecu25.sqlplus.SQLPlus.java

public static void main(String[] args) throws IOException {
    // Create and load the properties from the application properties file
    Properties properties = new Properties();
    properties.load(SQLPlus.class.getClassLoader().getResourceAsStream(SQLPlus.APPLICATION_PROPERTIES_FILE));

    SQLPlus.logger.info("Initializing " + SQLPlus.PROGRAM_NAME + " version "
            + properties.getProperty(SQLPlus.APPLICATION_PROPERTIES_FILE_VERSION));

    // Check if the user is using a valid console (i.e. not from Eclipse)
    if (System.console() == null) {
        // The Console object for the JVM could not be found. Alert the user 
        SQLPlus.logger.fatal(Messages.FATAL + "A JVM Console object was not found. Try running "
                + SQLPlus.PROGRAM_NAME + "from the command line");
        System.out.println(//from w  w  w .ja va2 s .c  o  m
                Messages.FATAL + SQLPlus.PROGRAM_NAME + " was not able to find your JVM's Console object. "
                        + "Try running " + SQLPlus.PROGRAM_NAME + " from the command line.");

        SQLPlus.exitSQLPlus();

        SQLPlus.logger.fatal(Messages.FATAL + Messages.QUIT_PROGRAM_ERROR(PROGRAM_NAME));
        return;
    }

    // UI intro
    System.out.println("Welcome to " + SQLPlus.PROGRAM_NAME
            + "! This program has a DSL to add alerts to various SQL DML events.");
    System.out.println("Be sure to use " + SQLPlus.PROGRAM_NAME + " from the command line.");
    System.out.println();

    // Get the version
    System.out.println("Version: " + properties.getProperty(SQLPlus.APPLICATION_PROPERTIES_FILE_VERSION));
    System.out.println();

    // Read the license file
    BufferedReader bufferedReader;
    bufferedReader = new BufferedReader(new FileReader(SQLPlus.LICENSE_FILE));

    // Read a line
    String line = bufferedReader.readLine();

    // While the line is not null
    while (line != null) {
        System.out.println(line);

        // Read a new lines
        line = bufferedReader.readLine();
    }

    // Close the buffer
    bufferedReader.close();
    System.out.println();

    // Create the jline console that allows us to remember commands, use arrow keys, and catch interruptions
    // from the user
    SQLPlus.console = new ConsoleReader();
    SQLPlus.console.setHandleUserInterrupt(true);

    try {
        // Get credentials from the user
        SQLPlus.logger.info("Create SQLPlusConnection");
        SQLPlus.createSQLPlusConnection();
    } catch (NullPointerException | SQLException | IllegalArgumentException e) {
        // NPE: This exception can occur if the user is running the program where the JVM Console
        // object cannot be found.
        // SQLE: TODO should I add here the error code?
        // This exception can occur when trying to establish a connection
        // IAE: This exception can occur when trying to establish a connection
        SQLPlus.logger
                .fatal(Messages.FATAL + Messages.FATAL_EXIT(SQLPlus.PROGRAM_NAME, e.getClass().getName()));
        System.out.println(Messages.FATAL + Messages.FATAL_EXCEPTION_ACTION(e.getClass().getSimpleName()) + " "
                + Messages.CHECK_LOG_FILES);
        SQLPlus.exitSQLPlus();

        SQLPlus.logger.fatal(Messages.FATAL + Messages.QUIT_PROGRAM_ERROR(SQLPlus.PROGRAM_NAME));
        return;
    } catch (UserInterruptException uie) {
        SQLPlus.logger.warn(Messages.WARNING + "The user typed an interrupt instruction.");
        SQLPlus.exitSQLPlus();

        return;
    }

    System.out.println("Connection established! Commands end with " + SQLPlus.END_OF_COMMAND);
    System.out.println("Type " + SQLPlus.EXIT + " or " + SQLPlus.QUIT + " to exit the application ");

    try {
        // Execute the input scanner
        while (true) {
            // Get a line from the user until the hit enter (carriage return, line feed/ new line).
            System.out.print(SQLPlus.PROMPT);
            try {
                line = SQLPlus.console.readLine().trim();
            } catch (NullPointerException npe) {
                // TODO test this behavior
                // If this exception is catch, it is very likely that the user entered the end of line command.
                // This means that the program should quit.
                SQLPlus.logger.warn(Messages.WARNING + "The input from the user is null. It is very likely that"
                        + "the user entered the end of line command and they want to quit.");
                SQLPlus.exitSQLPlus();

                return;
            }

            // If the user did not enter anything
            if (line.isEmpty()) {
                // Continue to the next iteration
                continue;
            }

            if (line.equals(".")) {
                line = "use courses;";
            }

            if (line.equals("-")) {
                line = "select created_at from classes;";
            }

            if (line.equals("--")) {
                line = "select name, year from classes;";
            }

            if (line.equals("*")) {
                line = "select * from classes;";
            }

            if (line.equals("x")) {
                line = "select name from classes, classes;";
            }

            if (line.equals("e")) {
                line = "select * feom classes;";
            }

            // Logic to quit
            if (line.equals(SQLPlus.QUIT) || line.equals(SQLPlus.EXIT)) {
                SQLPlus.logger.info("The user wants to quit " + SQLPlus.PROGRAM_NAME);
                SQLPlus.exitSQLPlus();
                break;
            }

            // Use a StringBuilder since jline works weird when it has read a line. The issue we were having was with the
            // end of command logic. jline does not keep the input from the user in the variable that was stored in. Each
            // time jline reads a new line, the variable is empty
            StringBuilder query = new StringBuilder();
            query.append(line);

            // While the user does not finish the command with the SQLPlus.END_OF_COMMAND
            while (query.charAt(query.length() - 1) != SQLPlus.END_OF_COMMAND) {
                // Print the wait for command prompt and get the next line for the user
                System.out.print(SQLPlus.WAIT_FOR_END_OF_COMMAND);
                query.append(" ");
                line = StringUtils.stripEnd(SQLPlus.console.readLine(), null);
                query.append(line);
            }

            SQLPlus.logger.info("Raw input from the user: " + query);

            try {
                Statement statement;

                try {
                    // Execute the antlr code to parse the user input
                    SQLPlus.logger.info("Will parse the user input to determine what to execute");
                    ANTLRStringStream input = new ANTLRStringStream(query.toString());
                    SQLPlusLex lexer = new SQLPlusLex(input);
                    CommonTokenStream tokens = new CommonTokenStream(lexer);
                    SQLPlusParser parser = new SQLPlusParser(tokens);

                    statement = parser.sqlplus();
                } catch (RecognitionException re) {
                    // TODO move this to somehwere else
                    //                        String message = Messages.WARNING + "You have an error in your SQL syntax. Check the manual"
                    //                                + " that corresponds to your " + SQLPlus.sqlPlusConnection.getCurrentDatabase()
                    //                                + " server or " + SQLPlus.PROGRAM_NAME + " for the correct syntax";
                    //                        SQLPlus.logger.warn(message);
                    //                        System.out.println(message);
                    statement = new StatementDefault();
                }

                statement.setStatement(query.toString());
                SQLPlus.sqlPlusConnection.execute(statement);
            } catch (UnsupportedOperationException uoe) {
                // This exception can occur when the user entered a command allowed by the parsers, but not currently
                // supported by SQLPlus. This can occur because the parser is written in such a way that supports
                // the addition of features.
                SQLPlus.logger.warn(Messages.WARNING + uoe);
                System.out.println(
                        Messages.WARNING + Messages.FATAL_EXCEPTION_ACTION(uoe.getClass().getSimpleName()) + " "
                                + Messages.CHECK_LOG_FILES);
                SQLPlus.logger.warn(Messages.WARNING + "The previous command is not currently supported.");
            }

        }
    } catch (IllegalArgumentException iae) {
        // This exception can occur when a command is executed but it had illegal arguments. Most likely
        // it is a programmer's error and should be addressed by the developer.
        SQLPlus.logger
                .fatal(Messages.FATAL + Messages.FATAL_EXIT(SQLPlus.PROGRAM_NAME, iae.getClass().getName()));
        SQLPlus.exitSQLPlus();

        SQLPlus.logger.fatal(Messages.FATAL + Messages.QUIT_PROGRAM_ERROR(SQLPlus.PROGRAM_NAME));
    } catch (UserInterruptException uie) {
        SQLPlus.logger.warn(Messages.WARNING + "The user typed an interrupt instruction.");
        SQLPlus.exitSQLPlus();
    }
}

From source file:com.github.codingtogenomic.CodingToGenomic.java

public static void main(String[] args) throws Exception {

    //parse commandline
    Options options = new Options();
    CommandLineParser parser = new PosixParser();
    String gene = new String();
    String transcript = new String();
    String species = "human";
    boolean mapCdna = false;
    String coordinate = new String();
    StringBuilder errorMsg = new StringBuilder();
    try {//w  ww . ja  v  a 2s.c  o m
        options = getOptions(args);
    } catch (org.apache.commons.cli.ParseException ex) {
        System.err.println("Parsing failed.  Reason: " + ex.getMessage());
        System.exit(1);
    }
    CommandLine line = parser.parse(options, args);
    if (line.hasOption("help")) {
        showHelp(options);
    }
    if (line.hasOption("gene")) {
        gene = line.getOptionValue("gene");
    } else {
        if (!line.hasOption("transcript")) {
            errorMsg.append("Either --gene or --transcript argument is required\n");
        }
    }
    if (line.hasOption("transcript")) {
        if (line.hasOption("gene")) {
            errorMsg.append("Please specify only one of " + "--gene or --transcript arguments, not both\n");
        } else {
            transcript = line.getOptionValue("transcript");
            if (line.hasOption("species")) {
                System.out.println("Ignoring --species option when using --transcript argument");
            }
        }
    }
    if (line.hasOption("coordinate")) {
        coordinate = line.getOptionValue("coordinate");
    } else {
        errorMsg.append("--coordinate argument is required\n");
    }
    if (line.hasOption("species")) {
        species = line.getOptionValue("species").replaceAll("\\s+", "_");
    }
    if (line.hasOption("b37")) {
        if (species.equalsIgnoreCase("human") || species.equalsIgnoreCase("homo sapiens")) {
            SERVER = GRCh37Server;
        } else {
            System.out.println("--b37 argument will be ignored - it can only be "
                    + "used when human is the species of interest. Current species" + " is " + species + ".\n");
        }
    }
    if (line.hasOption("noncoding")) {
        mapCdna = true;
    }

    if (errorMsg.length() > 0) {
        showHelp(options, errorMsg.toString(), 2);
    }
    int c = 0;
    boolean threePrimeUtr = false;
    String prefix = "c.";
    if (mapCdna) {
        prefix = "n.";
        try {
            c = Integer.parseInt(coordinate);
        } catch (NumberFormatException ex) {
            showHelp(options,
                    "--coordinate argument '" + coordinate + "' could not " + "be parsed as an integer", 2);
        }
    } else if (coordinate.startsWith("*")) {
        threePrimeUtr = true;
        prefix = "c.*";
        String coord = coordinate.replaceFirst("\\*", "");
        try {
            c = Integer.parseInt(coord);
        } catch (NumberFormatException ex) {
            showHelp(options, "--coordinate argument '" + coordinate + "' could not "
                    + "be parsed as an integer or UTR coordinate", 2);
        }
    } else {
        try {
            c = Integer.parseInt(coordinate);
        } catch (NumberFormatException ex) {
            showHelp(options,
                    "--coordinate argument '" + coordinate + "' could not " + "be parsed as an integer", 2);
        }
    }
    //got arguments
    String result;
    String header = "Input\tSymbol\tEnsemblGene\tEnsemblTranscript\tGenomicCoordinate";
    if (!gene.isEmpty()) {
        IdParser idParser = new IdParser(gene);
        System.out.println("Interpretting " + gene + " as of type " + idParser.getIdentifierType());
        if (idParser.isEnsemblId()) {
            if (line.hasOption("species")) {
                System.out.println("Ignoring --species option when searching Ensembl ID.");
            }
            if (idParser.isTranscript()) {
                result = codingToGenomicTranscript(gene, c, threePrimeUtr, mapCdna);
            } else if (idParser.isEnsp()) {
                result = codingToGenomicEnsp(gene, c, threePrimeUtr, mapCdna);
            } else {
                result = codingToGenomicId(gene, c, threePrimeUtr, mapCdna);
            }
        } else {
            if (idParser.isTranscript()) {
                //append user input to beginning
                result = codingToGenomicXrefTranscript(species, gene, c, threePrimeUtr, mapCdna);
            } else {
                result = codingToGenomicXref(species, gene, c, threePrimeUtr, mapCdna);
            }
        }
        if (idParser.isTranscript() || idParser.isEnsp()) {

            result = gene + ":" + prefix + c + "\t" + result;
        } else {
            result = convertGeneResult(result, gene, c, prefix);
        }

    } else {
        System.out.println("Searching for " + transcript + " as Ensembl transcript ID");
        result = codingToGenomicTranscript(transcript, c, threePrimeUtr, mapCdna);
        //append user input to beginning
        result = transcript + ":" + prefix + c + "\t" + result;
    }

    System.out.println(header);
    System.out.println(result);

}

From source file:at.illecker.hama.hybrid.examples.onlinecf.OnlineCFTrainHybridBSP.java

public static void main(String[] args) throws Exception {
    // Defaults//from   w ww.  j  a  v  a 2s . co  m
    int numBspTask = 1; // CPU + GPU tasks
    int numGpuBspTask = 1; // GPU tasks
    int blockSize = BLOCK_SIZE;
    int gridSize = GRID_SIZE;

    int maxIteration = 3; // 150;
    int matrixRank = 3;
    int skipCount = 1;

    double alpha = ALPHA;
    int userCount = 0;
    int itemCount = 0;
    int percentNonZeroValues = 0;

    int GPUPercentage = 20;

    boolean useTestExampleInput = true;
    boolean isDebugging = true;
    String inputFile = "";
    String separator = "\\t";

    Configuration conf = new HamaConfiguration();
    FileSystem fs = FileSystem.get(conf);

    // Set numBspTask to maxTasks
    // BSPJobClient jobClient = new BSPJobClient(conf);
    // ClusterStatus cluster = jobClient.getClusterStatus(true);
    // numBspTask = cluster.getMaxTasks();

    if (args.length > 0) {
        if (args.length >= 14) {
            numBspTask = Integer.parseInt(args[0]);
            numGpuBspTask = Integer.parseInt(args[1]);
            blockSize = Integer.parseInt(args[2]);
            gridSize = Integer.parseInt(args[3]);

            maxIteration = Integer.parseInt(args[4]);
            matrixRank = Integer.parseInt(args[5]);
            skipCount = Integer.parseInt(args[6]);

            alpha = Double.parseDouble(args[7]);
            userCount = Integer.parseInt(args[8]);
            itemCount = Integer.parseInt(args[9]);
            percentNonZeroValues = Integer.parseInt(args[10]);

            GPUPercentage = Integer.parseInt(args[11]);

            useTestExampleInput = Boolean.parseBoolean(args[12]);
            isDebugging = Boolean.parseBoolean(args[13]);

            // optional parameters
            if (args.length > 14) {
                inputFile = args[14];
            }
            if (args.length > 15) {
                separator = args[15];
            }

        } else {
            System.out.println("Wrong argument size!");
            System.out.println("    Argument1=numBspTask");
            System.out.println("    Argument2=numGpuBspTask");
            System.out.println("    Argument3=blockSize");
            System.out.println("    Argument4=gridSize");
            System.out.println(
                    "    Argument5=maxIterations | Number of maximal iterations (" + maxIteration + ")");
            System.out.println("    Argument6=matrixRank | matrixRank (" + matrixRank + ")");
            System.out.println("    Argument7=skipCount | skipCount (" + skipCount + ")");
            System.out.println("    Argument8=alpha | alpha (" + alpha + ")");
            System.out.println("    Argument9=userCount | userCount (" + userCount + ")");
            System.out.println("    Argument10=itemCount | itemCount (" + itemCount + ")");
            System.out.println("    Argument11=percentNonZeroValues | percentNonZeroValues ("
                    + percentNonZeroValues + ")");
            System.out.println("    Argument12=GPUPercentage (percentage of input)");
            System.out.println("    Argument13=testExample | Use testExample input (true|false=default)");
            System.out.println("    Argument14=debug | Enable debugging (true|false=default)");
            System.out.println("    Argument15=inputFile (optional) | MovieLens inputFile");
            System.out.println("    Argument16=separator (optional) | default '" + separator + "' ");
            return;
        }
    }

    // Check if inputFile exists
    if ((!inputFile.isEmpty()) && (!new File(inputFile).exists())) {
        System.out.println("Error: inputFile: " + inputFile + " does not exist!");
        return;
    }

    // Check parameters
    if ((inputFile.isEmpty()) && (!useTestExampleInput) && (userCount <= 0) && (itemCount <= 0)
            && (percentNonZeroValues <= 0)) {
        System.out.println("Invalid parameter: userCount: " + userCount + " itemCount: " + itemCount
                + " percentNonZeroValues: " + percentNonZeroValues);
        return;
    }

    // Check if blockSize < matrixRank when using GPU
    if ((numGpuBspTask > 0) && (blockSize < matrixRank)) {
        System.out.println("Error: BlockSize < matrixRank");
        return;
    }

    // Check GPUPercentage
    if ((GPUPercentage < 0) && (GPUPercentage > 100)) {
        System.out.println("Error: GPUPercentage must be between 0 and 100 percent");
        return;
    }

    // Set config variables
    conf.setBoolean(CONF_DEBUG, isDebugging);
    conf.setBoolean("hama.pipes.logging", isDebugging);
    // Set CPU tasks
    conf.setInt("bsp.peers.num", numBspTask);
    // Set GPU tasks
    conf.setInt("bsp.peers.gpu.num", numGpuBspTask);
    // Set GPU blockSize and gridSize
    conf.set(CONF_BLOCKSIZE, "" + blockSize);
    conf.set(CONF_GRIDSIZE, "" + gridSize);

    conf.setInt(OnlineCF.CONF_ITERATION_COUNT, maxIteration);
    conf.setInt(OnlineCF.CONF_MATRIX_RANK, matrixRank);
    conf.setInt(OnlineCF.CONF_SKIP_COUNT, skipCount);

    // Debug output
    LOG.info("NumBspTask: " + conf.getInt("bsp.peers.num", 0));
    LOG.info("NumGpuBspTask: " + conf.getInt("bsp.peers.gpu.num", 0));
    LOG.info("bsp.tasks.maximum: " + conf.get("bsp.tasks.maximum"));
    LOG.info("BlockSize: " + conf.get(CONF_BLOCKSIZE));
    LOG.info("GridSize: " + conf.get(CONF_GRIDSIZE));
    LOG.info("GPUPercentage: " + GPUPercentage);

    LOG.info("isDebugging: " + isDebugging);
    LOG.info("useTestExampleInput: " + useTestExampleInput);
    LOG.info("inputPath: " + CONF_INPUT_DIR);
    LOG.info("outputPath: " + CONF_OUTPUT_DIR);

    LOG.info("maxIteration: " + maxIteration);
    LOG.info("matrixRank: " + matrixRank);
    LOG.info("skipCount: " + skipCount);

    LOG.info("alpha: " + alpha);
    LOG.info("userCount: " + userCount);
    LOG.info("itemCount: " + itemCount);
    LOG.info("percentNonZeroValues: " + percentNonZeroValues);

    if (!inputFile.isEmpty()) {
        LOG.info("inputFile: " + inputFile);
        LOG.info("separator: " + separator);
    }

    // prepare Input
    int maxTestPrefs = 10;
    Path preferencesIn = new Path(CONF_INPUT_DIR, "preferences_in.seq");
    List<Preference<Long, Long>> testPrefs = null;
    if (useTestExampleInput) {

        testPrefs = prepareTestInputData(conf, fs, CONF_INPUT_DIR, preferencesIn);

    } else if (inputFile.isEmpty()) {

        testPrefs = generateRandomInputData(conf, fs, CONF_INPUT_DIR, numBspTask, numGpuBspTask, userCount,
                itemCount, percentNonZeroValues, GPUPercentage, maxTestPrefs);

    } else if (!inputFile.isEmpty()) {
        // parse inputFile and return first entries for testing
        testPrefs = convertInputData(conf, fs, CONF_INPUT_DIR, preferencesIn, inputFile, separator,
                maxTestPrefs);
    }

    // Generate Job config
    BSPJob job = createOnlineCFTrainHybridBSPConf(conf, CONF_INPUT_DIR, CONF_OUTPUT_DIR);

    // Execute Job
    long startTime = System.currentTimeMillis();
    if (job.waitForCompletion(true)) {

        LOG.info("Job Finished in " + (System.currentTimeMillis() - startTime) / 1000.0 + " seconds");

        // Load Job results for testing
        OnlineCF recommender = new OnlineCF();
        recommender.load(CONF_OUTPUT_DIR.toString(), false);

        // Test results
        int error = 0;
        double totalError = 0;
        for (Preference<Long, Long> test : testPrefs) {
            double expected = test.getValue().get();
            double estimated = recommender.estimatePreference(test.getUserId(), test.getItemId());

            if (testPrefs.size() <= 20) {
                LOG.info("(" + test.getUserId() + ", " + test.getItemId() + ", " + expected + "): " + estimated
                        + " error: " + Math.abs(expected - estimated));
            }
            totalError += Math.abs(expected - estimated);
            error += (Math.abs(expected - estimated) < 0.5) ? 1 : 0;
        }

        LOG.info("totalError: " + totalError);
        LOG.info("assertEquals(expected: " + (testPrefs.size() * 0.75) + " == " + error
                + " actual) with delta: 1");

        if (isDebugging) {
            printOutput(conf, fs, ".log", new IntWritable(), new PipesVectorWritable());
        }
    }

}