Example usage for org.apache.commons.cli CommandLine getOptionValues

List of usage examples for org.apache.commons.cli CommandLine getOptionValues

Introduction

In this page you can find the example usage for org.apache.commons.cli CommandLine getOptionValues.

Prototype

public String[] getOptionValues(char opt) 

Source Link

Document

Retrieves the array of values, if any, of an option.

Usage

From source file:com.jgaap.backend.CLI.java

/**
 * Parses the arguments passed to jgaap from the command line. Will either
 * display the help or run jgaap on an experiment.
 * /* ww w . j a v  a2 s .c  o  m*/
 * @param args
 *            command line arguments
 * @throws Exception
 */
public static void main(String[] args) throws Exception {
    CommandLineParser parser = new GnuParser();
    CommandLine cmd = parser.parse(options, args);
    if (cmd.hasOption('h')) {
        String command = cmd.getOptionValue('h');
        if (command == null) {
            HelpFormatter helpFormatter = new HelpFormatter();
            helpFormatter.setLeftPadding(5);
            helpFormatter.setWidth(100);
            helpFormatter.printHelp(
                    "jgaap -c [canon canon ...] -es [event] -ec [culler culler ...] -a [analysis] <-d [distance]> -l [file] <-s [file]>",
                    "Welcome to JGAAP the Java Graphical Authorship Attribution Program.\nMore information can be found at http://jgaap.com",
                    options, "Copyright 2013 Evaluating Variation in Language Lab, Duquesne University");
        } else {
            List<Displayable> list = new ArrayList<Displayable>();
            if (command.equalsIgnoreCase("c")) {
                list.addAll(Canonicizers.getCanonicizers());
            } else if (command.equalsIgnoreCase("es")) {
                list.addAll(EventDrivers.getEventDrivers());
            } else if (command.equalsIgnoreCase("ec")) {
                list.addAll(EventCullers.getEventCullers());
            } else if (command.equalsIgnoreCase("a")) {
                list.addAll(AnalysisDrivers.getAnalysisDrivers());
            } else if (command.equalsIgnoreCase("d")) {
                list.addAll(DistanceFunctions.getDistanceFunctions());
            } else if (command.equalsIgnoreCase("lang")) {
                list.addAll(Languages.getLanguages());
            }
            for (Displayable display : list) {
                if (display.showInGUI())
                    System.out.println(display.displayName() + " - " + display.tooltipText());
            }
            if (list.isEmpty()) {
                System.out.println("Option " + command + " was not found.");
                System.out.println("Please use c, es, d, a, or lang");
            }
        }
    } else if (cmd.hasOption('v')) {
        System.out.println("Java Graphical Authorship Attribution Program version 5.2.0");
    } else if (cmd.hasOption("ee")) {
        String eeFile = cmd.getOptionValue("ee");
        String lang = cmd.getOptionValue("lang");
        ExperimentEngine.runExperiment(eeFile, lang);
        System.exit(0);
    } else {
        JGAAP.commandline = true;
        API api = API.getPrivateInstance();
        String documentsFilePath = cmd.getOptionValue('l');
        if (documentsFilePath == null) {
            throw new Exception("No Documents CSV specified");
        }
        List<Document> documents;
        if (documentsFilePath.startsWith(JGAAPConstants.JGAAP_RESOURCE_PACKAGE)) {
            documents = Utils.getDocumentsFromCSV(
                    CSVIO.readCSV(com.jgaap.JGAAP.class.getResourceAsStream(documentsFilePath)));
        } else {
            documents = Utils.getDocumentsFromCSV(CSVIO.readCSV(documentsFilePath));
        }
        for (Document document : documents) {
            api.addDocument(document);
        }
        String language = cmd.getOptionValue("lang", "english");
        api.setLanguage(language);
        String[] canonicizers = cmd.getOptionValues('c');
        if (canonicizers != null) {
            for (String canonicizer : canonicizers) {
                api.addCanonicizer(canonicizer);
            }
        }
        String[] events = cmd.getOptionValues("es");
        if (events == null) {
            throw new Exception("No EventDriver specified");
        }
        for (String event : events) {
            api.addEventDriver(event);
        }
        String[] eventCullers = cmd.getOptionValues("ec");
        if (eventCullers != null) {
            for (String eventCuller : eventCullers) {
                api.addEventCuller(eventCuller);
            }
        }
        String analysis = cmd.getOptionValue('a');
        if (analysis == null) {
            throw new Exception("No AnalysisDriver specified");
        }
        AnalysisDriver analysisDriver = api.addAnalysisDriver(analysis);
        String distanceFunction = cmd.getOptionValue('d');
        if (distanceFunction != null) {
            api.addDistanceFunction(distanceFunction, analysisDriver);
        }
        api.execute();
        List<Document> unknowns = api.getUnknownDocuments();
        OutputStreamWriter outputStreamWriter;
        String saveFile = cmd.getOptionValue('s');
        if (saveFile == null) {
            outputStreamWriter = new OutputStreamWriter(System.out);
        } else {
            outputStreamWriter = new OutputStreamWriter(new FileOutputStream(saveFile));
        }
        Writer writer = new BufferedWriter(outputStreamWriter);
        for (Document unknown : unknowns) {
            writer.append(unknown.getFormattedResult(analysisDriver));
        }
        writer.append('\n');
    }
}

From source file:com.zimbra.cs.account.AttributeManagerUtil.java

public static void main(String[] args) throws IOException, ServiceException {
    CliUtil.toolSetup();/*from  w w  w. j a v  a  2 s.  c  om*/
    CommandLine cl = parseArgs(args);

    if (!cl.hasOption('a'))
        usage("no action specified");
    String actionStr = cl.getOptionValue('a');
    Action action = null;
    try {
        action = Action.valueOf(actionStr);
    } catch (IllegalArgumentException iae) {
        usage("unknown action: " + actionStr);
    }

    AttributeManager am = null;
    if (action != Action.dump && action != Action.listAttrs) {
        if (!cl.hasOption('i'))
            usage("no input attribute xml files specified");
        am = new AttributeManager(cl.getOptionValue('i'));
        if (am.hasErrors()) {
            ZimbraLog.misc.warn(am.getErrors());
            System.exit(1);
        }
    }

    OutputStream os = System.out;
    if (cl.hasOption('o')) {
        os = new FileOutputStream(cl.getOptionValue('o'));
    }
    PrintWriter pw = new PrintWriter(new BufferedWriter(new OutputStreamWriter(os, "utf8")));

    AttributeManagerUtil amu = new AttributeManagerUtil(am);

    switch (action) {
    case dump:
        LdapProv.getInst().dumpLdapSchema(pw);
        break;
    case generateDefaultCOSLdif:
        amu.generateDefaultCOSLdif(pw);
        break;
    case generateDefaultExternalCOSLdif:
        amu.generateDefaultExternalCOSLdif(pw);
        break;
    case generateGetters:
        amu.generateGetters(cl.getOptionValue('c'), cl.getOptionValue('r'));
        break;
    case generateGlobalConfigLdif:
        amu.generateGlobalConfigLdif(pw);
        break;
    case generateLdapSchema:
        if (!cl.hasOption('t')) {
            usage("no schema template specified");
        }
        amu.generateLdapSchema(pw, cl.getOptionValue('t'));
        break;
    case generateMessageProperties:
        amu.generateMessageProperties(cl.getOptionValue('r'));
        break;
    case generateProvisioning:
        amu.generateProvisioningConstants(cl.getOptionValue('r'));
        break;
    case generateSchemaLdif:
        amu.generateSchemaLdif(pw);
        break;
    case listAttrs:
        amu.listAttrs(pw, cl.getOptionValues('c'), cl.getOptionValues('n'), cl.getOptionValues('f'));
        break;
    }

    pw.close();
}

From source file:com.bytelightning.opensource.pokerface.PokerFaceApp.java

public static void main(String[] args) {
    if (JavaVersionAsFloat() < (1.8f - Float.MIN_VALUE)) {
        System.err.println("PokerFace requires at least Java v8 to run.");
        return;//from  ww  w  .  j  a v a 2  s  .c om
    }
    // Configure the command line options parser
    Options options = new Options();
    options.addOption("h", false, "help");
    options.addOption("listen", true, "(http,https,secure,tls,ssl,CertAlias)=Address:Port for https.");
    options.addOption("keystore", true, "Filepath for PokerFace certificate keystore.");
    options.addOption("storepass", true, "The store password of the keystore.");
    options.addOption("keypass", true, "The key password of the keystore.");
    options.addOption("target", true, "Remote Target requestPattern=targetUri"); // NOTE: targetUri may contain user-info and if so will be interpreted as the alias of a cert to be presented to the remote target
    options.addOption("servercpu", true, "Number of cores the server should use.");
    options.addOption("targetcpu", true, "Number of cores the http targets should use.");
    options.addOption("trustany", false, "Ignore certificate identity errors from target servers.");
    options.addOption("files", true, "Filepath to a directory of static files.");
    options.addOption("config", true, "Path for XML Configuration file.");
    options.addOption("scripts", true, "Filepath for root scripts directory.");
    options.addOption("library", true, "JavaScript library to load into global context.");
    options.addOption("watch", false, "Dynamically watch scripts directory for changes.");
    options.addOption("dynamicTargetScripting", false,
            "WARNING! This option allows scripts to redirect requests to *any* other remote server.");

    CommandLine cmdLine = null;
    // parse the command line.
    try {
        CommandLineParser parser = new PosixParser();
        cmdLine = parser.parse(options, args);
        if (args.length == 0 || cmdLine.hasOption('h')) {
            HelpFormatter formatter = new HelpFormatter();
            formatter.setWidth(120);
            formatter.printHelp(PokerFaceApp.class.getSimpleName(), options);
            return;
        }
    } catch (ParseException exp) {
        System.err.println("Parsing failed.  Reason: " + exp.getMessage());
        return;
    } catch (Exception ex) {
        ex.printStackTrace(System.err);
        return;
    }

    XMLConfiguration config = new XMLConfiguration();
    try {
        if (cmdLine.hasOption("config")) {
            Path tmp = Utils.MakePath(cmdLine.getOptionValue("config"));
            if (!Files.exists(tmp))
                throw new FileNotFoundException("Configuration file does not exist.");
            if (Files.isDirectory(tmp))
                throw new FileNotFoundException("'config' path is not a file.");
            // This is a bit of a pain, but but let's make sure we have a valid configuration file before we actually try to use it.
            config.setEntityResolver(new DefaultEntityResolver() {
                @Override
                public InputSource resolveEntity(String publicId, String systemId) throws SAXException {
                    InputSource retVal = super.resolveEntity(publicId, systemId);
                    if ((retVal == null) && (systemId != null)) {
                        try {
                            URL entityURL;
                            if (systemId.endsWith("/PokerFace_v1Config.xsd"))
                                entityURL = PokerFaceApp.class.getResource("/PokerFace_v1Config.xsd");
                            else
                                entityURL = new URL(systemId);
                            URLConnection connection = entityURL.openConnection();
                            connection.setUseCaches(false);
                            InputStream stream = connection.getInputStream();
                            retVal = new InputSource(stream);
                            retVal.setSystemId(entityURL.toExternalForm());
                        } catch (Throwable e) {
                            return retVal;
                        }
                    }
                    return retVal;
                }

            });
            config.setSchemaValidation(true);
            config.setURL(tmp.toUri().toURL());
            config.load();
            if (cmdLine.hasOption("listen"))
                System.out.println("IGNORING 'listen' option because a configuration file was supplied.");
            if (cmdLine.hasOption("target"))
                System.out.println("IGNORING 'target' option(s) because a configuration file was supplied.");
            if (cmdLine.hasOption("scripts"))
                System.out.println("IGNORING 'scripts' option because a configuration file was supplied.");
            if (cmdLine.hasOption("library"))
                System.out.println("IGNORING 'library' option(s) because a configuration file was supplied.");
        } else {
            String[] serverStrs;
            String[] addr = { null };
            String[] port = { null };
            serverStrs = cmdLine.getOptionValues("listen");
            if (serverStrs == null)
                throw new MissingOptionException("No listening addresses specified specified");
            for (int i = 0; i < serverStrs.length; i++) {
                String addrStr;
                String alias = null;
                String protocol = null;
                Boolean https = null;
                int addrPos = serverStrs[i].indexOf('=');
                if (addrPos >= 0) {
                    if (addrPos < 2)
                        throw new IllegalArgumentException("Invalid http argument.");
                    else if (addrPos + 1 >= serverStrs[i].length())
                        throw new IllegalArgumentException("Invalid http argument.");
                    addrStr = serverStrs[i].substring(addrPos + 1, serverStrs[i].length());
                    String[] types = serverStrs[i].substring(0, addrPos).split(",");
                    for (String type : types) {
                        if (type.equalsIgnoreCase("http"))
                            break;
                        else if (type.equalsIgnoreCase("https") || type.equalsIgnoreCase("secure"))
                            https = true;
                        else if (type.equalsIgnoreCase("tls") || type.equalsIgnoreCase("ssl"))
                            protocol = type.toUpperCase();
                        else
                            alias = type;
                    }
                } else
                    addrStr = serverStrs[i];
                ParseAddressString(addrStr, addr, port, alias != null ? 443 : 80);
                config.addProperty("server.listen(" + i + ")[@address]", addr[0]);
                config.addProperty("server.listen(" + i + ")[@port]", port[0]);
                if (alias != null)
                    config.addProperty("server.listen(" + i + ")[@alias]", alias);
                if (protocol != null)
                    config.addProperty("server.listen(" + i + ")[@protocol]", protocol);
                if (https != null)
                    config.addProperty("server.listen(" + i + ")[@secure]", https);
            }
            String servercpu = cmdLine.getOptionValue("servercpu");
            if (servercpu != null)
                config.setProperty("server[@cpu]", servercpu);
            String clientcpu = cmdLine.getOptionValue("targetcpu");
            if (clientcpu != null)
                config.setProperty("targets[@cpu]", clientcpu);

            // Configure static files
            if (cmdLine.hasOption("files")) {
                Path tmp = Utils.MakePath(cmdLine.getOptionValue("files"));
                if (!Files.exists(tmp))
                    throw new FileNotFoundException("Files directory does not exist.");
                if (!Files.isDirectory(tmp))
                    throw new FileNotFoundException("'files' path is not a directory.");
                config.setProperty("files.rootDirectory", tmp.toAbsolutePath().toUri());
            }

            // Configure scripting
            if (cmdLine.hasOption("scripts")) {
                Path tmp = Utils.MakePath(cmdLine.getOptionValue("scripts"));
                if (!Files.exists(tmp))
                    throw new FileNotFoundException("Scripts directory does not exist.");
                if (!Files.isDirectory(tmp))
                    throw new FileNotFoundException("'scripts' path is not a directory.");
                config.setProperty("scripts.rootDirectory", tmp.toAbsolutePath().toUri());
                config.setProperty("scripts.dynamicWatch", cmdLine.hasOption("watch"));
                String[] libraries = cmdLine.getOptionValues("library");
                if (libraries != null) {
                    for (int i = 0; i < libraries.length; i++) {
                        Path lib = Utils.MakePath(libraries[i]);
                        if (!Files.exists(lib))
                            throw new FileNotFoundException(
                                    "Script library does not exist [" + libraries[i] + "].");
                        if (Files.isDirectory(lib))
                            throw new FileNotFoundException(
                                    "Script library is not a file [" + libraries[i] + "].");
                        config.setProperty("scripts.library(" + i + ")", lib.toAbsolutePath().toUri());
                    }
                }
            } else if (cmdLine.hasOption("watch"))
                System.out.println("IGNORING 'watch' option as no 'scripts' directory was specified.");
            else if (cmdLine.hasOption("library"))
                System.out.println("IGNORING 'library' option as no 'scripts' directory was specified.");
        }
        String keyStorePath = cmdLine.getOptionValue("keystore");
        if (keyStorePath != null)
            config.setProperty("keystore", keyStorePath);
        String keypass = cmdLine.getOptionValue("keypass");
        if (keypass != null)
            config.setProperty("keypass", keypass);
        String storepass = cmdLine.getOptionValue("storepass");
        if (storepass != null)
            config.setProperty("storepass", keypass);
        if (cmdLine.hasOption("trustany"))
            config.setProperty("targets[@trustAny]", true);

        config.setProperty("scripts.dynamicTargetScripting", cmdLine.hasOption("dynamicTargetScripting"));

        String[] targetStrs = cmdLine.getOptionValues("target");
        if (targetStrs != null) {
            for (int i = 0; i < targetStrs.length; i++) {
                int uriPos = targetStrs[i].indexOf('=');
                if (uriPos < 2)
                    throw new IllegalArgumentException("Invalid target argument.");
                else if (uriPos + 1 >= targetStrs[i].length())
                    throw new IllegalArgumentException("Invalid target argument.");
                String patternStr = targetStrs[i].substring(0, uriPos);
                String urlStr = targetStrs[i].substring(uriPos + 1, targetStrs[i].length());
                String alias;
                try {
                    URL url = new URL(urlStr);
                    alias = url.getUserInfo();
                    String scheme = url.getProtocol();
                    if ((!"http".equals(scheme)) && (!"https".equals(scheme)))
                        throw new IllegalArgumentException("Invalid target uri scheme.");
                    int port = url.getPort();
                    if (port < 0)
                        port = url.getDefaultPort();
                    urlStr = scheme + "://" + url.getHost() + ":" + port + url.getPath();
                    String ref = url.getRef();
                    if (ref != null)
                        urlStr += "#" + ref;
                } catch (MalformedURLException ex) {
                    throw new IllegalArgumentException("Malformed target uri");
                }
                config.addProperty("targets.target(" + i + ")[@pattern]", patternStr);
                config.addProperty("targets.target(" + i + ")[@url]", urlStr);
                if (alias != null)
                    config.addProperty("targets.target(" + i + ")[@alias]", alias);
            }
        }
        //         config.save(System.out);
    } catch (Throwable e) {
        e.printStackTrace(System.err);
        return;
    }
    // If we get here, we have a possibly valid configuration.
    try {
        final PokerFace p = new PokerFace();
        p.config(config);
        if (p.start()) {
            PokerFace.Logger.warn("Started!");
            Runtime.getRuntime().addShutdownHook(new Thread() {
                public void run() {
                    try {
                        PokerFace.Logger.warn("Initiating shutdown...");
                        p.stop();
                        PokerFace.Logger.warn("Shutdown completed!");
                    } catch (Throwable e) {
                        PokerFace.Logger.error("Failed to shutdown cleanly!");
                        e.printStackTrace(System.err);
                    }
                }
            });
        } else {
            PokerFace.Logger.error("Failed to start!");
            System.exit(-1);
        }
    } catch (Throwable e) {
        e.printStackTrace(System.err);
    }
}

