Example usage for java.lang String split

List of usage examples for java.lang String split

Introduction

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

Prototype

public String[] split(String regex) 

Source Link

Document

Splits this string around matches of the given regular expression.

Usage

From source file:SparkStreaming.java

public static void main(String[] args) throws Exception {
    JSONObject jo = new JSONObject();
    SparkConf sparkConf = new SparkConf().setAppName("mytestapp").setMaster("local[2]");
    JavaStreamingContext jssc = new JavaStreamingContext(sparkConf, new Duration(2000));

    int numThreads = Integer.parseInt("2");
    Map<String, Integer> topicMap = new HashMap<>();
    String[] topics = "testdemo".split(",");
    for (String topic : topics) {
        topicMap.put(topic, numThreads);
    }/*ww w .  j  a va2s . com*/

    JavaPairReceiverInputDStream<String, String> messages = KafkaUtils.createStream(jssc, "localhost:2181",
            "testdemo", topicMap);

    JavaDStream<String> lines = messages.map(new Function<Tuple2<String, String>, String>() {
        @Override
        public String call(Tuple2<String, String> tuple2) {
            return tuple2._2();
        }
    });

    JavaDStream<String> words = lines.flatMap(new FlatMapFunction<String, String>() {
        @Override
        public Iterable<String> call(String x) throws IOException {
            String temperature = x.substring(x.indexOf(":") + 1);
            /*  System.out.println("Temperature from spark++++++++++ "+temperature);
              URL url = new URL("http://localhost:8080/apachetest/Test?var1="+temperature);
              URLConnection conn = url.openConnection();
               conn.setDoOutput(true);*/
            final ThermometerDemo2 demo = new ThermometerDemo2("Thermometer Demo 2", temperature);
            demo.pack();
            demo.setVisible(true);
            return Arrays.asList(x.split(" "));
        }
    });

    JavaPairDStream<String, Integer> wordCounts = words.mapToPair(new PairFunction<String, String, Integer>() {
        @Override
        public Tuple2<String, Integer> call(String s) {
            return new Tuple2<>(s, 1);
        }
    }).reduceByKey(new Function2<Integer, Integer, Integer>() {
        @Override
        public Integer call(Integer i1, Integer i2) {
            return i1 + i2;
        }
    });

    wordCounts.print();
    jssc.start();
    jssc.awaitTermination();
}

From source file:com.twitter.hraven.rest.client.HRavenRestClient.java

public static void main(String[] args) throws IOException {
    String apiHostname = null;//from   w  w w  . j  a v  a  2s  . c o m
    String cluster = null;
    String username = null;
    String batchDesc = null;
    String signature = null;
    int limit = 2;
    boolean useHBaseAPI = false;
    boolean dumpJson = false;
    boolean hydrateTasks = false;
    List<String> taskResponseFilters = new ArrayList<String>();
    List<String> jobResponseFilters = new ArrayList<String>();
    List<String> flowResponseFilters = new ArrayList<String>();
    List<String> configFields = new ArrayList<String>();

    StringBuffer usage = new StringBuffer("Usage: java ");
    usage.append(HRavenRestClient.class.getName()).append(" [-options]\n");
    usage.append("Returns data from recent flows and their associated jobs\n");
    usage.append("where options include: \n");
    usage.append(" -a <API hostname> [required]\n");
    usage.append(" -c <cluster> [required]\n");
    usage.append(" -u <username> [required]\n");
    usage.append(" -f <flowName> [required]\n");
    usage.append(" -s <signature>\n");
    usage.append(" -l <limit>\n");
    usage.append(" -h - print this message and return\n");
    usage.append(" -H - use HBase API, not the REST API\n");
    usage.append(" -j - output json\n");
    usage.append(" -t - retrieve task information as well");
    usage.append(" -w - config field to be included in job response");
    usage.append(" -z - field to be included in task response");
    usage.append(" -y - field to be included in job response");
    usage.append(" -x - field to be included in flow response");

    for (int i = 0; i < args.length; i++) {
        if ("-a".equals(args[i])) {
            apiHostname = args[++i];
            continue;
        } else if ("-c".equals(args[i])) {
            cluster = args[++i];
            continue;
        } else if ("-u".equals(args[i])) {
            username = args[++i];
            continue;
        } else if ("-f".equals(args[i])) {
            batchDesc = args[++i];
            continue;
        } else if ("-s".equals(args[i])) {
            signature = args[++i];
            continue;
        } else if ("-l".equals(args[i])) {
            limit = Integer.parseInt(args[++i]);
            continue;
        } else if ("-H".equals(args[i])) {
            useHBaseAPI = true;
            continue;
        } else if ("-j".equals(args[i])) {
            dumpJson = true;
            continue;
        } else if ("-t".equals(args[i])) {
            hydrateTasks = true;
            continue;
        } else if ("-z".equals(args[i])) {
            String taskFilters = args[++i];
            taskResponseFilters = Arrays.asList(taskFilters.split(","));
            continue;
        } else if ("-y".equals(args[i])) {
            String jobFilters = args[++i];
            jobResponseFilters = Arrays.asList(jobFilters.split(","));
            continue;
        } else if ("-x".equals(args[i])) {
            String flowFilters = args[++i];
            flowResponseFilters = Arrays.asList(flowFilters.split(","));
            continue;
        } else if ("-w".equals(args[i])) {
            String configFilters = args[++i];
            configFields = Arrays.asList(configFilters.split(","));
            continue;
        } else if ("-h".equals(args[i])) {
            System.err.println(usage.toString());
            System.exit(1);
        } else {

        }
    }

    if (apiHostname == null || cluster == null || username == null || batchDesc == null) {
        System.err.println(usage.toString());
        System.exit(1);
    }

    List<Flow> flows;
    if (useHBaseAPI) {
        JobHistoryService jobHistoryService = new JobHistoryService(HBaseConfiguration.create());
        flows = jobHistoryService.getFlowSeries(cluster, username, batchDesc, signature, hydrateTasks, limit);
    } else {
        HRavenRestClient client = new HRavenRestClient(apiHostname, 100000, 100000);

        // use this call to call flows without configs
        flows = client.fetchFlows(cluster, username, batchDesc, signature, flowResponseFilters,
                jobResponseFilters, limit);
        // use this call to call flows with configs
        flows = client.fetchFlowsWithConfig(cluster, username, batchDesc, signature, limit, flowResponseFilters,
                jobResponseFilters, configFields);
        // use this call to call flows with config patterns
        flows = client.fetchFlowsWithConfig(cluster, username, batchDesc, signature, limit, flowResponseFilters,
                jobResponseFilters, configFields);

        if (hydrateTasks) {
            for (Flow flow : flows) {
                for (JobDetails jd : flow.getJobs()) {
                    String jobId = jd.getJobId();
                    List<TaskDetails> td = client.fetchTaskDetails(cluster, jobId, taskResponseFilters);
                    jd.addTasks(td);
                }
            }
        }
    }

    if (dumpJson) {
        ObjectMapper om = ObjectMapperProvider.createCustomMapper();
        SimpleModule module = new SimpleModule("hRavenModule", new Version(0, 4, 0, null));
        module.addSerializer(Flow.class, new FlowSerializer());
        module.addSerializer(JobDetails.class, new JobDetailsSerializer());
        om.registerModule(module);
        if (flows.size() > 0) {
            System.out.println(om.writeValueAsString(flows.get(0)));
        }
        return;
    }

    System.out.println("Found " + flows.size() + " flows");
    StringBuilder sb = new StringBuilder();
    sb.append("\t\t").append("jobId");
    sb.append("\t\t").append("version");
    sb.append("\t\t").append("status");
    sb.append("\t").append("maps");
    sb.append("\t").append("reduces");
    sb.append("\t").append("rBytesRead");
    sb.append("\t").append("feature");
    sb.append("\t\t\t").append("alias");
    System.out.println(sb.toString());

    int i = 0;
    for (Flow flow : flows) {
        long minSubmitTime = -1, maxFinishTime = -1;
        for (JobDetails job : flow.getJobs()) {
            if (minSubmitTime == -1 && job.getSubmitTime() > 0) {
                minSubmitTime = job.getSubmitTime();
            }
            minSubmitTime = Math.min(minSubmitTime, job.getSubmitTime());
            maxFinishTime = Math.max(maxFinishTime, job.getFinishTime());
        }

        if (minSubmitTime > 0 && maxFinishTime > 0) {
            System.out.println(String.format("Flow #%d: %s - %s", i++, DATE_FORMAT.format(minSubmitTime),
                    DATE_FORMAT.format(maxFinishTime)));
        } else {
            System.out.println(String.format("Flow #%d:", i++));
        }

        for (JobDetails job : flow.getJobs()) {
            sb = new StringBuilder();
            sb.append(" - ").append(job.getJobId());
            sb.append("\t").append(job.getVersion());
            sb.append("\t").append(job.getStatus());
            sb.append("\t").append(job.getTotalMaps());
            sb.append("\t").append(job.getTotalReduces());
            long reduceBytesRead = job.getReduceCounters().getCounter("FileSystemCounters",
                    "FILE_BYTES_READ") != null
                            ? job.getReduceCounters().getCounter("FileSystemCounters", "FILE_BYTES_READ")
                                    .getValue()
                            : -1;
            sb.append("\t").append(reduceBytesRead);
            sb.append("\t").append(job.getConfiguration().get("pig.job.feature"));
            sb.append("\t").append(job.getConfiguration().get("pig.alias"));
            System.out.println(sb.toString());
        }
    }
}

