Example usage for org.apache.commons.cli BasicParser BasicParser

List of usage examples for org.apache.commons.cli BasicParser BasicParser

Introduction

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

Prototype

BasicParser

Source Link

Usage

From source file:fr.inria.edelweiss.kgdqp.core.CentralizedInferrencingNoSpin.java

public static void main(String args[])
        throws ParseException, EngineException, InterruptedException, IOException, LoadException {

    List<String> endpoints = new ArrayList<String>();
    String queryPath = null;//from   w w  w  .  j  a va 2 s.  co m
    boolean rulesSelection = false;
    File rulesDir = null;
    File ontDir = null;

    /////////////////
    Graph graph = Graph.create();
    QueryProcess exec = QueryProcess.create(graph);

    Options options = new Options();
    Option helpOpt = new Option("h", "help", false, "print this message");
    //        Option queryOpt = new Option("q", "query", true, "specify the sparql query file");
    //        Option endpointOpt = new Option("e", "endpoint", true, "a federated sparql endpoint URL");
    Option versionOpt = new Option("v", "version", false, "print the version information and exit");
    Option rulesOpt = new Option("r", "rulesDir", true, "directory containing the inference rules");
    Option ontOpt = new Option("o", "ontologiesDir", true,
            "directory containing the ontologies for rules selection");
    //        Option locOpt = new Option("c", "centralized", false, "performs centralized inferences");
    Option dataOpt = new Option("l", "load", true, "data file or directory to be loaded");
    //        Option selOpt = new Option("s", "rulesSelection", false, "if set to true, only the applicable rules are run");
    //        options.addOption(queryOpt);
    //        options.addOption(endpointOpt);
    options.addOption(helpOpt);
    options.addOption(versionOpt);
    options.addOption(rulesOpt);
    options.addOption(ontOpt);
    //        options.addOption(selOpt);
    //        options.addOption(locOpt);
    options.addOption(dataOpt);

    String header = "Corese/KGRAM rule engine experiment command line interface";
    String footer = "\nPlease report any issue to alban.gaignard@cnrs.fr, olivier.corby@inria.fr";

    CommandLineParser parser = new BasicParser();
    CommandLine cmd = parser.parse(options, args);
    if (cmd.hasOption("h")) {
        HelpFormatter formatter = new HelpFormatter();
        formatter.printHelp("kgdqp", header, options, footer, true);
        System.exit(0);
    }
    if (cmd.hasOption("o")) {
        rulesSelection = true;
        String ontDirPath = cmd.getOptionValue("o");
        ontDir = new File(ontDirPath);
        if (!ontDir.isDirectory()) {
            logger.warn(ontDirPath + " is not a valid directory path.");
            System.exit(0);
        }
    }
    if (!cmd.hasOption("r")) {
        logger.info("You must specify a path for inference rules directory !");
        System.exit(0);
    }

    if (cmd.hasOption("l")) {
        String[] dataPaths = cmd.getOptionValues("l");
        for (String path : dataPaths) {
            Load ld = Load.create(graph);
            ld.load(path);
            logger.info("Loaded " + path);
        }
    }

    if (cmd.hasOption("v")) {
        logger.info("version 3.0.4-SNAPSHOT");
        System.exit(0);
    }

    String rulesDirPath = cmd.getOptionValue("r");
    rulesDir = new File(rulesDirPath);
    if (!rulesDir.isDirectory()) {
        logger.warn(rulesDirPath + " is not a valid directory path.");
        System.exit(0);
    }

    // Local rules graph initialization
    Graph rulesG = Graph.create();
    Load ld = Load.create(rulesG);

    if (rulesSelection) {
        // Ontology loading
        if (ontDir.isDirectory()) {
            for (File o : ontDir.listFiles()) {
                logger.info("Loading " + o.getAbsolutePath());
                ld.load(o.getAbsolutePath());
            }
        }
    }

    // Rules loading
    if (rulesDir.isDirectory()) {
        for (File r : rulesDir.listFiles()) {
            if (r.getAbsolutePath().endsWith(".rq")) {
                logger.info("Loading " + r.getAbsolutePath());
                //                ld.load(r.getAbsolutePath());

                //                    byte[] encoded = Files.readAllBytes(Paths.get(r.getAbsolutePath()));
                //                    String construct = new String(encoded, "UTF-8"); //StandardCharsets.UTF_8);

                FileInputStream f = new FileInputStream(r);
                QueryLoad ql = QueryLoad.create();
                String construct = ql.read(f);
                f.close();

                SPINProcess sp = SPINProcess.create();
                String spinConstruct = sp.toSpin(construct);

                ld.load(new ByteArrayInputStream(spinConstruct.getBytes()), Load.TURTLE_FORMAT);
                logger.info("Rules graph size : " + rulesG.size());

            }
        }
    }

    // Rule engine initialization
    RuleEngine ruleEngine = RuleEngine.create(graph);
    ruleEngine.set(exec);
    ruleEngine.setOptimize(true);
    ruleEngine.setConstructResult(true);
    ruleEngine.setTrace(true);

    StopWatch sw = new StopWatch();
    logger.info("Federated graph size : " + graph.size());
    logger.info("Rules graph size : " + rulesG.size());

    // Rule selection
    logger.info("Rules selection");
    QueryProcess localKgram = QueryProcess.create(rulesG);
    ArrayList<String> applicableRules = new ArrayList<String>();
    sw.start();
    String rulesSelQuery = "";
    if (rulesSelection) {
        rulesSelQuery = pertinentRulesQuery;
    } else {
        rulesSelQuery = allRulesQuery;
    }
    Mappings maps = localKgram.query(rulesSelQuery);
    logger.info("Rules selected in " + sw.getTime() + " ms");
    logger.info("Applicable rules : " + maps.size());

    // Selected rule loading
    for (Mapping map : maps) {
        IDatatype dt = (IDatatype) map.getValue("?res");
        String rule = dt.getLabel();
        //loading rule in the rule engine
        //            logger.info("Adding rule : ");
        //            System.out.println("-------");
        //            System.out.println(rule);
        //            System.out.println("");
        //            if (! rule.toLowerCase().contains("sameas")) {
        applicableRules.add(rule);
        ruleEngine.addRule(rule);
        //            }
    }

    // Rules application on distributed sparql endpoints
    logger.info("Rules application (" + applicableRules.size() + " rules)");
    ExecutorService threadPool = Executors.newCachedThreadPool();
    RuleEngineThread ruleThread = new RuleEngineThread(ruleEngine);
    sw.reset();
    sw.start();

    //        ruleEngine.process();
    threadPool.execute(ruleThread);
    threadPool.shutdown();

    //monitoring loop
    while (!threadPool.isTerminated()) {
        //            System.out.println("******************************");
        //            System.out.println(Util.jsonDqpCost(QueryProcessDQP.queryCounter, QueryProcessDQP.queryVolumeCounter, QueryProcessDQP.sourceCounter, QueryProcessDQP.sourceVolumeCounter));
        //            System.out.println("Rule engine running for " + sw.getTime() + " ms");
        //            System.out.println("Federated graph size : " + graph.size());
        System.out.println(sw.getTime() + " , " + graph.size());
        Thread.sleep(5000);
    }

    logger.info("Federated graph size : " + graph.size());
    //        logger.info(Util.jsonDqpCost(QueryProcessDQP.queryCounter, QueryProcessDQP.queryVolumeCounter, QueryProcessDQP.sourceCounter, QueryProcessDQP.sourceVolumeCounter));

    //        TripleFormat f = TripleFormat.create(graph, true);
    //        f.write("/tmp/gAll.ttl");
}