From source file:de.unileipzig.ub.indexer.App.java

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

    // create Options object
    Options options = new Options();

    options.addOption("h", "help", false, "display this help");

    options.addOption("f", "filename", true, "name of the JSON file whose content should be indexed");
    options.addOption("i", "index", true, "the name of the target index");
    options.addOption("d", "doctype", true, "the name of the doctype (title, local, ...)");

    options.addOption("t", "host", true, "elasticsearch hostname (default: 0.0.0.0)");
    options.addOption("p", "port", true, "transport port (that's NOT the http port, default: 9300)");
    options.addOption("c", "cluster", true, "cluster name (default: elasticsearch_mdma)");

    options.addOption("b", "bulksize", true, "number of docs sent in one request (default: 3000)");
    options.addOption("v", "verbose", false, "show processing speed while indexing");
    options.addOption("s", "status", false, "only show status of index for file");

    options.addOption("r", "repair", false, "attempt to repair recoverable inconsistencies on the go");
    options.addOption("e", "debug", false, "set logging level to debug");
    options.addOption("l", "logfile", true, "logfile - in not specified only log to stdout");

    options.addOption("m", "memcached", true, "host and port of memcached (default: localhost:11211)");
    options.addOption("z", "latest-flag-on", true,
            "enable latest flag according to field (within content, e.g. 001)");
    options.addOption("a", "flat", false, "flat-mode: do not check for inconsistencies");

    CommandLineParser parser = new PosixParser();
    CommandLine cmd = null;

    try {//  w w w . j ava  2  s. c  om
        cmd = parser.parse(options, args);
    } catch (ParseException ex) {
        logger.error(ex);
        System.exit(1);
    }

    // setup logging
    Properties systemProperties = System.getProperties();
    systemProperties.put("net.spy.log.LoggerImpl", "net.spy.memcached.compat.log.Log4JLogger");
    System.setProperties(systemProperties);
    Logger.getLogger("net.spy.memcached").setLevel(Level.ERROR);

    Properties props = new Properties();
    props.load(props.getClass().getResourceAsStream("/log4j.properties"));

    if (cmd.hasOption("debug")) {
        props.setProperty("log4j.logger.de.unileipzig", "DEBUG");
    }

    if (cmd.hasOption("logfile")) {
        props.setProperty("log4j.rootLogger", "INFO, stdout, F");
        props.setProperty("log4j.appender.F", "org.apache.log4j.FileAppender");
        props.setProperty("log4j.appender.F.File", cmd.getOptionValue("logfile"));
        props.setProperty("log4j.appender.F.layout", "org.apache.log4j.PatternLayout");
        props.setProperty("log4j.appender.F.layout.ConversionPattern", "%5p | %d | %F | %L | %m%n");
    }

    PropertyConfigurator.configure(props);

    InetAddress addr = InetAddress.getLocalHost();
    String memcachedHostAndPort = addr.getHostAddress() + ":11211";
    if (cmd.hasOption("m")) {
        memcachedHostAndPort = cmd.getOptionValue("m");
    }

    // setup caching
    try {
        if (memcachedClient == null) {
            memcachedClient = new MemcachedClient(
                    new ConnectionFactoryBuilder().setFailureMode(FailureMode.Cancel).build(),
                    AddrUtil.getAddresses("0.0.0.0:11211"));
            try {
                // give client and server 500ms
                Thread.sleep(300);
            } catch (InterruptedException ex) {
            }

            Collection availableServers = memcachedClient.getAvailableServers();
            logger.info(availableServers);
            if (availableServers.size() == 0) {
                logger.info("no memcached servers found");
                memcachedClient.shutdown();
                memcachedClient = null;
            } else {
                logger.info(availableServers.size() + " memcached server(s) detected, fine.");
            }
        }
    } catch (IOException ex) {
        logger.warn("couldn't create a connection, bailing out: " + ex.getMessage());
    }

    // process options

    if (cmd.hasOption("h")) {
        HelpFormatter formatter = new HelpFormatter();
        formatter.printHelp("indexer", options, true);
        quit(0);
    }

    boolean verbose = false;
    if (cmd.hasOption("verbose")) {
        verbose = true;
    }

    // ES options
    String[] hosts = new String[] { "0.0.0.0" };
    int port = 9300;
    String clusterName = "elasticsearch_mdma";
    int bulkSize = 3000;

    if (cmd.hasOption("host")) {
        hosts = cmd.getOptionValues("host");
    }
    if (cmd.hasOption("port")) {
        port = Integer.parseInt(cmd.getOptionValue("port"));
    }
    if (cmd.hasOption("cluster")) {
        clusterName = cmd.getOptionValue("cluster");
    }
    if (cmd.hasOption("bulksize")) {
        bulkSize = Integer.parseInt(cmd.getOptionValue("bulksize"));
        if (bulkSize < 1 || bulkSize > 100000) {
            logger.error("bulksize must be between 1 and 100,000");
            quit(1);
        }
    }

    // ES Client
    final Settings settings = ImmutableSettings.settingsBuilder().put("cluster.name", "elasticsearch_mdma")
            .build();
    final TransportClient client = new TransportClient(settings);
    for (String host : hosts) {
        client.addTransportAddress(new InetSocketTransportAddress(host, port));
    }

    if (cmd.hasOption("filename") && cmd.hasOption("index") && cmd.hasOption("doctype")) {

        final String filename = cmd.getOptionValue("filename");

        final File _file = new File(filename);
        if (_file.length() == 0) {
            logger.info(_file.getAbsolutePath() + " is empty, skipping");
            quit(0); // file is empty
        }

        // for flat mode: leave a stampfile beside the json to 
        // indicate previous successful processing
        File directory = new File(filename).getParentFile();
        File stampfile = new File(directory, DigestUtils.shaHex(filename) + ".indexed");

        long start = System.currentTimeMillis();
        long lineCount = 0;

        final String indexName = cmd.getOptionValue("index");
        final String docType = cmd.getOptionValue("doctype");
        BulkRequestBuilder bulkRequest = client.prepareBulk();

        try {
            if (cmd.hasOption("flat")) {
                // flat mode
                // .........
                if (stampfile.exists()) {
                    logger.info("SKIPPING, since it seems this file has already " + "been imported (found: "
                            + stampfile.getAbsolutePath() + ")");
                    quit(0);
                }
            } else {

                final String srcSHA1 = extractSrcSHA1(filename);

                logger.debug(filename + " srcsha1: " + srcSHA1);

                long docsInIndex = getIndexedRecordCount(client, indexName, srcSHA1);
                logger.debug(filename + " indexed: " + docsInIndex);

                long docsInFile = getLineCount(filename);
                logger.debug(filename + " lines: " + docsInFile);

                // in non-flat-mode, indexing would take care
                // of inconsistencies
                if (docsInIndex == docsInFile) {
                    logger.info("UP-TO DATE: " + filename + " (" + docsInIndex + ", " + srcSHA1 + ")");
                    client.close();
                    quit(0);
                }

                if (docsInIndex > 0) {
                    logger.warn("INCONSISTENCY DETECTED: " + filename + ": indexed:" + docsInIndex + " lines:"
                            + docsInFile);

                    if (!cmd.hasOption("r")) {
                        logger.warn(
                                "Please re-run indexer with --repair flag or delete residues first with: $ curl -XDELETE "
                                        + hosts[0] + ":9200/" + indexName
                                        + "/_query -d ' {\"term\" : { \"meta.srcsha1\" : \"" + srcSHA1
                                        + "\" }}'");
                        client.close();
                        quit(1);
                    } else {
                        logger.info("Attempting to clear residues...");
                        // attempt to repair once
                        DeleteByQueryResponse dbqr = client.prepareDeleteByQuery(indexName)
                                .setQuery(termQuery("meta.srcsha1", srcSHA1)).execute().actionGet();

                        Iterator<IndexDeleteByQueryResponse> it = dbqr.iterator();
                        long deletions = 0;
                        while (it.hasNext()) {
                            IndexDeleteByQueryResponse response = it.next();
                            deletions += 1;
                        }
                        logger.info("Deleted residues of " + filename);
                        logger.info("Refreshing [" + indexName + "]");
                        RefreshResponse refreshResponse = client.admin().indices()
                                .refresh(new RefreshRequest(indexName)).actionGet();

                        long indexedAfterDelete = getIndexedRecordCount(client, indexName, srcSHA1);
                        logger.info(indexedAfterDelete + " docs remained");
                        if (indexedAfterDelete > 0) {
                            logger.warn("Not all residues cleaned. Try to fix this manually: $ curl -XDELETE "
                                    + hosts[0] + ":9200/" + indexName
                                    + "/_query -d ' {\"term\" : { \"meta.srcsha1\" : \"" + srcSHA1 + "\" }}'");
                            quit(1);
                        } else {
                            logger.info("Residues are gone. Now trying to reindex: " + filename);
                        }
                    }
                }
            }

            logger.info("INDEXING-REQUIRED: " + filename);
            if (cmd.hasOption("status")) {
                quit(0);
            }

            HashSet idsInBatch = new HashSet();

            String idField = null;
            if (cmd.hasOption("z")) {
                idField = cmd.getOptionValue("z");
            }

            final FileReader fr = new FileReader(filename);
            final BufferedReader br = new BufferedReader(fr);

            String line;
            // one line is one document
            while ((line = br.readLine()) != null) {

                // "Latest-Flag" machine
                // This gets obsolete with a "flat" index
                if (cmd.hasOption("z")) {
                    // flag that indicates, whether the document
                    // about to be indexed will be the latest
                    boolean willBeLatest = true;

                    // check if there is a previous (lower meta.timestamp) document with 
                    // the same identifier (whatever that may be - queried under "content")
                    final String contentIdentifier = getContentIdentifier(line, idField);
                    idsInBatch.add(contentIdentifier);

                    // assumed in meta.timestamp
                    final Long timestamp = Long.parseLong(getTimestamp(line));

                    logger.debug("Checking whether record is latest (line: " + lineCount + ")");
                    logger.debug(contentIdentifier + ", " + timestamp);

                    // get all docs, which match the contentIdentifier
                    // by filter, which doesn't score
                    final TermFilterBuilder idFilter = new TermFilterBuilder("content." + idField,
                            contentIdentifier);
                    final TermFilterBuilder kindFilter = new TermFilterBuilder("meta.kind", docType);
                    final AndFilterBuilder afb = new AndFilterBuilder();
                    afb.add(idFilter).add(kindFilter);
                    final FilteredQueryBuilder fb = filteredQuery(matchAllQuery(), afb);

                    final SearchResponse searchResponse = client.prepareSearch(indexName)
                            .setSearchType(SearchType.DFS_QUERY_THEN_FETCH).setQuery(fb).setFrom(0)
                            .setSize(1200) // 3 years and 105 days assuming daily updates at the most
                            .setExplain(false).execute().actionGet();

                    final SearchHits searchHits = searchResponse.getHits();

                    logger.debug("docs with this id in the index: " + searchHits.getTotalHits());

                    for (final SearchHit hit : searchHits.getHits()) {
                        final String docId = hit.id();
                        final Map<String, Object> source = hit.sourceAsMap();
                        final Map meta = (Map) source.get("meta");
                        final Long docTimestamp = Long.parseLong(meta.get("timestamp").toString());
                        // if the indexed doc timestamp is lower the the current one, 
                        // remove any latest flag
                        if (timestamp >= docTimestamp) {
                            source.remove("latest");
                            final ObjectMapper mapper = new ObjectMapper();
                            // put the updated doc back
                            // IndexResponse response = 
                            client.prepareIndex(indexName, docType).setCreate(false).setId(docId)
                                    .setSource(mapper.writeValueAsBytes(source))
                                    .execute(new ActionListener<IndexResponse>() {
                                        public void onResponse(IndexResponse rspns) {
                                            logger.debug("Removed latest flag from " + contentIdentifier + ", "
                                                    + docTimestamp + ", " + hit.id() + " since (" + timestamp
                                                    + " > " + docTimestamp + ")");
                                        }

                                        public void onFailure(Throwable thrwbl) {
                                            logger.error("Could not remove flag from " + hit.id() + ", "
                                                    + contentIdentifier);
                                        }
                                    });
                            // .execute()
                            //.actionGet();
                        } else {
                            logger.debug("Doc " + hit.id() + " is newer (" + docTimestamp + ")");
                            willBeLatest = false;
                        }
                    }

                    if (willBeLatest) {
                        line = setLatestFlag(line);
                        logger.info("Setting latest flag on " + contentIdentifier + ", " + timestamp);
                    }

                    // end of latest-flag machine
                    // beware - this will be correct as long as there
                    // are no dups within one bulk!
                }

                bulkRequest.add(client.prepareIndex(indexName, docType).setSource(line));
                lineCount++;
                logger.debug("Added line " + lineCount + " to BULK");
                logger.debug(line);

                if (lineCount % bulkSize == 0) {

                    if (idsInBatch.size() != bulkSize && cmd.hasOption("z")) {
                        logger.error(
                                "This batch has duplications in the ID. That's not bad for the index, just makes the latest flag fuzzy");
                        logger.error(
                                "Bulk size was: " + bulkSize + ", but " + idsInBatch.size() + " IDs (only)");
                    }
                    idsInBatch.clear();

                    logger.debug("Issuing BULK request");

                    final long actionCount = bulkRequest.numberOfActions();
                    final BulkResponse bulkResponse = bulkRequest.execute().actionGet();
                    final long tookInMillis = bulkResponse.getTookInMillis();

                    if (bulkResponse.hasFailures()) {
                        logger.fatal("FAILED, bulk not indexed. exiting now.");
                        Iterator<BulkItemResponse> it = bulkResponse.iterator();
                        while (it.hasNext()) {
                            BulkItemResponse bir = it.next();
                            if (bir.isFailed()) {
                                Failure failure = bir.getFailure();
                                logger.fatal("id: " + failure.getId() + ", message: " + failure.getMessage()
                                        + ", type: " + failure.getType() + ", index: " + failure.getIndex());
                            }
                        }
                        quit(1);
                    } else {
                        if (verbose) {
                            final double elapsed = System.currentTimeMillis() - start;
                            final double speed = (lineCount / elapsed * 1000);
                            logger.info("OK (" + filename + ") " + lineCount + " docs indexed (" + actionCount
                                    + "/" + tookInMillis + "ms" + "/" + String.format("%.2f", speed) + "r/s)");
                        }
                    }
                    bulkRequest = client.prepareBulk();
                }
            }

            // handle the remaining items
            final long actionCount = bulkRequest.numberOfActions();
            if (actionCount > 0) {
                final BulkResponse bulkResponse = bulkRequest.execute().actionGet();
                final long tookInMillis = bulkResponse.getTookInMillis();

                if (bulkResponse.hasFailures()) {
                    logger.fatal("FAILED, bulk not indexed. exiting now.");
                    Iterator<BulkItemResponse> it = bulkResponse.iterator();
                    while (it.hasNext()) {
                        BulkItemResponse bir = it.next();
                        if (bir.isFailed()) {
                            Failure failure = bir.getFailure();
                            logger.fatal("id: " + failure.getId() + ", message: " + failure.getMessage()
                                    + ", type: " + failure.getType() + ", index: " + failure.getIndex());
                        }
                    }
                    quit(1);
                } else {

                    // trigger update now
                    RefreshResponse refreshResponse = client.admin().indices()
                            .refresh(new RefreshRequest(indexName)).actionGet();

                    if (verbose) {
                        final double elapsed = System.currentTimeMillis() - start;
                        final double speed = (lineCount / elapsed * 1000);
                        logger.info("OK (" + filename + ") " + lineCount + " docs indexed (" + actionCount + "/"
                                + tookInMillis + "ms" + "/" + String.format("%.2f", speed) + "r/s)");
                    }

                }

            }

            br.close();
            client.close();
            final double elapsed = (System.currentTimeMillis() - start) / 1000;
            final double speed = (lineCount / elapsed);
            logger.info("indexing (" + filename + ") " + lineCount + " docs took " + elapsed + "s (speed: "
                    + String.format("%.2f", speed) + "r/s)");
            if (cmd.hasOption("flat")) {
                try {
                    FileUtils.touch(stampfile);
                } catch (IOException ioe) {
                    logger.warn(".indexed files not created. Will reindex everything everytime.");
                }
            }
        } catch (IOException e) {
            client.close();
            logger.error(e);
            quit(1);
        } finally {
            client.close();
        }
    }
    quit(0);
}

From source file:mod.org.dcm4che2.tool.DcmSnd.java