From source file:de.tudarmstadt.ukp.experiments.dip.wp1.documents.Step7CollectMTurkResults.java

public static void main(String[] args) throws Exception {
    // input dir - list of xml query containers
    // /home/user-ukp/research/data/dip/wp1-documents/step4-boiler-plate/
    File inputDir = new File(args[0] + "/");

    // MTurk result file

    // output dir
    File outputDir = new File(args[2]);
    if (!outputDir.exists()) {
        outputDir.mkdirs();//from w w w . j  a  v  a 2 s .  c o m

    }

    // Folder with success files
    File mturkSuccessDir = new File(args[1]);

    Collection<File> files = FileUtils.listFiles(mturkSuccessDir, new String[] { "result" }, false);
    if (files.isEmpty()) {
        throw new IllegalArgumentException("Input folder is empty. " + mturkSuccessDir);
    }

    HashMap<String, List<MTurkAnnotation>> mturkAnnotations = new HashMap<>();

    // parsing all CSV files
    for (File mturkCSVResultFile : files) {
        System.out.println("Parsing " + mturkCSVResultFile.getName());

        MTurkOutputReader outputReader = new MTurkOutputReader(
                new HashSet<>(Arrays.asList("annotation", "workerid")), mturkCSVResultFile);

        // for fixing broken data input: for each hit, collect all sentence IDs
        Map<String, SortedSet<String>> hitSentences = new HashMap<>();

        // first iteration: collect the sentences
        for (Map<String, String> record : outputReader) {
            String hitID = record.get("hitid");
            if (!hitSentences.containsKey(hitID)) {
                hitSentences.put(hitID, new TreeSet<>());
            }

            String relevantSentences = record.get("Answer.relevant_sentences");
            String irrelevantSentences = record.get("Answer.irrelevant_sentences");

            if (relevantSentences != null) {
                hitSentences.get(hitID).addAll(Arrays.asList(relevantSentences.split(",")));
            }

            if (irrelevantSentences != null) {
                hitSentences.get(hitID).addAll(Arrays.asList(irrelevantSentences.split(",")));
            }
        }

        // and now second iteration
        for (Map<String, String> record : outputReader) {
            String hitID = record.get("hitid");
            String annotatorID = record.get("workerid");
            String acceptTime = record.get("assignmentaccepttime");
            String submitTime = record.get("assignmentsubmittime");
            String relevantSentences = record.get("Answer.relevant_sentences");
            String irrelevantSentences = record.get("Answer.irrelevant_sentences");
            String reject = record.get("reject");
            String filename[];
            String comment;
            String clueWeb;
            String[] relevant = {};
            String[] irrelevant = {};

            filename = record.get("annotation").split("_");
            String fileXml = filename[0];
            clueWeb = filename[1].trim();
            comment = record.get("Answer.comment");

            if (relevantSentences != null) {
                relevant = relevantSentences.split(",");
            }

            if (irrelevantSentences != null) {
                irrelevant = irrelevantSentences.split(",");
            }

            // sanitizing data: if both relevant and irrelevant are empty, that's a bug
            // we're gonna look up all sentences from this HIT and treat this assignment
            // as if there were only irrelevant ones
            if (relevant.length == 0 && irrelevant.length == 0) {
                SortedSet<String> strings = hitSentences.get(hitID);
                irrelevant = new String[strings.size()];
                strings.toArray(irrelevant);
            }

            if (reject != null) {
                System.out.println(" HIT " + hitID + " annotated by " + annotatorID + " was rejected ");
            } else {
                /*
                // relevant sentences is a comma-delimited string,
                // this regular expression is rather strange
                // it must contain digits, it might be that there is only one space or a comma or some other char
                // digits are the sentence ids. if relevant sentences do not contain digits then it is wrong
                if (relevantSentences.matches("^\\D*$") &&
                    irrelevantSentences.matches("^\\D*$")) {
                try {
                    throw new IllegalStateException(
                            "No annotations found for HIT " + hitID + " in " +
                                    fileXml + " for document " + clueWeb);
                }
                catch (IllegalStateException ex) {
                    ex.printStackTrace();
                }
                        
                }
                */
                MTurkAnnotation mturkAnnotation;
                try {
                    mturkAnnotation = new MTurkAnnotation(hitID, annotatorID, acceptTime, submitTime, comment,
                            clueWeb, relevant, irrelevant);
                } catch (IllegalArgumentException ex) {
                    throw new IllegalArgumentException("Record: " + record, ex);
                }

                List<MTurkAnnotation> listOfAnnotations = mturkAnnotations.get(fileXml);

                if (listOfAnnotations == null) {
                    listOfAnnotations = new ArrayList<>();
                }
                listOfAnnotations.add(mturkAnnotation);
                mturkAnnotations.put(fileXml, listOfAnnotations);
            }

        }
        //            parser.close();
    }

    // Debugging: output number of HITs of a query
    System.out.println("Accepted HITs for a query:");
    for (Map.Entry e : mturkAnnotations.entrySet()) {
        ArrayList<MTurkAnnotation> a = (ArrayList<MTurkAnnotation>) e.getValue();
        System.out.println(e.getKey() + " " + a.size());
    }

    for (File f : FileUtils.listFiles(inputDir, new String[] { "xml" }, false)) {
        QueryResultContainer queryResultContainer = QueryResultContainer
                .fromXML(FileUtils.readFileToString(f, "utf-8"));
        String fileName = f.getName();
        List<MTurkAnnotation> listOfAnnotations = mturkAnnotations.get(fileName);

        if (listOfAnnotations == null || listOfAnnotations.isEmpty()) {
            throw new IllegalStateException("No annotations for " + f.getName());
        }

        for (QueryResultContainer.SingleRankedResult rankedResults : queryResultContainer.rankedResults) {
            for (MTurkAnnotation mtAnnotation : listOfAnnotations) {
                String clueWeb = mtAnnotation.clueWeb;
                if (rankedResults.clueWebID.equals(clueWeb)) {
                    List<QueryResultContainer.MTurkRelevanceVote> mTurkRelevanceVotes = rankedResults.mTurkRelevanceVotes;
                    QueryResultContainer.MTurkRelevanceVote relevanceVote = new QueryResultContainer.MTurkRelevanceVote();
                    String annotatorID = mtAnnotation.annotatorID;
                    String hitID = mtAnnotation.hitID;
                    String acceptTime = mtAnnotation.acceptTime;
                    String submitTime = mtAnnotation.submitTime;
                    String comment = mtAnnotation.comment;
                    String[] relevant = mtAnnotation.relevant;
                    String[] irrelevant = mtAnnotation.irrelevant;
                    relevanceVote.turkID = annotatorID.trim();
                    relevanceVote.hitID = hitID.trim();
                    relevanceVote.acceptTime = acceptTime.trim();
                    relevanceVote.submitTime = submitTime.trim();
                    relevanceVote.comment = comment != null ? comment.trim() : null;
                    if (relevant.length == 0 && irrelevant.length == 0) {
                        try {
                            throw new IllegalStateException("the length of the annotations is 0"
                                    + rankedResults.clueWebID + " for HIT " + relevanceVote.hitID);
                        } catch (IllegalStateException e) {
                            e.printStackTrace();
                        }
                    }
                    for (String r : relevant) {
                        String sentenceId = r.trim();
                        if (!sentenceId.isEmpty() && sentenceId.matches("\\d+")) {
                            QueryResultContainer.SingleSentenceRelevanceVote singleSentenceVote = new QueryResultContainer.SingleSentenceRelevanceVote();
                            singleSentenceVote.sentenceID = sentenceId;
                            singleSentenceVote.relevant = "true";
                            relevanceVote.singleSentenceRelevanceVotes.add(singleSentenceVote);
                        }
                    }
                    for (String r : irrelevant) {
                        String sentenceId = r.trim();
                        if (!sentenceId.isEmpty() && sentenceId.matches("\\d+")) {
                            QueryResultContainer.SingleSentenceRelevanceVote singleSentenceVote = new QueryResultContainer.SingleSentenceRelevanceVote();
                            singleSentenceVote.sentenceID = sentenceId;
                            singleSentenceVote.relevant = "false";
                            relevanceVote.singleSentenceRelevanceVotes.add(singleSentenceVote);
                        }
                    }
                    mTurkRelevanceVotes.add(relevanceVote);
                }
            }

        }
        File outputFile = new File(outputDir, f.getName());
        FileUtils.writeStringToFile(outputFile, queryResultContainer.toXML(), "utf-8");
        System.out.println("Finished " + outputFile);
    }

}