From source file:ed.net.lb.LB.java

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

    int verbose = 0;

    for (int i = 0; i < args.length; i++) {
        if (args[i].matches("\\-v+")) {
            verbose = args[i].length() - 1;
            args[i] = "";
        }// w  w w.j  a va2s. c  o  m
    }

    Options o = new Options();
    o.addOption("p", "port", true, "Port to Listen On");
    o.addOption("v", "verbose", false, "Verbose");
    o.addOption("mapfile", "m", true, "file from which to load mappings");

    CommandLine cl = (new BasicParser()).parse(o, args);

    final int port = Integer.parseInt(cl.getOptionValue("p", "8080"));

    MappingFactory mf = null;
    if (cl.hasOption("m"))
        mf = new TextMapping.Factory(cl.getOptionValue("m", null));
    else
        mf = new GridMapping.Factory();

    System.out.println("10gen load balancer");
    System.out.println("\t port \t " + port);
    System.out.println("\t verbose \t " + verbose);

    int retriesLeft = 2;

    HttpMonitor.setApplicationType("Load Balancer");

    LB lb = null;

    while (retriesLeft-- > 0) {

        try {
            lb = new LB(port, mf, verbose);
            break;
        } catch (BindException be) {
            be.printStackTrace();
            System.out.println("can't bind to port.  going to try to kill old one");
            HttpDownload.downloadToJS(new URL("http://127.0.0.1:" + port + "/~kill"));
            Thread.sleep(100);
        }
    }

    if (lb == null) {
        System.err.println("couldn't ever bind");
        System.exit(-1);
        return;
    }

    lb.start();
    lb.join();
    System.exit(0);
}

From source file:com.c4om.xsdfriendlyvalidator.XSDFriendlyValidatorLauncher.java

/**
 * Method called at application start./*w w  w.  j a  v a 2  s .co m*/
 * 
 * @param args command line args
 */
public static void main(String[] args) throws Exception {
    CommandLineParser parser = new BasicParser();
    CommandLine commandLine1 = parser.parse(OPTIONS, args);
    CommandLine commandLine = commandLine1;
    if (commandLine.hasOption(OPTION_NAME_HELP)) {
        HelpFormatter formatter = new HelpFormatter();
        formatter.printHelp(COMMAND_LINE_SYNTAX, OPTIONS);
        System.exit(0);
    } else if (!commandLine.hasOption(OPTION_NAME_INPUT_FILES)) {
        System.err.println("You must specify input files. Invoke with --help to see how.");
        System.exit(1);
    }
    String[] inputFileOptionValues = commandLine.getOptionValues(OPTION_NAME_INPUT_FILES);
    List<Document> inputFilesList = getListOfInputDocumentsFromCommandLine(inputFileOptionValues);
    String[] schemasOptionValues = commandLine.getOptionValues(OPTION_NAME_SCHEMAS);
    Map<String, Document> schemasMap = getSchemaFilesFromCommandLine(schemasOptionValues);
    XSDFriendlyValidator validator = new XSDFriendlyValidatorImpl();
    ValidationResults validationResults = validator.validate(inputFilesList,
            Arrays.asList(inputFileOptionValues), schemasMap);
    System.out.println(validationResults.toString());
}

From source file:com.adobe.aem.demo.communities.Loader.java