@SuppressWarnings("unchecked")
public static void main(String[] args) {
    CommandLine cl = parse(args);
    DcmSnd dcmsnd = new DcmSnd(cl.hasOption("device") ? cl.getOptionValue("device") : "DCMSND");
    final List<String> argList = cl.getArgList();
    String remoteAE = argList.get(0);
    String[] calledAETAddress = split(remoteAE, '@');
    dcmsnd.setCalledAET(calledAETAddress[0]);
    if (calledAETAddress[1] == null) {
        dcmsnd.setRemoteHost("127.0.0.1");
        dcmsnd.setRemotePort(104);/* w ww .j a  v  a  2s . c o  m*/
    } else {
        String[] hostPort = split(calledAETAddress[1], ':');
        dcmsnd.setRemoteHost(hostPort[0]);
        dcmsnd.setRemotePort(toPort(hostPort[1]));
    }
    if (cl.hasOption("L")) {
        String localAE = cl.getOptionValue("L");
        String[] localPort = split(localAE, ':');
        if (localPort[1] != null) {
            dcmsnd.setLocalPort(toPort(localPort[1]));
        }
        String[] callingAETHost = split(localPort[0], '@');
        dcmsnd.setCalling(callingAETHost[0]);
        if (callingAETHost[1] != null) {
            dcmsnd.setLocalHost(callingAETHost[1]);
        }
    }
    dcmsnd.setOfferDefaultTransferSyntaxInSeparatePresentationContext(cl.hasOption("ts1"));
    dcmsnd.setSendFileRef(cl.hasOption("fileref"));
    if (cl.hasOption("username")) {
        String username = cl.getOptionValue("username");
        UserIdentity userId;
        if (cl.hasOption("passcode")) {
            String passcode = cl.getOptionValue("passcode");
            userId = new UserIdentity.UsernamePasscode(username, passcode.toCharArray());
        } else {
            userId = new UserIdentity.Username(username);
        }
        userId.setPositiveResponseRequested(cl.hasOption("uidnegrsp"));
        dcmsnd.setUserIdentity(userId);
    }
    dcmsnd.setStorageCommitment(cl.hasOption("stgcmt"));
    String remoteStgCmtAE = null;
    if (cl.hasOption("stgcmtae")) {
        try {
            remoteStgCmtAE = cl.getOptionValue("stgcmtae");
            String[] aet_hostport = split(remoteStgCmtAE, '@');
            String[] host_port = split(aet_hostport[1], ':');
            dcmsnd.setStgcmtCalledAET(aet_hostport[0]);
            dcmsnd.setRemoteStgcmtHost(host_port[0]);
            dcmsnd.setRemoteStgcmtPort(toPort(host_port[1]));
        } catch (Exception e) {
            exit("illegal argument of option -stgcmtae");
        }
    }
    if (cl.hasOption("set")) {
        String[] vals = cl.getOptionValues("set");
        for (int i = 0; i < vals.length; i++, i++) {
            dcmsnd.addCoerceAttr(Tag.toTag(vals[i]), vals[i + 1]);
        }
    }
    if (cl.hasOption("setuid")) {
        dcmsnd.setSuffixUID(cl.getOptionValues("setuid"));
    }
    if (cl.hasOption("connectTO"))
        dcmsnd.setConnectTimeout(parseInt(cl.getOptionValue("connectTO"),
                "illegal argument of option -connectTO", 1, Integer.MAX_VALUE));
    if (cl.hasOption("reaper"))
        dcmsnd.setAssociationReaperPeriod(parseInt(cl.getOptionValue("reaper"),
                "illegal argument of option -reaper", 1, Integer.MAX_VALUE));
    if (cl.hasOption("rspTO"))
        dcmsnd.setDimseRspTimeout(parseInt(cl.getOptionValue("rspTO"), "illegal argument of option -rspTO", 1,
                Integer.MAX_VALUE));
    if (cl.hasOption("acceptTO"))
        dcmsnd.setAcceptTimeout(parseInt(cl.getOptionValue("acceptTO"), "illegal argument of option -acceptTO",
                1, Integer.MAX_VALUE));
    if (cl.hasOption("releaseTO"))
        dcmsnd.setReleaseTimeout(parseInt(cl.getOptionValue("releaseTO"),
                "illegal argument of option -releaseTO", 1, Integer.MAX_VALUE));
    if (cl.hasOption("soclosedelay"))
        dcmsnd.setSocketCloseDelay(parseInt(cl.getOptionValue("soclosedelay"),
                "illegal argument of option -soclosedelay", 1, 10000));
    if (cl.hasOption("shutdowndelay"))
        dcmsnd.setShutdownDelay(parseInt(cl.getOptionValue("shutdowndelay"),
                "illegal argument of option -shutdowndelay", 1, 10000));
    if (cl.hasOption("anonymize"))
        dcmsnd.setAnonymize(Long.parseLong(cl.getOptionValue("anonymize")));
    if (cl.hasOption("rcvpdulen"))
        dcmsnd.setMaxPDULengthReceive(
                parseInt(cl.getOptionValue("rcvpdulen"), "illegal argument of option -rcvpdulen", 1, 10000)
                        * KB);
    if (cl.hasOption("sndpdulen"))
        dcmsnd.setMaxPDULengthSend(
                parseInt(cl.getOptionValue("sndpdulen"), "illegal argument of option -sndpdulen", 1, 10000)
                        * KB);
    if (cl.hasOption("sosndbuf"))
        dcmsnd.setSendBufferSize(
                parseInt(cl.getOptionValue("sosndbuf"), "illegal argument of option -sosndbuf", 1, 10000) * KB);
    if (cl.hasOption("sorcvbuf"))
        dcmsnd.setReceiveBufferSize(
                parseInt(cl.getOptionValue("sorcvbuf"), "illegal argument of option -sorcvbuf", 1, 10000) * KB);
    if (cl.hasOption("bufsize"))
        dcmsnd.setTranscoderBufferSize(
                parseInt(cl.getOptionValue("bufsize"), "illegal argument of option -bufsize", 1, 10000) * KB);
    if (cl.hasOption("batchsize"))
        dcmsnd.setBatchSize(Integer.parseInt(cl.getOptionValue("batchsize")));
    dcmsnd.setPackPDV(!cl.hasOption("pdv1"));
    dcmsnd.setTcpNoDelay(!cl.hasOption("tcpdelay"));
    if (cl.hasOption("async"))
        dcmsnd.setMaxOpsInvoked(
                parseInt(cl.getOptionValue("async"), "illegal argument of option -async", 0, 0xffff));
    if (cl.hasOption("lowprior"))
        dcmsnd.setPriority(CommandUtils.LOW);
    if (cl.hasOption("highprior"))
        dcmsnd.setPriority(CommandUtils.HIGH);
    System.out.println("Scanning files to send");
    long t1 = System.currentTimeMillis();
    for (int i = 1, n = argList.size(); i < n; ++i)
        dcmsnd.addFile(new File(argList.get(i)));
    long t2 = System.currentTimeMillis();
    if (dcmsnd.getNumberOfFilesToSend() == 0) {
        System.exit(2);
    }
    System.out.println("\nScanned " + dcmsnd.getNumberOfFilesToSend() + " files in " + ((t2 - t1) / 1000F)
            + "s (=" + ((t2 - t1) / dcmsnd.getNumberOfFilesToSend()) + "ms/file)");
    dcmsnd.configureTransferCapability();
    if (cl.hasOption("tls")) {
        String cipher = cl.getOptionValue("tls");
        if ("NULL".equalsIgnoreCase(cipher)) {
            dcmsnd.setTlsWithoutEncyrption();
        } else if ("3DES".equalsIgnoreCase(cipher)) {
            dcmsnd.setTls3DES_EDE_CBC();
        } else if ("AES".equalsIgnoreCase(cipher)) {
            dcmsnd.setTlsAES_128_CBC();
        } else {
            exit("Invalid parameter for option -tls: " + cipher);
        }

        if (cl.hasOption("tls1")) {
            dcmsnd.setTlsProtocol(TLS1);
        } else if (cl.hasOption("ssl3")) {
            dcmsnd.setTlsProtocol(SSL3);
        } else if (cl.hasOption("no_tls1")) {
            dcmsnd.setTlsProtocol(NO_TLS1);
        } else if (cl.hasOption("no_ssl3")) {
            dcmsnd.setTlsProtocol(NO_SSL3);
        } else if (cl.hasOption("no_ssl2")) {
            dcmsnd.setTlsProtocol(NO_SSL2);
        }
        dcmsnd.setTlsNeedClientAuth(!cl.hasOption("noclientauth"));

        if (cl.hasOption("keystore")) {
            dcmsnd.setKeyStoreURL(cl.getOptionValue("keystore"));
        }
        if (cl.hasOption("keystorepw")) {
            dcmsnd.setKeyStorePassword(cl.getOptionValue("keystorepw"));
        }
        if (cl.hasOption("keypw")) {
            dcmsnd.setKeyPassword(cl.getOptionValue("keypw"));
        }
        if (cl.hasOption("truststore")) {
            dcmsnd.setTrustStoreURL(cl.getOptionValue("truststore"));
        }
        if (cl.hasOption("truststorepw")) {
            dcmsnd.setTrustStorePassword(cl.getOptionValue("truststorepw"));
        }
        try {
            dcmsnd.initTLS();
        } catch (Exception e) {
            System.err.println("ERROR: Failed to initialize TLS context:" + e.getMessage());
            System.exit(2);
        }
    }
    while (dcmsnd.getLastSentFile() < dcmsnd.getNumberOfFilesToSend()) {
        try {
            dcmsnd.start();
        } catch (Exception e) {
            System.err.println("ERROR: Failed to start server for receiving " + "Storage Commitment results:"
                    + e.getMessage());
            System.exit(2);
        }
        try {
            t1 = System.currentTimeMillis();
            try {
                dcmsnd.open();
            } catch (Exception e) {
                System.err.println("ERROR: Failed to establish association:" + e.getMessage());
                System.exit(2);
            }
            t2 = System.currentTimeMillis();
            System.out.println("Connected to " + remoteAE + " in " + ((t2 - t1) / 1000F) + "s");

            t1 = System.currentTimeMillis();
            dcmsnd.send();
            t2 = System.currentTimeMillis();
            prompt(dcmsnd, (t2 - t1) / 1000F);
            if (dcmsnd.isStorageCommitment()) {
                t1 = System.currentTimeMillis();
                if (dcmsnd.commit()) {
                    t2 = System.currentTimeMillis();
                    System.out.println(
                            "Request Storage Commitment from " + remoteAE + " in " + ((t2 - t1) / 1000F) + "s");
                    System.out.println("Waiting for Storage Commitment Result..");
                    try {
                        DicomObject cmtrslt = dcmsnd.waitForStgCmtResult();
                        t1 = System.currentTimeMillis();
                        promptStgCmt(cmtrslt, ((t1 - t2) / 1000F));
                    } catch (InterruptedException e) {
                        System.err.println("ERROR:" + e.getMessage());
                    }
                }
            }
            dcmsnd.close();
            System.out.println("Released connection to " + remoteAE);
            if (remoteStgCmtAE != null) {
                t1 = System.currentTimeMillis();
                try {
                    dcmsnd.openToStgcmtAE();
                } catch (Exception e) {
                    System.err.println("ERROR: Failed to establish association:" + e.getMessage());
                    System.exit(2);
                }
                t2 = System.currentTimeMillis();
                System.out.println("Connected to " + remoteStgCmtAE + " in " + ((t2 - t1) / 1000F) + "s");
                t1 = System.currentTimeMillis();
                if (dcmsnd.commit()) {
                    t2 = System.currentTimeMillis();
                    System.out.println("Request Storage Commitment from " + remoteStgCmtAE + " in "
                            + ((t2 - t1) / 1000F) + "s");
                    System.out.println("Waiting for Storage Commitment Result..");
                    try {
                        DicomObject cmtrslt = dcmsnd.waitForStgCmtResult();
                        t1 = System.currentTimeMillis();
                        promptStgCmt(cmtrslt, ((t1 - t2) / 1000F));
                    } catch (InterruptedException e) {
                        System.err.println("ERROR:" + e.getMessage());
                    }
                }
                dcmsnd.close();
                System.out.println("Released connection to " + remoteStgCmtAE);
            }
        } finally {
            dcmsnd.stop();
        }
    }
}

From source file:com.upload.DcmSnd.java

@SuppressWarnings("unchecked")
public static void main(String[] args) {
    CommandLine cl = parse(args);
    DcmSnd dcmsnd = new DcmSnd(cl.hasOption("device") ? cl.getOptionValue("device") : "DCMSND");
    final List<String> argList = cl.getArgList();
    String remoteAE = argList.get(0);
    String[] calledAETAddress = split(remoteAE, '@');
    dcmsnd.setCalledAET(calledAETAddress[0]);
    if (calledAETAddress[1] == null) {
        dcmsnd.setRemoteHost("127.0.0.1");
        dcmsnd.setRemotePort(104);/*from   w ww.  j  av  a  2s . c o  m*/
    } else {
        String[] hostPort = split(calledAETAddress[1], ':');
        dcmsnd.setRemoteHost(hostPort[0]);
        dcmsnd.setRemotePort(toPort(hostPort[1]));
    }
    if (cl.hasOption("L")) {
        String localAE = cl.getOptionValue("L");
        String[] localPort = split(localAE, ':');
        if (localPort[1] != null) {
            dcmsnd.setLocalPort(toPort(localPort[1]));
        }
        String[] callingAETHost = split(localPort[0], '@');
        dcmsnd.setCalling(callingAETHost[0]);
        if (callingAETHost[1] != null) {
            dcmsnd.setLocalHost(callingAETHost[1]);
        }
    }
    dcmsnd.setOfferDefaultTransferSyntaxInSeparatePresentationContext(cl.hasOption("ts1"));
    dcmsnd.setSendFileRef(cl.hasOption("fileref"));
    if (cl.hasOption("username")) {
        String username = cl.getOptionValue("username");
        UserIdentity userId;
        if (cl.hasOption("passcode")) {
            String passcode = cl.getOptionValue("passcode");
            userId = new UserIdentity.UsernamePasscode(username, passcode.toCharArray());
        } else {
            userId = new UserIdentity.Username(username);
        }
        userId.setPositiveResponseRequested(cl.hasOption("uidnegrsp"));
        dcmsnd.setUserIdentity(userId);
    }
    dcmsnd.setStorageCommitment(cl.hasOption("stgcmt"));
    String remoteStgCmtAE = null;
    if (cl.hasOption("stgcmtae")) {
        try {
            remoteStgCmtAE = cl.getOptionValue("stgcmtae");
            String[] aet_hostport = split(remoteStgCmtAE, '@');
            String[] host_port = split(aet_hostport[1], ':');
            dcmsnd.setStgcmtCalledAET(aet_hostport[0]);
            dcmsnd.setRemoteStgcmtHost(host_port[0]);
            dcmsnd.setRemoteStgcmtPort(toPort(host_port[1]));
        } catch (Exception e) {
            exit("illegal argument of option -stgcmtae");
        }
    }
    if (cl.hasOption("set")) {
        String[] vals = cl.getOptionValues("set");
        for (int i = 0; i < vals.length; i++, i++) {
            dcmsnd.addCoerceAttr(Tag.toTag(vals[i]), vals[i + 1]);
        }
    }
    if (cl.hasOption("setuid")) {
        dcmsnd.setSuffixUID(cl.getOptionValues("setuid"));
    }
    if (cl.hasOption("connectTO"))
        dcmsnd.setConnectTimeout(parseInt(cl.getOptionValue("connectTO"),
                "illegal argument of option -connectTO", 1, Integer.MAX_VALUE));
    if (cl.hasOption("reaper"))
        dcmsnd.setAssociationReaperPeriod(parseInt(cl.getOptionValue("reaper"),
                "illegal argument of option -reaper", 1, Integer.MAX_VALUE));
    if (cl.hasOption("rspTO"))
        dcmsnd.setDimseRspTimeout(parseInt(cl.getOptionValue("rspTO"), "illegal argument of option -rspTO", 1,
                Integer.MAX_VALUE));
    if (cl.hasOption("acceptTO"))
        dcmsnd.setAcceptTimeout(parseInt(cl.getOptionValue("acceptTO"), "illegal argument of option -acceptTO",
                1, Integer.MAX_VALUE));
    if (cl.hasOption("releaseTO"))
        dcmsnd.setReleaseTimeout(parseInt(cl.getOptionValue("releaseTO"),
                "illegal argument of option -releaseTO", 1, Integer.MAX_VALUE));
    if (cl.hasOption("soclosedelay"))
        dcmsnd.setSocketCloseDelay(parseInt(cl.getOptionValue("soclosedelay"),
                "illegal argument of option -soclosedelay", 1, 10000));
    if (cl.hasOption("shutdowndelay"))
        dcmsnd.setShutdownDelay(parseInt(cl.getOptionValue("shutdowndelay"),
                "illegal argument of option -shutdowndelay", 1, 10000));
    if (cl.hasOption("anonymize"))
        dcmsnd.setAnonymize(Long.parseLong(cl.getOptionValue("anonymize")));
    if (cl.hasOption("rcvpdulen"))
        dcmsnd.setMaxPDULengthReceive(
                parseInt(cl.getOptionValue("rcvpdulen"), "illegal argument of option -rcvpdulen", 1, 10000)
                        * KB);
    if (cl.hasOption("sndpdulen"))
        dcmsnd.setMaxPDULengthSend(
                parseInt(cl.getOptionValue("sndpdulen"), "illegal argument of option -sndpdulen", 1, 10000)
                        * KB);
    if (cl.hasOption("sosndbuf"))
        dcmsnd.setSendBufferSize(
                parseInt(cl.getOptionValue("sosndbuf"), "illegal argument of option -sosndbuf", 1, 10000) * KB);
    if (cl.hasOption("sorcvbuf"))
        dcmsnd.setReceiveBufferSize(
                parseInt(cl.getOptionValue("sorcvbuf"), "illegal argument of option -sorcvbuf", 1, 10000) * KB);
    if (cl.hasOption("bufsize"))
        dcmsnd.setTranscoderBufferSize(
                parseInt(cl.getOptionValue("bufsize"), "illegal argument of option -bufsize", 1, 10000) * KB);
    if (cl.hasOption("batchsize"))
        dcmsnd.setBatchSize(Integer.parseInt(cl.getOptionValue("batchsize")));
    dcmsnd.setPackPDV(!cl.hasOption("pdv1"));
    dcmsnd.setTcpNoDelay(!cl.hasOption("tcpdelay"));
    if (cl.hasOption("async"))
        dcmsnd.setMaxOpsInvoked(
                parseInt(cl.getOptionValue("async"), "illegal argument of option -async", 0, 0xffff));
    if (cl.hasOption("lowprior"))
        dcmsnd.setPriority(CommandUtils.LOW);
    if (cl.hasOption("highprior"))
        dcmsnd.setPriority(CommandUtils.HIGH);
    System.out.println("Scanning files to send");
    long t1 = System.currentTimeMillis();
    for (int i = 1, n = argList.size(); i < n; ++i)
        dcmsnd.addFile(new File(argList.get(i)));
    long t2 = System.currentTimeMillis();
    if (dcmsnd.getNumberOfFilesToSend() == 0) {
        // System.exit(2);
    }
    System.out.println("\nScanned " + dcmsnd.getNumberOfFilesToSend() + " files in " + ((t2 - t1) / 1000F)
            + "s (=" + ((t2 - t1) / dcmsnd.getNumberOfFilesToSend()) + "ms/file)");
    dcmsnd.configureTransferCapability();
    if (cl.hasOption("tls")) {
        String cipher = cl.getOptionValue("tls");
        if ("NULL".equalsIgnoreCase(cipher)) {
            dcmsnd.setTlsWithoutEncyrption();
        } else if ("3DES".equalsIgnoreCase(cipher)) {
            dcmsnd.setTls3DES_EDE_CBC();
        } else if ("AES".equalsIgnoreCase(cipher)) {
            dcmsnd.setTlsAES_128_CBC();
        } else {
            exit("Invalid parameter for option -tls: " + cipher);
        }

        if (cl.hasOption("tls1")) {
            dcmsnd.setTlsProtocol(TLS1);
        } else if (cl.hasOption("ssl3")) {
            dcmsnd.setTlsProtocol(SSL3);
        } else if (cl.hasOption("no_tls1")) {
            dcmsnd.setTlsProtocol(NO_TLS1);
        } else if (cl.hasOption("no_ssl3")) {
            dcmsnd.setTlsProtocol(NO_SSL3);
        } else if (cl.hasOption("no_ssl2")) {
            dcmsnd.setTlsProtocol(NO_SSL2);
        }
        dcmsnd.setTlsNeedClientAuth(!cl.hasOption("noclientauth"));

        if (cl.hasOption("keystore")) {
            dcmsnd.setKeyStoreURL(cl.getOptionValue("keystore"));
        }
        if (cl.hasOption("keystorepw")) {
            dcmsnd.setKeyStorePassword(cl.getOptionValue("keystorepw"));
        }
        if (cl.hasOption("keypw")) {
            dcmsnd.setKeyPassword(cl.getOptionValue("keypw"));
        }
        if (cl.hasOption("truststore")) {
            dcmsnd.setTrustStoreURL(cl.getOptionValue("truststore"));
        }
        if (cl.hasOption("truststorepw")) {
            dcmsnd.setTrustStorePassword(cl.getOptionValue("truststorepw"));
        }
        try {
            dcmsnd.initTLS();
        } catch (Exception e) {
            System.err.println("ERROR: Failed to initialize TLS context:" + e.getMessage());
            // System.exit(2);
        }
    }
    while (dcmsnd.getLastSentFile() < dcmsnd.getNumberOfFilesToSend()) {
        try {
            dcmsnd.start();
        } catch (Exception e) {
            System.err.println("ERROR: Failed to start server for receiving " + "Storage Commitment results:"
                    + e.getMessage());
            //  System.exit(2);
        }
        try {
            t1 = System.currentTimeMillis();
            try {
                dcmsnd.open();
            } catch (Exception e) {
                System.err.println("ERROR: Failed to establish association:" + e.getMessage());
                // System.exit(2);
            }
            t2 = System.currentTimeMillis();
            System.out.println("Connected to " + remoteAE + " in " + ((t2 - t1) / 1000F) + "s");

            t1 = System.currentTimeMillis();
            dcmsnd.send();
            t2 = System.currentTimeMillis();
            prompt(dcmsnd, (t2 - t1) / 1000F);
            if (dcmsnd.isStorageCommitment()) {
                t1 = System.currentTimeMillis();
                if (dcmsnd.commit()) {
                    t2 = System.currentTimeMillis();
                    System.out.println(
                            "Request Storage Commitment from " + remoteAE + " in " + ((t2 - t1) / 1000F) + "s");
                    System.out.println("Waiting for Storage Commitment Result..");
                    try {
                        DicomObject cmtrslt = dcmsnd.waitForStgCmtResult();
                        t1 = System.currentTimeMillis();
                        promptStgCmt(cmtrslt, ((t1 - t2) / 1000F));
                    } catch (InterruptedException e) {
                        System.err.println("ERROR:" + e.getMessage());
                    }
                }
            }
            dcmsnd.close();
            System.out.println("Released connection to " + remoteAE);
            if (remoteStgCmtAE != null) {
                t1 = System.currentTimeMillis();
                try {
                    dcmsnd.openToStgcmtAE();
                } catch (Exception e) {
                    System.err.println("ERROR: Failed to establish association:" + e.getMessage());
                    // System.exit(2);
                }
                t2 = System.currentTimeMillis();
                System.out.println("Connected to " + remoteStgCmtAE + " in " + ((t2 - t1) / 1000F) + "s");
                t1 = System.currentTimeMillis();
                if (dcmsnd.commit()) {
                    t2 = System.currentTimeMillis();
                    System.out.println("Request Storage Commitment from " + remoteStgCmtAE + " in "
                            + ((t2 - t1) / 1000F) + "s");
                    System.out.println("Waiting for Storage Commitment Result..");
                    try {
                        DicomObject cmtrslt = dcmsnd.waitForStgCmtResult();
                        t1 = System.currentTimeMillis();
                        promptStgCmt(cmtrslt, ((t1 - t2) / 1000F));
                    } catch (InterruptedException e) {
                        System.err.println("ERROR:" + e.getMessage());
                    }
                }
                dcmsnd.close();
                System.out.println("Released connection to " + remoteStgCmtAE);
            }
        } finally {
            dcmsnd.stop();
        }
    }
}