From source file:com.cws.esolutions.security.main.PasswordUtility.java

public static void main(final String[] args) {
    final String methodName = PasswordUtility.CNAME + "#main(final String[] args)";

    if (DEBUG) {//from   w ww . j a  va 2s  .c o  m
        DEBUGGER.debug("Value: {}", methodName);
    }

    if (args.length == 0) {
        HelpFormatter usage = new HelpFormatter();
        usage.printHelp(PasswordUtility.CNAME, options, true);

        System.exit(1);
    }

    BufferedReader bReader = null;
    BufferedWriter bWriter = null;

    try {
        // load service config first !!
        SecurityServiceInitializer.initializeService(PasswordUtility.SEC_CONFIG, PasswordUtility.LOG_CONFIG,
                false);

        if (DEBUG) {
            DEBUGGER.debug("Options options: {}", options);

            for (String arg : args) {
                DEBUGGER.debug("Value: {}", arg);
            }
        }

        CommandLineParser parser = new PosixParser();
        CommandLine commandLine = parser.parse(options, args);

        if (DEBUG) {
            DEBUGGER.debug("CommandLineParser parser: {}", parser);
            DEBUGGER.debug("CommandLine commandLine: {}", commandLine);
            DEBUGGER.debug("CommandLine commandLine.getOptions(): {}", (Object[]) commandLine.getOptions());
            DEBUGGER.debug("CommandLine commandLine.getArgList(): {}", commandLine.getArgList());
        }

        final SecurityConfigurationData secConfigData = PasswordUtility.svcBean.getConfigData();
        final SecurityConfig secConfig = secConfigData.getSecurityConfig();
        final PasswordRepositoryConfig repoConfig = secConfigData.getPasswordRepo();
        final SystemConfig systemConfig = secConfigData.getSystemConfig();

        if (DEBUG) {
            DEBUGGER.debug("SecurityConfigurationData secConfig: {}", secConfigData);
            DEBUGGER.debug("SecurityConfig secConfig: {}", secConfig);
            DEBUGGER.debug("RepositoryConfig secConfig: {}", repoConfig);
            DEBUGGER.debug("SystemConfig systemConfig: {}", systemConfig);
        }

        if (commandLine.hasOption("encrypt")) {
            if ((StringUtils.isBlank(repoConfig.getPasswordFile()))
                    || (StringUtils.isBlank(repoConfig.getSaltFile()))) {
                System.err.println("The password/salt files are not configured. Entries will not be stored!");
            }

            File passwordFile = FileUtils.getFile(repoConfig.getPasswordFile());
            File saltFile = FileUtils.getFile(repoConfig.getSaltFile());

            if (DEBUG) {
                DEBUGGER.debug("File passwordFile: {}", passwordFile);
                DEBUGGER.debug("File saltFile: {}", saltFile);
            }

            final String entryName = commandLine.getOptionValue("entry");
            final String username = commandLine.getOptionValue("username");
            final String password = commandLine.getOptionValue("password");
            final String salt = RandomStringUtils.randomAlphanumeric(secConfig.getSaltLength());

            if (DEBUG) {
                DEBUGGER.debug("String entryName: {}", entryName);
                DEBUGGER.debug("String username: {}", username);
                DEBUGGER.debug("String password: {}", password);
                DEBUGGER.debug("String salt: {}", salt);
            }

            final String encodedSalt = PasswordUtils.base64Encode(salt);
            final String encodedUserName = PasswordUtils.base64Encode(username);
            final String encryptedPassword = PasswordUtils.encryptText(password, salt,
                    secConfig.getSecretAlgorithm(), secConfig.getIterations(), secConfig.getKeyBits(),
                    secConfig.getEncryptionAlgorithm(), secConfig.getEncryptionInstance(),
                    systemConfig.getEncoding());
            final String encodedPassword = PasswordUtils.base64Encode(encryptedPassword);

            if (DEBUG) {
                DEBUGGER.debug("String encodedSalt: {}", encodedSalt);
                DEBUGGER.debug("String encodedUserName: {}", encodedUserName);
                DEBUGGER.debug("String encodedPassword: {}", encodedPassword);
            }

            if (commandLine.hasOption("store")) {
                try {
                    new File(passwordFile.getParent()).mkdirs();
                    new File(saltFile.getParent()).mkdirs();

                    boolean saltFileExists = (saltFile.exists()) ? true : saltFile.createNewFile();

                    if (DEBUG) {
                        DEBUGGER.debug("saltFileExists: {}", saltFileExists);
                    }

                    // write the salt out first
                    if (!(saltFileExists)) {
                        throw new IOException("Unable to create salt file");
                    }

                    boolean passwordFileExists = (passwordFile.exists()) ? true : passwordFile.createNewFile();

                    if (!(passwordFileExists)) {
                        throw new IOException("Unable to create password file");
                    }

                    if (commandLine.hasOption("replace")) {
                        File[] files = new File[] { saltFile, passwordFile };

                        if (DEBUG) {
                            DEBUGGER.debug("File[] files: {}", (Object) files);
                        }

                        for (File file : files) {
                            if (DEBUG) {
                                DEBUGGER.debug("File: {}", file);
                            }

                            String currentLine = null;
                            File tmpFile = new File(FileUtils.getTempDirectory() + "/" + "tmpFile");

                            if (DEBUG) {
                                DEBUGGER.debug("File tmpFile: {}", tmpFile);
                            }

                            bReader = new BufferedReader(new FileReader(file));
                            bWriter = new BufferedWriter(new FileWriter(tmpFile));

                            while ((currentLine = bReader.readLine()) != null) {
                                if (!(StringUtils.equals(currentLine.trim().split(",")[0], entryName))) {
                                    bWriter.write(currentLine + System.getProperty("line.separator"));
                                    bWriter.flush();
                                }
                            }

                            bWriter.close();

                            FileUtils.deleteQuietly(file);
                            FileUtils.copyFile(tmpFile, file);
                            FileUtils.deleteQuietly(tmpFile);
                        }
                    }

                    FileUtils.writeStringToFile(saltFile, entryName + "," + encodedUserName + "," + encodedSalt
                            + System.getProperty("line.separator"), true);
                    FileUtils.writeStringToFile(passwordFile, entryName + "," + encodedUserName + ","
                            + encodedPassword + System.getProperty("line.separator"), true);
                } catch (IOException iox) {
                    ERROR_RECORDER.error(iox.getMessage(), iox);
                }
            }

            System.out.println("Entry Name " + entryName + " stored.");
        }

        if (commandLine.hasOption("decrypt")) {
            String saltEntryName = null;
            String saltEntryValue = null;
            String decryptedPassword = null;
            String passwordEntryName = null;

            if ((StringUtils.isEmpty(commandLine.getOptionValue("entry"))
                    && (StringUtils.isEmpty(commandLine.getOptionValue("username"))))) {
                throw new ParseException("No entry or username was provided to decrypt.");
            }

            if (StringUtils.isEmpty(commandLine.getOptionValue("username"))) {
                throw new ParseException("no entry provided to decrypt");
            }

            String entryName = commandLine.getOptionValue("entry");
            String username = commandLine.getOptionValue("username");

            if (DEBUG) {
                DEBUGGER.debug("String entryName: {}", entryName);
                DEBUGGER.debug("String username: {}", username);
            }

            File passwordFile = FileUtils.getFile(repoConfig.getPasswordFile());
            File saltFile = FileUtils.getFile(repoConfig.getSaltFile());

            if (DEBUG) {
                DEBUGGER.debug("File passwordFile: {}", passwordFile);
                DEBUGGER.debug("File saltFile: {}", saltFile);
            }

            if ((!(saltFile.canRead())) || (!(passwordFile.canRead()))) {
                throw new IOException(
                        "Unable to read configured password/salt file. Please check configuration and/or permissions.");
            }

            for (String lineEntry : FileUtils.readLines(saltFile, systemConfig.getEncoding())) {
                saltEntryName = lineEntry.split(",")[0];

                if (DEBUG) {
                    DEBUGGER.debug("String saltEntryName: {}", saltEntryName);
                }

                if (StringUtils.equals(saltEntryName, entryName)) {
                    saltEntryValue = PasswordUtils.base64Decode(lineEntry.split(",")[2]);

                    break;
                }
            }

            if (StringUtils.isEmpty(saltEntryValue)) {
                throw new SecurityException("No entries were found that matched the provided information");
            }

            for (String lineEntry : FileUtils.readLines(passwordFile, systemConfig.getEncoding())) {
                passwordEntryName = lineEntry.split(",")[0];

                if (DEBUG) {
                    DEBUGGER.debug("String passwordEntryName: {}", passwordEntryName);
                }

                if (StringUtils.equals(passwordEntryName, saltEntryName)) {
                    String decodedPassword = PasswordUtils.base64Decode(lineEntry.split(",")[2]);

                    decryptedPassword = PasswordUtils.decryptText(decodedPassword, saltEntryValue,
                            secConfig.getSecretAlgorithm(), secConfig.getIterations(), secConfig.getKeyBits(),
                            secConfig.getEncryptionAlgorithm(), secConfig.getEncryptionInstance(),
                            systemConfig.getEncoding());

                    break;
                }
            }

            if (StringUtils.isEmpty(decryptedPassword)) {
                throw new SecurityException("No entries were found that matched the provided information");
            }

            System.out.println(decryptedPassword);
        } else if (commandLine.hasOption("encode")) {
            System.out.println(PasswordUtils.base64Encode((String) commandLine.getArgList().get(0)));
        } else if (commandLine.hasOption("decode")) {
            System.out.println(PasswordUtils.base64Decode((String) commandLine.getArgList().get(0)));
        }
    } catch (IOException iox) {
        ERROR_RECORDER.error(iox.getMessage(), iox);

        System.err.println("An error occurred during processing: " + iox.getMessage());
        System.exit(1);
    } catch (ParseException px) {
        ERROR_RECORDER.error(px.getMessage(), px);

        System.err.println("An error occurred during processing: " + px.getMessage());
        System.exit(1);
    } catch (SecurityException sx) {
        ERROR_RECORDER.error(sx.getMessage(), sx);

        System.err.println("An error occurred during processing: " + sx.getMessage());
        System.exit(1);
    } catch (SecurityServiceException ssx) {
        ERROR_RECORDER.error(ssx.getMessage(), ssx);
        System.exit(1);
    } finally {
        try {
            if (bReader != null) {
                bReader.close();
            }

            if (bWriter != null) {
                bReader.close();
            }
        } catch (IOException iox) {
        }
    }

    System.exit(0);
}