public static void main(String[] args) {

    String hostname = null;/* w w  w. j  a v a 2 s . c o m*/
    String port = null;
    String altport = null;
    String csvfile = null;
    String location = null;
    String language = "en";
    String analytics = null;
    String adminPassword = "admin";
    String[] url = new String[10]; // Handling 10 levels maximum for nested comments 
    boolean reset = false;
    boolean configure = false;
    int urlLevel = 0;
    int row = 0;
    HashMap<String, ArrayList<String>> learningpaths = new HashMap<String, ArrayList<String>>();

    // Command line options for this tool
    Options options = new Options();
    options.addOption("h", true, "Hostname");
    options.addOption("p", true, "Port");
    options.addOption("a", true, "Alternate Port");
    options.addOption("f", true, "CSV file");
    options.addOption("r", false, "Reset");
    options.addOption("u", true, "Admin Password");
    options.addOption("c", false, "Configure");
    options.addOption("s", true, "Analytics Endpoint");
    options.addOption("t", false, "Analytics Tracking");
    CommandLineParser parser = new BasicParser();
    try {
        CommandLine cmd = parser.parse(options, args);

        if (cmd.hasOption("h")) {
            hostname = cmd.getOptionValue("h");
        }

        if (cmd.hasOption("p")) {
            port = cmd.getOptionValue("p");
        }

        if (cmd.hasOption("a")) {
            altport = cmd.getOptionValue("a");
        }

        if (cmd.hasOption("f")) {
            csvfile = cmd.getOptionValue("f");
        }

        if (cmd.hasOption("u")) {
            adminPassword = cmd.getOptionValue("u");
        }

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

        if (cmd.hasOption("r")) {
            reset = true;
        }

        if (cmd.hasOption("c")) {
            configure = true;
        }

        if (csvfile == null || port == null || hostname == null) {
            System.out.println(
                    "Request parameters: -h hostname -p port -a alternateport -u adminPassword -f path_to_CSV_file -r (true|false, delete content before import) -c (true|false, post additional properties)");
            System.exit(-1);
        }

    } catch (ParseException ex) {

        logger.error(ex.getMessage());

    }

    String componentType = null;

    try {

        logger.debug("AEM Demo Loader: Processing file " + csvfile);

        // Reading the CSV file, line by line
        Reader in = new FileReader(csvfile);

        Iterable<CSVRecord> records = CSVFormat.EXCEL.parse(in);
        for (CSVRecord record : records) {

            row = row + 1;
            logger.info("Row: " + row + ", new record: " + record.get(0));

            // Let's see if we deal with a comment
            if (record.get(0).startsWith("#")) {

                // We can ignore the comment line and move on
                continue;

            }

            // Let's see if we need to terminate this process
            if (record.get(0).equals(KILL)) {

                System.exit(1);

            }

            // Let's see if we need to create a new Community site
            if (record.get(0).equals(SITE)) {

                // Building the form entity to be posted
                MultipartEntityBuilder builder = MultipartEntityBuilder.create();
                builder.setCharset(MIME.UTF8_CHARSET);
                builder.addTextBody(":operation", "social:createSite",
                        ContentType.create("text/plain", MIME.UTF8_CHARSET));
                builder.addTextBody("_charset_", "UTF-8", ContentType.create("text/plain", MIME.UTF8_CHARSET));

                String urlName = null;

                for (int i = 2; i < record.size() - 1; i = i + 2) {

                    if (record.get(i) != null && record.get(i + 1) != null && record.get(i).length() > 0) {

                        String name = record.get(i).trim();
                        String value = record.get(i + 1).trim();
                        if (value.equals("TRUE")) {
                            value = "true";
                        }
                        if (value.equals("FALSE")) {
                            value = "false";
                        }
                        if (name.equals("urlName")) {
                            urlName = value;
                        }
                        if (name.equals(LANGUAGE)) {
                            language = value;
                        }
                        if (name.equals(BANNER)) {

                            File attachment = new File(
                                    csvfile.substring(0, csvfile.indexOf(".csv")) + File.separator + value);
                            builder.addBinaryBody(BANNER, attachment, ContentType.MULTIPART_FORM_DATA,
                                    attachment.getName());

                        } else if (name.equals(THUMBNAIL)) {

                            File attachment = new File(
                                    csvfile.substring(0, csvfile.indexOf(".csv")) + File.separator + value);
                            builder.addBinaryBody(THUMBNAIL, attachment, ContentType.MULTIPART_FORM_DATA,
                                    attachment.getName());

                        } else {

                            builder.addTextBody(name, value,
                                    ContentType.create("text/plain", MIME.UTF8_CHARSET));

                        }
                    }
                }

                // Site creation
                String siteId = doPost(hostname, port, "/content.social.json", "admin", adminPassword,
                        builder.build(), "response/siteId");

                // Site publishing, if there's a publish instance to publish to
                if (!port.equals(altport)) {

                    List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
                    nameValuePairs.add(new BasicNameValuePair("id", "nobot"));
                    nameValuePairs.add(new BasicNameValuePair(":operation", "social:publishSite"));
                    nameValuePairs
                            .add(new BasicNameValuePair("path", "/content/sites/" + urlName + "/" + language));

                    doPost(hostname, port, "/communities/sites.html", "admin", adminPassword,
                            new UrlEncodedFormEntity(nameValuePairs), null);

                    // Wait for site to be available on Publish
                    doWait(hostname, altport, "admin", adminPassword,
                            (siteId != null ? siteId : urlName) + "-groupadministrators");
                }

                continue;
            }

            // Let's see if we need to create a new Tag
            if (record.get(0).equals(TAG)) {

                // Building the form entity to be posted
                MultipartEntityBuilder builder = MultipartEntityBuilder.create();
                builder.setCharset(MIME.UTF8_CHARSET);
                builder.addTextBody("_charset_", "UTF-8", ContentType.create("text/plain", MIME.UTF8_CHARSET));

                for (int i = 1; i < record.size() - 1; i = i + 2) {

                    if (record.get(i) != null && record.get(i + 1) != null && record.get(i).length() > 0
                            && record.get(i + 1).length() > 0) {

                        String name = record.get(i).trim();
                        String value = record.get(i + 1).trim();
                        builder.addTextBody(name, value, ContentType.create("text/plain", MIME.UTF8_CHARSET));

                    }
                }

                // Tag creation
                doPost(hostname, port, "/bin/tagcommand", "admin", adminPassword, builder.build(), null);

                continue;
            }

            // Let's see if we need to create a new Community site template, and if we can do it (script run against author instance)
            if (record.get(0).equals(SITETEMPLATE)) {

                // Building the form entity to be posted
                MultipartEntityBuilder builder = MultipartEntityBuilder.create();
                builder.setCharset(MIME.UTF8_CHARSET);
                builder.addTextBody(":operation", "social:createSiteTemplate",
                        ContentType.create("text/plain", MIME.UTF8_CHARSET));
                builder.addTextBody("_charset_", "UTF-8", ContentType.create("text/plain", MIME.UTF8_CHARSET));

                for (int i = 2; i < record.size() - 1; i = i + 2) {

                    if (record.get(i) != null && record.get(i + 1) != null && record.get(i).length() > 0) {

                        String name = record.get(i).trim();
                        String value = record.get(i + 1).trim();
                        builder.addTextBody(name, value, ContentType.create("text/plain", MIME.UTF8_CHARSET));

                    }
                }

                // Site template creation
                doPost(hostname, port, "/content.social.json", "admin", adminPassword, builder.build(), null);

                continue;
            }

            // Let's see if we need to create a new Community group
            if (record.get(0).equals(GROUP)) {

                // Building the form entity to be posted
                MultipartEntityBuilder builder = MultipartEntityBuilder.create();
                builder.setCharset(MIME.UTF8_CHARSET);
                builder.addTextBody(":operation", "social:createCommunityGroup",
                        ContentType.create("text/plain", MIME.UTF8_CHARSET));
                builder.addTextBody("_charset_", "UTF-8", ContentType.create("text/plain", MIME.UTF8_CHARSET));

                for (int i = 3; i < record.size() - 1; i = i + 2) {

                    if (record.get(i) != null && record.get(i + 1) != null && record.get(i).length() > 0) {

                        String name = record.get(i).trim();
                        String value = record.get(i + 1).trim();
                        if (value.equals("TRUE")) {
                            value = "true";
                        }
                        if (value.equals("FALSE")) {
                            value = "false";
                        }
                        if (name.equals(IMAGE)) {

                            File attachment = new File(
                                    csvfile.substring(0, csvfile.indexOf(".csv")) + File.separator + value);
                            builder.addBinaryBody(IMAGE, attachment, ContentType.MULTIPART_FORM_DATA,
                                    attachment.getName());

                        } else {

                            builder.addTextBody(name, value,
                                    ContentType.create("text/plain", MIME.UTF8_CHARSET));

                        }
                    }
                }

                // Group creation
                String memberGroupId = doPost(hostname, port, record.get(1), getUserName(record.get(2)),
                        getPassword(record.get(2), adminPassword), builder.build(), "response/memberGroupId");

                // Wait for group to be available on Publish, if available
                logger.debug("Waiting for completion of Community Group creation");
                doWait(hostname, port, "admin", adminPassword, memberGroupId);

                continue;

            }

            // Let's see if it's simple Sling Delete request
            if (record.get(0).equals(SLINGDELETE)) {

                doDelete(hostname, port, record.get(1), "admin", adminPassword);

                continue;

            }

            // Let's see if we need to add users to an AEM Group
            if ((record.get(0).equals(GROUPMEMBERS) || record.get(0).equals(SITEMEMBERS))
                    && record.get(GROUP_INDEX_NAME) != null) {

                // Checking if we have a member group for this site
                String groupName = record.get(GROUP_INDEX_NAME);
                if (record.get(0).equals(SITEMEMBERS)) {

                    // Let's fetch the siteId for this Community Site Url
                    String siteConfig = doGet(hostname, port, groupName, "admin", adminPassword, null);

                    try {

                        String siteId = new JSONObject(siteConfig).getString("siteId");
                        if (siteId != null)
                            groupName = "community-" + siteId + "-members";
                        logger.debug("Member group name is " + groupName);

                    } catch (Exception e) {

                        logger.error(e.getMessage());

                    }

                }

                // Pause until the group can found
                doWait(hostname, port, "admin", adminPassword, groupName);

                List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
                nameValuePairs.add(new BasicNameValuePair("filter",
                        "[{\"operation\":\"like\",\"rep:principalName\":\"" + groupName + "\"}]"));
                nameValuePairs.add(new BasicNameValuePair("type", "groups"));
                String groupList = doGet(hostname, port,
                        "/libs/social/console/content/content/userlist.social.0.10.json", "admin",
                        adminPassword, nameValuePairs);

                logger.debug("List of groups" + groupList);

                if (groupList.indexOf(groupName) > 0) {

                    logger.debug("Group was found on " + port);
                    try {
                        JSONArray jsonArray = new JSONObject(groupList).getJSONArray("items");
                        if (jsonArray.length() == 1) {
                            JSONObject jsonObject = jsonArray.getJSONObject(0);
                            String groupPath = jsonObject.getString("path");

                            logger.debug("Group path is " + groupPath);

                            // Constructing a multi-part POST for group membership
                            MultipartEntityBuilder builder = MultipartEntityBuilder.create();
                            builder.setCharset(MIME.UTF8_CHARSET);
                            builder.setMode(HttpMultipartMode.BROWSER_COMPATIBLE);

                            List<NameValuePair> groupNameValuePairs = buildNVP(record, 2);
                            for (NameValuePair nameValuePair : groupNameValuePairs) {
                                builder.addTextBody(nameValuePair.getName(), nameValuePair.getValue(),
                                        ContentType.create("text/plain", MIME.UTF8_CHARSET));
                            }

                            // Adding the list of group members
                            doPost(hostname, port, groupPath + ".rw.userprops.html", "admin", adminPassword,
                                    builder.build(), null);

                        } else {
                            logger.info("We have more than one match for a group with this name!");
                        }
                    } catch (Exception e) {
                        logger.error(e.getMessage());
                    }
                }

                continue;

            }

            // Let's see if it's user related
            if (record.get(0).equals(USERS)) {

                //First we need to get the path to the user node
                String json = doGet(hostname, port, "/libs/granite/security/currentuser.json",
                        getUserName(record.get(1)), getPassword(record.get(1), adminPassword), null);

                if (json != null) {

                    try {

                        // Fetching the home property
                        String home = new JSONObject(json).getString("home");
                        if (record.get(2).equals(PREFERENCES)) {
                            home = home + "/preferences";
                        } else {
                            home = home + "/profile";
                        }
                        logger.debug(home);

                        // Now we can post all the preferences or the profile
                        List<NameValuePair> nameValuePairs = buildNVP(record, 3);
                        doPost(hostname, port, home, "admin", adminPassword,
                                new UrlEncodedFormEntity(nameValuePairs), null);

                    } catch (Exception e) {
                        logger.error(e.getMessage());
                    }

                }

                continue;

            }

            // Let's see if we deal with a new block of content or just a new entry
            if (record.get(0).equals(CALENDAR) || record.get(0).equals(SLINGPOST)
                    || record.get(0).equals(RATINGS) || record.get(0).equals(BLOG)
                    || record.get(0).equals(JOURNAL) || record.get(0).equals(COMMENTS)
                    || record.get(0).equals(REVIEWS) || record.get(0).equals(FILES)
                    || record.get(0).equals(SUMMARY) || record.get(0).equals(ACTIVITIES)
                    || record.get(0).equals(JOIN) || record.get(0).equals(FOLLOW)
                    || record.get(0).equals(MESSAGE) || record.get(0).equals(ASSET)
                    || record.get(0).equals(AVATAR) || record.get(0).equals(RESOURCE)
                    || record.get(0).equals(LEARNING) || record.get(0).equals(QNA)
                    || record.get(0).equals(FORUM)) {

                // New block of content, we need to reset the processing to first Level
                componentType = record.get(0);
                url[0] = record.get(1);
                urlLevel = 0;

                if (!componentType.equals(SLINGPOST) && reset) {

                    int pos = record.get(1).indexOf("/jcr:content");
                    if (pos > 0)
                        doDelete(hostname, port, "/content/usergenerated" + record.get(1).substring(0, pos),
                                "admin", adminPassword);

                }

                // If the Configure command line flag is set, we try to configure the component with all options enabled
                if (componentType.equals(SLINGPOST) || configure) {

                    String configurePath = getConfigurePath(record.get(1));

                    List<NameValuePair> nameValuePairs = buildNVP(record, 2);
                    if (nameValuePairs.size() > 2) // Only do this when really have configuration settings
                        doPost(hostname, port, configurePath, "admin", adminPassword,
                                new UrlEncodedFormEntity(nameValuePairs), null);

                }

                // We're done with this line, moving on to the next line in the CSV file
                continue;
            }

            // Let's see if we need to indent the list, if it's a reply or a reply to a reply
            if (record.get(1).length() != 1)
                continue; // We need a valid level indicator

            if (Integer.parseInt(record.get(1)) > urlLevel) {
                url[++urlLevel] = location;
                logger.debug("Incremented urlLevel to: " + urlLevel + ", with a new location:" + location);
            } else if (Integer.parseInt(record.get(1)) < urlLevel) {
                urlLevel = Integer.parseInt(record.get(1));
                logger.debug("Decremented urlLevel to: " + urlLevel);
            }

            // Get the credentials or fall back to password
            String password = getPassword(record.get(0), adminPassword);
            String userName = getUserName(record.get(0));

            // Adding the generic properties for all POST requests
            List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();

            if (!componentType.equals(RESOURCE))
                nameValuePairs.add(new BasicNameValuePair("id", "nobot"));

            nameValuePairs.add(new BasicNameValuePair("_charset_", "UTF-8"));

            // Setting some specific fields depending on the content type
            if (componentType.equals(COMMENTS)) {

                nameValuePairs.add(new BasicNameValuePair(":operation", "social:createComment"));
                nameValuePairs.add(new BasicNameValuePair("message", record.get(2)));

            }

            // Creates a forum post (or reply)
            if (componentType.equals(FORUM)) {

                nameValuePairs.add(new BasicNameValuePair(":operation", "social:createForumPost"));
                nameValuePairs.add(new BasicNameValuePair("subject", record.get(2)));
                nameValuePairs.add(new BasicNameValuePair("message", record.get(3)));

            }

            // Follows a user (followedId) for the user posting the request
            if (componentType.equals(FOLLOW)) {

                nameValuePairs.add(new BasicNameValuePair(":operation", "social:follow"));
                nameValuePairs.add(new BasicNameValuePair("userId", "/social/authors/" + userName));
                nameValuePairs.add(new BasicNameValuePair("followedId", "/social/authors/" + record.get(2)));

            }

            // Uploading Avatar picture
            if (componentType.equals(AVATAR)) {

                nameValuePairs.add(new BasicNameValuePair(":operation", "social:changeAvatar"));

            }

            // Joins a user (posting the request) to a Community Group (path)
            if (componentType.equals(JOIN)) {
                nameValuePairs.add(new BasicNameValuePair(":operation", "social:joinCommunityGroup"));
                int pos = url[0].indexOf("/configuration.social.json");
                if (pos > 0)
                    nameValuePairs.add(new BasicNameValuePair("path", url[0].substring(0, pos) + ".html"));
                else
                    continue; // Invalid record
            }

            // Creates a new private message
            if (componentType.equals(MESSAGE)) {
                nameValuePairs.add(new BasicNameValuePair(":operation", "social:createMessage"));
                nameValuePairs.add(new BasicNameValuePair("sendMail", "Sending..."));
                nameValuePairs.add(new BasicNameValuePair("content", record.get(4)));
                nameValuePairs.add(new BasicNameValuePair("subject", record.get(3)));
                nameValuePairs.add(new BasicNameValuePair("serviceSelector", "/bin/community"));
                nameValuePairs.add(new BasicNameValuePair("to", "/social/authors/" + record.get(2)));
                nameValuePairs.add(new BasicNameValuePair("userId", "/social/authors/" + record.get(2)));
                nameValuePairs.add(new BasicNameValuePair(":redirect", "//messaging.html"));
                nameValuePairs.add(new BasicNameValuePair(":formid", "generic_form"));
                nameValuePairs.add(new BasicNameValuePair(":formstart",
                        "/content/sites/communities/messaging/compose/jcr:content/content/primary/start"));
            }

            // Creates a file or a folder
            if (componentType.equals(FILES)) {

                // Top level is always assumed to be a folder, second level files, and third and subsequent levels comments on files
                if (urlLevel == 0) {
                    nameValuePairs.add(new BasicNameValuePair(":operation", "social:createFileLibraryFolder"));
                    nameValuePairs.add(new BasicNameValuePair("name", record.get(2)));
                    nameValuePairs.add(new BasicNameValuePair("message", record.get(3)));
                } else if (urlLevel == 1) {
                    nameValuePairs.add(new BasicNameValuePair(":operation", "social:createComment"));
                }

            }

            // Creates a question, a reply or mark a reply as the best answer
            if (componentType.equals(QNA)) {
                if (urlLevel == 0) {
                    nameValuePairs.add(new BasicNameValuePair(":operation", "social:createQnaPost"));
                    nameValuePairs.add(new BasicNameValuePair("subject", record.get(2)));
                    nameValuePairs.add(new BasicNameValuePair("message", record.get(3)));
                } else if (urlLevel == 1) {
                    nameValuePairs.add(new BasicNameValuePair(":operation", "social:createQnaPost"));
                    nameValuePairs.add(new BasicNameValuePair("message", record.get(3)));
                } else if (urlLevel == 2) {
                    nameValuePairs.add(new BasicNameValuePair(":operation", "social:selectAnswer"));
                }
            }

            // Creates an article or a comment
            if (componentType.equals(JOURNAL) || componentType.equals(BLOG)) {

                nameValuePairs.add(new BasicNameValuePair(":operation", "social:createJournalComment"));
                nameValuePairs.add(new BasicNameValuePair("subject", record.get(2)));
                StringBuffer message = new StringBuffer("<p>" + record.get(3) + "</p>");

                //We might have more paragraphs to add to the blog or journal article
                for (int i = 6; i < record.size(); i++) {
                    if (record.get(i).length() > 0) {
                        message.append("<p>" + record.get(i) + "</p>");
                    }
                }

                //We might have some tags to add to the blog or journal article
                if (record.get(5).length() > 0) {
                    nameValuePairs.add(new BasicNameValuePair("tags", record.get(5)));
                }

                nameValuePairs.add(new BasicNameValuePair("message", message.toString()));

            }

            // Creates a review or a comment
            if (componentType.equals(REVIEWS)) {

                nameValuePairs.add(new BasicNameValuePair("message", record.get(2)));

                // This might be a top level review, or a comment on a review or another comment
                if (urlLevel == 0) {
                    nameValuePairs.add(new BasicNameValuePair(":operation", "social:createReview"));
                    nameValuePairs.add(new BasicNameValuePair("ratings", record.get(3)));
                    if (record.size() > 4 && record.get(4).length() > 0) {
                        // If we are dealing with a non-existent resource, then the design drives the behavior
                        nameValuePairs.add(new BasicNameValuePair("scf:resourceType",
                                "social/reviews/components/hbs/reviews"));
                        nameValuePairs.add(new BasicNameValuePair("scf:included", record.get(4)));
                    }
                } else {
                    nameValuePairs.add(new BasicNameValuePair(":operation", "social:createComment"));
                }

            }

            // Creates a rating
            if (componentType.equals(RATINGS)) {

                nameValuePairs.add(new BasicNameValuePair(":operation", "social:postTallyResponse"));
                nameValuePairs.add(new BasicNameValuePair("tallyType", "Rating"));
                nameValuePairs.add(new BasicNameValuePair("response", record.get(2)));

            }

            // Creates a DAM asset
            if (componentType.equals(ASSET) && record.get(ASSET_INDEX_NAME).length() > 0) {

                nameValuePairs.add(new BasicNameValuePair("fileName", record.get(ASSET_INDEX_NAME)));

            }

            // Creates an enablement resource
            if (componentType.equals(RESOURCE)) {

                nameValuePairs.add(new BasicNameValuePair(":operation", "se:createResource"));

                List<NameValuePair> otherNameValuePairs = buildNVP(record, RESOURCE_INDEX_PROPERTIES);
                nameValuePairs.addAll(otherNameValuePairs);

                // Adding the site
                nameValuePairs.add(new BasicNameValuePair("site",
                        "/content/sites/" + record.get(RESOURCE_INDEX_SITE) + "/resources/en"));

                // Building the cover image fragment
                if (record.get(RESOURCE_INDEX_THUMBNAIL).length() > 0) {
                    nameValuePairs.add(new BasicNameValuePair("cover-image", doThumbnail(hostname, port,
                            adminPassword, csvfile, record.get(RESOURCE_INDEX_THUMBNAIL))));
                } else {
                    nameValuePairs.add(new BasicNameValuePair("cover-image", ""));
                }

                // Building the asset fragment
                String coverPath = "/content/dam/" + record.get(RESOURCE_INDEX_SITE) + "/resource-assets/"
                        + record.get(2) + "/jcr:content/renditions/cq5dam.thumbnail.319.319.png";
                String coverSource = "dam";
                String assets = "[{\"cover-img-path\":\"" + coverPath + "\",\"thumbnail-source\":\""
                        + coverSource
                        + "\",\"asset-category\":\"enablementAsset:dam\",\"resource-asset-name\":null,\"state\":\"A\",\"asset-path\":\"/content/dam/"
                        + record.get(RESOURCE_INDEX_SITE) + "/resource-assets/" + record.get(2) + "\"}]";
                nameValuePairs.add(new BasicNameValuePair("assets", assets));

                logger.debug("assets:" + assets);

            }

            // Creates a learning path
            if (componentType.equals(LEARNING)) {

                nameValuePairs.add(new BasicNameValuePair(":operation", "se:editLearningPath"));

                List<NameValuePair> otherNameValuePairs = buildNVP(record, RESOURCE_INDEX_PROPERTIES);
                nameValuePairs.addAll(otherNameValuePairs);

                // Adding the site
                nameValuePairs.add(new BasicNameValuePair("site",
                        "/content/sites/" + record.get(RESOURCE_INDEX_SITE) + "/resources/en"));

                // Building the cover image fragment
                if (record.get(RESOURCE_INDEX_THUMBNAIL).length() > 0) {
                    nameValuePairs.add(new BasicNameValuePair("card-image", doThumbnail(hostname, port,
                            adminPassword, csvfile, record.get(RESOURCE_INDEX_THUMBNAIL))));
                }

                // Building the learning path fragment
                StringBuffer assets = new StringBuffer("[\"");
                if (learningpaths.get(record.get(2)) != null) {

                    ArrayList<String> paths = learningpaths.get(record.get(2));
                    int i = 0;
                    for (String path : paths) {
                        assets.append("{\\\"type\\\":\\\"linked-resource\\\",\\\"path\\\":\\\"");
                        assets.append(path);
                        assets.append("\\\"}");
                        if (i++ < paths.size() - 1) {
                            assets.append("\",\"");
                        }
                    }

                } else {
                    logger.debug("No asset for this learning path");
                }
                assets.append("\"]");
                nameValuePairs.add(new BasicNameValuePair("learningpath-items", assets.toString()));
                logger.debug("Learning path:" + assets.toString());

            }

            // Creates a calendar event
            if (componentType.equals(CALENDAR)) {

                nameValuePairs.add(new BasicNameValuePair(":operation", "social:createEvent"));
                try {
                    JSONObject event = new JSONObject();

                    // Building the JSON fragment for a new calendar event
                    event.accumulate("subject", record.get(2));
                    event.accumulate("message", record.get(3));
                    event.accumulate("location", record.get(4));
                    event.accumulate("tags", "");
                    event.accumulate("undefined", "update");

                    String startDate = record.get(5);
                    startDate = startDate.replaceAll("YYYY",
                            Integer.toString(Calendar.getInstance().get(Calendar.YEAR)));
                    startDate = startDate.replaceAll("MM",
                            Integer.toString(1 + Calendar.getInstance().get(Calendar.MONTH)));
                    event.accumulate("start", startDate);

                    String endDate = record.get(6);
                    endDate = endDate.replaceAll("YYYY",
                            Integer.toString(Calendar.getInstance().get(Calendar.YEAR)));
                    endDate = endDate.replaceAll("MM",
                            Integer.toString(1 + Calendar.getInstance().get(Calendar.MONTH)));
                    event.accumulate("end", endDate);
                    nameValuePairs.add(new BasicNameValuePair("event", event.toString()));

                } catch (Exception ex) {

                    logger.error(ex.getMessage());

                }

            }

            // Constructing a multi-part POST request
            MultipartEntityBuilder builder = MultipartEntityBuilder.create();
            builder.setCharset(MIME.UTF8_CHARSET);
            builder.setMode(HttpMultipartMode.BROWSER_COMPATIBLE);
            for (NameValuePair nameValuePair : nameValuePairs) {
                builder.addTextBody(nameValuePair.getName(), nameValuePair.getValue(),
                        ContentType.create("text/plain", MIME.UTF8_CHARSET));
            }

            // See if we have attachments for this new post - or some other actions require a form nonetheless
            if ((componentType.equals(ASSET) || componentType.equals(AVATAR) || componentType.equals(FORUM)
                    || (componentType.equals(JOURNAL)) || componentType.equals(BLOG)) && record.size() > 4
                    && record.get(ASSET_INDEX_NAME).length() > 0) {

                File attachment = new File(csvfile.substring(0, csvfile.indexOf(".csv")) + File.separator
                        + record.get(ASSET_INDEX_NAME));

                ContentType ct = ContentType.MULTIPART_FORM_DATA;
                if (record.get(ASSET_INDEX_NAME).indexOf(".mp4") > 0) {
                    ct = ContentType.create("video/mp4", MIME.UTF8_CHARSET);
                } else if (record.get(ASSET_INDEX_NAME).indexOf(".jpg") > 0
                        || record.get(ASSET_INDEX_NAME).indexOf(".jpeg") > 0) {
                    ct = ContentType.create("image/jpeg", MIME.UTF8_CHARSET);
                } else if (record.get(ASSET_INDEX_NAME).indexOf(".png") > 0) {
                    ct = ContentType.create("image/png", MIME.UTF8_CHARSET);
                } else if (record.get(ASSET_INDEX_NAME).indexOf(".pdf") > 0) {
                    ct = ContentType.create("application/pdf", MIME.UTF8_CHARSET);
                } else if (record.get(ASSET_INDEX_NAME).indexOf(".zip") > 0) {
                    ct = ContentType.create("application/zip", MIME.UTF8_CHARSET);
                }
                builder.addBinaryBody("file", attachment, ct, attachment.getName());
                logger.debug("Adding file to payload with name: " + attachment.getName() + " and type: "
                        + ct.getMimeType());

            }

            // If it's a resource or a learning path, we need the path to the resource for subsequent publishing
            String jsonElement = "location";
            if (componentType.equals(RESOURCE)) {
                jsonElement = "changes/argument";
            }
            if (componentType.equals(LEARNING)) {
                jsonElement = "path";
            }
            if (componentType.equals(ASSET)) {
                jsonElement = null;
            }

            // This call generally returns the path to the content fragment that was just created
            location = Loader.doPost(hostname, port, url[urlLevel], userName, password, builder.build(),
                    jsonElement);

            // If we are loading a DAM asset, we are waiting for all renditions to be generated before proceeding
            if (componentType.equals(ASSET)) {
                int pathIndex = url[urlLevel].lastIndexOf(".createasset.html");
                if (pathIndex > 0)
                    doWaitPath(hostname, port, adminPassword, url[urlLevel].substring(0, pathIndex) + "/"
                            + record.get(ASSET_INDEX_NAME) + "/jcr:content/renditions", "nt:file");
            }

            // Let's see if it needs to be added to a learning path
            if (componentType.equals(RESOURCE) && record.get(RESOURCE_INDEX_PATH).length() > 0
                    && location != null) {

                // Adding the location to a list of a resources for this particular Learning Path
                if (learningpaths.get(record.get(RESOURCE_INDEX_PATH)) == null)
                    learningpaths.put(record.get(RESOURCE_INDEX_PATH), new ArrayList<String>());
                logger.debug("Adding resource to Learning path: " + record.get(RESOURCE_INDEX_PATH));
                ArrayList<String> locations = learningpaths.get(record.get(RESOURCE_INDEX_PATH));
                locations.add(location);
                learningpaths.put(record.get(RESOURCE_INDEX_PATH), locations);

            }

            // If it's a Learning Path, we publish it when possible
            if (componentType.equals(LEARNING) && !port.equals(altport) && location != null) {

                // Publishing the learning path 
                List<NameValuePair> publishNameValuePairs = new ArrayList<NameValuePair>();
                publishNameValuePairs.add(new BasicNameValuePair(":operation", "se:publishEnablementContent"));
                publishNameValuePairs.add(new BasicNameValuePair("replication-action", "activate"));
                logger.debug("Publishing a learning path from: " + location);
                Loader.doPost(hostname, port, location, userName, password,
                        new UrlEncodedFormEntity(publishNameValuePairs), null);

                // Waiting for the learning path to be published
                Loader.doWait(hostname, altport, "admin", adminPassword,
                        location.substring(1 + location.lastIndexOf("/")) // Only search for groups with the learning path in it
                );

                // Decorate the resources within the learning path with comments and ratings, randomly generated
                ArrayList<String> paths = learningpaths.get(record.get(2));
                for (String path : paths) {
                    doDecorate(hostname, altport, path, record, analytics);
                }

            }

            // If it's an Enablement Resource, a lot of things need to happen...
            // Step 1. If it's a SCORM resource, we wait for the SCORM metadata workflow to be complete before proceeding
            // Step 2. We publish the resource
            // Step 3. We set a new first published date on the resource (3 weeks earlier) so that reporting data is more meaningful
            // Step 4. We wait for the resource to be available on publish (checking that associated groups are available)
            // Step 5. We retrieve the json for the resource on publish to retrieve the Social endpoints
            // Step 6. We post ratings and comments for each of the enrollees on publish
            if (componentType.equals(RESOURCE) && !port.equals(altport) && location != null) {

                // Wait for the data to be fully copied
                doWaitPath(hostname, port, adminPassword, location + "/assets/asset", "nt:file");

                // If we are dealing with a SCORM asset, we wait a little bit before publishing the resource to that the SCORM workflow is completed 
                if (record.get(2).indexOf(".zip") > 0) {
                    doSleep(10000, "SCORM Resource, waiting for workflow to complete");
                }

                // Publishing the resource 
                List<NameValuePair> publishNameValuePairs = new ArrayList<NameValuePair>();
                publishNameValuePairs.add(new BasicNameValuePair(":operation", "se:publishEnablementContent"));
                publishNameValuePairs.add(new BasicNameValuePair("replication-action", "activate"));
                logger.debug("Publishing a resource from: " + location);
                Loader.doPost(hostname, port, location, userName, password,
                        new UrlEncodedFormEntity(publishNameValuePairs), null);

                // Waiting for the resource to be published
                Loader.doWait(hostname, altport, "admin", adminPassword,
                        location.substring(1 + location.lastIndexOf("/")) // Only search for groups with the resource path in it
                );

                // Setting the first published timestamp so that reporting always comes with 3 weeks of data after building a new demo instance
                DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss");
                Calendar cal = Calendar.getInstance();
                cal.add(Calendar.DATE, REPORTINGDAYS);
                List<NameValuePair> publishDateNameValuePairs = new ArrayList<NameValuePair>();
                publishDateNameValuePairs
                        .add(new BasicNameValuePair("date-first-published", dateFormat.format(cal.getTime())));
                logger.debug("Setting the publish date for a resource from: " + location);
                doPost(hostname, port, location, userName, password,
                        new UrlEncodedFormEntity(publishDateNameValuePairs), null);

                // Adding comments and ratings for this resource
                doDecorate(hostname, altport, location, record, analytics);

            }

        }
    } catch (IOException e) {

        logger.error(e.getMessage());

    }

}