From source file:edu.nyu.vida.data_polygamy.relationship_computation.Relationship.java

/**
 * @param args//w ww . j a va  2s.  com
 * @throws ParseException 
 */
@SuppressWarnings({ "deprecation" })
public static void main(String[] args) throws IOException, InterruptedException, ClassNotFoundException {

    Options options = new Options();

    Option forceOption = new Option("f", "force", false,
            "force the computation of the relationship " + "even if files already exist");
    forceOption.setRequired(false);
    options.addOption(forceOption);

    Option scoreOption = new Option("sc", "score", true, "set threhsold for relationship score");
    scoreOption.setRequired(false);
    scoreOption.setArgName("SCORE THRESHOLD");
    options.addOption(scoreOption);

    Option strengthOption = new Option("st", "strength", true, "set threhsold for relationship strength");
    strengthOption.setRequired(false);
    strengthOption.setArgName("STRENGTH THRESHOLD");
    options.addOption(strengthOption);

    Option completeRandomizationOption = new Option("c", "complete-randomization", false,
            "use complete randomization when performing significance tests");
    completeRandomizationOption.setRequired(false);
    options.addOption(completeRandomizationOption);

    Option idOption = new Option("id", "ids", false, "output id instead of names for datasets and attributes");
    idOption.setRequired(false);
    options.addOption(idOption);

    Option g1Option = new Option("g1", "first-group", true, "set first group of datasets");
    g1Option.setRequired(true);
    g1Option.setArgName("FIRST GROUP");
    g1Option.setArgs(Option.UNLIMITED_VALUES);
    options.addOption(g1Option);

    Option g2Option = new Option("g2", "second-group", true, "set second group of datasets");
    g2Option.setRequired(false);
    g2Option.setArgName("SECOND GROUP");
    g2Option.setArgs(Option.UNLIMITED_VALUES);
    options.addOption(g2Option);

    Option machineOption = new Option("m", "machine", true, "machine identifier");
    machineOption.setRequired(true);
    machineOption.setArgName("MACHINE");
    machineOption.setArgs(1);
    options.addOption(machineOption);

    Option nodesOption = new Option("n", "nodes", true, "number of nodes");
    nodesOption.setRequired(true);
    nodesOption.setArgName("NODES");
    nodesOption.setArgs(1);
    options.addOption(nodesOption);

    Option s3Option = new Option("s3", "s3", false, "data on Amazon S3");
    s3Option.setRequired(false);
    options.addOption(s3Option);

    Option awsAccessKeyIdOption = new Option("aws_id", "aws-id", true,
            "aws access key id; " + "this is required if the execution is on aws");
    awsAccessKeyIdOption.setRequired(false);
    awsAccessKeyIdOption.setArgName("AWS-ACCESS-KEY-ID");
    awsAccessKeyIdOption.setArgs(1);
    options.addOption(awsAccessKeyIdOption);

    Option awsSecretAccessKeyOption = new Option("aws_key", "aws-id", true,
            "aws secrect access key; " + "this is required if the execution is on aws");
    awsSecretAccessKeyOption.setRequired(false);
    awsSecretAccessKeyOption.setArgName("AWS-SECRET-ACCESS-KEY");
    awsSecretAccessKeyOption.setArgs(1);
    options.addOption(awsSecretAccessKeyOption);

    Option bucketOption = new Option("b", "s3-bucket", true,
            "bucket on s3; " + "this is required if the execution is on aws");
    bucketOption.setRequired(false);
    bucketOption.setArgName("S3-BUCKET");
    bucketOption.setArgs(1);
    options.addOption(bucketOption);

    Option helpOption = new Option("h", "help", false, "display this message");
    helpOption.setRequired(false);
    options.addOption(helpOption);

    Option removeOption = new Option("r", "remove-not-significant", false,
            "remove relationships that are not" + "significant from the final output");
    removeOption.setRequired(false);
    options.addOption(removeOption);

    HelpFormatter formatter = new HelpFormatter();
    CommandLineParser parser = new PosixParser();
    CommandLine cmd = null;

    try {
        cmd = parser.parse(options, args);
    } catch (ParseException e) {
        formatter.printHelp("hadoop jar data-polygamy.jar "
                + "edu.nyu.vida.data_polygamy.relationship_computation.Relationship", options, true);
        System.exit(0);
    }

    if (cmd.hasOption("h")) {
        formatter.printHelp("hadoop jar data-polygamy.jar "
                + "edu.nyu.vida.data_polygamy.relationship_computation.Relationship", options, true);
        System.exit(0);
    }

    boolean s3 = cmd.hasOption("s3");
    String s3bucket = "";
    String awsAccessKeyId = "";
    String awsSecretAccessKey = "";

    if (s3) {
        if ((!cmd.hasOption("aws_id")) || (!cmd.hasOption("aws_key")) || (!cmd.hasOption("b"))) {
            System.out.println(
                    "Arguments 'aws_id', 'aws_key', and 'b'" + " are mandatory if execution is on AWS.");
            formatter.printHelp(
                    "hadoop jar data-polygamy.jar "
                            + "edu.nyu.vida.data_polygamy.relationship_computation.Relationship",
                    options, true);
            System.exit(0);
        }
        s3bucket = cmd.getOptionValue("b");
        awsAccessKeyId = cmd.getOptionValue("aws_id");
        awsSecretAccessKey = cmd.getOptionValue("aws_key");
    }

    boolean snappyCompression = false;
    boolean bzip2Compression = false;
    String machine = cmd.getOptionValue("m");
    int nbNodes = Integer.parseInt(cmd.getOptionValue("n"));

    Configuration s3conf = new Configuration();
    if (s3) {
        s3conf.set("fs.s3.awsAccessKeyId", awsAccessKeyId);
        s3conf.set("fs.s3.awsSecretAccessKey", awsSecretAccessKey);
        s3conf.set("bucket", s3bucket);
    }

    Path path = null;
    FileSystem fs = FileSystem.get(new Configuration());

    ArrayList<String> shortDataset = new ArrayList<String>();
    ArrayList<String> firstGroup = new ArrayList<String>();
    ArrayList<String> secondGroup = new ArrayList<String>();
    HashMap<String, String> datasetAgg = new HashMap<String, String>();

    boolean removeNotSignificant = cmd.hasOption("r");
    boolean removeExistingFiles = cmd.hasOption("f");
    boolean completeRandomization = cmd.hasOption("c");
    boolean hasScoreThreshold = cmd.hasOption("sc");
    boolean hasStrengthThreshold = cmd.hasOption("st");
    boolean outputIds = cmd.hasOption("id");
    String scoreThreshold = hasScoreThreshold ? cmd.getOptionValue("sc") : "";
    String strengthThreshold = hasStrengthThreshold ? cmd.getOptionValue("st") : "";

    // all datasets
    ArrayList<String> all_datasets = new ArrayList<String>();
    if (s3) {
        path = new Path(s3bucket + FrameworkUtils.datasetsIndexDir);
        fs = FileSystem.get(path.toUri(), s3conf);
    } else {
        path = new Path(fs.getHomeDirectory() + "/" + FrameworkUtils.datasetsIndexDir);
    }
    BufferedReader br = new BufferedReader(new InputStreamReader(fs.open(path)));
    String line = br.readLine();
    while (line != null) {
        all_datasets.add(line.split("\t")[0]);
        line = br.readLine();
    }
    br.close();
    if (s3)
        fs.close();
    String[] all_datasets_array = new String[all_datasets.size()];
    all_datasets.toArray(all_datasets_array);

    String[] firstGroupCmd = cmd.getOptionValues("g1");
    String[] secondGroupCmd = cmd.hasOption("g2") ? cmd.getOptionValues("g2") : all_datasets_array;
    addDatasets(firstGroupCmd, firstGroup, shortDataset, datasetAgg, path, fs, s3conf, s3, s3bucket);
    addDatasets(secondGroupCmd, secondGroup, shortDataset, datasetAgg, path, fs, s3conf, s3, s3bucket);

    if (shortDataset.size() == 0) {
        System.out.println("No datasets to process.");
        System.exit(0);
    }

    if (firstGroup.isEmpty()) {
        System.out.println("No indices from datasets in G1.");
        System.exit(0);
    }

    if (secondGroup.isEmpty()) {
        System.out.println("No indices from datasets in G2.");
        System.exit(0);
    }

    // getting dataset ids

    String datasetNames = "";
    String datasetIds = "";
    HashMap<String, String> datasetId = new HashMap<String, String>();
    Iterator<String> it = shortDataset.iterator();
    while (it.hasNext()) {
        datasetId.put(it.next(), null);
    }

    if (s3) {
        path = new Path(s3bucket + FrameworkUtils.datasetsIndexDir);
        fs = FileSystem.get(path.toUri(), s3conf);
    } else {
        path = new Path(fs.getHomeDirectory() + "/" + FrameworkUtils.datasetsIndexDir);
    }
    br = new BufferedReader(new InputStreamReader(fs.open(path)));
    line = br.readLine();
    while (line != null) {
        String[] dt = line.split("\t");
        all_datasets.add(dt[0]);
        if (datasetId.containsKey(dt[0])) {
            datasetId.put(dt[0], dt[1]);
            datasetNames += dt[0] + ",";
            datasetIds += dt[1] + ",";
        }
        line = br.readLine();
    }
    br.close();
    if (s3)
        fs.close();

    datasetNames = datasetNames.substring(0, datasetNames.length() - 1);
    datasetIds = datasetIds.substring(0, datasetIds.length() - 1);
    it = shortDataset.iterator();
    while (it.hasNext()) {
        String dataset = it.next();
        if (datasetId.get(dataset) == null) {
            System.out.println("No dataset id for " + dataset);
            System.exit(0);
        }
    }

    String firstGroupStr = "";
    String secondGroupStr = "";
    for (String dataset : firstGroup) {
        firstGroupStr += datasetId.get(dataset) + ",";
    }
    for (String dataset : secondGroup) {
        secondGroupStr += datasetId.get(dataset) + ",";
    }
    firstGroupStr = firstGroupStr.substring(0, firstGroupStr.length() - 1);
    secondGroupStr = secondGroupStr.substring(0, secondGroupStr.length() - 1);

    String relationshipsDir = "";
    if (outputIds) {
        relationshipsDir = FrameworkUtils.relationshipsIdsDir;
    } else {
        relationshipsDir = FrameworkUtils.relationshipsDir;
    }

    FrameworkUtils.createDir(s3bucket + relationshipsDir, s3conf, s3);

    String random = completeRandomization ? "complete" : "restricted";

    String indexInputDirs = "";
    String noRelationship = "";

    HashSet<String> dirs = new HashSet<String>();

    String dataset1;
    String dataset2;
    String datasetId1;
    String datasetId2;
    for (int i = 0; i < firstGroup.size(); i++) {
        for (int j = 0; j < secondGroup.size(); j++) {

            if (Integer.parseInt(datasetId.get(firstGroup.get(i))) < Integer
                    .parseInt(datasetId.get(secondGroup.get(j)))) {
                dataset1 = firstGroup.get(i);
                dataset2 = secondGroup.get(j);
            } else {
                dataset1 = secondGroup.get(j);
                dataset2 = firstGroup.get(i);
            }

            datasetId1 = datasetId.get(dataset1);
            datasetId2 = datasetId.get(dataset2);

            if (dataset1.equals(dataset2))
                continue;
            String correlationOutputFileName = s3bucket + relationshipsDir + "/" + dataset1 + "-" + dataset2
                    + "/";

            if (removeExistingFiles) {
                FrameworkUtils.removeFile(correlationOutputFileName, s3conf, s3);
            }
            if (!FrameworkUtils.fileExists(correlationOutputFileName, s3conf, s3)) {
                dirs.add(s3bucket + FrameworkUtils.indexDir + "/" + dataset1);
                dirs.add(s3bucket + FrameworkUtils.indexDir + "/" + dataset2);
            } else {
                noRelationship += datasetId1 + "-" + datasetId2 + ",";
            }
        }
    }

    if (dirs.isEmpty()) {
        System.out.println("All the relationships were already computed.");
        System.out.println("Use -f in the beginning of the command line to force the computation.");
        System.exit(0);
    }

    for (String dir : dirs) {
        indexInputDirs += dir + ",";
    }

    Configuration conf = new Configuration();
    Machine machineConf = new Machine(machine, nbNodes);

    String jobName = "relationship" + "-" + random;
    String relationshipOutputDir = s3bucket + relationshipsDir + "/tmp/";

    FrameworkUtils.removeFile(relationshipOutputDir, s3conf, s3);

    for (int i = 0; i < shortDataset.size(); i++) {
        conf.set("dataset-" + datasetId.get(shortDataset.get(i)) + "-agg", datasetAgg.get(shortDataset.get(i)));
    }
    for (int i = 0; i < shortDataset.size(); i++) {
        conf.set("dataset-" + datasetId.get(shortDataset.get(i)) + "-agg-size",
                Integer.toString(datasetAgg.get(shortDataset.get(i)).split(",").length));
    }
    conf.set("dataset-keys", datasetIds);
    conf.set("dataset-names", datasetNames);
    conf.set("first-group", firstGroupStr);
    conf.set("second-group", secondGroupStr);
    conf.set("complete-random", String.valueOf(completeRandomization));
    conf.set("output-ids", String.valueOf(outputIds));
    conf.set("complete-random-str", random);
    conf.set("main-dataset-id", datasetId.get(shortDataset.get(0)));
    conf.set("remove-not-significant", String.valueOf(removeNotSignificant));
    if (noRelationship.length() > 0) {
        conf.set("no-relationship", noRelationship.substring(0, noRelationship.length() - 1));
    }
    if (hasScoreThreshold) {
        conf.set("score-threshold", scoreThreshold);
    }
    if (hasStrengthThreshold) {
        conf.set("strength-threshold", strengthThreshold);
    }

    conf.set("mapreduce.tasktracker.map.tasks.maximum", String.valueOf(machineConf.getMaximumTasks()));
    conf.set("mapreduce.tasktracker.reduce.tasks.maximum", String.valueOf(machineConf.getMaximumTasks()));
    conf.set("mapreduce.jobtracker.maxtasks.perjob", "-1");
    conf.set("mapreduce.reduce.shuffle.parallelcopies", "20");
    conf.set("mapreduce.input.fileinputformat.split.minsize", "0");
    conf.set("mapreduce.task.io.sort.mb", "200");
    conf.set("mapreduce.task.io.sort.factor", "100");
    conf.set("mapreduce.task.timeout", "2400000");

    if (s3) {
        machineConf.setMachineConfiguration(conf);
        conf.set("fs.s3.awsAccessKeyId", awsAccessKeyId);
        conf.set("fs.s3.awsSecretAccessKey", awsSecretAccessKey);
        conf.set("bucket", s3bucket);
    }

    if (snappyCompression) {
        conf.set("mapreduce.map.output.compress", "true");
        conf.set("mapreduce.map.output.compress.codec", "org.apache.hadoop.io.compress.SnappyCodec");
        //conf.set("mapreduce.output.fileoutputformat.compress.codec", "org.apache.hadoop.io.compress.SnappyCodec");
    }
    if (bzip2Compression) {
        conf.set("mapreduce.map.output.compress", "true");
        conf.set("mapreduce.map.output.compress.codec", "org.apache.hadoop.io.compress.BZip2Codec");
        //conf.set("mapreduce.output.fileoutputformat.compress.codec", "org.apache.hadoop.io.compress.BZip2Codec");
    }

    Job job = new Job(conf);
    job.setJobName(jobName);

    job.setMapOutputKeyClass(PairAttributeWritable.class);
    job.setMapOutputValueClass(TopologyTimeSeriesWritable.class);
    job.setOutputKeyClass(Text.class);
    job.setOutputValueClass(Text.class);

    job.setMapperClass(CorrelationMapper.class);
    job.setReducerClass(CorrelationReducer.class);
    job.setNumReduceTasks(machineConf.getNumberReduces());

    job.setInputFormatClass(SequenceFileInputFormat.class);
    //job.setOutputFormatClass(TextOutputFormat.class);
    LazyOutputFormat.setOutputFormatClass(job, TextOutputFormat.class);

    FileInputFormat.setInputDirRecursive(job, true);
    FileInputFormat.setInputPaths(job, indexInputDirs.substring(0, indexInputDirs.length() - 1));
    FileOutputFormat.setOutputPath(job, new Path(relationshipOutputDir));

    job.setJarByClass(Relationship.class);

    long start = System.currentTimeMillis();
    job.submit();
    job.waitForCompletion(true);
    System.out.println(jobName + "\t" + (System.currentTimeMillis() - start));

    // moving files to right place
    for (int i = 0; i < firstGroup.size(); i++) {
        for (int j = 0; j < secondGroup.size(); j++) {

            if (Integer.parseInt(datasetId.get(firstGroup.get(i))) < Integer
                    .parseInt(datasetId.get(secondGroup.get(j)))) {
                dataset1 = firstGroup.get(i);
                dataset2 = secondGroup.get(j);
            } else {
                dataset1 = secondGroup.get(j);
                dataset2 = firstGroup.get(i);
            }

            if (dataset1.equals(dataset2))
                continue;

            String from = s3bucket + relationshipsDir + "/tmp/" + dataset1 + "-" + dataset2 + "/";
            String to = s3bucket + relationshipsDir + "/" + dataset1 + "-" + dataset2 + "/";
            FrameworkUtils.renameFile(from, to, s3conf, s3);
        }
    }
}

From source file:com.github.wshackle.java4cpp.J4CppMain.java