From source file:PodbaseMetadataMigration2.java

public static void main(String[] args) throws Exception {
    System.out.println("Running data migration");

    String projectString = FileUtils.readFileToString(new File("projects.txt"));
    Map<String, Integer> projectIdMapping = new HashMap<String, Integer>();
    for (String line : projectString.split("\n")) {
        String[] split = line.split(":");
        int id = Integer.parseInt(split[0].trim());
        String name = split[1].trim();
        projectIdMapping.put(name, id);//  w  w  w  .  jav a2  s. c  o m
    }

    System.out.println("Reading projects..");
    List<ProjectEntry> projects = dataFromFile("./migrate/projects.data", ProjectEntry.class);
    projectIdMap = parseProjectMap(projects, projectIdMapping);

    System.out.println("Found " + projects.size() + " projects.");

    System.out.println("Reading tags..");
    List<TagEntry> tags = dataFromFile("./migrate/tags.data", TagEntry.class);
    System.out.println("Found " + tags.size() + " tags.");

    System.out.println("Reading templates..");
    List<TemplateEntry> templates = dataFromFile("./migrate/templates.data", TemplateEntry.class);
    System.out.println("Found " + templates.size() + " templates.");

    System.out.println("Reading template fields..");
    List<TemplateFieldEntry> templateFields = dataFromFile("./migrate/template_fields.data",
            TemplateFieldEntry.class);
    System.out.println("Found " + templateFields.size() + " templateFields.");

    int entryCount = tags.size() + templates.size() + templateFields.size();

    //System.out.println("Generating Project SQL");
    //String projectSql = generateSql((List<AbstractEntry>)(List<?>)projects);
    System.out.println("Generating Attribute SQL");
    String imageAttributes = generateSql((List<AbstractEntry>) (List<?>) tags);
    System.out.println("Generating Image SQL");
    String databaseImages = generateDatabaseImageSql();
    //System.out.println("Generating Directory SQL");
    //String directorySql = generateDirectorySql(projects);

    //System.out.println("Generating Template SQL");
    //String templateSql = generateSql((List<AbstractEntry>)(List<?>)templates);
    //System.out.println("Generating Field SQL");
    //String fieldsSql = generateSql((List<AbstractEntry>)(List<?>)templateFields);

    System.out.println("Writing database.sql");
    BufferedWriter bw = new BufferedWriter(new FileWriter(new File("./database.sql")));
    //bw.append(projectSql);
    //bw.append("\n\n");
    bw.append(databaseImages);
    bw.append("\n\n");
    //bw.append(directorySql);
    //bw.append("\n\n");
    bw.append(imageAttributes);
    bw.append("\n\n");
    //      bw.append(templateSql);
    //      bw.append("\n\n");
    //      bw.append(fieldsSql);
    //      bw.append("\n\n");
    bw.close();

    System.out.println("Writing missingImages.txt");
    bw = new BufferedWriter(new FileWriter(new File("./missingImages.txt")));
    for (String img : missingImages) {
        bw.append(img + "\n");
    }
    bw.close();

    System.out.println("Migration completed successfully!");
}

From source file:core.PlanC.java

/**
 * inicio de aplicacion/*from  ww w .  ja  v  a2s. com*/
 * 
 * @param arg - argumentos de entrada
 */