From source file:co.cask.cdap.cli.CLIMain.java

public static void main(String[] args) {
    final PrintStream output = System.out;

    Options options = getOptions();//ww w . j  a  v a2 s. c o  m
    CLIMainArgs cliMainArgs = CLIMainArgs.parse(args, options);

    CommandLineParser parser = new BasicParser();
    try {
        CommandLine command = parser.parse(options, cliMainArgs.getOptionTokens());
        if (command.hasOption(HELP_OPTION.getOpt())) {
            usage();
            System.exit(0);
        }

        LaunchOptions launchOptions = LaunchOptions.builder()
                .setUri(command.getOptionValue(URI_OPTION.getOpt(), getDefaultURI().toString()))
                .setDebug(command.hasOption(DEBUG_OPTION.getOpt()))
                .setVerifySSL(parseBooleanOption(command, VERIFY_SSL_OPTION, DEFAULT_VERIFY_SSL))
                .setAutoconnect(parseBooleanOption(command, AUTOCONNECT_OPTION, DEFAULT_AUTOCONNECT)).build();

        String scriptFile = command.getOptionValue(SCRIPT_OPTION.getOpt(), "");
        boolean hasScriptFile = command.hasOption(SCRIPT_OPTION.getOpt());

        String[] commandArgs = cliMainArgs.getCommandTokens();

        try {
            ClientConfig clientConfig = ClientConfig.builder().setConnectionConfig(null).build();
            final CLIConfig cliConfig = new CLIConfig(clientConfig, output, new AltStyleTableRenderer());
            CLIMain cliMain = new CLIMain(launchOptions, cliConfig);
            CLI cli = cliMain.getCLI();

            cliMain.tryAutoconnect();

            CLIConnectionConfig connectionConfig = new CLIConnectionConfig(
                    cliConfig.getClientConfig().getConnectionConfig(), Id.Namespace.DEFAULT, null);
            cliMain.updateCLIPrompt(connectionConfig);

            if (hasScriptFile) {
                File script = cliMain.getFilePathResolver().resolvePathToFile(scriptFile);
                if (!script.exists()) {
                    output.println("ERROR: Script file '" + script.getAbsolutePath() + "' does not exist");
                    System.exit(1);
                }
                List<String> scriptLines = Files.readLines(script, Charsets.UTF_8);
                for (String scriptLine : scriptLines) {
                    output.print(cliMain.getPrompt(connectionConfig));
                    output.println(scriptLine);
                    cli.execute(scriptLine, output);
                    output.println();
                }
            } else if (commandArgs.length == 0) {
                cli.startInteractiveMode(output);
            } else {
                cli.execute(Joiner.on(" ").join(commandArgs), output);
            }
        } catch (Exception e) {
            e.printStackTrace(output);
        }
    } catch (ParseException e) {
        output.println(e.getMessage());
        usage();
    }
}

