Example usage for java.util Iterator next

List of usage examples for java.util Iterator next

Introduction

In this page you can find the example usage for java.util Iterator next.

Prototype

E next();

Source Link

Document

Returns the next element in the iteration.

Usage

From source file:grnet.validation.XMLValidation.java

public static void main(String[] args) throws IOException {
    // TODO Auto-generated method ssstub

    Enviroment enviroment = new Enviroment(args[0]);

    if (enviroment.envCreation) {
        String schemaUrl = enviroment.getArguments().getSchemaURL();
        Core core = new Core(schemaUrl);

        XMLSource source = new XMLSource(args[0]);

        File sourceFile = source.getSource();

        if (sourceFile.exists()) {

            Collection<File> xmls = source.getXMLs();

            System.out.println("Validating repository:" + sourceFile.getName());

            System.out.println("Number of files to validate:" + xmls.size());

            Iterator<File> iterator = xmls.iterator();

            System.out.println("Validating against schema:" + schemaUrl + "...");

            ValidationReport report = null;
            if (enviroment.getArguments().createReport().equalsIgnoreCase("true")) {

                report = new ValidationReport(enviroment.getArguments().getDestFolderLocation(),
                        enviroment.getDataProviderValid().getName());

            }/*ww w.  j ava 2 s  .co  m*/

            ConnectionFactory factory = new ConnectionFactory();
            factory.setHost(enviroment.getArguments().getQueueHost());
            factory.setUsername(enviroment.getArguments().getQueueUserName());
            factory.setPassword(enviroment.getArguments().getQueuePassword());

            while (iterator.hasNext()) {

                StringBuffer logString = new StringBuffer();
                logString.append(sourceFile.getName());
                logString.append(" " + schemaUrl);

                File xmlFile = iterator.next();
                String name = xmlFile.getName();
                name = name.substring(0, name.indexOf(".xml"));

                logString.append(" " + name);

                boolean xmlIsValid = core.validateXMLSchema(xmlFile);

                if (xmlIsValid) {
                    logString.append(" " + "Valid");
                    slf4jLogger.info(logString.toString());

                    Connection connection = factory.newConnection();
                    Channel channel = connection.createChannel();
                    channel.queueDeclare(QUEUE_NAME, false, false, false, null);

                    channel.basicPublish("", QUEUE_NAME, null, logString.toString().getBytes());
                    channel.close();
                    connection.close();
                    try {
                        if (report != null) {

                            report.raiseValidFilesNum();
                        }

                        FileUtils.copyFileToDirectory(xmlFile, enviroment.getDataProviderValid());
                    } catch (IOException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }
                } else {
                    logString.append(" " + "Invalid");
                    slf4jLogger.info(logString.toString());

                    Connection connection = factory.newConnection();
                    Channel channel = connection.createChannel();
                    channel.queueDeclare(QUEUE_NAME, false, false, false, null);

                    channel.basicPublish("", QUEUE_NAME, null, logString.toString().getBytes());
                    channel.close();
                    connection.close();

                    try {
                        if (report != null) {

                            if (enviroment.getArguments().extendedReport().equalsIgnoreCase("true"))
                                report.appendXMLFileNameNStatus(xmlFile.getPath(), Constants.invalidData,
                                        core.getReason());

                            report.raiseInvalidFilesNum();
                        }
                        FileUtils.copyFileToDirectory(xmlFile, enviroment.getDataProviderInValid());
                    } catch (IOException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }
                }
            }

            if (report != null) {
                report.writeErrorBank(core.getErrorBank());
                report.appendGeneralInfo();
            }
            System.out.println("Validation is done.");

        }

    }
}

From source file:com.netscape.cms.servlet.test.ConfigurationTest.java

public static void main(String args[]) throws Exception {
    String host = null;//from  ww w .  j a v  a 2 s.  c  o m
    String port = null;
    String cstype = null;
    String token_pwd = null;
    String db_dir = "./";
    String protocol = "https";
    String pin = null;
    String extCertFile = null;
    int testnum = 1;

    // parse command line arguments
    Options options = new Options();
    options.addOption("t", true, "Subsystem type");
    options.addOption("h", true, "Hostname of the CS subsystem");
    options.addOption("p", true, "Port of the CS subsystem");
    options.addOption("w", true, "Token password");
    options.addOption("d", true, "Directory for tokendb");
    options.addOption("s", true, "preop pin");
    options.addOption("e", true, "File for externally signed signing cert");
    options.addOption("x", true, "Test number");

    try {
        CommandLineParser parser = new PosixParser();
        CommandLine cmd = parser.parse(options, args);

        if (cmd.hasOption("t")) {
            cstype = cmd.getOptionValue("t");
        } else {
            System.err.println("Error: no subsystem type provided.");
            usage(options);
        }

        if (cmd.hasOption("h")) {
            host = cmd.getOptionValue("h");
        } else {
            System.err.println("Error: no hostname provided.");
            usage(options);
        }

        if (cmd.hasOption("p")) {
            port = cmd.getOptionValue("p");
        } else {
            System.err.println("Error: no port provided");
            usage(options);
        }

        if (cmd.hasOption("w")) {
            token_pwd = cmd.getOptionValue("w");
        } else {
            System.err.println("Error: no token password provided");
            usage(options);
        }

        if (cmd.hasOption("d")) {
            db_dir = cmd.getOptionValue("d");
        }

        if (cmd.hasOption("s")) {
            pin = cmd.getOptionValue("s");
        }

        if (cmd.hasOption("e")) {
            extCertFile = cmd.getOptionValue("e");
        }

        if (cmd.hasOption("x")) {
            testnum = Integer.parseInt(cmd.getOptionValue("x"));
        }
    } catch (ParseException e) {
        System.err.println("Error in parsing command line options: " + e.getMessage());
        usage(options);
    }

    // Initialize token
    try {
        CryptoManager.initialize(db_dir);
    } catch (AlreadyInitializedException e) {
        // it is ok if it is already initialized
    } catch (Exception e) {
        System.out.println("INITIALIZATION ERROR: " + e.toString());
        System.exit(1);
    }

    // log into token
    CryptoManager manager = null;
    CryptoToken token = null;
    try {
        manager = CryptoManager.getInstance();
        token = manager.getInternalKeyStorageToken();
        Password password = new Password(token_pwd.toCharArray());
        try {
            token.login(password);
        } catch (Exception e) {
            System.out.println("login Exception: " + e.toString());
            if (!token.isLoggedIn()) {
                token.initPassword(password, password);
            }
        }
    } catch (Exception e) {
        System.out.println("Exception in logging into token:" + e.toString());
    }

    SystemConfigClient client = null;
    try {
        ClientConfig config = new ClientConfig();
        config.setServerURL(protocol + "://" + host + ":" + port);

        client = new SystemConfigClient(new PKIClient(config, null), cstype);
    } catch (URISyntaxException e1) {
        e1.printStackTrace();
        System.exit(1);
    }

    ConfigurationRequest data = null;
    switch (testnum) {
    case 1:
        data = constructCAData(host, port, pin, db_dir, token_pwd, token);
        break;
    case 2:
        data = constructCloneCAData(host, port, pin, db_dir, token_pwd, token);
        break;
    case 3:
        data = constructKRAData(host, port, pin, db_dir, token_pwd, token);
        break;
    case 4:
        data = constructOCSPData(host, port, pin, db_dir, token_pwd, token);
        break;
    case 5:
        data = constructTKSData(host, port, pin, db_dir, token_pwd, token);
        break;
    case 6:
        data = constructSubCAData(host, port, pin, db_dir, token_pwd, token);
        break;
    case 7:
        data = constructExternalCADataPart1(host, port, pin, db_dir, token_pwd, token);
        break;
    case 8:
        data = constructExternalCADataPart2(host, port, pin, db_dir, token_pwd, token, extCertFile);
        break;
    default:
        System.out.println("Invalid test");
        System.exit(1);
    }

    ConfigurationResponse response = client.configure(data);

    System.out.println("adminCert: " + response.getAdminCert().getCert());
    List<SystemCertData> certs = response.getSystemCerts();
    Iterator<SystemCertData> iterator = certs.iterator();
    while (iterator.hasNext()) {
        SystemCertData cdata = iterator.next();
        System.out.println("tag: " + cdata.getTag());
        System.out.println("cert: " + cdata.getCert());
        System.out.println("request: " + cdata.getRequest());
    }

}