public static void main(String[] args) throws IOException, ClassNotFoundException {
    main_completed = false;//from  w ww.  j a  v  a2  s  .  c  om

    Options options = new Options();
    options.addOption(Option.builder("?").desc("Print this message").longOpt("help").build());
    options.addOption(Option.builder("n").hasArg().desc("C++ namespace for newly generated classes.")
            .longOpt("namespace").build());
    options.addOption(
            Option.builder("c").hasArgs().desc("Single Java class to extract.").longOpt("classes").build());
    options.addOption(
            Option.builder("p").hasArgs().desc("Java Package prefix to extract").longOpt("packages").build());
    options.addOption(Option.builder("o").hasArg().desc("Output C++ source file.").longOpt("output").build());
    options.addOption(Option.builder("j").hasArg().desc("Input jar file").longOpt("jar").build());
    options.addOption(Option.builder("h").hasArg().desc("Output C++ header file.").longOpt("header").build());
    options.addOption(Option.builder("l").hasArg()
            .desc("Maximum limit on classes to extract from jars.[default=200]").longOpt("limit").build());
    options.addOption(Option.builder("v").desc("enable verbose output").longOpt("verbose").build());
    options.addOption(Option.builder().hasArg().desc("Classes per output file.[default=10]")
            .longOpt(CLASSESPEROUTPUT).build());
    options.addOption(Option.builder().hasArgs().desc(
            "Comma seperated list of nativeclass=javaclass native where nativeclass will be generated as an extension/implementation of the java class.")
            .longOpt("natives").build());
    options.addOption(Option.builder().hasArg()
            .desc("library name for System.loadLibrary(...) for native extension classes")
            .longOpt("loadlibname").build());
    String output = null;
    String header = null;
    String jar = null;
    Set<String> classnamesToFind = null;
    Set<String> packageprefixes = null;
    String loadlibname = null;

    Map<String, String> nativesNameMap = null;
    Map<String, Class> nativesClassMap = null;
    int limit = DEFAULT_LIMIT;
    int classes_per_file = 10;
    List<Class> classes = new ArrayList<>();

    String limitstring = Integer.toString(limit);

    try {
        // parse the command line arguments
        System.out.println("args = " + Arrays.toString(args));
        CommandLine line = new DefaultParser().parse(options, args);
        loadlibname = line.getOptionValue("loadlibname");
        verbose = line.hasOption("verbose");
        if (line.hasOption(CLASSESPEROUTPUT)) {
            String cpoStr = line.getOptionValue(CLASSESPEROUTPUT);
            try {
                int cpoI = Integer.valueOf(cpoStr);
                classes_per_file = cpoI;
            } catch (Exception e) {
                System.err.println("Option for " + CLASSESPEROUTPUT + "=\"" + cpoStr + "\"");
                printHelpAndExit(options, args);
            }
        }

        if (line.hasOption("natives")) {
            String natives[] = line.getOptionValues("natives");
            if (verbose) {
                System.out.println("natives = " + Arrays.toString(natives));
            }
            nativesNameMap = new HashMap<>();
            nativesClassMap = new HashMap<>();
            for (int i = 0; i < natives.length; i++) {
                int eq_index = natives[i].indexOf('=');
                String nativeClassName = natives[i].substring(0, eq_index).trim();
                String javaClassName = natives[i].substring(eq_index + 1).trim();
                Class javaClass = null;
                try {
                    javaClass = Class.forName(javaClassName);
                } catch (ClassNotFoundException e) {
                    //e.printStackTrace();
                    System.err.println("Class for " + javaClassName
                            + " not found. (It may be found later in jar if specified.)");
                }
                nativesNameMap.put(javaClassName, nativeClassName);
                if (javaClass != null) {
                    nativesClassMap.put(nativeClassName, javaClass);
                    if (!classes.contains(javaClass)) {
                        classes.add(javaClass);
                    }
                }
            }
        }
        //            // validate that block-size has been set
        //            if (line.hasOption("block-size")) {
        //                // print the value of block-size
        //                if(verbose) System.out.println(line.getOptionValue("block-size"));
        //            }
        jar = line.getOptionValue("jar", jar);
        if (verbose) {
            System.out.println("jar = " + jar);
        }
        if (null != jar) {
            if (jar.startsWith("~/")) {
                jar = new File(new File(getHomeDir()), jar.substring(2)).getCanonicalPath();
            }
            if (jar.startsWith("./")) {
                jar = new File(new File(getCurrentDir()), jar.substring(2)).getCanonicalPath();
            }
            if (jar.startsWith("../")) {
                jar = new File(new File(getCurrentDir()).getParentFile(), jar.substring(3)).getCanonicalPath();
            }
        }
        if (line.hasOption("classes")) {
            classnamesToFind = new HashSet<String>();
            String classStrings[] = line.getOptionValues("classes");
            if (verbose) {
                System.out.println("classStrings = " + Arrays.toString(classStrings));
            }
            classnamesToFind.addAll(Arrays.asList(classStrings));
            if (verbose) {
                System.out.println("classnamesToFind = " + classnamesToFind);
            }
        }
        //                if (!line.hasOption("namespace")) {
        //                    if (classname != null && classname.length() > 0) {
        //                        namespace = classname.toLowerCase().replace(".", "_");
        //                    } else if (jar != null && jar.length() > 0) {
        //                        int lastSep = jar.lastIndexOf(File.separator);
        //                        int start = Math.max(0, lastSep + 1);
        //                        int period = jar.indexOf('.', start + 1);
        //                        int end = Math.max(start + 1, period);
        //                        namespace = jar.substring(start, end).toLowerCase();
        //                        namespace = namespace.replace(" ", "_");
        //                        if (namespace.indexOf("-") > 0) {
        //                            namespace = namespace.substring(0, namespace.indexOf("-"));
        //                        }
        //                    }
        //                }

        namespace = line.getOptionValue("namespace", namespace);
        if (verbose) {
            System.out.println("namespace = " + namespace);
        }
        if (null != namespace) {
            output = namespace + ".cpp";
        }
        output = line.getOptionValue("output", output);
        if (verbose) {
            System.out.println("output = " + output);
        }
        if (null != output) {
            if (output.startsWith("~/")) {
                output = System.getProperty("user.home") + output.substring(1);
            }
            header = output.substring(0, output.lastIndexOf('.')) + ".h";
        } else {
            output = "out.cpp";
        }
        header = line.getOptionValue("header", header);
        if (verbose) {
            System.out.println("header = " + header);
        }
        if (null != header) {
            if (header.startsWith("~/")) {
                header = System.getProperty("user.home") + header.substring(1);
            }
        } else {
            header = "out.h";
        }

        if (line.hasOption("packages")) {
            packageprefixes = new HashSet<String>();
            packageprefixes.addAll(Arrays.asList(line.getOptionValues("packages")));
        }
        if (line.hasOption("limit")) {
            limitstring = line.getOptionValue("limit", Integer.toString(DEFAULT_LIMIT));
            limit = Integer.valueOf(limitstring);
        }
        if (line.hasOption("help")) {
            printHelpAndExit(options, args);
        }
    } catch (ParseException exp) {
        if (verbose) {
            System.out.println("Unexpected exception:" + exp.getMessage());
        }
        printHelpAndExit(options, args);
    }

    Set<Class> excludedClasses = new HashSet<>();
    Set<String> foundClassNames = new HashSet<>();
    excludedClasses.add(Object.class);
    excludedClasses.add(String.class);
    excludedClasses.add(void.class);
    excludedClasses.add(Void.class);
    excludedClasses.add(Class.class);
    excludedClasses.add(Enum.class);
    Set<String> packagesSet = new TreeSet<>();
    List<URL> urlsList = new ArrayList<>();
    String cp = System.getProperty("java.class.path");
    if (verbose) {
        System.out.println("System.getProperty(\"java.class.path\") = " + cp);
    }
    if (null != cp) {
        for (String cpe : cp.split(File.pathSeparator)) {
            if (verbose) {
                System.out.println("class path element = " + cpe);
            }
            File f = new File(cpe);
            if (f.isDirectory()) {
                urlsList.add(new URL("file:" + f.getCanonicalPath() + File.separator));
            } else if (cpe.endsWith(".jar")) {
                urlsList.add(new URL("jar:file:" + f.getCanonicalPath() + "!/"));
            }
        }
    }
    cp = System.getenv("CLASSPATH");
    if (verbose) {
        System.out.println("System.getenv(\"CLASSPATH\") = " + cp);
    }
    if (null != cp) {
        for (String cpe : cp.split(File.pathSeparator)) {
            if (verbose) {
                System.out.println("class path element = " + cpe);
            }
            File f = new File(cpe);
            if (f.isDirectory()) {
                urlsList.add(new URL("file:" + f.getCanonicalPath() + File.separator));
            } else if (cpe.endsWith(".jar")) {
                urlsList.add(new URL("jar:file:" + f.getCanonicalPath() + "!/"));
            }
        }
    }
    if (verbose) {
        System.out.println("urlsList = " + urlsList);
    }

    if (null != jar && jar.length() > 0) {
        Path jarPath = FileSystems.getDefault().getPath(jar);
        ZipInputStream zip = new ZipInputStream(Files.newInputStream(jarPath, StandardOpenOption.READ));

        URL jarUrl = new URL("jar:file:" + jarPath.toFile().getCanonicalPath() + "!/");
        urlsList.add(jarUrl);
        URL[] urls = urlsList.toArray(new URL[urlsList.size()]);
        if (verbose) {
            System.out.println("urls = " + Arrays.toString(urls));
        }
        URLClassLoader cl = URLClassLoader.newInstance(urls);
        for (ZipEntry entry = zip.getNextEntry(); entry != null; entry = zip.getNextEntry()) {
            // This ZipEntry represents a class. Now, what class does it represent?
            String entryName = entry.getName();
            if (verbose) {
                System.out.println("entryName = " + entryName);
            }

            if (!entry.isDirectory() && entryName.endsWith(".class")) {

                if (entryName.indexOf('$') >= 0) {
                    continue;
                }
                String classFileName = entry.getName().replace('/', '.');
                String className = classFileName.substring(0, classFileName.length() - ".class".length());
                if (classnamesToFind != null && classnamesToFind.size() > 0
                        && !classnamesToFind.contains(className)) {
                    if (verbose) {
                        System.out.println("skipping className=" + className + " because it does not found in="
                                + classnamesToFind);
                    }
                    continue;
                }
                try {
                    Class clss = cl.loadClass(className);
                    if (null != nativesClassMap && null != nativesNameMap
                            && nativesNameMap.containsKey(className)) {
                        nativesClassMap.put(nativesNameMap.get(className), clss);
                        if (!classes.contains(clss)) {
                            classes.add(clss);
                        }
                    }
                    if (packageprefixes != null && packageprefixes.size() > 0) {
                        if (null == clss.getPackage()) {
                            continue;
                        }
                        final String pkgName = clss.getPackage().getName();
                        boolean matchFound = false;
                        for (String prefix : packageprefixes) {
                            if (pkgName.startsWith(prefix)) {
                                matchFound = true;
                                break;
                            }
                        }
                        if (!matchFound) {
                            continue;
                        }
                    }
                    Package p = clss.getPackage();
                    if (null != p) {
                        packagesSet.add(clss.getPackage().getName());
                    }
                    if (!classes.contains(clss) && isAddableClass(clss, excludedClasses)) {
                        if (null != classnamesToFind && classnamesToFind.contains(className)
                                && !foundClassNames.contains(className)) {
                            foundClassNames.add(className);
                            if (verbose) {
                                System.out.println("foundClassNames = " + foundClassNames);
                            }
                        }
                        //                        if(verbose) System.out.println("clss = " + clss);
                        classes.add(clss);
                        //                        Class superClass = clss.getSuperclass();
                        //                        while (null != superClass
                        //                                && !classes.contains(superClass)
                        //                                && isAddableClass(superClass, excludedClasses)) {
                        //                            classes.add(superClass);
                        //                            superClass = superClass.getSuperclass();
                        //                        }
                    }
                } catch (ClassNotFoundException | NoClassDefFoundError ex) {
                    System.err.println(
                            "Caught " + ex.getClass().getName() + ":" + ex.getMessage() + " for className="
                                    + className + ", entryName=" + entryName + ", jarPath=" + jarPath);
                }
            }
        }
    }
    if (null != classnamesToFind) {
        if (verbose) {
            System.out.println("Checking classnames arguments");
        }
        for (String classname : classnamesToFind) {
            if (verbose) {
                System.out.println("classname = " + classname);
            }
            if (foundClassNames.contains(classname)) {
                if (verbose) {
                    System.out.println("foundClassNames.contains(" + classname + ")");
                }
                continue;
            }
            try {
                if (classes.contains(Class.forName(classname))) {
                    if (verbose) {
                        System.out.println("Classes list already contains:  " + classname);
                    }
                    continue;
                }
            } catch (Exception e) {

            }

            if (null != classname && classname.length() > 0) {
                urlsList.add(new URL("file://" + System.getProperty("user.dir") + "/"));

                URL[] urls = urlsList.toArray(new URL[urlsList.size()]);
                if (verbose) {
                    System.out.println("urls = " + Arrays.toString(urls));
                }
                URLClassLoader cl = URLClassLoader.newInstance(urls);
                Class c = null;
                try {
                    c = cl.loadClass(classname);
                } catch (ClassNotFoundException e) {
                    System.err.println("Class " + classname + " not found ");
                }
                if (verbose) {
                    System.out.println("c = " + c);
                }
                if (null == c) {
                    try {
                        c = ClassLoader.getSystemClassLoader().loadClass(classname);
                    } catch (ClassNotFoundException e) {
                        if (verbose) {
                            System.out.println("System ClassLoader failed to find " + classname);
                        }
                    }
                }
                if (null != c) {
                    classes.add(c);
                } else {
                    System.err.println("Class " + classname + " not found");
                }
            }
        }
        if (verbose) {
            System.out.println("Finished checking classnames arguments");
        }
    }
    if (classes == null || classes.size() < 1) {
        if (null == nativesClassMap || nativesClassMap.keySet().size() < 1) {
            System.err.println("No Classes specified or found.");
            System.exit(1);
        }
    }
    if (verbose) {
        System.out.println("Classes found = " + classes.size());
    }
    if (classes.size() > limit) {
        System.err.println("limit=" + limit);
        System.err.println(
                "Too many classes please use -c or -p options to limit classes or -l to increase limit.");
        if (verbose) {
            System.out.println("packagesSet = " + packagesSet);
        }
        System.exit(1);
    }
    List<Class> newClasses = new ArrayList<Class>();
    for (Class clss : classes) {
        Class superClass = clss.getSuperclass();
        while (null != superClass && !classes.contains(superClass) && !newClasses.contains(superClass)
                && isAddableClass(superClass, excludedClasses)) {
            newClasses.add(superClass);
            superClass = superClass.getSuperclass();
        }
        try {
            Field fa[] = clss.getDeclaredFields();
            for (Field f : fa) {
                if (Modifier.isPublic(f.getModifiers())) {
                    Class fClass = f.getType();
                    if (!classes.contains(fClass) && !newClasses.contains(fClass)
                            && isAddableClass(fClass, excludedClasses)) {
                        newClasses.add(fClass);
                    }
                }
            }
        } catch (NoClassDefFoundError e) {
            e.printStackTrace();
        }
        for (Method m : clss.getDeclaredMethods()) {
            if (m.isSynthetic()) {
                continue;
            }
            if (!Modifier.isPublic(m.getModifiers()) || Modifier.isAbstract(m.getModifiers())) {
                continue;
            }
            Class retType = m.getReturnType();
            if (verbose) {
                System.out.println("Checking dependancies for Method = " + m);
            }
            if (!classes.contains(retType) && !newClasses.contains(retType)
                    && isAddableClass(retType, excludedClasses)) {
                newClasses.add(retType);
                superClass = retType.getSuperclass();
                while (null != superClass && !classes.contains(superClass) && !newClasses.contains(superClass)
                        && isAddableClass(superClass, excludedClasses)) {
                    newClasses.add(superClass);
                    superClass = superClass.getSuperclass();
                }
            }
            for (Class paramType : m.getParameterTypes()) {
                if (!classes.contains(paramType) && !newClasses.contains(paramType)
                        && isAddableClass(paramType, excludedClasses)) {
                    newClasses.add(paramType);
                    superClass = paramType.getSuperclass();
                    while (null != superClass && !classes.contains(superClass)
                            && !newClasses.contains(superClass) && !excludedClasses.contains(superClass)) {
                        newClasses.add(superClass);
                        superClass = superClass.getSuperclass();
                    }
                }
            }
        }
    }
    if (null != nativesClassMap) {
        for (Class clss : nativesClassMap.values()) {
            if (null != clss) {
                Class superClass = clss.getSuperclass();
                while (null != superClass && !classes.contains(superClass) && !newClasses.contains(superClass)
                        && !excludedClasses.contains(superClass)) {
                    newClasses.add(superClass);
                    superClass = superClass.getSuperclass();
                }
            }
        }
    }
    if (verbose) {
        System.out.println("Dependency classes needed = " + newClasses.size());
    }
    classes.addAll(newClasses);
    List<Class> newOrderClasses = new ArrayList<>();
    for (Class clss : classes) {
        if (newOrderClasses.contains(clss)) {
            continue;
        }
        Class superClass = clss.getSuperclass();
        Stack<Class> stack = new Stack<>();
        while (null != superClass && !newOrderClasses.contains(superClass)
                && !superClass.equals(java.lang.Object.class)) {
            stack.push(superClass);
            superClass = superClass.getSuperclass();
        }
        while (!stack.empty()) {
            newOrderClasses.add(stack.pop());
        }
        newOrderClasses.add(clss);
    }
    classes = newOrderClasses;
    if (verbose) {
        System.out.println("Total number of classes = " + classes.size());
        System.out.println("classes = " + classes);
    }

    String forward_header = header.substring(0, header.lastIndexOf('.')) + "_fwd.h";
    Map<String, String> map = new HashMap<>();
    map.put(JAR, jar != null ? jar : "");
    map.put("%CLASSPATH_PREFIX%",
            getCurrentDir() + ((jar != null)
                    ? (File.pathSeparator + ((new File(jar).getCanonicalPath()).replace("\\", "\\\\")))
                    : ""));
    map.put("%FORWARD_HEADER%", forward_header);
    map.put("%HEADER%", header);
    map.put("%PATH_SEPERATOR%", File.pathSeparator);
    String tabs = "";
    String headerDefine = forward_header.substring(Math.max(0, forward_header.indexOf(File.separator)))
            .replace(".", "_");
    map.put(HEADER_DEFINE, headerDefine);
    map.put(NAMESPACE, namespace);
    if (null != nativesClassMap) {
        for (Entry<String, Class> e : nativesClassMap.entrySet()) {
            final Class javaClass = e.getValue();
            final String nativeClassName = e.getKey();
            try (PrintWriter pw = new PrintWriter(new FileWriter(nativeClassName + ".java"))) {
                if (javaClass.isInterface()) {
                    pw.println("public class " + nativeClassName + " implements " + javaClass.getCanonicalName()
                            + ", AutoCloseable{");
                } else {
                    pw.println("public class " + nativeClassName + " extends " + javaClass.getCanonicalName()
                            + " implements AutoCloseable{");
                }
                if (null != loadlibname && loadlibname.length() > 0) {
                    pw.println(TAB_STRING + "static {");
                    pw.println(TAB_STRING + TAB_STRING + "System.loadLibrary(\"" + loadlibname + "\");");
                    pw.println(TAB_STRING + "}");
                    pw.println();
                }
                pw.println(TAB_STRING + "public " + nativeClassName + "() {");
                pw.println(TAB_STRING + "}");
                pw.println();
                pw.println(TAB_STRING + "private long nativeAddress=0;");
                pw.println(TAB_STRING + "private boolean nativeDeleteOnClose=false;");
                pw.println();

                pw.println(TAB_STRING + "protected " + nativeClassName + "(final long nativeAddress) {");
                pw.println(TAB_STRING + TAB_STRING + "this.nativeAddress = nativeAddress;");
                pw.println(TAB_STRING + "}");

                pw.println(TAB_STRING + "private native void nativeDelete();");
                pw.println();
                pw.println(TAB_STRING + "@Override");
                pw.println(TAB_STRING + "public synchronized void  close() {");
                //                        pw.println(TAB_STRING + TAB_STRING + "if(nativeAddress != 0 && nativeDeleteOnClose) {");
                pw.println(TAB_STRING + TAB_STRING + "nativeDelete();");
                //                        pw.println(TAB_STRING + TAB_STRING + "}");
                //                        pw.println(TAB_STRING + TAB_STRING + "nativeAddress=0;");
                //                        pw.println(TAB_STRING + TAB_STRING + "nativeDeleteOnClose=false;");
                pw.println(TAB_STRING + "}");

                pw.println();
                pw.println(TAB_STRING + "protected void finalizer() {");
                pw.println(TAB_STRING + TAB_STRING + "close();");
                pw.println(TAB_STRING + "}");

                pw.println();
                Method ma[] = javaClass.getDeclaredMethods();
                for (Method m : ma) {
                    int modifiers = m.getModifiers();
                    if (Modifier.isAbstract(modifiers) && Modifier.isPublic(modifiers)
                            && !Modifier.isStatic(modifiers)
                            //                                && !m.isDefault()
                            && !m.isSynthetic()) {
                        pw.println();
                        pw.println(TAB_STRING + "@Override");
                        String params = "";
                        for (int i = 0; i < m.getParameterTypes().length; i++) {
                            params += m.getParameterTypes()[i].getCanonicalName() + " param" + i;
                            if (i < m.getParameterTypes().length - 1) {
                                params += ",";
                            }
                        }
                        pw.println(TAB_STRING + "native public " + m.getReturnType().getCanonicalName() + " "
                                + m.getName() + "(" + params + ");");
                        //                                    + IntStream.range(0, m.getParameterTypes().length)
                        //                                    .mapToObj(i -> m.getParameterTypes()[i].getCanonicalName() + " param" + i)
                        //                                    .collect(Collectors.joining(",")) + ");");
                    }
                }
                pw.println("}");
            }
        }
    }
    try (PrintWriter pw = new PrintWriter(new FileWriter(forward_header))) {
        tabs = "";
        processTemplate(pw, map, "header_fwd_template_start.h", tabs);
        Class lastClass = null;
        for (int class_index = 0; class_index < classes.size(); class_index++) {
            Class clss = classes.get(class_index);
            String clssOnlyName = getCppClassName(clss);
            tabs = openClassNamespace(clss, pw, tabs, lastClass);
            tabs += TAB_STRING;
            pw.println(tabs + "class " + clssOnlyName + ";");
            tabs = tabs.substring(0, tabs.length() - 1);
            Class nextClass = (class_index < (classes.size() - 1)) ? classes.get(class_index + 1) : null;
            tabs = closeClassNamespace(clss, pw, tabs, nextClass);
            lastClass = clss;
        }
        processTemplate(pw, map, "header_fwd_template_end.h", tabs);
    }
    headerDefine = header.substring(Math.max(0, header.indexOf(File.separator))).replace(".", "_");
    map.put(HEADER_DEFINE, headerDefine);
    map.put(NAMESPACE, namespace);
    if (classes_per_file < 1) {
        classes_per_file = classes.size();
    }
    final int num_class_segments = (classes.size() > 1)
            ? ((classes.size() + classes_per_file - 1) / classes_per_file)
            : 1;
    String fmt = "%d";
    if (num_class_segments > 10) {
        fmt = "%02d";
    }
    if (num_class_segments > 100) {
        fmt = "%03d";
    }
    String header_file_base = header;
    if (header_file_base.endsWith(".h")) {
        header_file_base = header_file_base.substring(0, header_file_base.length() - 2);
    } else if (header_file_base.endsWith(".hh")) {
        header_file_base = header_file_base.substring(0, header_file_base.length() - 3);
    } else if (header_file_base.endsWith(".hpp")) {
        header_file_base = header_file_base.substring(0, header_file_base.length() - 4);
    }
    try (PrintWriter pw = new PrintWriter(new FileWriter(header))) {
        tabs = "";
        processTemplate(pw, map, HEADER_TEMPLATE_STARTH, tabs);
        for (int segment_index = 0; segment_index < num_class_segments; segment_index++) {
            String header_segment_file = header_file_base + String.format(fmt, segment_index) + ".h";
            pw.println("#include \"" + header_segment_file + "\"");
        }
        if (null != nativesClassMap) {
            tabs = TAB_STRING;
            for (Entry<String, Class> e : nativesClassMap.entrySet()) {
                final Class javaClass = e.getValue();
                final String nativeClassName = e.getKey();
                pw.println();
                pw.println(tabs + "class " + nativeClassName + "Context;");
                pw.println();
                map.put(CLASS_NAME, nativeClassName);
                map.put("%BASE_CLASS_FULL_NAME%",
                        "::" + namespace + "::" + getModifiedClassName(javaClass).replace(".", "::"));
                map.put(OBJECT_CLASS_FULL_NAME, "::" + namespace + "::java::lang::Object");
                processTemplate(pw, map, HEADER_CLASS_STARTH, tabs);
                tabs += TAB_STRING;
                pw.println(tabs + nativeClassName + "Context *context;");
                pw.println(tabs + nativeClassName + "();");
                pw.println(tabs + "~" + nativeClassName + "();");
                Method methods[] = javaClass.getDeclaredMethods();
                for (int j = 0; j < methods.length; j++) {
                    Method method = methods[j];
                    int modifiers = method.getModifiers();
                    if (!Modifier.isPublic(modifiers)) {
                        continue;
                    }
                    if (Modifier.isAbstract(modifiers) && Modifier.isPublic(modifiers)
                            && !Modifier.isStatic(modifiers)
                            //                                && !method.isDefault()
                            && !method.isSynthetic()) {
                        pw.println(tabs + getNativeMethodCppDeclaration(method, javaClass));
                    }
                }
                pw.println(tabs + "void initContext(" + nativeClassName + "Context *ctx,bool isref);");
                pw.println(tabs + "void deleteContext();");
                tabs = tabs.substring(TAB_STRING.length());
                pw.println(tabs + "}; // end class " + nativeClassName);
            }
        }
        tabs = "";
        processTemplate(pw, map, HEADER_TEMPLATE_ENDH, tabs);
    }
    for (int segment_index = 0; segment_index < num_class_segments; segment_index++) {
        String header_segment_file = header_file_base + String.format(fmt, segment_index) + ".h";
        try (PrintWriter pw = new PrintWriter(new FileWriter(header_segment_file))) {
            tabs = "";
            //processTemplate(pw, map, HEADER_TEMPLATE_STARTH, tabs);
            pw.println("// Never include this file (" + header_segment_file + ") directly. include " + header
                    + " instead.");
            pw.println();
            Class lastClass = null;
            final int start_segment_index = segment_index * classes_per_file;
            final int end_segment_index = Math.min(segment_index * classes_per_file + classes_per_file,
                    classes.size());
            List<Class> classesSegList = classes.subList(start_segment_index, end_segment_index);
            pw.println();
            pw.println(tabs + "// start_segment_index = " + start_segment_index);
            pw.println(tabs + "// start_segment_index = " + end_segment_index);
            pw.println(tabs + "// segment_index = " + segment_index);
            pw.println(tabs + "// classesSegList=" + classesSegList);
            pw.println();
            for (int class_index = 0; class_index < classesSegList.size(); class_index++) {
                Class clss = classesSegList.get(class_index);
                pw.println();
                pw.println(tabs + "// class_index = " + class_index + " clss=" + clss);
                pw.println();
                String clssName = clss.getCanonicalName();
                tabs = openClassNamespace(clss, pw, tabs, lastClass);
                String clssOnlyName = getCppClassName(clss);
                map.put(CLASS_NAME, clssOnlyName);
                map.put("%BASE_CLASS_FULL_NAME%", classToCppBase(clss));
                map.put(OBJECT_CLASS_FULL_NAME, getCppRelativeName(Object.class, clss));
                tabs += TAB_STRING;
                processTemplate(pw, map, HEADER_CLASS_STARTH, tabs);
                tabs += TAB_STRING;

                Constructor constructors[] = clss.getDeclaredConstructors();
                if (!hasNoArgConstructor(constructors)) {
                    if (Modifier.isAbstract(clss.getModifiers()) || clss.isInterface()) {
                        pw.println(tabs + "protected:");
                        pw.println(tabs + clssOnlyName + "() {};");
                        pw.println(tabs + "public:");
                    } else {
                        if (constructors.length > 0) {
                            pw.println(tabs + "protected:");
                        }
                        pw.println(tabs + clssOnlyName + "();");
                        if (constructors.length > 0) {
                            pw.println(tabs + "public:");
                        }
                    }
                }
                for (Constructor c : constructors) {
                    if (c.getParameterTypes().length == 0 && Modifier.isProtected(c.getModifiers())) {
                        pw.println(tabs + "protected:");
                        pw.println(tabs + clssOnlyName + "();");
                        pw.println(tabs + "public:");
                    }
                    if (checkConstructor(c, clss, classes)) {
                        continue;
                    }

                    if (c.getParameterTypes().length == 1 && clss.isAssignableFrom(c.getParameterTypes()[0])) {
                        continue;
                    }
                    if (!Modifier.isPublic(c.getModifiers())) {
                        continue;
                    }
                    if (c.getParameterTypes().length == 1) {
                        if (c.getParameterTypes()[0].getName().equals(clss.getName())) {
                            //                                    if(verbose) System.out.println("skipping constructor.");
                            continue;
                        }
                    }

                    if (!checkParameters(c.getParameterTypes(), classes)) {
                        continue;
                    }
                    pw.println(
                            tabs + clssOnlyName + getCppParamDeclarations(c.getParameterTypes(), clss) + ";");
                    if (isConstructorToMakeEasy(c, clss)) {
                        pw.println(tabs + clssOnlyName
                                + getEasyCallCppParamDeclarations(c.getParameterTypes(), clss) + ";");
                    }
                }

                pw.println(tabs + "~" + clssOnlyName + "();");
                Field fa[] = clss.getDeclaredFields();
                for (int findex = 0; findex < fa.length; findex++) {
                    Field field = fa[findex];
                    if (addGetterMethod(field, clss, classes)) {
                        pw.println(tabs + getCppFieldGetterDeclaration(field, clss));
                    }
                    if (addSetterMethod(field, clss, classes)) {
                        pw.println(tabs + getCppFieldSetterDeclaration(field, clss));
                    }
                }
                Method methods[] = clss.getDeclaredMethods();
                for (int j = 0; j < methods.length; j++) {
                    Method method = methods[j];
                    if (!checkMethod(method, classes)) {
                        continue;
                    }
                    if ((method.getModifiers() & Modifier.PUBLIC) == Modifier.PUBLIC) {
                        pw.println(tabs + getCppDeclaration(method, clss));
                    }
                    if (isArrayStringMethod(method)) {
                        pw.println(tabs + getCppModifiers(method.getModifiers())
                                + getCppType(method.getReturnType(), clss) + " " + fixMethodName(method)
                                + "(int argc,const char **argv);");
                    }
                    if (isMethodToMakeEasy(method)) {
                        pw.println(tabs + getEasyCallCppDeclaration(method, clss));
                    }
                }
                tabs = tabs.substring(TAB_STRING.length());
                pw.println(tabs + "}; // end class " + clssOnlyName);
                tabs = tabs.substring(0, tabs.length() - 1);
                Class nextClass = (class_index < (classesSegList.size() - 1))
                        ? classesSegList.get(class_index + 1)
                        : null;
                tabs = closeClassNamespace(clss, pw, tabs, nextClass);
                pw.println();
                lastClass = clss;
            }
            //processTemplate(pw, map, HEADER_TEMPLATE_ENDH, tabs);
        }
    }
    for (int segment_index = 0; segment_index < num_class_segments; segment_index++) {
        String output_segment_file = output;
        if (output_segment_file.endsWith(".cpp")) {
            output_segment_file = output_segment_file.substring(0, output_segment_file.length() - 4);
        } else if (output_segment_file.endsWith(".cc")) {
            output_segment_file = output_segment_file.substring(0, output_segment_file.length() - 3);
        }
        output_segment_file += "" + String.format(fmt, segment_index) + ".cpp";
        try (PrintWriter pw = new PrintWriter(new FileWriter(output_segment_file))) {
            tabs = "";
            if (segment_index < 1) {
                processTemplate(pw, map, "cpp_template_start_first.cpp", tabs);
            } else {
                processTemplate(pw, map, CPP_TEMPLATE_STARTCPP, tabs);
            }
            final int start_segment_index = segment_index * classes_per_file;
            final int end_segment_index = Math.min(segment_index * classes_per_file + classes_per_file,
                    classes.size());
            List<Class> classesSegList = classes.subList(start_segment_index, end_segment_index);
            pw.println();
            pw.println(tabs + "// start_segment_index = " + start_segment_index);
            pw.println(tabs + "// start_segment_index = " + end_segment_index);
            pw.println(tabs + "// segment_index = " + segment_index);
            pw.println(tabs + "// classesSegList=" + classesSegList);
            pw.println();
            Class lastClass = null;
            for (int class_index = 0; class_index < classesSegList.size(); class_index++) {
                Class clss = classesSegList.get(class_index);
                pw.println();
                pw.println(tabs + "// class_index = " + class_index + " clss=" + clss);
                pw.println();
                String clssName = clss.getCanonicalName();
                tabs = openClassNamespace(clss, pw, tabs, lastClass);
                String clssOnlyName = getCppClassName(clss);
                map.put(CLASS_NAME, clssOnlyName);
                map.put("%BASE_CLASS_FULL_NAME%", classToCppBase(clss));

                map.put(FULL_CLASS_NAME, clssName);
                String fcjs = classToJNISignature(clss);
                fcjs = fcjs.substring(1, fcjs.length() - 1);
                map.put(FULL_CLASS_JNI_SIGNATURE, fcjs);
                if (verbose) {
                    System.out.println("fcjs = " + fcjs);
                }
                map.put(OBJECT_CLASS_FULL_NAME, getCppRelativeName(Object.class, clss));
                map.put("%INITIALIZE_CONTEXT%", "");
                map.put("%INITIALIZE_CONTEXT_REF%", "");
                processTemplate(pw, map, CPP_START_CLASSCPP, tabs);
                Constructor constructors[] = clss.getDeclaredConstructors();

                if (!hasNoArgConstructor(constructors)) {
                    if (!Modifier.isAbstract(clss.getModifiers()) && !clss.isInterface()) {
                        pw.println(tabs + clssOnlyName + "::" + clssOnlyName + "() : " + classToCppBase(clss)
                                + "((jobject)NULL,false) {");
                        map.put(JNI_SIGNATURE, "()V");
                        map.put(CONSTRUCTOR_ARGS, "");
                        processTemplate(pw, map, CPP_NEWCPP, tabs);
                        pw.println(tabs + "}");
                        pw.println();
                    }

                }
                for (Constructor c : constructors) {
                    if (checkConstructor(c, clss, classes)) {
                        continue;
                    }
                    Class[] paramClasses = c.getParameterTypes();
                    pw.println(tabs + clssOnlyName + "::" + clssOnlyName
                            + getCppParamDeclarations(paramClasses, clss) + " : " + classToCppBase(clss)
                            + "((jobject)NULL,false) {");
                    tabs = tabs + TAB_STRING;
                    map.put(JNI_SIGNATURE, "(" + getJNIParamSignature(paramClasses) + ")V");
                    map.put(CONSTRUCTOR_ARGS,
                            (paramClasses.length > 0 ? "," : "") + getCppParamNames(paramClasses));
                    processTemplate(pw, map, CPP_NEWCPP, tabs);
                    tabs = tabs.substring(0, tabs.length() - 1);
                    pw.println(tabs + "}");
                    pw.println();
                    if (isConstructorToMakeEasy(c, clss)) {
                        pw.println(tabs + clssOnlyName + "::" + clssOnlyName
                                + getEasyCallCppParamDeclarations(c.getParameterTypes(), clss) + " : "
                                + classToCppBase(clss) + "((jobject)NULL,false) {");
                        processTemplate(pw, map, "cpp_start_easy_constructor.cpp", tabs);
                        for (int paramIndex = 0; paramIndex < paramClasses.length; paramIndex++) {
                            Class paramClasse = paramClasses[paramIndex];
                            String parmName = getParamNameIn(paramClasse, paramIndex);
                            if (isString(paramClasse)) {
                                pw.println(tabs + "jstring " + parmName + " = env->NewStringUTF(easyArg_"
                                        + paramIndex + ");");
                            } else if (isPrimitiveArray(paramClasse)) {
                                String callString = getMethodCallString(paramClasse.getComponentType());
                                pw.println(tabs + getCppArrayType(paramClasse.getComponentType()) + " "
                                        + classToParamNameDecl(paramClasse, paramIndex) + "= env->New"
                                        + callString + "Array(easyArg_" + paramIndex + "_length);");
                                pw.println(tabs + "env->Set" + callString + "ArrayRegion("
                                        + classToParamNameDecl(paramClasse, paramIndex) + ",0,easyArg_"
                                        + paramIndex + "_length,easyArg_" + paramIndex + ");");
                            } else {
                                pw.println(tabs + getCppType(paramClasse, clss) + " "
                                        + classToParamNameDecl(paramClasse, paramIndex) + "= easyArg_"
                                        + paramIndex + ";");
                            }
                        }
                        processTemplate(pw, map, "cpp_new_easy_internals.cpp", tabs);
                        for (int paramIndex = 0; paramIndex < paramClasses.length; paramIndex++) {
                            Class paramClasse = paramClasses[paramIndex];
                            String parmName = getParamNameIn(paramClasse, paramIndex);
                            if (isString(paramClasse)) {
                                pw.println(tabs + "jobjectRefType ref_" + paramIndex
                                        + " = env->GetObjectRefType(" + parmName + ");");
                                pw.println(tabs + "if(ref_" + paramIndex + " == JNIGlobalRefType) {");
                                pw.println(tabs + TAB_STRING + "env->DeleteGlobalRef(" + parmName + ");");
                                pw.println(tabs + "}");
                            } else if (isPrimitiveArray(paramClasse)) {
                                String callString = getMethodCallString(paramClasse.getComponentType());
                                pw.println(tabs + "env->Get" + callString + "ArrayRegion("
                                        + classToParamNameDecl(paramClasse, paramIndex) + ",0,easyArg_"
                                        + paramIndex + "_length,easyArg_" + paramIndex + ");");
                                pw.println(tabs + "jobjectRefType ref_" + paramIndex
                                        + " = env->GetObjectRefType(" + parmName + ");");
                                pw.println(tabs + "if(ref_" + paramIndex + " == JNIGlobalRefType) {");
                                pw.println(tabs + TAB_STRING + "env->DeleteGlobalRef(" + parmName + ");");
                                pw.println(tabs + "}");
                            } else {

                            }
                        }
                        processTemplate(pw, map, "cpp_end_easy_constructor.cpp", tabs);
                        pw.println(tabs + "}");
                    }
                }

                pw.println();
                pw.println(tabs + "// Destructor for " + clssName);
                pw.println(tabs + clssOnlyName + "::~" + clssOnlyName + "() {");
                pw.println(tabs + "\t// Place-holder for later extensibility.");
                pw.println(tabs + "}");
                pw.println();
                Field fa[] = clss.getDeclaredFields();
                for (int findex = 0; findex < fa.length; findex++) {
                    Field field = fa[findex];
                    if (addGetterMethod(field, clss, classes)) {
                        pw.println();
                        pw.println(tabs + "// Field getter for " + field.getName());
                        pw.println(getCppFieldGetterDefinitionStart(tabs, clssOnlyName, field, clss));
                        map.put("%FIELD_NAME%", field.getName());
                        map.put(JNI_SIGNATURE, classToJNISignature(field.getType()));
                        Class returnClass = field.getType();
                        map.put(METHOD_ONFAIL, getOnFailString(returnClass, clss));
                        map.put(METHOD_ARGS, "");
                        map.put("%METHOD_RETURN%", isVoid(returnClass) ? "" : "return");
                        map.put("%METHOD_CALL_TYPE%", getMethodCallString(returnClass));
                        map.put("%METHOD_RETURN_TYPE%", getCppType(returnClass, clss));
                        map.put("%RETURN_VAR_DECLARE%", getMethodReturnVarDeclare(returnClass));
                        String retStore = isVoid(returnClass) ? ""
                                : "retVal= (" + getMethodReturnVarType(returnClass) + ") ";
                        map.put("%METHOD_RETURN_STORE%", retStore);
                        map.put("%METHOD_RETURN_GET%", getMethodReturnGet(tabs, returnClass, clss));

                        if (Modifier.isStatic(field.getModifiers())) {
                            processTemplate(pw, map, "cpp_static_getfield.cpp", tabs);
                        } else {
                            processTemplate(pw, map, "cpp_getfield.cpp", tabs);
                        }

                        pw.println(tabs + "}");
                    }
                    if (addSetterMethod(field, clss, classes)) {
                        pw.println();
                        pw.println(tabs + "// Field setter for " + field.getName());
                        pw.println(getCppFieldSetterDefinitionStart(tabs, clssOnlyName, field, clss));
                        map.put("%FIELD_NAME%", field.getName());
                        map.put(JNI_SIGNATURE, classToJNISignature(field.getType()));
                        Class returnClass = void.class;
                        map.put(METHOD_ONFAIL, getOnFailString(returnClass, clss));
                        Class[] paramClasses = new Class[] { field.getType() };
                        String methodArgs = getCppParamNames(paramClasses);
                        map.put(METHOD_ARGS, (paramClasses.length > 0 ? "," : "") + methodArgs);
                        map.put("%METHOD_RETURN%", isVoid(returnClass) ? "" : "return");
                        map.put("%METHOD_CALL_TYPE%", getMethodCallString(field.getType()));
                        map.put("%METHOD_RETURN_TYPE%", getCppType(returnClass, clss));
                        map.put("%RETURN_VAR_DECLARE%", getMethodReturnVarDeclare(returnClass));
                        String retStore = isVoid(returnClass) ? ""
                                : "retVal= (" + getMethodReturnVarType(returnClass) + ") ";
                        map.put("%METHOD_RETURN_STORE%", retStore);
                        map.put("%METHOD_RETURN_GET%", getMethodReturnGet(tabs, returnClass, clss));

                        if (Modifier.isStatic(field.getModifiers())) {
                            processTemplate(pw, map, "cpp_static_setfield.cpp", tabs);
                        } else {
                            processTemplate(pw, map, "cpp_setfield.cpp", tabs);
                        }

                        pw.println(tabs + "}");
                    }
                }
                Method methods[] = clss.getDeclaredMethods();
                for (int j = 0; j < methods.length; j++) {
                    Method method = methods[j];

                    if (checkMethod(method, classes)) {
                        pw.println();
                        pw.println(getCppMethodDefinitionStart(tabs, clssOnlyName, method, clss));
                        map.put(METHOD_NAME, method.getName());
                        if (fixMethodName(method).contains("equals2")) {
                            if (verbose) {
                                System.out.println("debug me");
                            }
                        }
                        map.put("%JAVA_METHOD_NAME%", method.getName());
                        Class[] paramClasses = method.getParameterTypes();
                        String methodArgs = getCppParamNames(paramClasses);
                        map.put(METHOD_ARGS, (paramClasses.length > 0 ? "," : "") + methodArgs);
                        Class returnClass = method.getReturnType();
                        map.put(JNI_SIGNATURE, "(" + getJNIParamSignature(paramClasses) + ")"
                                + classToJNISignature(returnClass));
                        map.put(METHOD_ONFAIL, getOnFailString(returnClass, clss));
                        map.put("%METHOD_RETURN%", isVoid(returnClass) ? "" : "return");
                        map.put("%METHOD_CALL_TYPE%", getMethodCallString(returnClass));
                        map.put("%METHOD_RETURN_TYPE%", getCppType(returnClass, clss));
                        map.put("%RETURN_VAR_DECLARE%", getMethodReturnVarDeclare(returnClass));
                        String retStore = isVoid(returnClass) ? ""
                                : "retVal= (" + getMethodReturnVarType(returnClass) + ") ";
                        map.put("%METHOD_RETURN_STORE%", retStore);
                        map.put("%METHOD_RETURN_GET%", getMethodReturnGet(tabs, returnClass, clss));
                        tabs += TAB_STRING;
                        if (!Modifier.isStatic(method.getModifiers())) {
                            processTemplate(pw, map, CPP_METHODCPP, tabs);
                        } else {
                            processTemplate(pw, map, CPP_STATIC_METHODCPP, tabs);
                        }
                        tabs = tabs.substring(0, tabs.length() - TAB_STRING.length());
                        pw.println(tabs + "}");
                        if (isArrayStringMethod(method)) {
                            map.put(METHOD_RETURN_STORE,
                                    isVoid(returnClass) ? "" : getCppType(returnClass, clss) + " returnVal=");
                            map.put(METHOD_RETURN_GET, isVoid(returnClass) ? "return ;" : "return returnVal;");
                            processTemplate(pw, map, CPP_EASY_STRING_ARRAY_METHODCPP, tabs);
                        } else if (isMethodToMakeEasy(method)) {
                            pw.println();
                            pw.println(tabs + "// Easy call alternative for " + method.getName());
                            pw.println(getEasyCallCppMethodDefinitionStart(tabs, clssOnlyName, method, clss));
                            tabs += TAB_STRING;
                            map.put("%RETURN_VAR_DECLARE%", getMethodReturnVarDeclareOut(returnClass, clss));
                            processTemplate(pw, map, "cpp_start_easy_method.cpp", tabs);
                            for (int paramIndex = 0; paramIndex < paramClasses.length; paramIndex++) {
                                Class paramClasse = paramClasses[paramIndex];
                                String parmName = getParamNameIn(paramClasse, paramIndex);
                                if (isString(paramClasse)) {
                                    pw.println(tabs + "jstring " + parmName + " = env->NewStringUTF(easyArg_"
                                            + paramIndex + ");");
                                } else if (isPrimitiveArray(paramClasse)) {
                                    String callString = getMethodCallString(paramClasse.getComponentType());
                                    pw.println(tabs + getCppArrayType(paramClasse.getComponentType()) + " "
                                            + classToParamNameDecl(paramClasse, paramIndex) + "= env->New"
                                            + callString + "Array(easyArg_" + paramIndex + "_length);");
                                    pw.println(tabs + "env->Set" + callString + "ArrayRegion("
                                            + classToParamNameDecl(paramClasse, paramIndex) + ",0,easyArg_"
                                            + paramIndex + "_length,easyArg_" + paramIndex + ");");
                                } else {
                                    pw.println(tabs + getCppType(paramClasse, clss) + " "
                                            + classToParamNameDecl(paramClasse, paramIndex) + "= easyArg_"
                                            + paramIndex + ";");
                                }
                            }
                            String methodArgsIn = getCppParamNamesIn(paramClasses);
                            String retStoreOut = isVoid(returnClass) ? ""
                                    : "retVal= (" + getMethodReturnOutVarType(returnClass, clss) + ") ";

                            pw.println(tabs + retStoreOut + fixMethodName(method) + "(" + methodArgsIn + ");");
                            for (int paramIndex = 0; paramIndex < paramClasses.length; paramIndex++) {
                                Class paramClasse = paramClasses[paramIndex];
                                String parmName = getParamNameIn(paramClasse, paramIndex);
                                if (isString(paramClasse)) {
                                    pw.println(tabs + "jobjectRefType ref_" + paramIndex
                                            + " = env->GetObjectRefType(" + parmName + ");");
                                    pw.println(tabs + "if(ref_" + paramIndex + " == JNIGlobalRefType) {");
                                    pw.println(tabs + TAB_STRING + "env->DeleteGlobalRef(" + parmName + ");");
                                    pw.println(tabs + "}");
                                } else if (isPrimitiveArray(paramClasse)) {
                                    String callString = getMethodCallString(paramClasse.getComponentType());
                                    pw.println(tabs + "env->Get" + callString + "ArrayRegion("
                                            + classToParamNameDecl(paramClasse, paramIndex) + ",0,easyArg_"
                                            + paramIndex + "_length,easyArg_" + paramIndex + ");");
                                    pw.println(tabs + "jobjectRefType ref_" + paramIndex
                                            + " = env->GetObjectRefType(" + parmName + ");");
                                    pw.println(tabs + "if(ref_" + paramIndex + " == JNIGlobalRefType) {");
                                    pw.println(tabs + TAB_STRING + "env->DeleteGlobalRef(" + parmName + ");");
                                    pw.println(tabs + "}");
                                } else {

                                }
                            }
                            processTemplate(pw, map, "cpp_end_easy_method.cpp", tabs);
                            if (!isVoid(returnClass)) {
                                pw.println(tabs + "return retVal;");
                            }
                            tabs = tabs.substring(TAB_STRING.length());
                            pw.println(tabs + "}");
                            pw.println();
                        }
                    }
                }
                processTemplate(pw, map, CPP_END_CLASSCPP, tabs);
                tabs = tabs.substring(0, tabs.length() - 1);
                Class nextClass = (class_index < (classesSegList.size() - 1))
                        ? classesSegList.get(class_index + 1)
                        : null;
                tabs = closeClassNamespace(clss, pw, tabs, nextClass);
                lastClass = clss;
            }

            if (segment_index < 1) {
                if (null != nativesClassMap) {
                    for (Entry<String, Class> e : nativesClassMap.entrySet()) {
                        final Class javaClass = e.getValue();
                        final String nativeClassName = e.getKey();
                        map.put(CLASS_NAME, nativeClassName);
                        map.put(FULL_CLASS_NAME, nativeClassName);
                        map.put(FULL_CLASS_JNI_SIGNATURE, nativeClassName);
                        map.put("%BASE_CLASS_FULL_NAME%",
                                "::" + namespace + "::" + getModifiedClassName(javaClass).replace(".", "::"));
                        map.put(OBJECT_CLASS_FULL_NAME, "::" + namespace + "::java::lang::Object");
                        map.put("%INITIALIZE_CONTEXT%", "context=NULL; initContext(NULL,false);");
                        map.put("%INITIALIZE_CONTEXT_REF%", "context=NULL; initContext(objref.context,true);");
                        tabs += TAB_STRING;

                        processTemplate(pw, map, CPP_START_CLASSCPP, tabs);
                        pw.println();
                        pw.println(tabs + nativeClassName + "::" + nativeClassName + "() : "
                                + getModifiedClassName(javaClass).replace(".", "::")
                                + "((jobject)NULL,false) {");
                        tabs += TAB_STRING;
                        pw.println(tabs + "context=NULL;");
                        pw.println(tabs + "initContext(NULL,false);");
                        map.put(JNI_SIGNATURE, "()V");
                        map.put(CONSTRUCTOR_ARGS, "");
                        processTemplate(pw, map, "cpp_new_native.cpp", tabs);
                        tabs = tabs.substring(TAB_STRING.length());
                        pw.println(tabs + "}");
                        pw.println();
                        pw.println(tabs + "// Destructor for " + nativeClassName);
                        pw.println(tabs + nativeClassName + "::~" + nativeClassName + "() {");
                        pw.println(tabs + TAB_STRING + "if(NULL != context) {");
                        pw.println(tabs + TAB_STRING + TAB_STRING + "deleteContext();");
                        pw.println(tabs + TAB_STRING + "}");
                        pw.println(tabs + TAB_STRING + "context=NULL;");
                        pw.println(tabs + "}");
                        pw.println();
                        //                            pw.println(tabs + "public:");
                        //                            pw.println(tabs + nativeClassName + "();");
                        //                            pw.println(tabs + "~" + nativeClassName + "();");
                        tabs = tabs.substring(TAB_STRING.length());
                        //                            Method methods[] = javaClass.getDeclaredMethods();
                        //                            for (int j = 0; j < methods.length; j++) {
                        //                                Method method = methods[j];
                        //                                int modifiers = method.getModifiers();
                        //                                if (!Modifier.isPublic(modifiers)) {
                        //                                    continue;
                        //                                }
                        //                                pw.println(tabs + getCppDeclaration(method, javaClass));
                        //                            }
                        //                            pw.println(tabs + "}; // end class " + nativeClassName);
                        processTemplate(pw, map, CPP_END_CLASSCPP, tabs);
                    }
                }
                processTemplate(pw, map, "cpp_template_end_first.cpp", tabs);
                tabs = "";
                if (null != nativesClassMap) {
                    pw.println("using namespace " + namespace + ";");
                    pw.println("#ifdef __cplusplus");
                    pw.println("extern \"C\" {");
                    pw.println("#endif");
                    int max_method_count = 0;
                    tabs = "";
                    for (Entry<String, Class> e : nativesClassMap.entrySet()) {
                        final Class javaClass = e.getValue();
                        final String nativeClassName = e.getKey();
                        map.put(CLASS_NAME, nativeClassName);
                        map.put(FULL_CLASS_NAME, nativeClassName);
                        map.put("%BASE_CLASS_FULL_NAME%",
                                "::" + namespace + "::" + getModifiedClassName(javaClass).replace(".", "::"));
                        map.put(OBJECT_CLASS_FULL_NAME, "::" + namespace + "::java::lang::Object");
                        map.put(FULL_CLASS_JNI_SIGNATURE, nativeClassName);
                        map.put(METHOD_ONFAIL, "return;");
                        Method methods[] = javaClass.getDeclaredMethods();
                        if (max_method_count < methods.length) {
                            max_method_count = methods.length;
                        }
                        for (int j = 0; j < methods.length; j++) {
                            Method method = methods[j];
                            int modifiers = method.getModifiers();
                            if (!Modifier.isPublic(modifiers)) {
                                continue;
                            }
                            if (Modifier.isAbstract(modifiers) && Modifier.isPublic(modifiers)
                                    && !Modifier.isStatic(modifiers)
                                    //                                        && !method.isDefault()
                                    && !method.isSynthetic()) {
                                Class[] paramClasses = method.getParameterTypes();
                                String methodArgs = getCppParamNames(paramClasses);
                                map.put(METHOD_ARGS, methodArgs);
                                map.put(METHOD_NAME, method.getName());
                                Class returnClass = method.getReturnType();
                                String retStore = isVoid(returnClass) ? ""
                                        : "retVal= (" + getMethodReturnVarType(returnClass) + ") ";
                                map.put(METHOD_ONFAIL, getOnFailString(returnClass, javaClass));
                                map.put("%RETURN_VAR_DECLARE%", getMethodReturnVarDeclare(returnClass));
                                map.put("%METHOD_RETURN_STORE%", retStore);
                                map.put("%METHOD_RETURN_GET%",
                                        getMethodReturnGet(tabs, returnClass, javaClass));
                                pw.println();
                                String paramDecls = getCppParamDeclarations(paramClasses, javaClass);
                                String argsToAdd = method.getParameterTypes().length > 0
                                        ? "," + paramDecls.substring(1, paramDecls.length() - 1)
                                        : "";
                                pw.println("JNIEXPORT " + getCppType(returnClass, javaClass) + " JNICALL Java_"
                                        + nativeClassName + "_" + method.getName()
                                        + "(JNIEnv *env, jobject jthis" + argsToAdd + ") {");
                                tabs = TAB_STRING;
                                processTemplate(pw, map, "cpp_native_wrap.cpp", tabs);
                                tabs = tabs.substring(TAB_STRING.length());
                                pw.println("}");
                                pw.println();
                            }
                        }
                        pw.println("JNIEXPORT void JNICALL Java_" + nativeClassName
                                + "_nativeDelete(JNIEnv *env, jobject jthis) {");
                        tabs += TAB_STRING;
                        map.put(METHOD_ONFAIL, getOnFailString(void.class, javaClass));
                        processTemplate(pw, map, "cpp_native_delete.cpp", tabs);
                        tabs = tabs.substring(TAB_STRING.length());
                        pw.println(tabs + "}");
                        pw.println();
                    }
                    pw.println("#ifdef __cplusplus");
                    pw.println("} // end extern \"C\"");
                    pw.println("#endif");
                    map.put("%MAX_METHOD_COUNT%", Integer.toString(max_method_count + 1));
                    processTemplate(pw, map, "cpp_start_register_native.cpp", tabs);
                    for (Entry<String, Class> e : nativesClassMap.entrySet()) {
                        final Class javaClass = e.getValue();
                        final String nativeClassName = e.getKey();
                        map.put(CLASS_NAME, nativeClassName);
                        map.put(FULL_CLASS_NAME, nativeClassName);
                        map.put("%BASE_CLASS_FULL_NAME%",
                                "::" + namespace + "::" + getModifiedClassName(javaClass).replace(".", "::"));
                        map.put(OBJECT_CLASS_FULL_NAME, "::" + namespace + "::java::lang::Object");
                        processTemplate(pw, map, "cpp_start_register_native_class.cpp", tabs);
                        tabs += TAB_STRING;
                        Method methods[] = javaClass.getDeclaredMethods();
                        int method_number = 0;
                        for (int j = 0; j < methods.length; j++) {
                            Method method = methods[j];
                            int modifiers = method.getModifiers();
                            if (!Modifier.isPublic(modifiers)) {
                                continue;
                            }
                            if (Modifier.isAbstract(modifiers) && Modifier.isPublic(modifiers)
                                    && !Modifier.isStatic(modifiers)
                                    //                                        && !method.isDefault()
                                    && !method.isSynthetic()) {
                                map.put("%METHOD_NUMBER%", Integer.toString(method_number));
                                map.put(METHOD_NAME, method.getName());

                                map.put(JNI_SIGNATURE, "(" + getJNIParamSignature(method.getParameterTypes())
                                        + ")" + classToJNISignature(method.getReturnType()));
                                processTemplate(pw, map, "cpp_register_native_item.cpp", tabs);
                                method_number++;
                            }
                        }
                        map.put("%METHOD_NUMBER%", Integer.toString(method_number));
                        map.put(METHOD_NAME, "nativeDelete");
                        map.put(JNI_SIGNATURE, "()V");
                        processTemplate(pw, map, "cpp_register_native_item.cpp", tabs);
                        map.put("%NUM_NATIVE_METHODS%", Integer.toString(method_number));
                        processTemplate(pw, map, "cpp_end_register_native_class.cpp", tabs);
                        tabs = tabs.substring(TAB_STRING.length());
                    }
                    processTemplate(pw, map, "cpp_end_register_native.cpp", tabs);
                } else {
                    pw.println("// No Native classes : registerNativMethods not needed.");
                    pw.println("static void registerNativeMethods(JNIEnv *env) {}");
                }

            } else {
                processTemplate(pw, map, CPP_TEMPLATE_ENDCPP, tabs);
            }
        }
        if (null != nativesClassMap) {
            tabs = "";
            for (Entry<String, Class> e : nativesClassMap.entrySet()) {
                String nativeClassName = e.getKey();
                File nativeClassHeaderFile = new File(
                        namespace.toLowerCase() + "_" + nativeClassName.toLowerCase() + ".h");
                map.put("%NATIVE_CLASS_HEADER%", nativeClassHeaderFile.getName());
                map.put(CLASS_NAME, nativeClassName);
                if (nativeClassHeaderFile.exists()) {
                    if (verbose) {
                        System.out.println("skipping " + nativeClassHeaderFile.getCanonicalPath()
                                + " since it already exists.");
                    }
                } else {
                    try (PrintWriter pw = new PrintWriter(new FileWriter(nativeClassHeaderFile))) {
                        processTemplate(pw, map, "header_native_imp.h", tabs);
                    }
                }
                File nativeClassCppFile = new File(
                        namespace.toLowerCase() + "_" + nativeClassName.toLowerCase() + ".cpp");
                if (nativeClassCppFile.exists()) {
                    if (verbose) {
                        System.out.println("skipping " + nativeClassCppFile.getCanonicalPath()
                                + " since it already exists.");
                    }
                } else {
                    try (PrintWriter pw = new PrintWriter(new FileWriter(nativeClassCppFile))) {
                        processTemplate(pw, map, "cpp_native_imp_start.cpp", tabs);
                        Class javaClass = e.getValue();
                        Method methods[] = javaClass.getDeclaredMethods();
                        int method_number = 0;
                        for (int j = 0; j < methods.length; j++) {
                            Method method = methods[j];
                            int modifiers = method.getModifiers();
                            if (!Modifier.isPublic(modifiers)) {
                                continue;
                            }
                            if (Modifier.isAbstract(modifiers) && Modifier.isPublic(modifiers)
                                    && !Modifier.isStatic(modifiers)
                                    //                                        && !method.isDefault()
                                    && !method.isSynthetic()) {
                                Class[] paramClasses = method.getParameterTypes();
                                //                                    String methodArgs = getCppParamNames(paramClasses);
                                String paramDecls = getCppParamDeclarations(paramClasses, javaClass);
                                String methodArgs = method.getParameterTypes().length > 0
                                        ? paramDecls.substring(1, paramDecls.length() - 1)
                                        : "";
                                map.put(METHOD_ARGS, methodArgs);
                                map.put(METHOD_NAME, method.getName());
                                Class returnClass = method.getReturnType();
                                String retStore = isVoid(returnClass) ? ""
                                        : "retVal= (" + getMethodReturnVarType(returnClass) + ") ";
                                map.put(METHOD_ONFAIL, getOnFailString(returnClass, javaClass));
                                map.put("%RETURN_TYPE%", getCppType(returnClass, javaClass));
                                map.put("%RETURN_VAR_DECLARE%", getMethodReturnVarDeclare(returnClass));
                                map.put("%METHOD_RETURN_STORE%", retStore);
                                map.put("%METHOD_RETURN_GET%",
                                        getMethodReturnGet(tabs, returnClass, javaClass));
                                processTemplate(pw, map, "cpp_native_imp_stub.cpp", tabs);
                            }
                        }
                        processTemplate(pw, map, "cpp_native_imp_end.cpp", tabs);
                    }
                }
            }
        }
    }

    main_completed = true;
}