From source file:co.cask.cdap.etl.tool.UpgradeTool.java

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

    Options options = new Options().addOption(new Option("h", "help", false, "Print this usage message."))
            .addOption(new Option("u", "uri", true,
                    "CDAP instance URI to interact with in the format "
                            + "[http[s]://]<hostname>:<port>. Defaults to localhost:10000."))
            .addOption(new Option("a", "accesstoken", true,
                    "File containing the access token to use when interacting "
                            + "with a secure CDAP instance."))
            .addOption(new Option("t", "timeout", true,
                    "Timeout in milliseconds to use when interacting with the "
                            + "CDAP RESTful APIs. Defaults to " + DEFAULT_READ_TIMEOUT_MILLIS + "."))
            .addOption(new Option("n", "namespace", true,
                    "Namespace to perform the upgrade in. If none is given, "
                            + "pipelines in all namespaces will be upgraded."))
            .addOption(new Option("p", "pipeline", true,
                    "Name of the pipeline to upgrade. If specified, a namespace " + "must also be given."))
            .addOption(new Option("f", "configfile", true, "File containing old application details to update. "
                    + "The file contents are expected to be in the same format as the request body for creating an "
                    + "ETL application from one of the etl artifacts. "
                    + "It is expected to be a JSON Object containing 'artifact' and 'config' fields."
                    + "The value for 'artifact' must be a JSON Object that specifies the artifact scope, name, and version. "
                    + "The value for 'config' must be a JSON Object specifies the source, transforms, and sinks of the pipeline, "
                    + "as expected by older versions of the etl artifacts."))
            .addOption(new Option("o", "outputfile", true,
                    "File to write the converted application details provided in "
                            + "the configfile option. If none is given, results will be written to the input file + '.converted'. "
                            + "The contents of this file can be sent directly to CDAP to update or create an application."))
            .addOption(new Option("e", "errorDir", true,
                    "Optional directory to write any upgraded pipeline configs that "
                            + "failed to upgrade. The problematic configs can then be manually edited and upgraded separately. "
                            + "Upgrade errors may happen for pipelines that use plugins that are not backwards compatible. "
                            + "This directory must be writable by the user that is running this tool."));

    CommandLineParser parser = new BasicParser();
    CommandLine commandLine = parser.parse(options, args);
    String[] commandArgs = commandLine.getArgs();

    // if help is an option, or if there isn't a single 'upgrade' command, print usage and exit.
    if (commandLine.hasOption("h") || commandArgs.length != 1 || !"upgrade".equalsIgnoreCase(commandArgs[0])) {
        HelpFormatter helpFormatter = new HelpFormatter();
        helpFormatter.printHelp(UpgradeTool.class.getName() + " upgrade",
                "Upgrades Hydrator pipelines created for 3.2.x versions"
                        + "of the cdap-etl-batch and cdap-etl-realtime artifacts into pipelines compatible with 3.3.x versions of "
                        + "cdap-etl-batch and cdap-etl-realtime. Connects to an instance of CDAP to find any 3.2.x pipelines, then "
                        + "upgrades those pipelines.",
                options, "");
        System.exit(0);// ww w.ja  v  a  2 s.com
    }

    ClientConfig clientConfig = getClientConfig(commandLine);

    if (commandLine.hasOption("f")) {
        String inputFilePath = commandLine.getOptionValue("f");
        String outputFilePath = commandLine.hasOption("o") ? commandLine.getOptionValue("o")
                : inputFilePath + ".new";
        convertFile(inputFilePath, outputFilePath, new ArtifactClient(clientConfig));
        System.exit(0);
    }

    File errorDir = commandLine.hasOption("e") ? new File(commandLine.getOptionValue("e")) : null;
    if (errorDir != null) {
        if (!errorDir.exists()) {
            if (!errorDir.mkdirs()) {
                LOG.error("Unable to create error directory {}.", errorDir.getAbsolutePath());
                System.exit(1);
            }
        } else if (!errorDir.isDirectory()) {
            LOG.error("{} is not a directory.", errorDir.getAbsolutePath());
            System.exit(1);
        } else if (!errorDir.canWrite()) {
            LOG.error("Unable to write to error directory {}.", errorDir.getAbsolutePath());
            System.exit(1);
        }
    }
    UpgradeTool upgradeTool = new UpgradeTool(clientConfig, errorDir);

    String namespace = commandLine.getOptionValue("n");
    String pipelineName = commandLine.getOptionValue("p");

    if (pipelineName != null) {
        if (namespace == null) {
            throw new IllegalArgumentException("Must specify a namespace when specifying a pipeline.");
        }
        Id.Application appId = Id.Application.from(namespace, pipelineName);
        if (upgradeTool.upgrade(appId)) {
            LOG.info("Successfully upgraded {}.", appId);
        } else {
            LOG.info("{} did not need to be upgraded.", appId);
        }
        System.exit(0);
    }

    if (namespace != null) {
        printUpgraded(upgradeTool.upgrade(Id.Namespace.from(namespace)));
        System.exit(0);
    }

    printUpgraded(upgradeTool.upgrade());
}

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;/*  w w w .  j a va2  s.  co 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:hivemall.utils.lang.CommandLineUtils.java

public static CommandLine parseOptions(final String[] args, final Options opts) {
    final BasicParser parser = new BasicParser();
    final CommandLine cl;
    try {/* w w  w .  j  av a2 s.c o m*/
        cl = parser.parse(opts, args);
    } catch (ParseException e) {
        throw new IllegalArgumentException(e);
    }
    return cl;
}