public static void main(String[] args) {

    // user.name

    /*
     * Properties prp = System.getProperties(); System.out.println(getWmicValue("bios", "SerialNumber"));
     * System.out.println(getWmicValue("cpu", "SystemName"));
     */

    try {
        // log
        // -Djava.util.logging.SimpleFormatter.format='%1$tY-%1$tm-%1$td %1$tH:%1$tM:%1$tS %4$s %2$s %5$s%6$s%n'
        // System.setProperty("java.util.logging.SimpleFormatter.format",
        // "%1$tY-%1$tm-%1$td %1$tH:%1$tM:%1$tS %4$s %2$s %5$s%6$s%n");
        System.setProperty("java.util.logging.SimpleFormatter.format",
                "%1$tY-%1$tm-%1$td %1$tH:%1$tM:%1$tS %5$s%6$s%n");

        FileHandler fh = new FileHandler(LOG_FILE);
        fh.setFormatter(new SimpleFormatter());
        fh.setLevel(Level.INFO);
        ConsoleHandler ch = new ConsoleHandler();
        ch.setFormatter(new SimpleFormatter());
        ch.setLevel(Level.INFO);
        logger = Logger.getLogger("");
        Handler[] hs = logger.getHandlers();
        for (int x = 0; x < hs.length; x++) {
            logger.removeHandler(hs[x]);
        }
        logger.addHandler(fh);
        logger.addHandler(ch);
        // point apache log to this log
        System.setProperty("org.apache.commons.logging.Log", Jdk14Logger.class.getName());

        TPreferences.init();
        TStringUtils.init();

        Font fo = Font.createFont(Font.TRUETYPE_FONT, TResourceUtils.getFile("Dosis-Light.ttf"));
        GraphicsEnvironment.getLocalGraphicsEnvironment().registerFont(fo);
        fo = Font.createFont(Font.TRUETYPE_FONT, TResourceUtils.getFile("Dosis-Medium.ttf"));
        GraphicsEnvironment.getLocalGraphicsEnvironment().registerFont(fo);
        fo = Font.createFont(Font.TRUETYPE_FONT, TResourceUtils.getFile("AERO_ITALIC.ttf"));
        GraphicsEnvironment.getLocalGraphicsEnvironment().registerFont(fo);

        SwingTimerTimingSource ts = new SwingTimerTimingSource();
        AnimatorBuilder.setDefaultTimingSource(ts);
        ts.init();

        // parse app argument parameters and append to tpreferences to futher uses
        for (String arg : args) {
            String[] kv = arg.split("=");
            TPreferences.setProperty(kv[0], kv[1]);
        }
        RUNNING_MODE = TPreferences.getProperty("runningMode", RM_NORMAL);

        newMsg = Applet.newAudioClip(TResourceUtils.getURL("newMsg.wav"));

    } catch (Exception e) {
        SystemLog.logException1(e, true);
    }

    // pass icon from metal to web look and feel
    Icon i1 = UIManager.getIcon("OptionPane.errorIcon");
    Icon i2 = UIManager.getIcon("OptionPane.informationIcon");
    Icon i3 = UIManager.getIcon("OptionPane.questionIcon");
    Icon i4 = UIManager.getIcon("OptionPane.warningIcon");
    // Object fcui = UIManager.get("FileChooserUI");
    // JFileChooser fc = new JFileChooser();

    WebLookAndFeel.install();
    // WebLookAndFeel.setDecorateFrames(true);
    // WebLookAndFeel.setDecorateDialogs(true);

    UIManager.put("OptionPane.errorIcon", i1);
    UIManager.put("OptionPane.informationIcon", i2);
    UIManager.put("OptionPane.questionIcon", i3);
    UIManager.put("OptionPane.warningIcon", i4);
    // UIManager.put("TFileChooserUI", fcui);

    // warm up the IDW.
    // in my computer, some weird error ocurr if i don't execute this preload.
    new RootWindow(null);

    frame = new TWebFrame();
    frame.addWindowListener(new WindowAdapter() {
        public void windowClosing(WindowEvent e) {
            Exit.shutdown();
        }
    });

    if (RUNNING_MODE.equals(RM_NORMAL)) {
        initEnviorement();
    }
    if (RUNNING_MODE.equals(RM_CONSOLE)) {
        initConsoleEnviorement();
    }

    if (RUNNING_MODE.equals(ONE_TASK)) {
        String cln = TPreferences.getProperty("taskName", "*TaskNotFound");
        PlanC.logger.log(Level.INFO, "OneTask parameter found in .properties. file Task name = " + cln);
        try {
            Class cls = Class.forName(cln);
            Object dobj = cls.newInstance();
            // new class must be extends form AbstractExternalTask
            TTaskManager.executeTask((Runnable) dobj);
            return;
        } catch (Exception e) {
            PlanC.logger.log(Level.SEVERE, e.getMessage(), e);
            Exit.shutdown();
        }
    }
}

From source file:com.github.brandtg.stl.StlDecomposition.java

/**
 * Runs STL on a CSV of time,measure./*from  ww  w.  ja v a 2s . c  o m*/
 *
 * <p>
 *   Outputs a CSV of time,measure,trend,seasonal,remainder.
 * </p>
 *
 * @param args
 *  args[0] = numberOfObservations
 * @throws Exception
 *  If could not process data
 */
public static void main(String[] args) throws Exception {
    List<Number> times = new ArrayList<Number>();
    List<Number> measures = new ArrayList<Number>();

    // Read from STDIN
    String line;
    BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
    while ((line = reader.readLine()) != null) {
        String[] tokens = line.split(",");
        times.add(Long.valueOf(tokens[0]));
        measures.add(Double.valueOf(tokens[1]));
    }

    // Compute STL
    StlDecomposition stl = new StlDecomposition(Integer.valueOf(args[0]));
    stl.getConfig().setSeasonalComponentBandwidth(Double.valueOf(
            System.getProperty("seasonal.bandwidth", String.valueOf(StlConfig.DEFAULT_SEASONAL_BANDWIDTH))));
    stl.getConfig().setTrendComponentBandwidth(Double
            .valueOf(System.getProperty("trend.bandwidth", String.valueOf(StlConfig.DEFAULT_TREND_BANDWIDTH))));
    stl.getConfig().setNumberOfInnerLoopPasses(Integer
            .valueOf(System.getProperty("inner.loop", String.valueOf(StlConfig.DEFAULT_INNER_LOOP_PASSES))));
    StlResult res = stl.decompose(times, measures);

    // Output to STDOUT
    for (int i = 0; i < times.size(); i++) {
        System.out.println(String.format("%d,%02f,%02f,%02f,%02f", (long) res.getTimes()[i], res.getSeries()[i],
                res.getTrend()[i], res.getSeasonal()[i], res.getRemainder()[i]));
    }
}

From source file:edu.harvard.med.iccbl.screensaver.soaputils.PubchemChembankQueryUtility.java