From source file:com.samsung.sjs.Compiler.java

@SuppressWarnings("static-access")
public static void main(String[] args) throws IOException, SolverException, InterruptedException {
    boolean debug = false;
    boolean use_gc = true;
    CompilerOptions.Platform p = CompilerOptions.Platform.Native;
    CompilerOptions opts = null;/*from  ww w .  ja  v a2s.  c o m*/
    boolean field_opts = false;
    boolean typecheckonly = false;
    boolean showconstraints = false;
    boolean showconstraintsolution = false;
    String[] decls = null;
    String[] links = null;
    String[] objs = null;
    boolean guest = false;
    boolean stop_at_c = false;
    String external_compiler = null;
    boolean encode_vals = false;
    boolean x32 = false;
    boolean validate = true;
    boolean interop = false;
    boolean boot_interop = false;
    boolean oldExpl = false;
    String explanationStrategy = null;
    boolean efl = false;

    Options options = new Options();
    options.addOption("debugcompiler", false, "Enable compiler debug spew");
    //options.addOption("o", true, "Set compiler output file (must be .c)");
    options.addOption(OptionBuilder //.withArgName("o")
            .withLongOpt("output-file").withDescription("Output file (must be .c)").hasArg().withArgName("file")
            .create("o"));
    options.addOption(OptionBuilder.withLongOpt("target")
            .withDescription("Select target platform: 'native' or 'web'").hasArg().create());
    options.addOption(OptionBuilder.withLongOpt("gc")
            .withDescription("Disable GC: param is 'on' (default) or 'off'").hasArg().create());

    options.addOption(OptionBuilder
            .withDescription("Enable field access optimizations: param is 'true' (default) or 'false'").hasArg()
            .create("Xfields"));

    options.addOption(OptionBuilder
            .withDescription("Compile for encoded values.  TEMPORARY.  For testing interop codegen").hasArg()
            .create("XEncodedValues"));

    options.addOption(OptionBuilder.withLongOpt("typecheck-only")
            .withDescription("Only typecheck the file, don't compile").create());
    options.addOption(OptionBuilder.withLongOpt("show-constraints")
            .withDescription("Show constraints generated during type inference").create());
    options.addOption(OptionBuilder.withLongOpt("show-constraint-solution")
            .withDescription("Show solution to type inference constraints").create());
    options.addOption(OptionBuilder.withLongOpt("extra-decls")
            .withDescription("Specify extra declaration files, comma-separated").hasArg()
            .withValueSeparator(',').create());
    options.addOption(OptionBuilder.withLongOpt("native-libs")
            .withDescription("Specify extra linkage files, comma-separated").hasArg().withValueSeparator(',')
            .create());
    Option extraobjs = OptionBuilder.withLongOpt("extra-objs")
            .withDescription("Specify extra .c/.cpp files, comma-separated").hasArg().withValueSeparator(',')
            .create();
    extraobjs.setArgs(Option.UNLIMITED_VALUES);
    options.addOption(extraobjs);
    options.addOption(OptionBuilder.withLongOpt("guest-runtime")
            .withDescription(
                    "Emit code to be called by another runtime (i.e., main() is written in another language).")
            .create());
    options.addOption(OptionBuilder.withLongOpt("only-c")
            .withDescription("Generate C code, but do not attempt to compile it").create());
    options.addOption(OptionBuilder.withLongOpt("c-compiler")
            .withDescription("Disable GC: param is 'on' (default) or 'off'").hasArg().create("cc"));
    options.addOption(OptionBuilder.withLongOpt("runtime-src")
            .withDescription("Specify path to runtime system source").hasArg().create());
    options.addOption(OptionBuilder.withLongOpt("ext-path")
            .withDescription("Specify path to external dependency dir (GC, etc.)").hasArg().create());
    options.addOption(OptionBuilder.withLongOpt("skip-validation")
            .withDescription("Run the backend without validating the results of type inference").create());

    options.addOption(OptionBuilder.withLongOpt("m32").withDescription("Force 32-bit compilation").create());
    options.addOption(OptionBuilder.withLongOpt("Xbootinterop")
            .withDescription("Programs start with global interop dirty flag set (experimental)").create());
    options.addOption(OptionBuilder.withLongOpt("Xinterop")
            .withDescription("Enable (experimental) interoperability backend").create());

    options.addOption(OptionBuilder.withDescription("C compiler default optimization level").create("O"));
    options.addOption(OptionBuilder.withDescription("C compiler optimization level 0").create("O0"));
    options.addOption(OptionBuilder.withDescription("C compiler optimization level 1").create("O1"));
    options.addOption(OptionBuilder.withDescription("C compiler optimization level 2").create("O2"));
    options.addOption(OptionBuilder.withDescription("C compiler optimization level 3").create("O3"));

    options.addOption(
            OptionBuilder.withLongOpt("oldExpl").withDescription("Use old error explanations").create());

    String explanationStrategyHelp = "default: " + FixingSetFinder.defaultStrategy() + "; other choices: "
            + FixingSetFinder.strategyNames().stream().filter(s -> !s.equals(FixingSetFinder.defaultStrategy()))
                    .collect(Collectors.joining(", "));

    options.addOption(OptionBuilder.withLongOpt("explanation-strategy")
            .withDescription("Error explanation strategy to use (" + explanationStrategyHelp + ')').hasArg()
            .create());

    options.addOption(
            OptionBuilder.withLongOpt("efl").withDescription("Set up efl environment in main()").create());

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

        String[] newargs = cmd.getArgs();
        if (newargs.length != 1) {
            throw new ParseException("Invalid number of arguments");
        }

        String sourcefile = newargs[0];
        if (!sourcefile.endsWith(".js")) {
            throw new ParseException("Invalid file extension on input file: " + sourcefile);
        }

        String gc = cmd.getOptionValue("gc", "on");
        if (gc.equals("on")) {
            use_gc = true;
        } else if (gc.equals("off")) {
            use_gc = false;
        } else {
            throw new ParseException("Invalid GC option: " + gc);
        }
        String fields = cmd.getOptionValue("Xfields", "true");
        if (fields.equals("true")) {
            field_opts = true;
        } else if (fields.equals("false")) {
            field_opts = false;
        } else {
            throw new ParseException("Invalid field optimization option: " + fields);
        }
        String encoding = cmd.getOptionValue("XEncodedValues", "false");
        if (encoding.equals("true")) {
            encode_vals = true;
        } else if (encoding.equals("false")) {
            encode_vals = false;
        } else {
            throw new ParseException("Invalid value encoding option: " + encode_vals);
        }
        String plat = cmd.getOptionValue("target", "native");
        if (plat.equals("native")) {
            p = CompilerOptions.Platform.Native;
        } else if (plat.equals("web")) {
            p = CompilerOptions.Platform.Web;
        } else {
            throw new ParseException("Invalid target platform: " + plat);
        }
        if (cmd.hasOption("cc")) {
            external_compiler = cmd.getOptionValue("cc");
        }
        if (cmd.hasOption("skip-validation")) {
            validate = false;
        }
        if (cmd.hasOption("typecheck-only")) {
            typecheckonly = true;
        }
        if (cmd.hasOption("show-constraints")) {
            showconstraints = true;
        }
        if (cmd.hasOption("show-constraint-solution")) {
            showconstraintsolution = true;
        }
        if (cmd.hasOption("debugcompiler")) {
            debug = true;
        }
        if (cmd.hasOption("m32")) {
            x32 = true;
        }
        if (cmd.hasOption("Xinterop")) {
            interop = true;
        }
        if (cmd.hasOption("Xbootinterop")) {
            boot_interop = true;
            if (!interop) {
                System.err.println("WARNING: --Xbootinterop enabled without --Xinterop (no effect)");
            }
        }
        if (cmd.hasOption("oldExpl")) {
            oldExpl = true;
        }
        if (cmd.hasOption("explanation-strategy")) {
            explanationStrategy = cmd.getOptionValue("explanation-strategy");
        }
        String output = cmd.getOptionValue("o");
        if (output == null) {
            output = sourcefile.replaceFirst(".js$", ".c");
        } else {
            if (!output.endsWith(".c")) {
                throw new ParseException("Invalid file extension on output file: " + output);
            }
        }
        String runtime_src = cmd.getOptionValue("runtime-src");
        String ext_path = cmd.getOptionValue("ext-path");
        if (ext_path == null) {
            ext_path = new File(".").getCanonicalPath() + "/external";
        }

        if (cmd.hasOption("extra-decls")) {
            decls = cmd.getOptionValues("extra-decls");
        }
        if (cmd.hasOption("native-libs")) {
            links = cmd.getOptionValues("native-libs");
        }
        if (cmd.hasOption("extra-objs")) {
            objs = cmd.getOptionValues("extra-objs");
        }
        if (cmd.hasOption("guest-runtime")) {
            guest = true;
        }
        if (cmd.hasOption("only-c")) {
            stop_at_c = true;
        }
        if (cmd.hasOption("efl")) {
            efl = true;
        }

        int coptlevel = -1; // default optimization
        if (cmd.hasOption("O3")) {
            coptlevel = 3;
        } else if (cmd.hasOption("O2")) {
            coptlevel = 2;
        } else if (cmd.hasOption("O1")) {
            coptlevel = 1;
        } else if (cmd.hasOption("O0")) {
            coptlevel = 0;
        } else if (cmd.hasOption("O")) {
            coptlevel = -1;
        } else {
            coptlevel = 3;
        }

        if (!Files.exists(Paths.get(sourcefile))) {
            System.err.println("File " + sourcefile + " was not found.");
            throw new FileNotFoundException(sourcefile);
        }

        String cwd = new java.io.File(".").getCanonicalPath();

        opts = new CompilerOptions(p, sourcefile, debug, output, use_gc,
                external_compiler == null ? "clang" : external_compiler,
                external_compiler == null ? "emcc" : external_compiler, cwd + "/a.out", // emcc automatically adds .js
                new File(".").getCanonicalPath(), true, field_opts, showconstraints, showconstraintsolution,
                runtime_src, encode_vals, x32, oldExpl, explanationStrategy, efl, coptlevel);
        if (guest) {
            opts.setGuestRuntime();
        }
        if (interop) {
            opts.enableInteropMode();
        }
        if (boot_interop) {
            opts.startInInteropMode();
        }
        opts.setExternalDeps(ext_path);
        if (decls != null) {
            for (String s : decls) {
                Path fname = FileSystems.getDefault().getPath(s);
                opts.addDeclarationFile(fname);
            }
        }
        if (links != null) {
            for (String s : links) {
                Path fname = FileSystems.getDefault().getPath(s);
                opts.addLinkageFile(fname);
            }
        }
        if (objs == null) {
            objs = new String[0];
        }

    } catch (Exception e) {
        System.err.println(e.getMessage());
        e.printStackTrace();
        HelpFormatter formatter = new HelpFormatter();
        formatter.printHelp("sjsc", options);
    }

    if (opts != null) {
        // This typechecks, and depending on flags, also generates C
        compile(opts, typecheckonly, validate); // don't worry about type validation on command line for now
        if (!typecheckonly && !stop_at_c) {
            int ret = 0;
            // Kept around for debugging 32-bit...
            if (p == CompilerOptions.Platform.Native) {
                if (opts.m32()) {
                    String[] x = new String[2];
                    x[0] = "-m32";
                    x[1] = opts.getExternalDeps() + "/gc/x86/lib/libgc.a";
                    ret = clang_compile(opts, objs, x);
                } else {
                    ret = clang_compile(opts, objs, new String[0]);
                }
            } else {
                ret = emcc_compile(opts, objs, new String[0]);
            }
            // If clang failed, propagate the failure outwards
            if (ret != 0) {
                System.exit(ret);
            }
        }
    }
}