From source file:com.github.thesmartenergy.sparql.generate.generator.CMDConfigurations.java

public static CommandLine parseArguments(String[] args) throws ParseException {

    BasicParser commandLineParser = new BasicParser();
    CommandLine cl = commandLineParser.parse(getCMDOptions(), args);

    /*Process Options*/

    //print help menu
    if (cl.hasOption('h')) {
        CMDConfigurations.displayHelp();
    }//w  w  w .  j a v  a 2s. c om

    return cl;
}

From source file:com.github.sakserv.utils.CmdLineUtils.java

public static PropertyParser parseCmdLineAndProps(String[] args, PropertyParser propertyParser)
        throws IOException {
    // Arg parsing for input props file
    Options options = new Options();
    options.addOption("c", true, "configuration file");

    CommandLineParser parser = new BasicParser();
    CommandLine cmd;/*from w ww.  j  a v  a2 s .co  m*/
    String propertyFileName = "";
    try {
        cmd = parser.parse(options, args);

        // Load the props file
        if (cmd.hasOption("c")) {
            propertyFileName = cmd.getOptionValue("c");
        } else {
            propertyFileName = ConfigVars.DEFAULT_PROPS_FILE;
        }
    } catch (ParseException e) {
        LOG.error("ERROR: Failed to parse commandline args!");
        e.printStackTrace();
    }
    LOG.info("Loading and parsing the property file: " + new File(propertyFileName).getAbsolutePath());
    propertyParser.setPropFileName(propertyFileName);
    propertyParser.parsePropsFile();
    return propertyParser;
}