@SuppressWarnings("static-access")
public static void main(String[] args) throws IOException, InterruptedException {
    final PubchemChembankQueryUtility app = new PubchemChembankQueryUtility(args);

    String[] option = LIBRARY_NAME;
    app.addCommandLineOption(//from w  ww . ja va 2s.  c  o  m
            OptionBuilder.hasArg().withArgName(option[ARG_INDEX]).withDescription(option[DESCRIPTION_INDEX])
                    .withLongOpt(option[LONG_OPTION_INDEX]).create(option[SHORT_OPTION_INDEX]));

    option = QUERY_ALL_LIBRARIES;
    app.addCommandLineOption(
            OptionBuilder.withArgName(option[ARG_INDEX]).withDescription(option[DESCRIPTION_INDEX])
                    .withLongOpt(option[LONG_OPTION_INDEX]).create(option[SHORT_OPTION_INDEX]));

    option = OUTPUT_FILE;
    app.addCommandLineOption(
            OptionBuilder.hasArg().withArgName(option[ARG_INDEX]).withDescription(option[DESCRIPTION_INDEX])
                    .withLongOpt(option[LONG_OPTION_INDEX]).create(option[SHORT_OPTION_INDEX]));

    option = TRY_LIMIT;
    app.addCommandLineOption(
            OptionBuilder.hasArg().withArgName(option[ARG_INDEX]).withDescription(option[DESCRIPTION_INDEX])
                    .withLongOpt(option[LONG_OPTION_INDEX]).create(option[SHORT_OPTION_INDEX]));

    option = INTERVAL_BETWEEN_TRIES;
    app.addCommandLineOption(
            OptionBuilder.hasArg().withArgName(option[ARG_INDEX]).withDescription(option[DESCRIPTION_INDEX])
                    .withLongOpt(option[LONG_OPTION_INDEX]).create(option[SHORT_OPTION_INDEX]));

    option = QUERY_PUBCHEM;
    app.addCommandLineOption(
            OptionBuilder.withArgName(option[ARG_INDEX]).withDescription(option[DESCRIPTION_INDEX])
                    .withLongOpt(option[LONG_OPTION_INDEX]).create(option[SHORT_OPTION_INDEX]));

    option = QUERY_CHEMBANK;
    app.addCommandLineOption(
            OptionBuilder.withArgName(option[ARG_INDEX]).withDescription(option[DESCRIPTION_INDEX])
                    .withLongOpt(option[LONG_OPTION_INDEX]).create(option[SHORT_OPTION_INDEX]));
    try {
        if (!app.processOptions(/* acceptDatabaseOptions= */true, /* showHelpOnError= */true)) {
            return;
        }

        final boolean queryPubchem = app.isCommandLineFlagSet(QUERY_PUBCHEM[SHORT_OPTION_INDEX]);
        final boolean queryChembank = app.isCommandLineFlagSet(QUERY_CHEMBANK[SHORT_OPTION_INDEX]);

        if (!(queryPubchem || queryChembank)) {
            log.error("Must specify either " + QUERY_PUBCHEM[LONG_OPTION_INDEX] + " or "
                    + QUERY_CHEMBANK[LONG_OPTION_INDEX]);
            app.showHelp();
            return;
        }
        if (!app.isCommandLineFlagSet(LIBRARY_NAME[SHORT_OPTION_INDEX])
                && !app.isCommandLineFlagSet(QUERY_ALL_LIBRARIES[SHORT_OPTION_INDEX])) {
            log.error("Must specify either " + LIBRARY_NAME[LONG_OPTION_INDEX] + " or "
                    + QUERY_ALL_LIBRARIES[LONG_OPTION_INDEX]);
            app.showHelp();
            return;
        }
        if (app.isCommandLineFlagSet(LIBRARY_NAME[SHORT_OPTION_INDEX])
                && app.isCommandLineFlagSet(QUERY_ALL_LIBRARIES[SHORT_OPTION_INDEX])) {
            log.error("Must specify either " + LIBRARY_NAME[LONG_OPTION_INDEX] + " or "
                    + QUERY_ALL_LIBRARIES[LONG_OPTION_INDEX]);
            app.showHelp();
            return;
        }
        if (app.isCommandLineFlagSet(QUERY_ALL_LIBRARIES[SHORT_OPTION_INDEX])
                && app.isCommandLineFlagSet(OUTPUT_FILE[SHORT_OPTION_INDEX])) {
            log.error("option \"" + OUTPUT_FILE[LONG_OPTION_INDEX] + "\" not allowed with \""
                    + QUERY_ALL_LIBRARIES[LONG_OPTION_INDEX] + "\" option.");
            app.showHelp();
            return;
        }
        //      if(app.isCommandLineFlagSet(LIBRARY_NAME[SHORT_OPTION_INDEX])
        //        && !app.isCommandLineFlagSet(OUTPUT_FILE[SHORT_OPTION_INDEX])) {
        //        log.error("option \"" + OUTPUT_FILE[LONG_OPTION_INDEX] + "\" must be specified with \"" + LIBRARY_NAME[LONG_OPTION_INDEX] + "\" option.");
        //        app.showHelp();
        //        return;
        //      }

        final GenericEntityDAO dao = (GenericEntityDAO) app.getSpringBean("genericEntityDao");
        dao.doInTransaction(new DAOTransaction() {
            public void runTransaction() {
                PrintWriter writer = null;
                PrintWriter errorWriter = null;
                try {

                    int intervalMs = PugSoapUtil.INTERVAL_BETWEEN_TRIES_MS;
                    if (app.isCommandLineFlagSet(INTERVAL_BETWEEN_TRIES[SHORT_OPTION_INDEX])) {
                        intervalMs = app.getCommandLineOptionValue(INTERVAL_BETWEEN_TRIES[SHORT_OPTION_INDEX],
                                Integer.class);
                    }

                    int numberOfTries = PugSoapUtil.TRY_LIMIT;
                    if (app.isCommandLineFlagSet(TRY_LIMIT[SHORT_OPTION_INDEX])) {
                        numberOfTries = app.getCommandLineOptionValue(TRY_LIMIT[SHORT_OPTION_INDEX],
                                Integer.class);
                    }

                    List<Library> libraries = Lists.newArrayList();
                    if (app.isCommandLineFlagSet(LIBRARY_NAME[SHORT_OPTION_INDEX])) {
                        String temp = app.getCommandLineOptionValue(LIBRARY_NAME[SHORT_OPTION_INDEX]);
                        for (String libraryName : temp.split(",")) {
                            Library library = dao.findEntityByProperty(Library.class, "shortName",
                                    libraryName.trim());
                            if (library == null) {
                                throw new IllegalArgumentException(
                                        "no library with short name: " + libraryName);
                            }
                            libraries.add(library);
                        }

                        // if there is only one library to query, then set these values from the command line option
                        if (libraries.size() == 1) {
                            String outputFilename = app
                                    .getCommandLineOptionValue(OUTPUT_FILE[SHORT_OPTION_INDEX]);
                            writer = app.getOutputFile(outputFilename);
                            errorWriter = app.getOutputFile(outputFilename + ".errors");
                        }
                    } else if (app.isCommandLineFlagSet(QUERY_ALL_LIBRARIES[SHORT_OPTION_INDEX])) {
                        libraries = dao.findEntitiesByProperty(Library.class, "screenType",
                                ScreenType.SMALL_MOLECULE);
                        for (Iterator<Library> iter = libraries.iterator(); iter.hasNext();) {
                            Library library = iter.next();
                            if (library.getLibraryType() == LibraryType.ANNOTATION
                                    || library.getLibraryType() == LibraryType.NATURAL_PRODUCTS) {
                                iter.remove();
                            }
                        }
                    }

                    Collections.sort(libraries, new NullSafeComparator<Library>() {
                        @Override
                        protected int doCompare(Library o1, Library o2) {
                            return o1.getShortName().compareTo(o2.getShortName());
                        }

                    });

                    List<String> libraryNames = Lists.transform(libraries, new Function<Library, String>() {
                        @Override
                        public String apply(Library from) {
                            return from.getShortName();
                        }
                    });
                    log.info("libraries to process:\n" + libraryNames);

                    int i = 0;
                    for (Library library : libraries) {
                        if (writer == null || i > 0) {
                            writer = app.getOutputFile(library.getShortName());
                        }
                        if (errorWriter == null || i > 0) {
                            errorWriter = app.getOutputFile(library.getShortName() + ".errors");
                        }
                        log.info("\nProcessing the library: " + library.getShortName() + "\nlong name: "
                                + library.getLibraryName() + "\noutput file: " + library.getShortName()
                                + ".csv");
                        app.query(library, queryPubchem, queryChembank, dao, intervalMs, numberOfTries, writer,
                                errorWriter);
                        i++;
                    }
                } catch (Exception e) {
                    throw new DAOTransactionRollbackException(e);
                } finally {
                    if (writer != null)
                        writer.close();
                    if (errorWriter != null)
                        errorWriter.close();
                }
            }

        });
        System.exit(0);
    } catch (ParseException e) {
        log.error("error parsing command line options: " + e.getMessage());
    }
}