From source file:de.clusteval.serverclient.BackendClient.java

/**
 * @param args/*w ww.  j a  va  2s .c  o  m*/
 *            Command line parameters for the backend client (see
 *            {@link #params}).
 * @throws IOException
 */
@SuppressWarnings("unused")
public static void main(String[] args) throws IOException {
    try {
        CommandLine params = parseParams(args, false);

        if (params.hasOption("help")) {
            HelpFormatter formatter = new HelpFormatter();
            formatter.printHelp("clustevalClient", clientCLIOptions);
            System.exit(0);
        }

        if (params.hasOption("version")) {
            System.out.println(VERSION);
            System.exit(0);
        }

        initLogging(params);

        Logger log = LoggerFactory.getLogger(BackendClient.class);

        System.out.println("Starting clusteval client");
        System.out.println(VERSION);
        System.out.println("=========================");

        // if command line arguments (except connection parameters) are
        // passed, we do not start a console
        Set<String> paramKeys = new HashSet<String>();
        @SuppressWarnings("unchecked")
        Iterator<Option> it = params.iterator();
        while (it.hasNext()) {
            paramKeys.add(it.next().getOpt());
        }

        paramKeys.remove("ip");
        paramKeys.remove("port");
        paramKeys.remove("clientId");
        if (paramKeys.size() > 0) {
            try {
                new BackendClient(args);
            } catch (Exception e) {
            }
        } else {
            String clientId;
            if (params.hasOption("clientId"))
                clientId = params.getOptionValue("clientId");
            else
                // parse args because of possible connection parameters
                clientId = new BackendClient(args).clientId;

            reader = new ConsoleReader();

            List<Completer> completers = new LinkedList<Completer>();
            completers.add(new BackendClientCompleter(clientId, args));

            String ip = params.hasOption("ip") ? params.getOptionValue("ip") : "localhost";
            String port = params.hasOption("port") ? params.getOptionValue("port") : "1099";

            setDefaultPromptAndCompleter(ip, port, completers);

            String line;

            while ((line = reader.readLine()) != null) {

                if (line.equalsIgnoreCase("quit") || line.equalsIgnoreCase("exit")) {
                    break;
                }

                boolean connectException = false;
                do {
                    try {
                        new BackendClient(
                                ArraysExt.merge(args, ("-clientId " + clientId + " -" + line).split(" ")));
                        connectException = false;
                    } catch (ConnectException e) {
                        e.printStackTrace();
                        connectException = true;
                    } catch (Exception e) {
                        log.warn(e.getMessage());
                    }
                    Thread.sleep(1000);
                } while (connectException);
                // }
            }
        }
    } catch (ParseException e) {
        System.err.println("Parsing failed.  Reason: " + e.getMessage());

        HelpFormatter formatter = new HelpFormatter();
        formatter.printHelp("clustevalClient",
                "Invoking this client without any parameters will open a shell with tab-completion.",
                clientCLIOptions, "", true);
    } catch (Throwable t) {
        // t.printStackTrace();
    }
}

From source file:com.comcast.cqs.test.stress.CqsStressTester.java

/**
* @param args/*from  ww  w  .ja  va 2 s.  c  om*/
*/
public static void main(String[] args) {

    try {

        CMBControllerServlet.valueAccumulator.initializeAllCounters();
        setup();
        Util.initLog4j();
        CqsStressTester tester = new CqsStressTester();
        ScheduledExecutorService scheduledExecutorService = tester
                .createQueuesAndInitializePublishersAndReceivers(); //will clear queues

        long totalMessagesReceived = 0;
        long totalMessagesDeleted = 0;
        long totalMessagesRevisibled = 0;
        long totalEmptyResponses = 0;
        long totalDuplicates = 0;
        long totalOutOfOrderMessages = 0;

        int testDurationSeconds = CQSStressTestProperties.getInstance().getTestDurationSeconds();
        Thread.sleep(testDurationSeconds * 1000);

        if (scheduledExecutorService != null) {
            scheduledExecutorService.shutdown();
        }

        logger.info("===Sender Shutdown Triggered==");

        for (String queueUrl : tester.receiverMap.keySet()) {
            for (Receiver receiver : tester.receiverMap.get(queueUrl)) {
                receiver.setContinueThread(false);
            }
        }

        for (String queueUrl : tester.receiverMap.keySet()) {

            Set<String> messageIdMaster = new HashSet<String>();
            List<Integer> deleteTimesMaster = new ArrayList<Integer>();
            List<Long> flightTimesMaster = new ArrayList<Long>();
            List<Long> receiveTimesMaster = new ArrayList<Long>();

            for (Receiver receiver : tester.receiverMap.get(queueUrl)) {

                receiver.join();

                logger.warn(
                        "===================================================================================================================");
                logger.warn("TheadId=" + receiver.getThreadId() + " receiveMessageCount="
                        + receiver.getTotalMessagesReceived() + " deletedMessageCount="
                        + receiver.getTotalMessagesDeleted() + " revisibledMessageCount="
                        + receiver.getTotalMessagesRevisibled());
                logger.warn(
                        "===================================================================================================================");

                totalMessagesReceived += receiver.getTotalMessagesReceived();
                totalMessagesDeleted += receiver.getTotalMessagesDeleted();
                totalMessagesRevisibled += receiver.getTotalMessagesRevisibled();
                totalOutOfOrderMessages += receiver.getTotalOutOfOrderMessages();
                totalDuplicates += checkAndCombine(messageIdMaster, receiver.messageIds);
                //deleteTimesMaster.addAll(receiver.deleteLatencyMSList);
                //flightTimesMaster.addAll(receiver.flightTimeList);
                totalEmptyResponses += receiver.emptyResponseCount;
            }

            logger.warn(
                    "===================================================================================================================");

            Iterator<String> iter = sendMessageIdSet.iterator();

            while (iter.hasNext()) {
                logger.error("Missed message:" + iter.next());
            }

            Collections.sort(deleteTimesMaster);
            Collections.sort(flightTimesMaster);
            //receiveTimesMaster.addAll(receiveLacencyMSList);
            Collections.sort(receiveTimesMaster);

            /*logger.warn("Receive message latencies");
                    
            if (receiveTimesMaster.size() > 0) {
                    
               for (int i=5; i<=100; i+=5) {
                    
                  int percentileIndex = receiveTimesMaster.size()*i/100 - 1;
                    
                  if (percentileIndex < 0) {
             percentileIndex = 0;
                  }
                    
                  logger.warn("" + i + "th percentile=" + receiveTimesMaster.get(percentileIndex));
               }
            }
                    
            logger.warn("===================================================================================================================");
            logger.warn("Message flight time latencies");
                    
            if (flightTimesMaster.size() > 0) {
                    
               for (int i=5; i<=100; i+=5) {
                    
                  int percentileIndex = flightTimesMaster.size()*i/100 - 1;
                    
                  if (percentileIndex < 0) {
             percentileIndex = 0;
                  }
                    
                  logger.warn("" + i + "th percentile=" + flightTimesMaster.get(percentileIndex));
               }
            }
                    
            logger.warn("===================================================================================================================");
            logger.warn("Delete message latencies");
                    
            if (deleteTimesMaster.size() > 0) {
                    
               for (int i=5; i<=100; i+=5) {
                    
                  int percentileIndex = deleteTimesMaster.size()*i/100 - 1;
                    
                  if (percentileIndex < 0) {
             percentileIndex = 0;
                  }
                    
                  logger.warn("" + i + "th percentile=" + deleteTimesMaster.get(percentileIndex));
               }
            }*/

        }

        logger.warn(
                "===================================================================================================================");
        logger.warn(
                "===================================================================================================================");
        logger.warn("totalMessagesSent=" + tester.messageCount.get() + " totalMessagesReceived="
                + totalMessagesReceived + " totalMessagesDeleted=" + totalMessagesDeleted
                + " totalMessagesRevisibled=" + totalMessagesRevisibled);
        logger.warn(
                "===================================================================================================================");
        logger.warn("totalEmptyResponses=" + totalEmptyResponses);
        logger.warn("totalDuplicates=" + totalDuplicates);
        logger.warn("totalOutOfOrderMessages=" + totalOutOfOrderMessages);
        logger.warn("totalMessagesLost=" + sendMessageIdSet.size());
        logger.warn(
                "===================================================================================================================");
        logger.warn("totalRunTimeMillis=" + (System.currentTimeMillis() - tester.startTime) + " status=Exit");
        logger.warn(
                "===================================================================================================================");
        logger.warn(
                "===================================================================================================================");

    } catch (Exception e) {
        logger.error("Thread=main status=exception message=setup_failure ", e);
    } finally {
        CMBControllerServlet.valueAccumulator.deleteAllCounters();
    }
}

From source file:edu.oregonstate.eecs.mcplan.abstraction.EvaluateSimilarityFunction.java

/**
 * @param args//from  w ww.  j av  a2 s . co m
 * @throws IOException
 * @throws FileNotFoundException
 */
public static void main(final String[] args) throws FileNotFoundException, IOException {
    final String experiment_file = args[0];
    final File root_directory;
    if (args.length > 1) {
        root_directory = new File(args[1]);
    } else {
        root_directory = new File(".");
    }
    final CsvConfigurationParser csv_config = new CsvConfigurationParser(new FileReader(experiment_file));
    final String experiment_name = FilenameUtils.getBaseName(experiment_file);

    final File expr_directory = new File(root_directory, experiment_name);
    expr_directory.mkdirs();

    final Csv.Writer csv = new Csv.Writer(
            new PrintStream(new FileOutputStream(new File(expr_directory, "results.csv"))));
    final String[] parameter_headers = new String[] { "kpca.kernel", "kpca.rbf.sigma",
            "kpca.random_forest.Ntrees", "kpca.random_forest.max_depth", "kpca.Nbases", "multiclass.classifier",
            "multiclass.random_forest.Ntrees", "multiclass.random_forest.max_depth",
            "pairwise_classifier.max_branching", "training.label_noise" };
    csv.cell("domain").cell("abstraction");
    for (final String p : parameter_headers) {
        csv.cell(p);
    }
    csv.cell("Ntrain").cell("Ntest").cell("ami.mean").cell("ami.variance").cell("ami.confidence").newline();

    for (int expr = 0; expr < csv_config.size(); ++expr) {
        try {
            final KeyValueStore expr_config = csv_config.get(expr);
            final Configuration config = new Configuration(root_directory.getPath(), expr_directory.getName(),
                    expr_config);

            System.out.println("[Loading '" + config.training_data_single + "']");
            final Instances single = WekaUtil
                    .readLabeledDataset(new File(root_directory, config.training_data_single + ".arff"));

            final Instances train = new Instances(single, 0);
            final int[] idx = Fn.range(0, single.size());
            int instance_counter = 0;
            Fn.shuffle(config.rng, idx);
            final int Ntrain = config.getInt("Ntrain_games"); // TODO: Rename?
            final double label_noise = config.getDouble("training.label_noise");
            final int Nlabels = train.classAttribute().numValues();
            assert (Nlabels > 0);
            for (int i = 0; i < Ntrain; ++i) {
                final Instance inst = single.get(idx[instance_counter++]);
                if (label_noise > 0 && config.rng.nextDouble() < label_noise) {
                    int noisy_label = 0;
                    do {
                        noisy_label = config.rng.nextInt(Nlabels);
                    } while (noisy_label == (int) inst.classValue());
                    System.out.println("Noisy label (" + inst.classValue() + " -> " + noisy_label + ")");
                    inst.setClassValue(noisy_label);
                }
                train.add(inst);
                inst.setDataset(train);
            }

            final Fn.Function2<Boolean, Instance, Instance> plausible_p = createPlausiblePredicate(config);

            final int Ntest = config.Ntest_games;
            int Ntest_added = 0;
            final ArrayList<Instances> tests = new ArrayList<Instances>();
            while (instance_counter < single.size() && Ntest_added < Ntest) {
                final Instance inst = single.get(idx[instance_counter++]);
                boolean found = false;
                for (final Instances test : tests) {
                    // Note that 'plausible_p' should be transitive
                    if (plausible_p.apply(inst, test.get(0))) {
                        WekaUtil.addInstance(test, inst);
                        if (test.size() == 30) {
                            Ntest_added += test.size();
                        } else if (test.size() > 30) {
                            Ntest_added += 1;
                        }
                        found = true;
                        break;
                    }
                }

                if (!found) {
                    final Instances test = new Instances(single, 0);
                    WekaUtil.addInstance(test, inst);
                    tests.add(test);
                }
            }
            final Iterator<Instances> test_itr = tests.iterator();
            while (test_itr.hasNext()) {
                if (test_itr.next().size() < 30) {
                    test_itr.remove();
                }
            }
            System.out.println("=== tests.size() = " + tests.size());
            System.out.println("=== Ntest_added = " + Ntest_added);

            System.out.println("[Training]");
            final Evaluator evaluator = createEvaluator(config, train);
            //            final Instances transformed_test = evaluator.prepareInstances( test );

            System.out.println("[Evaluating]");

            final int Nxval = evaluator.isSensitiveToOrdering() ? 10 : 1;
            final MeanVarianceAccumulator ami = new MeanVarianceAccumulator();

            final MeanVarianceAccumulator errors = new MeanVarianceAccumulator();
            final MeanVarianceAccumulator relative_error = new MeanVarianceAccumulator();

            int c = 0;
            for (int xval = 0; xval < Nxval; ++xval) {
                for (final Instances test : tests) {
                    // TODO: Debugging
                    WekaUtil.writeDataset(new File(config.root_directory), "test_" + (c++), test);

                    //               transformed_test.randomize( new RandomAdaptor( config.rng ) );
                    //               final ClusterContingencyTable ct = evaluator.evaluate( transformed_test );
                    test.randomize(new RandomAdaptor(config.rng));
                    final ClusterContingencyTable ct = evaluator.evaluate(test);
                    System.out.println(ct);

                    int Nerrors = 0;
                    final MeanVarianceAccumulator mv = new MeanVarianceAccumulator();
                    for (int i = 0; i < ct.R; ++i) {
                        final int max = Fn.max(ct.n[i]);
                        Nerrors += (ct.a[i] - max);
                        mv.add(((double) ct.a[i]) / ct.N * Nerrors / ct.a[i]);
                    }
                    errors.add(Nerrors);
                    relative_error.add(mv.mean());

                    System.out.println("exemplar: " + test.get(0));
                    System.out.println("Nerrors = " + Nerrors);
                    final PrintStream ct_out = new PrintStream(
                            new FileOutputStream(new File(expr_directory, "ct_" + expr + "_" + xval + ".csv")));
                    ct.writeCsv(ct_out);
                    ct_out.close();
                    final double ct_ami = ct.adjustedMutualInformation_max();
                    if (Double.isNaN(ct_ami)) {
                        System.out.println("! ct_ami = NaN");
                    } else {
                        ami.add(ct_ami);
                    }
                    System.out.println();
                }
            }
            System.out.println("errors = " + errors.mean() + " (" + errors.confidence() + ")");
            System.out.println(
                    "relative_error = " + relative_error.mean() + " (" + relative_error.confidence() + ")");
            System.out.println("AMI_max = " + ami.mean() + " (" + ami.confidence() + ")");

            csv.cell(config.domain).cell(config.get("abstraction.discovery"));
            for (final String p : parameter_headers) {
                csv.cell(config.get(p));
            }
            csv.cell(Ntrain).cell(Ntest).cell(ami.mean()).cell(ami.variance()).cell(ami.confidence()).newline();
        } catch (final Exception ex) {
            ex.printStackTrace();
        }
    }
}

From source file:com.netscape.cms.servlet.test.DRMTest.java

public static void main(String args[]) throws InvalidKeyException, NoSuchAlgorithmException,
        InvalidKeySpecException, SignatureException, IOException {
    String host = null;/*from   w ww  . ja v  a  2  s.  c  o  m*/
    String port = null;
    String token_pwd = null;
    String db_dir = "./";
    String protocol = "http";
    String clientCertNickname = "KRA Administrator of Instance pki-kra's SjcRedhat Domain ID";

    // parse command line arguments
    Options options = new Options();
    options.addOption("h", true, "Hostname of the DRM");
    options.addOption("p", true, "Port of the DRM");
    options.addOption("w", true, "Token password");
    options.addOption("d", true, "Directory for tokendb");
    options.addOption("s", true, "Attempt Optional Secure SSL connection");
    options.addOption("c", true, "Optional SSL Client cert Nickname");

    try {
        CommandLineParser parser = new PosixParser();
        CommandLine cmd = parser.parse(options, args);

        if (cmd.hasOption("h")) {
            host = cmd.getOptionValue("h");
        } else {
            System.err.println("Error: no hostname provided.");
            usage(options);
        }

        if (cmd.hasOption("p")) {
            port = cmd.getOptionValue("p");
        } else {
            System.err.println("Error: no port provided");
            usage(options);
        }

        if (cmd.hasOption("w")) {
            token_pwd = cmd.getOptionValue("w");
        } else {
            System.err.println("Error: no token password provided");
            usage(options);
        }

        if (cmd.hasOption("d")) {
            db_dir = cmd.getOptionValue("d");
        }

        if (cmd.hasOption("s")) {
            if (cmd.getOptionValue("s") != null && cmd.getOptionValue("s").equals("true")) {
                protocol = "https";
            }
        }

        if (cmd.hasOption("c")) {
            String nick = cmd.getOptionValue("c");

            if (nick != null && protocol.equals("https")) {
                clientCertNickname = nick;
            }
        }

    } catch (ParseException e) {
        System.err.println("Error in parsing command line options: " + e.getMessage());
        usage(options);
    }

    // used for crypto operations
    byte iv[] = { 0x1, 0x1, 0x1, 0x1, 0x1, 0x1, 0x1, 0x1 };

    try {
        iv = genIV(8);
    } catch (Exception e) {
        log("Can't generate initialization vector use default: " + e.toString());
    }

    // used for wrapping to send data to DRM
    String transportCert = null;

    // Data to be archived
    SymmetricKey vek = null;
    String passphrase = null;

    // Session keys and passphrases for recovery
    SymmetricKey sessionKey = null;
    byte[] wrappedRecoveryKey = null;
    String recoveryPassphrase = null;
    byte[] wrappedRecoveryPassphrase = null;

    // retrieved data (should match archived data)
    byte[] encryptedData = null;
    String recoveredKey = null;

    // various ids used in recovery/archival operations
    KeyId keyId = null;
    String clientKeyId = null;
    RequestId recoveryRequestId = null;

    // Variables for data structures from calls
    KeyRequestResponse requestResponse = null;
    Key keyData = null;
    KeyInfo keyInfo = null;

    // Initialize token
    try {
        CryptoManager.initialize(db_dir);
    } catch (AlreadyInitializedException e) {
        // it is ok if it is already initialized
    } catch (Exception e) {
        log("INITIALIZATION ERROR: " + e.toString());
        System.exit(1);
    }

    // Set base URI and get client

    KRAClient client;
    SystemCertClient systemCertClient;
    KeyClient keyClient;
    NSSCryptoProvider nssCrypto;
    try {
        ClientConfig config = new ClientConfig();
        config.setServerURI(protocol + "://" + host + ":" + port + "/kra");
        config.setCertNickname(clientCertNickname);
        config.setCertDatabase(db_dir);
        config.setCertPassword(token_pwd);
        nssCrypto = new NSSCryptoProvider(config);

        client = new KRAClient(new PKIClient(config, nssCrypto));
        systemCertClient = (SystemCertClient) client.getClient("systemcert");
        keyClient = (KeyClient) client.getClient("key");

    } catch (Exception e) {
        e.printStackTrace();
        return;
    }

    // Test 1: Get transport certificate from DRM
    transportCert = systemCertClient.getTransportCert().getEncoded();
    transportCert = transportCert.substring(CertData.HEADER.length(), transportCert.indexOf(CertData.FOOTER));
    keyClient.setTransportCert(transportCert);

    log("Transport Cert retrieved from DRM: " + transportCert);

    // Test 2: Get list of completed key archival requests
    log("\n\nList of completed archival requests");
    KeyRequestInfoCollection list = keyClient.listRequests("complete", "securityDataEnrollment");
    if (list.getTotal() == 0) {
        log("No requests found");
    } else {
        Iterator<KeyRequestInfo> iter = list.getEntries().iterator();
        while (iter.hasNext()) {
            KeyRequestInfo info = iter.next();
            printRequestInfo(info);
        }
    }

    // Test 3: Get list of key recovery requests
    log("\n\nList of completed recovery requests");
    KeyRequestInfoCollection list2 = keyClient.listRequests("complete", "securityDataRecovery");
    if (list2.getTotal() == 0) {
        log("No requests found");
    } else {
        Iterator<KeyRequestInfo> iter2 = list2.getEntries().iterator();
        while (iter2.hasNext()) {
            KeyRequestInfo info = iter2.next();
            printRequestInfo(info);
        }
    }

    // Test 4: Generate and archive a symmetric key
    log("Archiving symmetric key");
    clientKeyId = "UUID: 123-45-6789 VEK " + Calendar.getInstance().getTime().toString();
    try {
        vek = nssCrypto.generateSessionKey();
        byte[] encoded = nssCrypto.createPKIArchiveOptions(transportCert, vek, null,
                KeyRequestResource.DES3_ALGORITHM, 0, iv);

        KeyRequestResponse info = keyClient.archivePKIOptions(clientKeyId,
                KeyRequestResource.SYMMETRIC_KEY_TYPE, KeyRequestResource.DES3_ALGORITHM, 0, encoded);
        log("Archival Results:");
        printRequestInfo(info.getRequestInfo());
        keyId = info.getKeyId();
    } catch (Exception e) {
        log("Exception in archiving symmetric key:" + e.getMessage());
        e.printStackTrace();
    }

    //Test 5: Get keyId for active key with client ID

    log("Getting key ID for symmetric key");
    keyInfo = keyClient.getActiveKeyInfo(clientKeyId);
    printKeyInfo(keyInfo);
    KeyId keyId2 = keyInfo.getKeyId();
    if (keyId2 == null) {
        log("No archived key found");
    } else {
        log("Archived Key found: " + keyId);
    }

    if (!keyId.equals(keyId2)) {
        log("Error: key ids from search and archival do not match");
    } else {
        log("Success: keyids from search and archival match.");
    }

    // Test 6: Submit a recovery request for the symmetric key using a session key
    log("Submitting a recovery request for the  symmetric key using session key");
    try {
        sessionKey = nssCrypto.generateSessionKey();
        wrappedRecoveryKey = CryptoUtil.wrapSymmetricKey(nssCrypto.getManager(), nssCrypto.getToken(),
                transportCert, sessionKey);
        keyData = keyClient.retrieveKey(keyId, wrappedRecoveryKey);
    } catch (Exception e) {
        log("Exception in recovering symmetric key using session key: " + e.getMessage());
    }

    encryptedData = keyData.getEncryptedData();

    try {
        recoveredKey = Utils.base64encode(nssCrypto.unwrapWithSessionKey(encryptedData, sessionKey,
                KeyRequestResource.DES3_ALGORITHM, keyData.getNonceData()));
    } catch (Exception e) {
        log("Exception in unwrapping key: " + e.toString());
        e.printStackTrace();
    }

    if (!recoveredKey.equals(Utils.base64encode(vek.getEncoded()))) {
        log("Error: recovered and archived keys do not match!");
    } else {
        log("Success: recoverd and archived keys match!");
    }

    // Test 7: Submit a recovery request for the symmetric key using a passphrase
    log("Submitting a recovery request for the  symmetric key using a passphrase");
    recoveryPassphrase = "Gimme me keys please";

    try {
        sessionKey = nssCrypto.generateSessionKey();
        wrappedRecoveryPassphrase = nssCrypto.wrapWithSessionKey(recoveryPassphrase, iv, sessionKey,
                KeyRequestResource.DES3_ALGORITHM);
        wrappedRecoveryKey = nssCrypto.wrapSessionKeyWithTransportCert(sessionKey, transportCert);

        keyData = keyClient.retrieveKeyUsingWrappedPassphrase(keyId, wrappedRecoveryKey,
                wrappedRecoveryPassphrase, iv);
    } catch (Exception e) {
        log("Exception in recovering symmetric key using passphrase" + e.toString());
        e.printStackTrace();
    }

    encryptedData = keyData.getEncryptedData();

    try {
        recoveredKey = Utils.base64encode(nssCrypto.unwrapWithPassphrase(encryptedData, recoveryPassphrase));
    } catch (Exception e) {
        log("Error: unable to unwrap key using passphrase");
        e.printStackTrace();
    }

    if (recoveredKey == null || !recoveredKey.equals(Utils.base64encode(vek.getEncoded()))) {
        log("Error: recovered and archived keys do not match!");
    } else {
        log("Success: recovered and archived keys do match!");
    }

    passphrase = "secret12345";
    // Test 8: Generate and archive a passphrase
    clientKeyId = "UUID: 123-45-6789 RKEK " + Calendar.getInstance().getTime().toString();
    try {
        requestResponse = keyClient.archivePassphrase(clientKeyId, passphrase);

        log("Archival Results:");
        printRequestInfo(requestResponse.getRequestInfo());
        keyId = requestResponse.getKeyId();
    } catch (Exception e) {
        log("Exception in archiving symmetric key:" + e.toString());
        e.printStackTrace();
    }

    //Test 9: Get keyId for active passphrase with client ID
    log("Getting key ID for passphrase");
    keyInfo = keyClient.getActiveKeyInfo(clientKeyId);
    printKeyInfo(keyInfo);
    keyId2 = keyInfo.getKeyId();
    if (keyId2 == null) {
        log("No archived key found");
    } else {
        log("Archived Key found: " + keyId);
    }

    if (!keyId.equals(keyId2)) {
        log("Error: key ids from search and archival do not match");
    } else {
        log("Success: key ids from search and archival do match!");
    }

    // Test 10: Submit a recovery request for the passphrase using a session key
    log("Submitting a recovery request for the passphrase using session key");
    sessionKey = null;
    wrappedRecoveryKey = null;
    try {
        keyData = keyClient.retrieveKeyByPassphrase(keyId, recoveryPassphrase);
    } catch (Exception e) {
        log("Exception in recovering passphrase using session key: " + e.getMessage());
    }
    encryptedData = keyData.getEncryptedData();
    try {
        recoveredKey = new String(nssCrypto.unwrapWithPassphrase(encryptedData, recoveryPassphrase), "UTF-8");
    } catch (Exception e) {
        log("Exception in unwrapping key: " + e.toString());
        e.printStackTrace();
    }

    if (recoveredKey == null || !recoveredKey.equals(passphrase)) {
        log("Error: recovered and archived passphrases do not match!");
    } else {
        log("Success: recovered and archived passphrases do match!");
    }

    // Test 11: Submit a recovery request for the passphrase using a passphrase
    try {
        sessionKey = nssCrypto.generateSessionKey();
        wrappedRecoveryKey = nssCrypto.wrapSessionKeyWithTransportCert(sessionKey, transportCert);
        wrappedRecoveryPassphrase = nssCrypto.wrapWithSessionKey(recoveryPassphrase, iv, sessionKey,
                KeyRequestResource.DES3_ALGORITHM);
        keyData = keyClient.retrieveKeyUsingWrappedPassphrase(keyId, wrappedRecoveryKey,
                wrappedRecoveryPassphrase, iv);
    } catch (Exception e1) {
        e1.printStackTrace();
        System.out.println("Test 17: " + e1.getMessage());
        System.exit(-1);
    }
    encryptedData = keyData.getEncryptedData();
    try {
        recoveredKey = new String(nssCrypto.unwrapWithPassphrase(encryptedData, recoveryPassphrase), "UTF-8");
    } catch (Exception e) {
        log("Error: cannot unwrap key using passphrase");
        e.printStackTrace();
    }

    if (recoveredKey == null || !recoveredKey.equals(passphrase)) {
        log("Error: recovered and archived passphrases do not match!");
    } else {
        log("Success: recovered and archived passphrases do match!");
    }

    // Test 12: Get key
    log("Getting passphrase: " + keyId);
    try {
        keyData = keyClient.retrieveKeyByPassphrase(keyId, recoveryPassphrase);
    } catch (Exception e1) {
        e1.printStackTrace();
    }
    encryptedData = keyData.getEncryptedData();
    try {
        recoveredKey = new String(nssCrypto.unwrapWithPassphrase(encryptedData, recoveryPassphrase), "UTF-8");
    } catch (Exception e) {
        log("Error: Can't unwrap recovered key using passphrase");
        e.printStackTrace();
    }

    if (recoveredKey == null || !recoveredKey.equals(passphrase)) {
        log("Error: recovered and archived passphrases do not match!");
    } else {
        log("Success: recovered and archived passphrases do match!");
    }

    // Test 13: Get non-existent request
    RequestId requestId = new RequestId("0xabcdef");
    log("Getting non-existent request: " + requestId.toHexString());
    try {
        keyClient.getRequestInfo(requestId);
        log("Error: getting non-existent request does not throw an exception");
    } catch (RequestNotFoundException e) {
        log("Success: getting non-existent request throws an exception: " + e.getMessage() + " ("
                + e.getRequestId().toHexString() + ")");
    }

    // Test 14: Request x509 key recovery
    // This test requires to retrieve keyId and matching certificate
    // from installed instances of CA and DRM
    String keyID = "1";
    String b64Certificate = "MIIC+TCCAeGgAwIBAgIBDDANBgkqhkiG9w0BAQsFADBOMSswKQYDVQQKDCJ1c2Vy"
            + "c3lzLnJlZGhhdC5jb20gU2VjdXJpdHkgRG9tYWluMR8wHQYDVQQDDBZDQSBTaWdu"
            + "aW5nIENlcnRpZmljYXRlMB4XDTEzMTAyNTE5MzQwM1oXDTE0MDQyMzE5MzQwM1ow"
            + "EzERMA8GCgmSJomT8ixkAQEMAXgwgZ8wDQYJKoZIhvcNAQEBBQADgY0AMIGJAoGB"
            + "ALhLfGmKvxFsKXPh49q1QsluXU3WlyS1XnpDLgOAhgTNgO4sG6CpPdv6hZYIvQBb"
            + "ZQ5bhuML+NXK+Q+EIiNk1cUTxgL3a30sPzy6QaFWxwM8i4uXm4nCBYv7T+n4V6/O"
            + "xHIM2Ch/dviAb3vz+M9trErv9t+d2H8jNXT3sHuDb/kvAgMBAAGjgaAwgZ0wHwYD"
            + "VR0jBBgwFoAUh1cxWFRY+nMsx4odQQI1GqyFxP8wSwYIKwYBBQUHAQEEPzA9MDsG"
            + "CCsGAQUFBzABhi9odHRwOi8vZG9ndGFnMjAudXNlcnN5cy5yZWRoYXQuY29tOjgw"
            + "ODAvY2Evb2NzcDAOBgNVHQ8BAf8EBAMCBSAwHQYDVR0lBBYwFAYIKwYBBQUHAwIG"
            + "CCsGAQUFBwMEMA0GCSqGSIb3DQEBCwUAA4IBAQCvmbUzQOouE2LgQQcKfmgwwJMJ"
            + "9tMrPwDUtyFdaIFoPL4uZaujSscaN4IWK2r5vIMJ65jwYCI7sI9En2ZfO28J9dQj"
            + "lpqu6TaJ+xtaMk7OvXpVB7lJk73HAttMGjETlkoq/6EjxcugmJsDqVD0b2tO7Vd0"
            + "hroBe2uPDHM2ASewZF415lUcRh0URtmxSazTInbyxpmy1wgSJQ0C6fMCeT+hUFlA"
            + "0P4k1TIprapGVq7FpKcqlhK2gTBfTSnoO7gmXG/9MxJiYpb/Aph8ptXq6quHz1Mj"
            + "greWr3xTsy6gF2yphUEkGHh4v22XvK+FLx9Jb6zloMWA2GG9gpUpvMnl1fH4";

    log("Requesting X509 key recovery.");
    recoveryRequestId = keyClient.recoverKey(new KeyId(keyID), null, null, null, b64Certificate)
            .getRequestInfo().getRequestId();
    log("Requesting X509 key recovery request: " + recoveryRequestId);

    // Test 55: Approve x509 key recovery
    log("Approving X509 key recovery request: " + recoveryRequestId);
    keyClient.approveRequest(recoveryRequestId);

    // Test 16: Recover x509 key
    log("Recovering X509 key based on request: " + recoveryRequestId);
    try {
        // KeyData recoveredX509Key = client.recoverKey(recoveryRequestId, "netscape");
        // log("Success: X509Key recovered: "+ recoveredX509Key.getP12Data());
    } catch (RequestNotFoundException e) {
        log("Error: recovering X509Key");
    }

    // Test 1: Get transport certificate from DRM
    transportCert = systemCertClient.getTransportCert().getEncoded();
    transportCert = transportCert.substring(CertData.HEADER.length(), transportCert.indexOf(CertData.FOOTER));

    log("Transport Cert retrieved from DRM: " + transportCert);

    // Test 17: Get list of completed key archival requests
    log("\n\nList of completed archival requests");
    list = keyClient.listRequests("complete", IRequest.SYMKEY_GENERATION_REQUEST);
    if (list.getTotal() == 0) {
        log("No requests found");
    } else {
        Iterator<KeyRequestInfo> iter = list.getEntries().iterator();
        while (iter.hasNext()) {
            KeyRequestInfo info = iter.next();
            printRequestInfo(info);
        }
    }

    // test 18: Generate symmetric key
    clientKeyId = "Symmetric Key #1234f " + Calendar.getInstance().getTime().toString();
    List<String> usages = new ArrayList<String>();
    usages.add(SymKeyGenerationRequest.DECRYPT_USAGE);
    usages.add(SymKeyGenerationRequest.ENCRYPT_USAGE);
    KeyRequestResponse genKeyResponse = keyClient.generateSymmetricKey(clientKeyId,
            KeyRequestResource.AES_ALGORITHM, 128, usages, null);
    printRequestInfo(genKeyResponse.getRequestInfo());
    keyId = genKeyResponse.getKeyId();

    // test 19: Get keyId for active key with client ID
    log("Getting key ID for symmetric key");
    keyInfo = keyClient.getActiveKeyInfo(clientKeyId);
    printKeyInfo(keyInfo);
    keyId2 = keyInfo.getKeyId();
    if (keyId2 == null) {
        log("No archived key found");
    } else {
        log("Archived Key found: " + keyId);
    }

    if (!keyId.equals(keyId2)) {
        log("Error: key ids from search and archival do not match");
    } else {
        log("Success: keyids from search and archival match.");
    }

    // Test 20: Submit a recovery request for the symmetric key using a session key
    log("Submitting a recovery request for the  symmetric key using session key");
    try {
        sessionKey = nssCrypto.generateSessionKey();
        wrappedRecoveryKey = nssCrypto.wrapSessionKeyWithTransportCert(sessionKey, transportCert);
        keyData = keyClient.retrieveKey(keyId, wrappedRecoveryKey);
    } catch (Exception e) {
        log("Exception in recovering symmetric key using session key: " + e.getMessage());
    }

    encryptedData = keyData.getEncryptedData();

    try {
        recoveredKey = new String(nssCrypto.unwrapWithSessionKey(encryptedData, sessionKey,
                KeyRequestResource.DES3_ALGORITHM, keyData.getNonceData()));
    } catch (Exception e) {
        log("Exception in unwrapping key: " + e.toString());
        e.printStackTrace();
    }

    // test 21: Generate symmetric key - invalid algorithm
    try {
        genKeyResponse = keyClient.generateSymmetricKey("Symmetric Key #1235", "AFS", 128, usages, null);
    } catch (Exception e) {
        log("Exception: " + e);
    }

    // test 22: Generate symmetric key - invalid key size
    try {
        genKeyResponse = keyClient.generateSymmetricKey("Symmetric Key #1236", "AES", 0, usages, null);
    } catch (Exception e) {
        log("Exception: " + e);
    }

    // test 23: Generate symmetric key - usages not defined
    try {
        genKeyResponse = keyClient.generateSymmetricKey("Symmetric Key #1236", "DES", 56, null, null);
    } catch (Exception e) {
        log("Exception: " + e);
    }

    // Test 24: Generate and archive a symmetric key of type AES
    log("Archiving symmetric key");
    clientKeyId = "UUID: 123-45-6789 VEK " + Calendar.getInstance().getTime().toString();
    try {
        vek = nssCrypto.generateSymmetricKey(KeyRequestResource.AES_ALGORITHM, 128);

        byte[] encoded = CryptoUtil.createPKIArchiveOptions(nssCrypto.getManager(), nssCrypto.getToken(),
                transportCert, vek, null, KeyGenAlgorithm.DES3, 0, new IVParameterSpec(iv));

        KeyRequestResponse response = keyClient.archivePKIOptions(clientKeyId,
                KeyRequestResource.SYMMETRIC_KEY_TYPE, KeyRequestResource.AES_ALGORITHM, 128, encoded);
        log("Archival Results:");
        printRequestInfo(response.getRequestInfo());
        keyId = response.getKeyId();
    } catch (Exception e) {
        log("Exception in archiving symmetric key:" + e.getMessage());
        e.printStackTrace();
    }

    //Test 25: Get keyId for active key with client ID
    log("Getting key ID for symmetric key");
    keyInfo = keyClient.getActiveKeyInfo(clientKeyId);
    printKeyInfo(keyInfo);
    keyId2 = keyInfo.getKeyId();
    if (keyId2 == null) {
        log("No archived key found");
    } else {
        log("Archived Key found: " + keyId);
    }

    if (!keyId.equals(keyId2)) {
        log("Error: key ids from search and archival do not match");
    } else {
        log("Success: keyids from search and archival match.");
    }

    // Test 26: Submit a recovery request for the symmetric key
    log("Submitting a recovery request for the  symmetric key without using session key");
    try {
        keyData = keyClient.retrieveKey(keyId);
    } catch (Exception e) {
        log("Exception in recovering symmetric key using session key: " + e.getMessage());
    }

    // Since no session key is provided externally, the retrieveKey method
    // generates a session key, wraps it with transport cert and completes the request.
    // The encrypted data is then unwrapped using the temporary session key and set to
    // the attribute privateData.
    recoveredKey = Utils.base64encode(keyData.getData());

    if (!recoveredKey.equals(Utils.base64encode(vek.getEncoded()))) {
        log("Error: recovered and archived keys do not match!");
    } else {
        log("Success: recoverd and archived keys match!");
    }

    // Test 27: Get key info
    log("getting key info for existing key");
    printKeyInfo(keyClient.getKeyInfo(keyId));

    //Test 28: Modify status
    log("modify the key status");
    keyClient.modifyKeyStatus(keyId, KeyResource.KEY_STATUS_INACTIVE);
    keyInfo = keyClient.getKeyInfo(keyId);
    printKeyInfo(keyInfo);

    //Test 29:  Confirm no more active keys with this ID
    log("look for active keys with this id");
    clientKeyId = keyInfo.getClientKeyID();
    try {
        keyInfo = keyClient.getActiveKeyInfo(clientKeyId);
        printKeyInfo(keyInfo);
    } catch (ResourceNotFoundException e) {
        log("Success: ResourceNotFound exception thrown: " + e);
    }

    // Test asymmetric key generation.

    String[] algs = { "RSA", "DSA" };
    for (int i = 0; i < algs.length; i++) {
        // Test 30: Generate Asymmetric keys - RSA key
        System.out.println("\nTesting asymmetric key generation for algorithm " + algs[i]);
        clientKeyId = "AsymKey #" + Calendar.getInstance().getTimeInMillis();
        usages.clear();
        usages.add(AsymKeyGenerationRequest.SIGN);
        usages.add(AsymKeyGenerationRequest.VERIFY);
        KeyRequestResponse response = keyClient.generateAsymmetricKey(clientKeyId, algs[i], 1024, usages, null);
        printRequestInfo(response.getRequestInfo());
        System.out.println();

        // Test 31: Get information of the newly generated asymmetric keys
        System.out.println("Fetch information of the newly generated asymmetric keys.");
        System.out.println();
        KeyInfo info = keyClient.getKeyInfo(response.getKeyId());
        printKeyInfo(info);
        System.out.println();

        // Test 32: Retrieve private key data
        System.out.println("Retrieving and verifying the generated private key.");
        try {
            keyData = keyClient.retrieveKey(response.getKeyId());
        } catch (Exception e) {
            log("Exception retrieving the private key data.");
            e.printStackTrace();
        }

        // Test 33: Verify the generated key pair.
        if (isKeyPairValid(algs[i], keyData.getData(), info.getPublicKey())) {
            log("The key pair generated using " + algs[i] + " algorithm is valid.");
        } else {
            log("The key pair generated using " + algs[i] + " algorithm is invalid.");
        }
        System.out.println();
    }

    // Test 34:
}

From source file:com.g2inc.scap.library.domain.SCAPContentManager.java

/**
 * A main method only used for testing/*from w w w.  ja va 2 s  . co m*/
 *
 * @param args Command-line arguments
 * 
*/
public static void main(String[] args) throws Exception {
    BasicConfigurator.configure();
    LOG.trace("SchemaParser starting, schemaVersion =" + args[0] + ", xsd File=" + args[1]);
    File xsdFile = new File(args[1]);
    if (!xsdFile.exists()) {
        LOG.error("Usage: java SchemaParser <name of xsd file>");
    }
    SCAPDocumentTypeEnum version = SCAPDocumentTypeEnum.valueOf(args[0]);
    SCAPContentManager mgr = SCAPContentManager.getInstance(new File(args[1]));

    // schema-related data should now be populated
    List<String> platformTypes = mgr.getValidPlatforms(version);
    LOG.trace("STARTING PLATFORMS");
    for (int i = 0; i < platformTypes.size(); i++) {
        LOG.trace(platformTypes.get(i));
    }

    String[] platforms = { "windows", "independent" };

    List<NameDoc> list = null;
    Iterator<NameDoc> iter = null;
    LOG.trace("STARTING TESTS");
    list = mgr.getValidTestTypes(version, platforms);
    iter = list.iterator();
    while (iter.hasNext()) {
        NameDoc testName = iter.next();
        LOG.trace("\t" + testName);
    }
    LOG.trace("STARTING OBJECTS");
    list = mgr.getValidObjectTypes(version, platforms);
    iter = list.iterator();
    while (iter.hasNext()) {
        NameDoc objName = iter.next();
        LOG.trace("\t" + objName);
        LOG.trace("STARTING OBJECT ENTITIES FOR " + objName.getName());
        List<OvalEntity> entityList = mgr.getValidObjectEntityTypes(version, "windows", objName.getName());
        if (entityList != null && entityList.size() > 0) {
            for (int i = 0; i < entityList.size(); i++) {
                LOG.trace("\t\t" + entityList.get(i).toString());
            }
        }
    }

    LOG.trace("STARTING STATES");
    list = mgr.getValidStateTypes(version, platforms);
    iter = list.iterator();
    while (iter.hasNext()) {
        NameDoc stateName = iter.next();
        LOG.trace("\t" + stateName);
        LOG.trace("STARTING STATE ENTITIES FOR " + stateName.getName());
        List<OvalEntity> entityList = mgr.getValidStateEntityTypes(version, "windows", stateName.getName());
        if (entityList != null && entityList.size() > 0) {
            for (int i = 0; i < entityList.size(); i++) {
                LOG.trace("\t\t" + entityList.get(i).toString());
            }
        }
    }
}

From source file:com.act.biointerpretation.metadata.ProteinMetadataFactory.java

public static void main(String[] args) throws Exception {
    // TODO: This is referencing a temporary collection. Change it!
    // TODO: FIX THIS BEFORE MERGE!
    NoSQLAPI api = new NoSQLAPI("actv01_vijay_proteins", "actv01_vijay_proteins");
    Iterator<Reaction> iterator = api.readRxnsFromInKnowledgeGraph();

    //Create a single instance of the factory method to use for all json
    ProteinMetadataFactory factory = ProteinMetadataFactory.initiate();

    //Run some tests
    try {//from ww  w. ja  va2  s.c o  m
        if (factory.testHandlesubunits() == true) {
            System.out.println("Subunit test OK");
        }
    } catch (Exception err) {
        System.err.println("Failed to test subunits");
    }

    //Create a list to aggregate the results of the database scan
    List<ProteinMetadata> agg = new ArrayList<>();

    //Scan the database and store ProteinMetadata objects
    while (iterator.hasNext()) {
        Reaction rxn = iterator.next();

        Reaction.RxnDataSource source = rxn.getDataSource();
        if (!source.equals(Reaction.RxnDataSource.BRENDA)) {
            continue;
        }

        Set<JSONObject> jsons = rxn.getProteinData();

        for (JSONObject json : jsons) {
            ProteinMetadata meta = factory.create(json);
            agg.add(meta);
        }
    }

    //Write out any messages to file
    StringBuilder sb = new StringBuilder();
    for (String aline : factory.dataList) {
        sb.append(aline).append("\n");
    }

    File outfile = new File("output/ProteinMetadata/Factory_output.txt");
    if (outfile.exists()) {
        outfile.delete();
    }
    FileUtils.writeStringToFile(outfile, sb.toString());

    sb = new StringBuilder();
    for (String key : factory.dataMap.keySet()) {
        int value = factory.dataMap.get(key);
        sb.append(key + "\t" + value + "\n");
    }

    outfile = new File("output/ProteinMetadata/Factory_output_map.txt");
    if (outfile.exists()) {
        outfile.delete();
    }
    FileUtils.writeStringToFile(outfile, sb.toString());

    //Count up the results of modifications to get statistics
    int falsecount = 0;
    int truecount = 0;
    int nullcount = 0;

    for (ProteinMetadata datum : agg) {
        if (datum == null) {
            System.err.println("null datum");
            continue;
        }
        if (datum.modifications == null) {
            nullcount++;
        } else if (datum.modifications == false) {
            falsecount++;
        } else if (datum.modifications == true) {
            truecount++;
        }
    }
    System.out.println("Total # protein metadata: " + agg.size());
    System.out.println();
    System.out.println("modification true count: " + truecount);
    System.out.println("modification false count: " + falsecount);
    System.out.println("modification null count: " + nullcount);
    System.out.println();

    //Get some statistics for cloned
    nullcount = 0;
    int emptycount = 0;
    int colicount = 0;
    int humancount = 0;
    int bothcount = 0;
    for (ProteinMetadata datum : agg) {
        if (datum == null) {
            System.err.println("null datum");
            continue;
        }
        if (datum.cloned == null) {
            nullcount++;
            continue;
        }
        if (datum.cloned.isEmpty()) {
            emptycount++;
            continue;
        }
        Integer human = datum.cloned.get(Host.Hsapiens);
        if (human != null && human > 0) {
            humancount++;
        }
        Integer coli = datum.cloned.get(Host.Ecoli);
        if (coli != null && coli > 0) {
            colicount++;
            if (human != null && human > 0) {
                bothcount++;
            }
        }
    }

    System.out.println("cloned null count: " + nullcount);
    System.out.println("cloned empty count: " + emptycount);
    System.out.println("cloned coli count: " + colicount);
    System.out.println("cloned human count: " + humancount);
    System.out.println("cloned both count: " + bothcount);
    System.out.println();
}

From source file:com.cloud.test.stress.StressTestDirectAttach.java

public static void main(String[] args) {
    String host = "http://localhost";
    String port = "8092";
    String devPort = "8080";
    String apiUrl = "/client/api";

    try {/*from w ww  .java2 s.  c o m*/
        // Parameters
        List<String> argsList = Arrays.asList(args);
        Iterator<String> iter = argsList.iterator();
        while (iter.hasNext()) {
            String arg = iter.next();
            // host
            if (arg.equals("-h")) {
                host = "http://" + iter.next();
            }

            if (arg.equals("-p")) {
                port = iter.next();
            }
            if (arg.equals("-dp")) {
                devPort = iter.next();
            }

            if (arg.equals("-t")) {
                numThreads = Integer.parseInt(iter.next());
            }

            if (arg.equals("-s")) {
                sleepTime = Long.parseLong(iter.next());
            }
            if (arg.equals("-a")) {
                accountName = iter.next();
            }

            if (arg.equals("-c")) {
                cleanUp = Boolean.parseBoolean(iter.next());
                if (!cleanUp)
                    sleepTime = 0L; // no need to wait if we don't ever
                // cleanup
            }

            if (arg.equals("-r")) {
                repeat = Boolean.parseBoolean(iter.next());
            }

            if (arg.equals("-i")) {
                internet = Boolean.parseBoolean(iter.next());
            }

            if (arg.equals("-w")) {
                wait = Integer.parseInt(iter.next());
            }

            if (arg.equals("-z")) {
                zoneId = iter.next();
            }

            if (arg.equals("-so")) {
                serviceOfferingId = iter.next();
            }

        }

        final String server = host + ":" + port + "/";
        final String developerServer = host + ":" + devPort + apiUrl;
        s_logger.info("Starting test against server: " + server + " with " + numThreads + " thread(s)");
        if (cleanUp)
            s_logger.info("Clean up is enabled, each test will wait " + sleepTime + " ms before cleaning up");

        for (int i = 0; i < numThreads; i++) {
            new Thread(new Runnable() {
                @Override
                public void run() {
                    do {
                        String username = null;
                        try {
                            long now = System.currentTimeMillis();
                            Random ran = new Random();
                            username = Math.abs(ran.nextInt()) + "-user";
                            NDC.push(username);

                            s_logger.info("Starting test for the user " + username);
                            int response = executeDeployment(server, developerServer, username);
                            boolean success = false;
                            String reason = null;

                            if (response == 200) {
                                success = true;
                                if (internet) {
                                    s_logger.info("Deploy successful...waiting 5 minute before SSH tests");
                                    Thread.sleep(300000L); // Wait 60
                                    // seconds so
                                    // the windows VM
                                    // can boot up and do a sys prep.

                                    s_logger.info("Begin Linux SSH test for account " + _account.get());
                                    reason = sshTest(_linuxIP.get(), _linuxPassword.get());

                                    if (reason == null) {
                                        s_logger.info(
                                                "Linux SSH test successful for account " + _account.get());
                                    }
                                }
                                if (reason == null) {
                                    if (internet) {
                                        s_logger.info(
                                                "Windows SSH test successful for account " + _account.get());
                                    } else {
                                        s_logger.info("deploy test successful....now cleaning up");
                                        if (cleanUp) {
                                            s_logger.info(
                                                    "Waiting " + sleepTime + " ms before cleaning up vms");
                                            Thread.sleep(sleepTime);
                                        } else {
                                            success = true;
                                        }
                                    }

                                    if (usageIterator >= numThreads) {
                                        int eventsAndBillingResponseCode = executeEventsAndBilling(server,
                                                developerServer);
                                        s_logger.info(
                                                "events and usage records command finished with response code: "
                                                        + eventsAndBillingResponseCode);
                                        usageIterator = 1;

                                    } else {
                                        s_logger.info(
                                                "Skipping events and usage records for this user: usageIterator "
                                                        + usageIterator + " and number of Threads "
                                                        + numThreads);
                                        usageIterator++;
                                    }

                                    if ((users == null) && (accountName == null)) {
                                        s_logger.info("Sending cleanup command");
                                        int cleanupResponseCode = executeCleanup(server, developerServer,
                                                username);
                                        s_logger.info("cleanup command finished with response code: "
                                                + cleanupResponseCode);
                                        success = (cleanupResponseCode == 200);
                                    } else {
                                        s_logger.info("Sending stop DomR / destroy VM command");
                                        int stopResponseCode = executeStop(server, developerServer, username);
                                        s_logger.info("stop(destroy) command finished with response code: "
                                                + stopResponseCode);
                                        success = (stopResponseCode == 200);
                                    }

                                } else {
                                    // Just stop but don't destroy the
                                    // VMs/Routers
                                    s_logger.info("SSH test failed for account " + _account.get()
                                            + "with reason '" + reason + "', stopping VMs");
                                    int stopResponseCode = executeStop(server, developerServer, username);
                                    s_logger.info(
                                            "stop command finished with response code: " + stopResponseCode);
                                    success = false; // since the SSH test
                                    // failed, mark the
                                    // whole test as
                                    // failure
                                }
                            } else {
                                // Just stop but don't destroy the
                                // VMs/Routers
                                s_logger.info("Deploy test failed with reason '" + reason + "', stopping VMs");
                                int stopResponseCode = executeStop(server, developerServer, username);
                                s_logger.info("stop command finished with response code: " + stopResponseCode);
                                success = false; // since the deploy test
                                // failed, mark the
                                // whole test as failure
                            }

                            if (success) {
                                s_logger.info("***** Completed test for user : " + username + " in "
                                        + ((System.currentTimeMillis() - now) / 1000L) + " seconds");

                            } else {
                                s_logger.info("##### FAILED test for user : " + username + " in "
                                        + ((System.currentTimeMillis() - now) / 1000L)
                                        + " seconds with reason : " + reason);
                            }
                            s_logger.info("Sleeping for " + wait + " seconds before starting next iteration");
                            Thread.sleep(wait);
                        } catch (Exception e) {
                            s_logger.warn("Error in thread", e);
                            try {
                                int stopResponseCode = executeStop(server, developerServer, username);
                                s_logger.info("stop response code: " + stopResponseCode);
                            } catch (Exception e1) {
                            }
                        } finally {
                            NDC.clear();
                        }
                    } while (repeat);
                }
            }).start();
        }
    } catch (Exception e) {
        s_logger.error(e);
    }
}

From source file:com.ibm.watson.catalyst.corpus.tfidf.ApplyTemplate.java

public static void main(String[] args) {

    System.out.println("Loading Corpus.");
    JsonNode root;/*from w w  w . j  a  va  2s. c o  m*/
    TermCorpus c;
    JsonNode documents;
    try (InputStream in = new FileInputStream(new File("tfidf-health-1.json"))) {
        root = MAPPER.readTree(in);
        documents = root.get("documents");
        TermCorpusBuilder cb = new TermCorpusBuilder();
        cb.setDocumentCombiner(0, 0);
        cb.setJson(new File("health-corpus.json"));
        c = cb.build();
    } catch (FileNotFoundException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
        return;
    } catch (JsonProcessingException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
        return;
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
        return;
    }
    System.out.println("Corpus loaded.");

    List<TemplateMatch> matches = new ArrayList<TemplateMatch>();
    Iterator<TermDocument> documentIterator = c.getDocuments().iterator();

    int index = 0;
    for (JsonNode document : documents) {
        Pattern p1 = Template.getTemplatePattern(document, "\\b(an? |the )?(\\w+ ){0,4}",
                "( \\w+)?(?= is (an?|one|the)\\b)");
        if (p1.toString().equals("\\b(an? |the )?(\\w+ ){0,4}()( \\w+)?(?= is (an?|one|the)\\b)"))
            continue;
        Pattern p2 = Template.getTemplatePattern(document, "^(\\w+ ){0,2}",
                "( \\w+){0,1}?(?=( can| may)? causes?\\b)");
        Pattern p3 = Template.getTemplatePattern(document, "(?<=the use of )(\\w+ ){0,3}",
                "( \\w+| ){0,2}?(?=( (and|does|in|for|can|is|as|to|of)\\b|\\.))");
        Pattern p4 = Template.getTemplatePattern(document, "^(\\w+ ){0,3}",
                "( \\w+){0,1}(?=( can| may) leads? to\\b)");
        Pattern p5 = Template.getTemplatePattern(document, "(?<=\\bthe risk of )(\\w+ ){0,3}",
                "( (disease|stroke|attack|cancer))?\\b");
        Pattern p6 = Template.getTemplatePattern(document, "(\\w{3,} ){0,3}",
                "( (disease|stroke|attack|cancer))?(?= is caused by\\b)");
        Pattern p7 = Template.getTemplatePattern(document, "(?<= is caused by )(\\w+ ){0,10}", "");
        Pattern p8 = Template.getTemplatePattern(document, "\\b", "( \\w{4,})(?= can be used)");
        Pattern p9 = Template.getTemplatePattern(document, "(?<= can be used )(\\w+ ){0,10}", "\\b");
        TermDocument d = documentIterator.next();

        DocumentMatcher dm = new DocumentMatcher(d);
        matches.addAll(dm.getParagraphMatches(p1, "What is ", "?"));
        matches.addAll(dm.getParagraphMatches(p2, "What does ", " cause?"));
        matches.addAll(dm.getParagraphMatches(p3, "How is ", " used?"));
        matches.addAll(dm.getParagraphMatches(p4, "What can ", " lead to?"));
        matches.addAll(dm.getParagraphMatches(p5, "What impacts the risk of ", "?"));
        matches.addAll(dm.getParagraphMatches(p6, "What causes ", "?"));
        matches.addAll(dm.getParagraphMatches(p7, "What is caused by ", "?"));
        matches.addAll(dm.getParagraphMatches(p8, "How can ", " be used?"));
        matches.addAll(dm.getParagraphMatches(p9, "What can be used ", "?"));
        System.out.print("Progress: " + ((100 * ++index) / documents.size()) + "%\r");
    }
    System.out.println();

    List<TemplateMatch> condensedMatches = new ArrayList<TemplateMatch>();

    for (TemplateMatch match : matches) {
        for (TemplateMatch baseMatch : condensedMatches) {
            if (match.sameQuestion(baseMatch)) {
                baseMatch.addAnswers(match);
                break;
            }
        }
        condensedMatches.add(match);
    }

    try (BufferedWriter bw = new BufferedWriter(new FileWriter("health-questions.txt"))) {
        for (TemplateMatch match : condensedMatches) {
            bw.write(match.toString());
        }
        bw.write("\n");
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

    System.out.println("Done and generated: " + condensedMatches.size());

}