From source file:bogdanrechi.xmlo.Xmlo.java

/**
 * Main method.//  w  w w  .j  ava2 s .c  om
 *
 * @param args
 *          Program arguments.
 */
@SuppressWarnings("static-access")
public static void main(String[] args) {
    long timeStart = System.currentTimeMillis();

    _log = Logger.getLogger(Xmlo.class);

    Options argOptions = new Options();

    OptionGroup operationTypes = new OptionGroup();

    operationTypes.addOption(
            OptionBuilder.withDescription("XPath query").hasArg().withArgName("file").create("xpath"));
    operationTypes.addOption(
            OptionBuilder.withDescription("XSLT transformation").hasArg().withArgName("file").create("xslt"));
    operationTypes.addOption(
            OptionBuilder.withDescription("XQuery (with $sourceFilePath, see XmlOperations for details)")
                    .hasArg().withArgName("file").create("xquery"));
    operationTypes
            .addOption(OptionBuilder.withDescription("XSD verify").hasArgs().withArgName("file").create("xsd"));
    operationTypes
            .addOption(OptionBuilder.withDescription("DTD verify").hasArgs().withArgName("file").create("dtd"));

    argOptions.addOptionGroup(operationTypes);

    argOptions.addOption(OptionBuilder.withDescription("on screen information while performing")
            .withLongOpt("verbose").create("v"));

    argOptions.addOption(OptionBuilder.withDescription("output non-void or invalid-type results only")
            .withLongOpt("results-only").create("o"));

    argOptions.addOption(OptionBuilder.withDescription("replicate input structure on the destination side")
            .withLongOpt("keep-structure").create("k"));

    argOptions.addOption(OptionBuilder.withDescription("recursive browsing of the target structure")
            .withLongOpt("resursive").create("r"));

    argOptions.addOption(OptionBuilder.withDescription("XPath and XQuery results not numbered")
            .withLongOpt("not-numbered").create("nn"));

    argOptions.addOption(OptionBuilder.withDescription("destination files extension").hasArg()
            .withArgName("ext").withLongOpt("extension").create("x"));

    argOptions.addOption(OptionBuilder.withDescription("target files mask").hasArg().withArgName("files mask")
            .withLongOpt("target").create("t"));

    OptionGroup destinationGroup = new OptionGroup();

    destinationGroup.addOption(OptionBuilder.withDescription("destination file").hasArg().withArgName("file")
            .withLongOpt("destination-file").create("df"));
    destinationGroup.addOption(OptionBuilder.withDescription("destination folder").hasArg()
            .withArgName("folder").withLongOpt("destination-directory").create("dd"));
    destinationGroup.addOption(OptionBuilder.withDescription("destination terminal (and verbose)")
            .withLongOpt("destination-terminal").create("dt"));

    argOptions.addOptionGroup(destinationGroup);

    argOptions.addOption(OptionBuilder.withDescription("file containing namespaces aliases").hasArg()
            .withArgName("file").withLongOpt("namespaces").create("n"));

    argOptions.addOption(OptionBuilder.withDescription("usage information").withLongOpt("help").create("h"));

    argOptions.addOption(OptionBuilder.withDescription("show examples").withLongOpt("examples").create("e"));

    argOptions.addOption(OptionBuilder.withDescription("keep blank nodes while printing")
            .withLongOpt("keep-blanks").create("b"));

    argOptions.addOption(
            OptionBuilder.withDescription("show duration for each file when the verbose option is activated")
                    .withLongOpt("show-duration").create("d"));

    CommandLineParser parser = new BasicParser();
    try {
        CommandLine cmd = parser.parse(argOptions, args);

        if (cmd.hasOption('h') || cmd.hasOption('e') || cmd.getOptions().length == 0) {
            Format.println("\n" + General.getAboutInformation("resources/xmlo/metadata.properties"));

            if (cmd.hasOption('h') || cmd.getOptions().length == 0) {
                HelpFormatter formatter = new HelpFormatter();
                formatter.printHelp("xmlo", "where:", argOptions, null, true);
                Format.println();
            }

            if (cmd.hasOption("e")) {
                String examples = Files.readTextFileFromResources("resources/xmlo/examples.txt", _errorMessage);
                if (examples != null)
                    Format.println(examples);
                else {
                    Format.println("Internal error! Please see the log file for detalis.");
                    _log.error(_errorMessage.get());

                    System.exit(1);
                }
            }

            System.exit(0);
            return;
        }

        Format.println();

        // options

        if (cmd.hasOption('r'))
            _recursive = true;

        if (cmd.hasOption('k'))
            _keepStructure = true;

        if (cmd.hasOption('o'))
            _resultsOnly = true;

        if (cmd.hasOption('v'))
            _verbose = true;

        if (cmd.hasOption('b'))
            _keepBlanks = true;

        if (cmd.hasOption('d'))
            _showDuration = true;

        if (cmd.hasOption('x'))
            _extension = "." + cmd.getOptionValue('x');

        if (cmd.hasOption("nn"))
            _notNumbered = true;

        if (cmd.hasOption("df")) {
            _destination = cmd.getOptionValue("df");

            if (Files.isFolder(_destination))
                printErrorAndExit("The destination is a folder!");

            _destinationIsFile = true;
        }

        if (cmd.hasOption("dd")) {
            _destination = cmd.getOptionValue("dd");

            if (!Files.exists(_destination))
                printErrorAndExit("The destination folder does not exist!");

            if (!Files.isFolder(_destination))
                printErrorAndExit("The destination is not a folder!");
        }

        if (cmd.hasOption("dt"))
            _destinationIsTerminal = _verbose = true;

        if (cmd.hasOption('t'))
            _target = cmd.getOptionValue('t');

        if (cmd.hasOption('n')) {
            _namespaces = cmd.getOptionValue('n');
            extractNamespacesAliases();
        }

        // operations

        if (cmd.hasOption("xpath")) {
            if (_target == null)
                _target = Files.CURRENT_DIRECTORY + Files.FILE_SEPARATOR + "*" + EXTENSION_XPATH;

            doXPath(cmd.getOptionValue("xpath"));
        }

        if (cmd.hasOption("xslt")) {
            if (_target == null)
                _target = Files.CURRENT_DIRECTORY + Files.FILE_SEPARATOR + "*" + EXTENSION_XSLT;

            doXslt(cmd.getOptionValue("xslt"));
        }

        if (cmd.hasOption("xquery")) {
            if (_target == null)
                _target = Files.CURRENT_DIRECTORY + Files.FILE_SEPARATOR + "*" + EXTENSION_XQUERY;

            doXQuery(cmd.getOptionValue("xquery"));
        }

        if (cmd.hasOption("xsd")) {
            if (_target == null)
                _target = Files.CURRENT_DIRECTORY + Files.FILE_SEPARATOR + "*" + EXTENSION_XSD;

            doXsd(cmd.getOptionValues("xsd"));
        }

        if (cmd.hasOption("dtd")) {
            if (_target == null)
                _target = Files.CURRENT_DIRECTORY + Files.FILE_SEPARATOR + "*" + EXTENSION_DTD;

            doDtd(cmd.getOptionValues("dtd"));
        }
    } catch (ParseException e) {
        printErrorAndExit(e.getMessage());
    }

    Format.println("Finished%s.", _showDuration ? " in " + TimeMeasure.printDuration(timeStart) : "");

    if (Platform.SYSTEM_IS_LINUX)
        Format.println();

    System.exit(0);
}