From source file:com.hpe.nv.samples.basic.BasicAnalyzeNVTransactions.java

public static void main(String[] args) {
    try {//from w w w .  j ava  2s .  c  o m
        // program arguments
        Options options = new Options();
        options.addOption("i", "server-ip", true, "[mandatory] NV Test Manager IP");
        options.addOption("o", "server-port", true, "[mandatory] NV Test Manager port");
        options.addOption("u", "username", true, "[mandatory] NV username");
        options.addOption("w", "password", true, "[mandatory] NV password");
        options.addOption("e", "ssl", true, "[optional] Pass true to use SSL. Default: false");
        options.addOption("y", "proxy", true, "[optional] Proxy server host:port");
        options.addOption("z", "zip-result-file-path", true,
                "[optional] File path to store the analysis results as a .zip file");
        options.addOption("k", "analysis-ports", true,
                "[optional] A comma-separated list of ports for test analysis");
        options.addOption("b", "browser", true,
                "[optional] The browser for which the Selenium WebDriver is built. Possible values: Chrome and Firefox. Default: Firefox");
        options.addOption("d", "debug", true,
                "[optional] Pass true to view console debug messages during execution. Default: false");
        options.addOption("h", "help", false, "[optional] Generates and prints help information");

        // parse and validate the command line arguments
        CommandLineParser parser = new DefaultParser();
        CommandLine line = parser.parse(options, args);

        if (line.hasOption("help")) {
            // print help if help argument is passed
            HelpFormatter formatter = new HelpFormatter();
            formatter.printHelp("BasicAnalyzeNVTransactions.java", options);
            return;
        }

        if (line.hasOption("server-ip")) {
            serverIp = line.getOptionValue("server-ip");
            if (serverIp.equals("0.0.0.0")) {
                throw new Exception(
                        "Please replace the server IP argument value (0.0.0.0) with your NV Test Manager IP");
            }
        } else {
            throw new Exception("Missing argument -i/--server-ip <serverIp>");
        }

        if (line.hasOption("server-port")) {
            serverPort = Integer.parseInt(line.getOptionValue("server-port"));
        } else {
            throw new Exception("Missing argument -o/--server-port <serverPort>");
        }

        if (line.hasOption("username")) {
            username = line.getOptionValue("username");
        } else {
            throw new Exception("Missing argument -u/--username <username>");
        }

        if (line.hasOption("password")) {
            password = line.getOptionValue("password");
        } else {
            throw new Exception("Missing argument -w/--password <password>");
        }

        if (line.hasOption("ssl")) {
            ssl = Boolean.parseBoolean(line.getOptionValue("ssl"));
        }

        if (line.hasOption("zip-result-file-path")) {
            zipResultFilePath = line.getOptionValue("zip-result-file-path");
        }

        if (line.hasOption("proxy")) {
            proxySetting = line.getOptionValue("proxy");
        }

        if (line.hasOption("analysis-ports")) {
            String analysisPortsStr = line.getOptionValue("analysis-ports");
            analysisPorts = analysisPortsStr.split(",");
        } else {
            analysisPorts = new String[] { "80", "8080" };
        }

        if (line.hasOption("browser")) {
            browser = line.getOptionValue("browser");
        } else {
            browser = "Firefox";
        }

        if (line.hasOption("debug")) {
            debug = Boolean.parseBoolean(line.getOptionValue("debug"));
        }

        String newLine = System.getProperty("line.separator");
        String testDescription = "***   This sample demonstrates how to run transactions as part of an NV test.                                               ***"
                + newLine
                + "***                                                                                                                         ***"
                + newLine
                + "***   In this sample, the NV test starts with the \"3G Busy\" network scenario, running three transactions (see below).       ***"
                + newLine
                + "***   After the sample stops and analyzes the NV test, it prints the analysis .zip file path to the console.                ***"
                + newLine
                + "***                                                                                                                         ***"
                + newLine
                + "***   This sample runs three NV transactions:                                                                               ***"
                + newLine
                + "***   1. \"Home Page\" transaction: Navigates to the home page in the HPE Network Virtualization website                      ***"
                + newLine
                + "***   2. \"Get Started\" transaction: Navigates to the Get Started Now page in the HPE Network Virtualization website         ***"
                + newLine
                + "***   3. \"Overview\" transaction: Navigates back to the home page in the HPE Network Virtualization website                  ***"
                + newLine
                + "***                                                                                                                         ***"
                + newLine
                + "***   You can view the actual steps of this sample in the BasicAnalyzeNVTransactions.java file.                             ***"
                + newLine;

        // print the sample's description
        System.out.println(testDescription);

        // start console spinner
        if (!debug) {
            spinner = new Thread(new Spinner());
            spinner.start();
        }

        // sample execution steps
        /*****    Part 1 - Start the NV test with the "3G Busy" network scenario                                                                   *****/
        printPartDescription("\b------    Part 1 - Start the NV test with the \"3G Busy\" network scenario");
        initTestManager();
        startTest();
        testRunning = true;
        printPartSeparator();
        /*****    Part 2 - Run three transactions - "Home Page", "Get Started" and "Overview"                                                      *****/
        printPartDescription(
                "------    Part 2 - Run three transactions - \"Home Page\", \"Get Started\" and \"Overview\"");
        connectToTransactionManager();
        startTransaction(1);
        transactionInProgress = true;
        buildSeleniumWebDriver();
        seleniumNavigateToPage();
        stopTransaction(1);
        transactionInProgress = false;
        startTransaction(2);
        transactionInProgress = true;
        seleniumGetStartedClick();
        stopTransaction(2);
        transactionInProgress = false;
        startTransaction(3);
        transactionInProgress = true;
        seleniumOverviewClick();
        stopTransaction(3);
        transactionInProgress = false;
        driverCloseAndQuit();
        printPartSeparator();
        /*****    Part 3 - Stop the NV test, analyze it and print the results to the console                                                       *****/
        printPartDescription(
                "------    Part 3 - Stop the NV test, analyze it and print the results to the console");
        stopTestAndAnalyze();
        testRunning = false;
        printPartSeparator();
        doneCallback();
    } catch (Exception e) {
        try {
            handleError(e.getMessage());
        } catch (Exception e2) {
            System.out.println("Error occurred: " + e2.getMessage());
        }
    }
}

From source file:ddc.commons.ftp.FTPClientExample.java

public static void main(String[] args) throws UnknownHostException {
    boolean storeFile = false, binaryTransfer = false, error = false, listFiles = false, listNames = false,
            hidden = false;//from www.  j  a  v  a  2s  .co  m
    boolean localActive = false, useEpsvWithIPv4 = false, feat = false, printHash = false;
    boolean mlst = false, mlsd = false;
    boolean lenient = false;
    long keepAliveTimeout = -1;
    int controlKeepAliveReplyTimeout = -1;
    int minParams = 5; // listings require 3 params
    String protocol = null; // SSL protocol
    String doCommand = null;
    String trustmgr = null;
    String proxyHost = null;
    int proxyPort = 80;
    String proxyUser = null;
    String proxyPassword = null;
    String username = null;
    String password = null;

    int base = 0;
    for (base = 0; base < args.length; base++) {
        if (args[base].equals("-s")) {
            storeFile = true;
        } else if (args[base].equals("-a")) {
            localActive = true;
        } else if (args[base].equals("-A")) {
            username = "anonymous";
            password = System.getProperty("user.name") + "@" + InetAddress.getLocalHost().getHostName();
        } else if (args[base].equals("-b")) {
            binaryTransfer = true;
        } else if (args[base].equals("-c")) {
            doCommand = args[++base];
            minParams = 3;
        } else if (args[base].equals("-d")) {
            mlsd = true;
            minParams = 3;
        } else if (args[base].equals("-e")) {
            useEpsvWithIPv4 = true;
        } else if (args[base].equals("-f")) {
            feat = true;
            minParams = 3;
        } else if (args[base].equals("-h")) {
            hidden = true;
        } else if (args[base].equals("-k")) {
            keepAliveTimeout = Long.parseLong(args[++base]);
        } else if (args[base].equals("-l")) {
            listFiles = true;
            minParams = 3;
        } else if (args[base].equals("-L")) {
            lenient = true;
        } else if (args[base].equals("-n")) {
            listNames = true;
            minParams = 3;
        } else if (args[base].equals("-p")) {
            protocol = args[++base];
        } else if (args[base].equals("-t")) {
            mlst = true;
            minParams = 3;
        } else if (args[base].equals("-w")) {
            controlKeepAliveReplyTimeout = Integer.parseInt(args[++base]);
        } else if (args[base].equals("-T")) {
            trustmgr = args[++base];
        } else if (args[base].equals("-PrH")) {
            proxyHost = args[++base];
            String parts[] = StringUtils.split(proxyHost, ':');
            if (parts.length == 2) {
                proxyHost = parts[0];
                proxyPort = Integer.parseInt(parts[1]);
            }
        } else if (args[base].equals("-PrU")) {
            proxyUser = args[++base];
        } else if (args[base].equals("-PrP")) {
            proxyPassword = args[++base];
        } else if (args[base].equals("-#")) {
            printHash = true;
        } else {
            break;
        }
    }

    int remain = args.length - base;
    if (username != null) {
        minParams -= 2;
    }
    if (remain < minParams) // server, user, pass, remote, local [protocol]
    {
        System.err.println(USAGE);
        System.exit(1);
    }

    String server = args[base++];
    int port = 0;
    String parts[] = server.split(":");
    if (parts.length == 2) {
        server = parts[0];
        port = Integer.parseInt(parts[1]);
    }
    if (username == null) {
        username = args[base++];
        password = args[base++];
    }

    String remote = null;
    if (args.length - base > 0) {
        remote = args[base++];
    }

    String local = null;
    if (args.length - base > 0) {
        local = args[base++];
    }

    final FTPClient ftp;
    if (protocol == null) {
        if (proxyHost != null) {
            System.out.println("Using HTTP proxy server: " + proxyHost);
            ftp = new FTPHTTPClient(proxyHost, proxyPort, proxyUser, proxyPassword);
        } else {
            ftp = new FTPClient();
        }
    } else {
        FTPSClient ftps;
        if (protocol.equals("true")) {
            ftps = new FTPSClient(true);
        } else if (protocol.equals("false")) {
            ftps = new FTPSClient(false);
        } else {
            String prot[] = protocol.split(",");
            if (prot.length == 1) { // Just protocol
                ftps = new FTPSClient(protocol);
            } else { // protocol,true|false
                ftps = new FTPSClient(prot[0], Boolean.parseBoolean(prot[1]));
            }
        }
        ftp = ftps;
        if ("all".equals(trustmgr)) {
            ftps.setTrustManager(TrustManagerUtils.getAcceptAllTrustManager());
        } else if ("valid".equals(trustmgr)) {
            ftps.setTrustManager(TrustManagerUtils.getValidateServerCertificateTrustManager());
        } else if ("none".equals(trustmgr)) {
            ftps.setTrustManager(null);
        }
    }

    if (printHash) {
        ftp.setCopyStreamListener(createListener());
    }
    if (keepAliveTimeout >= 0) {
        ftp.setControlKeepAliveTimeout(keepAliveTimeout);
    }
    if (controlKeepAliveReplyTimeout >= 0) {
        ftp.setControlKeepAliveReplyTimeout(controlKeepAliveReplyTimeout);
    }
    ftp.setListHiddenFiles(hidden);

    // suppress login details
    ftp.addProtocolCommandListener(new PrintCommandListener(new PrintWriter(System.out), true));

    try {
        int reply;
        if (port > 0) {
            ftp.connect(server, port);
        } else {
            ftp.connect(server);
        }
        System.out.println("Connected to " + server + " on " + (port > 0 ? port : ftp.getDefaultPort()));

        // After connection attempt, you should check the reply code to verify
        // success.
        reply = ftp.getReplyCode();

        if (!FTPReply.isPositiveCompletion(reply)) {
            ftp.disconnect();
            System.err.println("FTP server refused connection.");
            System.exit(1);
        }
    } catch (IOException e) {
        if (ftp.isConnected()) {
            try {
                ftp.disconnect();
            } catch (IOException f) {
                // do nothing
            }
        }
        System.err.println("Could not connect to server.");
        e.printStackTrace();
        System.exit(1);
    }

    __main: try {
        if (!ftp.login(username, password)) {
            ftp.logout();
            error = true;
            break __main;
        }

        System.out.println("Remote system is " + ftp.getSystemType());

        if (binaryTransfer) {
            ftp.setFileType(FTP.BINARY_FILE_TYPE);
        } else {
            // in theory this should not be necessary as servers should default to ASCII
            // but they don't all do so - see NET-500
            ftp.setFileType(FTP.ASCII_FILE_TYPE);
        }

        // Use passive mode as default because most of us are
        // behind firewalls these days.
        if (localActive) {
            ftp.enterLocalActiveMode();
        } else {
            ftp.enterLocalPassiveMode();
        }

        ftp.setUseEPSVwithIPv4(useEpsvWithIPv4);

        if (storeFile) {
            InputStream input;

            input = new FileInputStream(local);

            ftp.storeFile(remote, input);

            input.close();
        } else if (listFiles) {
            if (lenient) {
                FTPClientConfig config = new FTPClientConfig();
                config.setLenientFutureDates(true);
                ftp.configure(config);
            }

            for (FTPFile f : ftp.listFiles(remote)) {
                System.out.println(f.getRawListing());
                System.out.println(f.toFormattedString());
            }
        } else if (mlsd) {
            for (FTPFile f : ftp.mlistDir(remote)) {
                System.out.println(f.getRawListing());
                System.out.println(f.toFormattedString());
            }
        } else if (mlst) {
            FTPFile f = ftp.mlistFile(remote);
            if (f != null) {
                System.out.println(f.toFormattedString());
            }
        } else if (listNames) {
            for (String s : ftp.listNames(remote)) {
                System.out.println(s);
            }
        } else if (feat) {
            // boolean feature check
            if (remote != null) { // See if the command is present
                if (ftp.hasFeature(remote)) {
                    System.out.println("Has feature: " + remote);
                } else {
                    if (FTPReply.isPositiveCompletion(ftp.getReplyCode())) {
                        System.out.println("FEAT " + remote + " was not detected");
                    } else {
                        System.out.println("Command failed: " + ftp.getReplyString());
                    }
                }

                // Strings feature check
                String[] features = ftp.featureValues(remote);
                if (features != null) {
                    for (String f : features) {
                        System.out.println("FEAT " + remote + "=" + f + ".");
                    }
                } else {
                    if (FTPReply.isPositiveCompletion(ftp.getReplyCode())) {
                        System.out.println("FEAT " + remote + " is not present");
                    } else {
                        System.out.println("Command failed: " + ftp.getReplyString());
                    }
                }
            } else {
                if (ftp.features()) {
                    //                        Command listener has already printed the output
                } else {
                    System.out.println("Failed: " + ftp.getReplyString());
                }
            }
        } else if (doCommand != null) {
            if (ftp.doCommand(doCommand, remote)) {
                //                  Command listener has already printed the output
                //                    for(String s : ftp.getReplyStrings()) {
                //                        System.out.println(s);
                //                    }
            } else {
                System.out.println("Failed: " + ftp.getReplyString());
            }
        } else {
            OutputStream output;

            output = new FileOutputStream(local);

            ftp.retrieveFile(remote, output);

            output.close();
        }

        ftp.noop(); // check that control connection is working OK

        ftp.logout();
    } catch (FTPConnectionClosedException e) {
        error = true;
        System.err.println("Server closed connection.");
        e.printStackTrace();
    } catch (IOException e) {
        error = true;
        e.printStackTrace();
    } finally {
        if (ftp.isConnected()) {
            try {
                ftp.disconnect();
            } catch (IOException f) {
                // do nothing
            }
        }
    }

    System.exit(error ? 1 : 0);
}