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.sogou.dockeronyarn.service.DockerApplicationMaster_24.java

/**
 * Parse command line options//www  .j a  va  2s.co  m
 *
 * @param args Command line args
 * @return Whether init successful and run should be invoked
 * @throws ParseException
 * @throws IOException
 */
public boolean init(String[] args) throws ParseException, IOException {
    Options opts = new Options();
    opts.addOption("app_attempt_id", true, "App Attempt ID. Not to be used unless for testing purposes");
    opts.addOption("shell_env", true, "Environment for shell script. Specified as env_key=env_val pairs");
    opts.addOption("container_memory", true, "Amount of memory in MB to be requested to run the shell command");
    opts.addOption("container_vcores", true,
            "Amount of virtual cores to be requested to run the shell command");
    opts.addOption("num_containers", true, "No. of containers on which the shell command needs to be executed");
    opts.addOption("priority", true, "Application Priority. Default 0");
    opts.addOption("container_retry", true, "Application container_retry. Default 3");
    opts.addOption("debug", false, "Dump out debug information");

    opts.addOption("help", false, "Print usage");
    CommandLine cliParser = new GnuParser().parse(opts, args);

    if (args.length == 0) {
        printUsage(opts);
        throw new IllegalArgumentException("No args specified for application master to initialize");
    }

    //Check whether customer log4j.properties file exists
    if (fileExist(log4jPath)) {
        try {
            Log4jPropertyHelper.updateLog4jConfiguration(DockerApplicationMaster_24.class, log4jPath);
        } catch (Exception e) {
            LOG.warn("Can not set up custom log4j properties. " + e);
        }
    }

    if (cliParser.hasOption("help")) {
        printUsage(opts);
        return false;
    }

    if (cliParser.hasOption("debug")) {
        dumpOutDebugInfo();
    }

    Map<String, String> envs = System.getenv();

    if (!envs.containsKey(Environment.CONTAINER_ID.name())) {
        if (cliParser.hasOption("app_attempt_id")) {
            String appIdStr = cliParser.getOptionValue("app_attempt_id", "");
            appAttemptID = ConverterUtils.toApplicationAttemptId(appIdStr);
        } else {
            throw new IllegalArgumentException("Application Attempt Id not set in the environment");
        }
    } else {
        ContainerId containerId = ConverterUtils.toContainerId(envs.get(Environment.CONTAINER_ID.name()));
        appAttemptID = containerId.getApplicationAttemptId();
    }

    if (!envs.containsKey(ApplicationConstants.APP_SUBMIT_TIME_ENV)) {
        throw new RuntimeException(ApplicationConstants.APP_SUBMIT_TIME_ENV + " not set in the environment");
    }
    if (!envs.containsKey(Environment.NM_HOST.name())) {
        throw new RuntimeException(Environment.NM_HOST.name() + " not set in the environment");
    }
    if (!envs.containsKey(Environment.NM_HTTP_PORT.name())) {
        throw new RuntimeException(Environment.NM_HTTP_PORT + " not set in the environment");
    }
    if (!envs.containsKey(Environment.NM_PORT.name())) {
        throw new RuntimeException(Environment.NM_PORT.name() + " not set in the environment");
    }

    LOG.info("Application master for app" + ", appId=" + appAttemptID.getApplicationId().getId()
            + ", clustertimestamp=" + appAttemptID.getApplicationId().getClusterTimestamp() + ", attemptId="
            + appAttemptID.getAttemptId());

    if (!fileExist(shellCommandPath) && envs.get(DSConstants.DISTRIBUTEDSHELLSCRIPTLOCATION).isEmpty()) {
        throw new IllegalArgumentException(
                "No shell command or shell script specified to be executed by application master");
    }

    if (fileExist(shellCommandPath)) {
        shellCommand = readContent(shellCommandPath);
    }

    if (fileExist(shellArgsPath)) {
        shellArgs = readContent(shellArgsPath);
    }

    if (cliParser.hasOption("shell_env")) {
        String shellEnvs[] = cliParser.getOptionValues("shell_env");
        for (String env : shellEnvs) {
            env = env.trim();
            int index = env.indexOf('=');
            if (index == -1) {
                shellEnv.put(env, "");
                continue;
            }
            String key = env.substring(0, index);
            String val = "";
            if (index < (env.length() - 1)) {
                val = env.substring(index + 1);
            }
            shellEnv.put(key, val);
        }
    }

    if (envs.containsKey(DSConstants.DISTRIBUTEDSHELLSCRIPTLOCATION)) {
        scriptPath = envs.get(DSConstants.DISTRIBUTEDSHELLSCRIPTLOCATION);

        if (envs.containsKey(DSConstants.DISTRIBUTEDSHELLSCRIPTTIMESTAMP)) {
            shellScriptPathTimestamp = Long.valueOf(envs.get(DSConstants.DISTRIBUTEDSHELLSCRIPTTIMESTAMP));
        }
        if (envs.containsKey(DSConstants.DISTRIBUTEDSHELLSCRIPTLEN)) {
            shellScriptPathLen = Long.valueOf(envs.get(DSConstants.DISTRIBUTEDSHELLSCRIPTLEN));
        }

        if (!scriptPath.isEmpty() && (shellScriptPathTimestamp <= 0 || shellScriptPathLen <= 0)) {
            LOG.error("Illegal values in env for shell script path" + ", path=" + scriptPath + ", len="
                    + shellScriptPathLen + ", timestamp=" + shellScriptPathTimestamp);
            throw new IllegalArgumentException("Illegal values in env for shell script path");
        }
    }

    containerMemory = Integer.parseInt(cliParser.getOptionValue("container_memory", "10"));
    containerVirtualCores = Integer.parseInt(cliParser.getOptionValue("container_vcores", "1"));
    numTotalContainers = Integer.parseInt(cliParser.getOptionValue("num_containers", "1"));
    if (numTotalContainers == 0) {
        throw new IllegalArgumentException("Cannot run distributed shell with no containers");
    }
    requestPriority = Integer.parseInt(cliParser.getOptionValue("priority", "0"));

    this.MAX_RETRY_COUNT.set(Integer.parseInt(cliParser.getOptionValue("container_retry", "3")));

    // Creating the Timeline Client
    timelineClient = TimelineClient.createTimelineClient();
    timelineClient.init(conf);
    timelineClient.start();

    return true;
}

From source file:com.alibaba.jstorm.yarn.utils.JstormYarnUtils.java

public static void checkAndSetOptions(CommandLine cliParser, JstormClientContext jstormClientContext) {
    if (cliParser.hasOption(JOYConstants.DEBUG)) {
        jstormClientContext.debugFlag = true;
    }//  ww w.j a  v a 2  s  . c om
    if (cliParser.hasOption(JOYConstants.KEEP_CONTAINERS_ACROSS_APPLICATION_ATTEMPTS)) {
        jstormClientContext.keepContainers = true;
    }
    if (cliParser.hasOption(JOYConstants.LOG_PROPERTIES)) {
        String log4jPath = cliParser.getOptionValue(JOYConstants.LOG_PROPERTIES);
        try {
            Log4jPropertyHelper.updateLog4jConfiguration(JstormOnYarn.class, log4jPath);
        } catch (Exception e) {
            LOG.warn("Can not set up custom log4j properties. " + e);
        }
    }
    if (jstormClientContext.amMemory < 0) {
        throw new IllegalArgumentException("Invalid memory specified for application master, exiting."
                + " Specified memory=" + jstormClientContext.amMemory);
    }
    if (jstormClientContext.amVCores < 0) {
        throw new IllegalArgumentException("Invalid virtual cores specified for application master, exiting."
                + " Specified virtual cores=" + jstormClientContext.amVCores);
    }
    if (!cliParser.hasOption(JOYConstants.JAR)) {
        throw new IllegalArgumentException("No jar file specified for application master");
    }
    if (!jstormClientContext.rmHost.equals(JOYConstants.EMPTY)) {
        jstormClientContext.conf.set(JOYConstants.RM_ADDRESS_KEY, jstormClientContext.rmHost,
                JOYConstants.YARN_CONF_MODE);
    }
    if (!jstormClientContext.nameNodeHost.equals(JOYConstants.EMPTY)) {
        jstormClientContext.conf.set(JOYConstants.FS_DEFAULTFS_KEY, jstormClientContext.nameNodeHost);
    }
    if (!StringUtils.isBlank(jstormClientContext.hadoopConfDir)) {
        try {
            Collection<File> files = FileUtils.listFiles(new File(jstormClientContext.hadoopConfDir),
                    new String[] { JOYConstants.XML }, true);
            for (File file : files) {
                LOG.info("adding hadoop conf file to conf: " + file.getAbsolutePath());
                jstormClientContext.conf.addResource(file.getAbsolutePath());
            }
        } catch (Exception ex) {
            LOG.error("failed to list hadoop conf dir: " + jstormClientContext.hadoopConfDir);
        }
    }
    String jarPath = JstormOnYarn.class.getProtectionDomain().getCodeSource().getLocation().getPath();
    if (jstormClientContext.confFile == null) {
        JstormYarnUtils.getYarnConfFromJar(jarPath);
        jstormClientContext.conf.addResource(JOYConstants.CONF_NAME);
    } else {
        Path jstormyarnConfPath = new Path(jstormClientContext.confFile);
        jstormClientContext.conf.addResource(jstormyarnConfPath);
    }
    if (!cliParser.hasOption(JOYConstants.SHELL_SCRIPT)) {
        String jarShellScriptPath = jarPath + JOYConstants.START_JSTORM_SHELL;
        try {
            InputStream stream = new FileInputStream(jarShellScriptPath);
            FileOutputStream out = new FileOutputStream(JOYConstants.START_JSTORM_SHELL);
            out.write(IOUtils.toByteArray(stream));
            out.close();
            jstormClientContext.shellScriptPath = JOYConstants.START_JSTORM_SHELL;
        } catch (Exception e) {
            throw new IllegalArgumentException(
                    "No shell script specified to be executed by application master to start nimbus and supervisor");
        }
    } else if (cliParser.hasOption(JOYConstants.SHELL_COMMAND)
            && cliParser.hasOption(JOYConstants.SHELL_SCRIPT)) {
        throw new IllegalArgumentException(
                "Can not specify shell_command option " + "and shell_script option at the same time");
    } else if (cliParser.hasOption(JOYConstants.SHELL_COMMAND)) {
        jstormClientContext.shellCommand = cliParser.getOptionValue(JOYConstants.SHELL_COMMAND);
    } else {
        jstormClientContext.shellScriptPath = cliParser.getOptionValue(JOYConstants.SHELL_SCRIPT);
    }
    if (cliParser.hasOption(JOYConstants.SHELL_ARGS)) {
        jstormClientContext.shellArgs = cliParser.getOptionValues(JOYConstants.SHELL_ARGS);
    }
    setShellEnv(cliParser, jstormClientContext);
    jstormClientContext.shellCmdPriority = Integer.parseInt(cliParser
            .getOptionValue(JOYConstants.SHELL_CMD_PRIORITY, JOYConstants.SHELL_CMD_PRIORITY_DEFAULT_VALUE));
    //set AM memory default to 1000mb
    jstormClientContext.containerMemory = Integer.parseInt(
            cliParser.getOptionValue(JOYConstants.CONTAINER_MEMORY, JOYConstants.DEFAULT_CONTAINER_MEMORY));
    jstormClientContext.containerVirtualCores = Integer.parseInt(
            cliParser.getOptionValue(JOYConstants.CONTAINER_VCORES, JOYConstants.DEFAULT_CONTAINER_VCORES));
    jstormClientContext.numContainers = Integer.parseInt(
            cliParser.getOptionValue(JOYConstants.NUM_CONTAINERS, JOYConstants.DEFAULT_NUM_CONTAINER));

    if (jstormClientContext.containerMemory < 0 || jstormClientContext.containerVirtualCores < 0
            || jstormClientContext.numContainers < 1) {
        throw new IllegalArgumentException("Invalid no. of containers or container memory/vcores specified,"
                + " exiting." + " Specified containerMemory=" + jstormClientContext.containerMemory
                + ", containerVirtualCores=" + jstormClientContext.containerVirtualCores + ", numContainer="
                + jstormClientContext.numContainers);
    }

    jstormClientContext.nodeLabelExpression = cliParser.getOptionValue(JOYConstants.NODE_LABEL_EXPRESSION,
            null);
    jstormClientContext.clientTimeout = Integer
            .parseInt(cliParser.getOptionValue(JOYConstants.TIMEOUT, JOYConstants.DEFAULT_CLIENT_TIME_OUT));

    jstormClientContext.attemptFailuresValidityInterval = Long
            .parseLong(cliParser.getOptionValue(JOYConstants.ATTEMPT_FAILURES_VALIDITY_INTERVAL,
                    JOYConstants.DEFAULT_ATTEMPT_FAILURES_VALIDITY_INTERVAL));

    jstormClientContext.log4jPropFile = cliParser.getOptionValue(JOYConstants.LOG_PROPERTIES,
            JOYConstants.EMPTY);

    // Get timeline domain options
    if (cliParser.hasOption(JOYConstants.DOMAIN)) {
        jstormClientContext.domainId = cliParser.getOptionValue(JOYConstants.DOMAIN);
        jstormClientContext.toCreateDomain = cliParser.hasOption(JOYConstants.CREATE);
        if (cliParser.hasOption(JOYConstants.VIEW_ACLS)) {
            jstormClientContext.viewACLs = cliParser.getOptionValue(JOYConstants.VIEW_ACLS);
        }
        if (cliParser.hasOption(JOYConstants.MODIFY_ACLS)) {
            jstormClientContext.modifyACLs = cliParser.getOptionValue(JOYConstants.MODIFY_ACLS);
        }
    }
}

From source file:hadoop.yarn.distributedshell.DshellApplicationMaster.java

/**
 * Parse command line options// w  ww  . ja  va  2  s.c om
 * 
 * @param args
 *            Command line args
 * @return Whether init successful and run should be invoked
 * @throws ParseException
 * @throws IOException
 */
public boolean init(String[] args) throws ParseException, IOException {
    Options opts = new Options();
    opts.addOption("app_attempt_id", true, "App Attempt ID. Not to be used unless for testing purposes");
    opts.addOption("shell_env", true, "Environment for shell script. Specified as env_key=env_val pairs");
    opts.addOption("container_memory", true, "Amount of memory in MB to be requested to run the shell command");
    opts.addOption("container_vcores", true,
            "Amount of virtual cores to be requested to run the shell command");
    opts.addOption("num_containers", true, "No. of containers on which the shell command needs to be executed");
    opts.addOption("priority", true, "Application Priority. Default 0");
    opts.addOption("debug", false, "Dump out debug information");

    opts.addOption("help", false, "Print usage");
    CommandLine cliParser = new GnuParser().parse(opts, args);

    if (args.length == 0) {
        printUsage(opts);
        throw new IllegalArgumentException("No args specified for application master to initialize");
    }

    // Check whether customer log4j.properties file exists
    if (fileExist(log4jPath)) {
        try {
            Log4jPropertyHelper.updateLog4jConfiguration(DshellApplicationMaster.class, log4jPath);
        } catch (Exception e) {
            LOG.warn("Can not set up custom log4j properties. " + e);
        }
    }

    if (cliParser.hasOption("help")) {
        printUsage(opts);
        return false;
    }

    if (cliParser.hasOption("debug")) {
        dumpOutDebugInfo();
    }

    Map<String, String> envs = System.getenv();

    if (!envs.containsKey(Environment.CONTAINER_ID.name())) {
        if (cliParser.hasOption("app_attempt_id")) {
            String appIdStr = cliParser.getOptionValue("app_attempt_id", "");
            appAttemptID = ConverterUtils.toApplicationAttemptId(appIdStr);
        } else {
            throw new IllegalArgumentException("Application Attempt Id not set in the environment");
        }
    } else {
        ContainerId containerId = ConverterUtils.toContainerId(envs.get(Environment.CONTAINER_ID.name()));
        appAttemptID = containerId.getApplicationAttemptId();
    }

    if (!envs.containsKey(ApplicationConstants.APP_SUBMIT_TIME_ENV)) {
        throw new RuntimeException(ApplicationConstants.APP_SUBMIT_TIME_ENV + " not set in the environment");
    }
    if (!envs.containsKey(Environment.NM_HOST.name())) {
        throw new RuntimeException(Environment.NM_HOST.name() + " not set in the environment");
    }
    if (!envs.containsKey(Environment.NM_HTTP_PORT.name())) {
        throw new RuntimeException(Environment.NM_HTTP_PORT + " not set in the environment");
    }
    if (!envs.containsKey(Environment.NM_PORT.name())) {
        throw new RuntimeException(Environment.NM_PORT.name() + " not set in the environment");
    }

    LOG.info("Application master for app" + ", appId=" + appAttemptID.getApplicationId().getId()
            + ", clustertimestamp=" + appAttemptID.getApplicationId().getClusterTimestamp() + ", attemptId="
            + appAttemptID.getAttemptId());

    if (!fileExist(shellCommandPath) && envs.get(DshellDSConstants.DISTRIBUTEDSHELLSCRIPTLOCATION).isEmpty()) {
        throw new IllegalArgumentException(
                "No shell command or shell script specified to be executed by application master");
    }

    if (fileExist(shellCommandPath)) {
        shellCommand = readContent(shellCommandPath);
    }

    if (fileExist(shellArgsPath)) {
        shellArgs = readContent(shellArgsPath);
    }

    if (cliParser.hasOption("shell_env")) {
        String shellEnvs[] = cliParser.getOptionValues("shell_env");
        for (String env : shellEnvs) {
            env = env.trim();
            int index = env.indexOf('=');
            if (index == -1) {
                shellEnv.put(env, "");
                continue;
            }
            String key = env.substring(0, index);
            String val = "";
            if (index < (env.length() - 1)) {
                val = env.substring(index + 1);
            }
            shellEnv.put(key, val);
        }
    }

    // ??envsjarsHDFSLEN?jarPaths
    for (String key : envs.keySet()) {
        if (key.contains(DshellDSConstants.DISTRIBUTEDJARLOCATION)) {
            DshellFile scistorJar = new DshellFile();
            scistorJar.setJarPath(envs.get(key));
            String num = key.split(DshellDSConstants.DISTRIBUTEDJARLOCATION)[1];
            scistorJar.setTimestamp(Long.parseLong(envs.get(DshellDSConstants.DISTRIBUTEDJARTIMESTAMP + num)));
            scistorJar.setSize(Long.parseLong(envs.get(DshellDSConstants.DISTRIBUTEDJARLEN + num)));
            scistorJars.add(scistorJar);
        }
    }
    // 

    // ??envsarchiveHDFSLEN?archivePaths
    for (String key : envs.keySet()) {
        if (key.contains(DshellDSConstants.DISTRIBUTEDARCHIVELOCATION)) {
            DshellArchive scistorArchive = new DshellArchive();
            scistorArchive.setArchivePath(envs.get(key));
            String num = key.split(DshellDSConstants.DISTRIBUTEDARCHIVELOCATION)[1];
            scistorArchive.setTimestamp(
                    Long.parseLong(envs.get(DshellDSConstants.DISTRIBUTEDARCHIVETIMESTAMP + num)));
            scistorArchive.setSize(Long.parseLong(envs.get(DshellDSConstants.DISTRIBUTEDARCHIVELEN + num)));
            scistorArchives.add(scistorArchive);
        }
    }
    // 
    if (envs.containsKey(DshellDSConstants.DISTRIBUTEDSHELLSCRIPTLOCATION)) {
        scriptPath = envs.get(DshellDSConstants.DISTRIBUTEDSHELLSCRIPTLOCATION);

        if (envs.containsKey(DshellDSConstants.DISTRIBUTEDSHELLSCRIPTTIMESTAMP)) {
            shellScriptPathTimestamp = Long
                    .valueOf(envs.get(DshellDSConstants.DISTRIBUTEDSHELLSCRIPTTIMESTAMP));
        }
        if (envs.containsKey(DshellDSConstants.DISTRIBUTEDSHELLSCRIPTLEN)) {
            shellScriptPathLen = Long.valueOf(envs.get(DshellDSConstants.DISTRIBUTEDSHELLSCRIPTLEN));
        }

        if (!scriptPath.isEmpty() && (shellScriptPathTimestamp <= 0 || shellScriptPathLen <= 0)) {
            LOG.error("Illegal values in env for shell script path" + ", path=" + scriptPath + ", len="
                    + shellScriptPathLen + ", timestamp=" + shellScriptPathTimestamp);
            throw new IllegalArgumentException("Illegal values in env for shell script path");
        }
    }

    containerMemory = Integer.parseInt(cliParser.getOptionValue("container_memory", "10"));
    containerVirtualCores = Integer.parseInt(cliParser.getOptionValue("container_vcores", "1"));
    numTotalContainers = Integer.parseInt(cliParser.getOptionValue("num_containers", "1"));
    if (numTotalContainers == 0) {
        throw new IllegalArgumentException("Cannot run distributed shell with no containers");
    }
    requestPriority = Integer.parseInt(cliParser.getOptionValue("priority", "0"));

    // Creating the Timeline Client
    timelineClient = TimelineClient.createTimelineClient();
    timelineClient.init(conf);
    timelineClient.start();

    return true;
}

From source file:hadoop.yarn.distributedshell.DshellClient.java

/**
 * Parse command line options//  w w  w.  java2s .  co m
 * 
 * @param args
 *            Parsed command line options
 * @return Whether the init was successful to run the client
 * @throws ParseException
 */
public boolean init(String[] args) throws ParseException {
    CommandLine cliParser = new GnuParser().parse(opts, args);
    if (args.length == 0) {
        throw new IllegalArgumentException("No args specified for client to initialize");
    }

    if (cliParser.hasOption("log_properties")) {
        String log4jPath = cliParser.getOptionValue("log_properties");
        try {
            Log4jPropertyHelper.updateLog4jConfiguration(DshellClient.class, log4jPath);
        } catch (Exception e) {
            LOG.warn("Can not set up custom log4j properties. " + e);
        }
    }
    // container_jarscontainer_archives??
    if (cliParser.hasOption("container_files")) {
        containerJarPaths = cliParser.getOptionValue("container_files").split(",");
    }
    if (cliParser.hasOption("container_archives")) {
        containerArchivePaths = cliParser.getOptionValue("container_archives").split(",");
    }
    // 
    if (cliParser.hasOption("help")) {
        printUsage();
        return false;
    }

    if (cliParser.hasOption("debug")) {
        debugFlag = true;

    }

    if (cliParser.hasOption("keep_containers_across_application_attempts")) {
        LOG.info("keep_containers_across_application_attempts");
        keepContainers = true;
    }

    appName = cliParser.getOptionValue("appname", "DistributedShell");
    amPriority = Integer.parseInt(cliParser.getOptionValue("priority", "0"));
    amQueue = cliParser.getOptionValue("queue", "default");
    amMemory = Integer.parseInt(cliParser.getOptionValue("master_memory", "10"));
    amVCores = Integer.parseInt(cliParser.getOptionValue("master_vcores", "1"));

    if (amMemory < 0) {
        throw new IllegalArgumentException(
                "Invalid memory specified for application master, exiting." + " Specified memory=" + amMemory);
    }
    if (amVCores < 0) {
        throw new IllegalArgumentException("Invalid virtual cores specified for application master, exiting."
                + " Specified virtual cores=" + amVCores);
    }

    if (!cliParser.hasOption("jar")) {
        throw new IllegalArgumentException("No jar file specified for application master");
    }

    appMasterJar = cliParser.getOptionValue("jar");

    if (!cliParser.hasOption("shell_command") && !cliParser.hasOption("shell_script")) {
        throw new IllegalArgumentException(
                "No shell command or shell script specified to be executed by application master");
    } else if (cliParser.hasOption("shell_command") && cliParser.hasOption("shell_script")) {
        throw new IllegalArgumentException(
                "Can not specify shell_command option " + "and shell_script option at the same time");
    } else if (cliParser.hasOption("shell_command")) {
        shellCommand = cliParser.getOptionValue("shell_command");
    } else {
        shellScriptPath = cliParser.getOptionValue("shell_script");
    }

    if (cliParser.hasOption("shell_args")) {
        shellArgs = cliParser.getOptionValues("shell_args");
    }
    if (cliParser.hasOption("shell_env")) {
        String envs[] = cliParser.getOptionValues("shell_env");
        for (String env : envs) {
            env = env.trim();
            int index = env.indexOf('=');
            if (index == -1) {
                shellEnv.put(env, "");
                continue;
            }
            String key = env.substring(0, index);
            String val = "";
            if (index < (env.length() - 1)) {
                val = env.substring(index + 1);
            }
            shellEnv.put(key, val);
        }
    }
    shellCmdPriority = Integer.parseInt(cliParser.getOptionValue("shell_cmd_priority", "0"));

    containerMemory = Integer.parseInt(cliParser.getOptionValue("container_memory", "10"));
    containerVirtualCores = Integer.parseInt(cliParser.getOptionValue("container_vcores", "1"));
    numContainers = Integer.parseInt(cliParser.getOptionValue("num_containers", "1"));

    if (containerMemory < 0 || containerVirtualCores < 0 || numContainers < 1) {
        throw new IllegalArgumentException("Invalid no. of containers or container memory/vcores specified,"
                + " exiting." + " Specified containerMemory=" + containerMemory + ", containerVirtualCores="
                + containerVirtualCores + ", numContainer=" + numContainers);
    }

    clientTimeout = Integer.parseInt(cliParser.getOptionValue("timeout", "600000"));

    log4jPropFile = cliParser.getOptionValue("log_properties", "");

    return true;
}

From source file:com.datatorrent.stram.cli.DTCli.java

public void preImpersonationInit(String[] args) throws IOException {
    Signal.handle(new Signal("INT"), new SignalHandler() {
        @Override/*from  www  .java 2  s. co m*/
        public void handle(Signal sig) {
            System.out.println("^C");
            if (commandThread != null) {
                commandThread.interrupt();
                mainThread.interrupt();
            } else {
                System.out.print(prompt);
                System.out.flush();
            }
        }
    });
    consolePresent = (System.console() != null);
    Options options = new Options();
    options.addOption("e", true, "Commands are read from the argument");
    options.addOption("v", false, "Verbose mode level 1");
    options.addOption("vv", false, "Verbose mode level 2");
    options.addOption("vvv", false, "Verbose mode level 3");
    options.addOption("vvvv", false, "Verbose mode level 4");
    options.addOption("r", false, "JSON Raw mode");
    options.addOption("p", true, "JSONP padding function");
    options.addOption("h", false, "Print this help");
    options.addOption("f", true, "Use the specified prompt at all time");
    options.addOption("kp", true, "Use the specified kerberos principal");
    options.addOption("kt", true, "Use the specified kerberos keytab");

    CommandLineParser parser = new BasicParser();
    try {
        CommandLine cmd = parser.parse(options, args);
        if (cmd.hasOption("v")) {
            verboseLevel = 1;
        }
        if (cmd.hasOption("vv")) {
            verboseLevel = 2;
        }
        if (cmd.hasOption("vvv")) {
            verboseLevel = 3;
        }
        if (cmd.hasOption("vvvv")) {
            verboseLevel = 4;
        }
        if (cmd.hasOption("r")) {
            raw = true;
        }
        if (cmd.hasOption("e")) {
            commandsToExecute = cmd.getOptionValues("e");
            consolePresent = false;
        }
        if (cmd.hasOption("p")) {
            jsonp = cmd.getOptionValue("p");
        }
        if (cmd.hasOption("f")) {
            forcePrompt = cmd.getOptionValue("f");
        }
        if (cmd.hasOption("h")) {
            HelpFormatter formatter = new HelpFormatter();
            formatter.printHelp(DTCli.class.getSimpleName(), options);
            System.exit(0);
        }
        if (cmd.hasOption("kp")) {
            kerberosPrincipal = cmd.getOptionValue("kp");
        }
        if (cmd.hasOption("kt")) {
            kerberosKeyTab = cmd.getOptionValue("kt");
        }
    } catch (ParseException ex) {
        System.err.println("Invalid argument: " + ex);
        System.exit(1);
    }

    if (kerberosPrincipal == null && kerberosKeyTab != null) {
        System.err.println(
                "Kerberos key tab is specified but not the kerberos principal. Please specify it using the -kp option.");
        System.exit(1);
    }
    if (kerberosPrincipal != null && kerberosKeyTab == null) {
        System.err.println(
                "Kerberos principal is specified but not the kerberos key tab. Please specify it using the -kt option.");
        System.exit(1);
    }

    Level logLevel;
    switch (verboseLevel) {
    case 0:
        logLevel = Level.OFF;
        break;
    case 1:
        logLevel = Level.ERROR;
        break;
    case 2:
        logLevel = Level.WARN;
        break;
    case 3:
        logLevel = Level.INFO;
        break;
    default:
        logLevel = Level.DEBUG;
        break;
    }

    for (org.apache.log4j.Logger logger : new org.apache.log4j.Logger[] {
            org.apache.log4j.Logger.getRootLogger(), org.apache.log4j.Logger.getLogger(DTCli.class) }) {
        @SuppressWarnings("unchecked")
        Enumeration<Appender> allAppenders = logger.getAllAppenders();
        while (allAppenders.hasMoreElements()) {
            Appender appender = allAppenders.nextElement();
            if (appender instanceof ConsoleAppender) {
                ((ConsoleAppender) appender).setThreshold(logLevel);
            }
        }
    }

    if (commandsToExecute != null) {
        for (String command : commandsToExecute) {
            LOG.debug("Command to be executed: {}", command);
        }
    }
    if (kerberosPrincipal != null && kerberosKeyTab != null) {
        StramUserLogin.authenticate(kerberosPrincipal, kerberosKeyTab);
    } else {
        Configuration config = new YarnConfiguration();
        StramClientUtils.addDTLocalResources(config);
        StramUserLogin.attemptAuthentication(config);
    }
}

From source file:com.datatorrent.stram.cli.ApexCli.java

public void preImpersonationInit(String[] args) throws IOException {
    Signal.handle(new Signal("INT"), new SignalHandler() {
        @Override// w  w w.j a  v a2 s  .c om
        public void handle(Signal sig) {
            System.out.println("^C");
            if (commandThread != null) {
                commandThread.interrupt();
                mainThread.interrupt();
            } else {
                System.out.print(prompt);
                System.out.flush();
            }
        }
    });
    consolePresent = (System.console() != null);
    Options options = new Options();
    options.addOption("e", true, "Commands are read from the argument");
    options.addOption("v", false, "Verbose mode level 1");
    options.addOption("vv", false, "Verbose mode level 2");
    options.addOption("vvv", false, "Verbose mode level 3");
    options.addOption("vvvv", false, "Verbose mode level 4");
    options.addOption("r", false, "JSON Raw mode");
    options.addOption("p", true, "JSONP padding function");
    options.addOption("h", false, "Print this help");
    options.addOption("f", true, "Use the specified prompt at all time");
    options.addOption("kp", true, "Use the specified kerberos principal");
    options.addOption("kt", true, "Use the specified kerberos keytab");

    CommandLineParser parser = new BasicParser();
    try {
        CommandLine cmd = parser.parse(options, args);
        if (cmd.hasOption("v")) {
            verboseLevel = 1;
        }
        if (cmd.hasOption("vv")) {
            verboseLevel = 2;
        }
        if (cmd.hasOption("vvv")) {
            verboseLevel = 3;
        }
        if (cmd.hasOption("vvvv")) {
            verboseLevel = 4;
        }
        if (cmd.hasOption("r")) {
            raw = true;
        }
        if (cmd.hasOption("e")) {
            commandsToExecute = cmd.getOptionValues("e");
            consolePresent = false;
        }
        if (cmd.hasOption("p")) {
            jsonp = cmd.getOptionValue("p");
        }
        if (cmd.hasOption("f")) {
            forcePrompt = cmd.getOptionValue("f");
        }
        if (cmd.hasOption("h")) {
            HelpFormatter formatter = new HelpFormatter();
            formatter.printHelp(ApexCli.class.getSimpleName(), options);
            System.exit(0);
        }
        if (cmd.hasOption("kp")) {
            kerberosPrincipal = cmd.getOptionValue("kp");
        }
        if (cmd.hasOption("kt")) {
            kerberosKeyTab = cmd.getOptionValue("kt");
        }
    } catch (ParseException ex) {
        System.err.println("Invalid argument: " + ex);
        System.exit(1);
    }

    if (kerberosPrincipal == null && kerberosKeyTab != null) {
        System.err.println(
                "Kerberos key tab is specified but not the kerberos principal. Please specify it using the -kp option.");
        System.exit(1);
    }
    if (kerberosPrincipal != null && kerberosKeyTab == null) {
        System.err.println(
                "Kerberos principal is specified but not the kerberos key tab. Please specify it using the -kt option.");
        System.exit(1);
    }

    Level logLevel;
    switch (verboseLevel) {
    case 0:
        logLevel = Level.OFF;
        break;
    case 1:
        logLevel = Level.ERROR;
        break;
    case 2:
        logLevel = Level.WARN;
        break;
    case 3:
        logLevel = Level.INFO;
        break;
    default:
        logLevel = Level.DEBUG;
        break;
    }

    for (org.apache.log4j.Logger logger : new org.apache.log4j.Logger[] {
            org.apache.log4j.Logger.getRootLogger(), org.apache.log4j.Logger.getLogger(ApexCli.class) }) {

        /*
         * Override logLevel specified by user, the same logLevel would be inherited by all
         * appenders related to logger.
         */
        logger.setLevel(logLevel);

        @SuppressWarnings("unchecked")
        Enumeration<Appender> allAppenders = logger.getAllAppenders();
        while (allAppenders.hasMoreElements()) {
            Appender appender = allAppenders.nextElement();
            if (appender instanceof ConsoleAppender) {
                ((ConsoleAppender) appender).setThreshold(logLevel);
            }
        }
    }

    if (commandsToExecute != null) {
        for (String command : commandsToExecute) {
            LOG.debug("Command to be executed: {}", command);
        }
    }
    if (kerberosPrincipal != null && kerberosKeyTab != null) {
        StramUserLogin.authenticate(kerberosPrincipal, kerberosKeyTab);
    } else {
        Configuration config = new YarnConfiguration();
        StramClientUtils.addDTLocalResources(config);
        StramUserLogin.attemptAuthentication(config);
    }
}

From source file:edu.usc.scrc.PriorityPruner.CommandLineOptions.java

/**
 * Parses command line arguments, evaluates them, and stores values if
 * they're correct.//  w  w w  .j  av a  2  s  . c o  m
 * 
 * @param options
 *            collection of Option-objects
 * @param args
 *            command line arguments
 * @throws PriorityPrunerException
 *             if error occurs during parsing
 */
private void parse(Options options, String[] args) throws PriorityPrunerException {
    try {
        CommandLineParser cmdLineParser = new GnuParser();
        CommandLine commandLine = cmdLineParser.parse(options, args);
        setParsedOptions(commandLine.getOptions());
        // gets set to true if -tfile option is entered through the command
        // line
        //boolean tFlag = false;
        // counts --tped & --tfam options entered through the command line
        //int tpedCount = 0;
        //int tfamCount = 0;

        // loops through all options to save the ones in effect as a String
        // for printing
        String tmp = "Options in effect:";
        for (Option opt : commandLine.getOptions()) {
            tmp += ("\r\n   -" + opt.getOpt());
            if (opt.getValues() != null) {
                for (int i = 0; i < opt.getValues().length; i++) {
                    tmp += (" " + opt.getValues()[i]);
                }
            }
        }

        // saves options in effect for printing
        this.setOptionsInEffect(tmp + "\r\n");

        // parse max_distance
        if (commandLine.hasOption("max_distance")) {
            this.maxDistance(getLongArgument("max_distance", commandLine.getOptionValue("max_distance"), 0,
                    Long.MAX_VALUE));
            checkInput(1, "max_distance", commandLine);
        }

        // parse min_maf
        if (commandLine.hasOption("min_maf")) {
            this.setMinMaf(getDoubleArgument("min_maf", commandLine.getOptionValue("min_maf"), 0, 0.5));
            checkInput(1, "min_maf", commandLine);
        }

        // parse min_hwe
        //         if (commandLine.hasOption("min_hwe")) {
        //            this.setMinHwe(getDoubleArgument("min_hwe",
        //                  commandLine.getOptionValue("min_hwe"), 0, 1));
        //            checkInput(1, "min_hwe", commandLine);
        //         }

        // parses min_snp_callrate
        if (commandLine.hasOption("min_snp_callrate")) {
            this.setMinSnpCallRate(getDoubleArgument("min_snp_callrate",
                    commandLine.getOptionValue("min_snp_callrate"), 0, 1));
            checkInput(1, "min_snp_callrate", commandLine);
        }

        // parses min_design_score
        if (commandLine.hasOption("min_design_score")) {
            this.setMinDesignScore(getDoubleArgument("min_design_score",
                    commandLine.getOptionValue("min_design_score"), 0, Double.MAX_VALUE));
            checkInput(1, "min_design_score", commandLine);
        }

        //         // parses option that sets the absolute minimum design score
        //         // value, allowed arguments are doubles between 0.0 and
        //         // Double.MAX_VALUE
        //         if (commandLine.hasOption("amds")) {
        //            this.setAbsoluteMinDesignScore(getDoubleArgument(
        //                  "absmindesignscore",
        //                  commandLine.getOptionValue("amds"), 0, Double.MAX_VALUE));
        //            checkInput(1, "amds", commandLine);
        //         }

        // parse metric
        if (commandLine.hasOption("metric")) {
            String[] str = commandLine.getOptionValues("metric");
            if (str.length % 2 == 1) {
                throw new PriorityPrunerException(
                        "Only one argument specified for option \"metric\", a column name (text string) and a weight (decimal number) are required.");
            } else {
                for (int i = 0; i < str.length; i++) {
                    if (str[i].equals("design_score")) {
                        throw new PriorityPrunerException("Invalid metric: \"" + str[i]
                                + "\". To filter on design score, simply add correct column in the SNP input file, no metric is needed.");
                    }
                    addMetric(str[i], this.getDoubleArgument("metric", str[i + 1], 0, Double.MAX_VALUE));
                    i++;
                }
            }
        }

        // parse st
        if (commandLine.hasOption("st")) {
            String[] str = commandLine.getOptionValues("st");
            if (str.length % 2 == 1) {
                throw new PriorityPrunerException(
                        "Only one argument specified for option: \"st\", a p-value (decimal number) and a number of surrogates (integer) are are required.");
            } else {
                for (int i = 0; i < str.length; i++) {
                    addSurrogateThreshold(this.getDoubleArgument("st", str[i], 0, 1),
                            this.getIntegerArgument("st", str[i + 1], 0, Integer.MAX_VALUE));
                    i++;
                }
            }
        }

        // parse r2t
        if (commandLine.hasOption("r2t")) {
            String[] str = commandLine.getOptionValues("r2t");
            if (str.length % 2 == 1) {
                throw new PriorityPrunerException(
                        "Only one argument specified for option: \"r2t\", a p-value and a threshold (decimal numbers between 0 and 1) are required.");
            } else {
                for (int i = 0; i < str.length; i++) {
                    addR2Threshold(this.getDoubleArgument("r2t", str[i], 0, 1),
                            this.getDoubleArgument("r2t", str[i + 1], 0, 1));
                    i++;
                }
            }
        }

        // parse r2
        if (commandLine.hasOption("r2")) {
            String value = commandLine.getOptionValue("r2");
            this.addR2Threshold(1, this.getDoubleArgument("r2", value, 0, 1));
            checkInput(1, "r2", commandLine);
        }

        // parse chr
        if (commandLine.hasOption("chr")) {
            String value = commandLine.getOptionValue("chr");
            // recoding chromosome X-representations to "23"
            if (value.toUpperCase().equals("X") || value.toUpperCase().equals("CHRX")) {
                //value = "23";
            }
            // if chromosome Y or mitochondrial DNA is encountered, an
            // exception is thrown
            else if (value.toUpperCase().equals("M") || value.toUpperCase().equals("MT")
                    || value.toUpperCase().equals("CHRM") || value.toUpperCase().equals("CHRMT")
                    || value.toUpperCase().equals("Y") || value.toUpperCase().equals("CHRY")
                    || value.toUpperCase().equals("24")) {
                throw new PriorityPrunerException("Chromosome \"" + value
                        + "\" specified in the command line, is not supported. Please update input files and rerun program.");
            }
            this.setChr(value);
            checkInput(1, "chr", commandLine);
        }

        // parse out
        if (commandLine.hasOption("out")) {
            String value = commandLine.getOptionValue("out");
            if (value.endsWith("/")) {
                value = new StringBuilder(value).append("prioritypruner").toString();
            }
            this.setOutputPrefix(value);
            checkInput(1, "out", commandLine);
        }

        //         // parse forceInclude
        //         if (commandLine.hasOption("force_include")) {
        //            String value = commandLine.getOptionValue("force_include");
        //            this.setForceIncludeFilePath(value);
        //            checkInput(1, "force_include", commandLine);
        //         }

        // parse do_not_pick_force_included_snps_first
        if (commandLine.hasOption("do_not_pick_force_included_snps_first")) {
            this.setSortByForceIncludeAndPValue(false);
        }

        // parse snp_table
        if (commandLine.hasOption("snp_table")) {
            String[] str = commandLine.getOptionValues("snp_table");
            this.setSnpTablePath(str[0]);
            // counts number of metrics added to command line
            int metrics = 0;
            for (int i = 0; i < getParsedOptions().length; i++) {
                if (getParsedOptions()[i].getOpt().equals("metric")) {
                    metrics++;
                }
            }
            this.setNumMetrics(metrics);
            checkInput(1, "snp_table", commandLine);
        }

        // parse tped
        if (commandLine.hasOption("tped")) {
            String value = commandLine.getOptionValue("tped");
            checkInput(1, "tped", commandLine);
            this.setTped(value);
        }

        // parse tfam
        if (commandLine.hasOption("tfam")) {
            String value = commandLine.getOptionValue("tfam");
            checkInput(1, "tfam", commandLine);
            this.setTfam(value);
        }

        // parse tfile
        if (commandLine.hasOption("tfile")) {
            String value = commandLine.getOptionValue("tfile");
            checkInput(1, "tfile", commandLine);
            this.setTped(value + ".tped");
            this.setTfam(value + ".tfam");
            this.setTfile(value);
        }

        // parse use_surrogate_for_non_passing_index_snps
        //         if (commandLine.hasOption("use_surrogate_for_non_passing_index_snps")) {
        //            this.setUseSurrogateForNonPassingIndexSnp(true);
        //         }

        // parses verbose
        if (commandLine.hasOption("verbose")) {
            this.setVerbose(true);
        }

        // parse outputLDTable
        if (commandLine.hasOption("ld")) {
            this.setOutputLDTable(true);
        }

        // parse remove
        if (commandLine.hasOption("remove")) {
            String value = commandLine.getOptionValue("remove");
            checkInput(1, "remove", commandLine);
            this.setRemove(value);
        }

        // parse keep
        if (commandLine.hasOption("keep")) {
            String value = commandLine.getOptionValue("keep");
            checkInput(1, "keep", commandLine);
            this.setKeep(value);
        }

        // parse keep_random
        if (commandLine.hasOption("keep_random")) {
            String value = commandLine.getOptionValue("keep_random");
            this.setKeepPercentage(this.getDoubleArgument("keep_random", value, 0, 1));
            checkInput(1, "keep_random", commandLine);
        }

        // parse no_surrogates_for_force_included_snps
        if (commandLine.hasOption("no_surrogates_for_force_included_snps")) {
            this.setAddSurrogatesForForceIncludedSnps(false);
        }

        // parse seed
        if (commandLine.hasOption("seed")) {
            String value = commandLine.getOptionValue("seed");
            this.seed = new Long(this.getLongArgument("seed", value, Long.MIN_VALUE, Long.MAX_VALUE));
        }

        // check that we have all required arguments
        checkRequiredArguments(commandLine);

        // checks if any unrecognized arguments been entered
        checkLeftArguments(commandLine);

        // if several options from the same options group been entered,
        // an AlreadySelectedException gets thrown. A custom message is
        // generated since we wanted another design than the one provided in
        // the library gave
    } catch (AlreadySelectedException e) {
        String message = "";
        for (int i = 0; i < e.getOptionGroup().getNames().toArray().length; i++) {
            message += "\"" + e.getOptionGroup().getNames().toArray()[i] + "\" ";
        }
        throw new PriorityPrunerException(
                "The options: " + message + " may not both be defined. Type --help for help.");

        // if an undefined option is entered an UnrecognizedOptionException
        // gets thrown
    } catch (UnrecognizedOptionException e) {
        throw new PriorityPrunerException(e.getOption() + " is not a valid option. Type --help for help.");

        // if an option that is supposed to have arguments is missing,
        // a MissingArgumentException gets thrown
    } catch (MissingArgumentException e) {
        throw new PriorityPrunerException("Missing argument for option \"" + e.getOption().getOpt()
                + "\". Expected: " + e.getOption().getArgName() + ". Type --help for help.");

        // if a required option is missing, a MissingOptionException gets
        // thrown
    } catch (MissingOptionException e) {
        // if any other problem occurs while parsing, a general
        // ParseException gets thrown
    } catch (ParseException parseException) {
        throw new PriorityPrunerException("Invalid command line options. Type --help for help.");
    }
}

From source file:com.bigjob.Client.java

/**
 * Parse command line options//from w w  w  .j a v  a  2  s  .  co  m
 * @param args Parsed command line options 
 * @return Whether the init was successful to run the client
 * @throws ParseException
 */
public boolean init(String[] args) throws ParseException {

    CommandLine cliParser = new GnuParser().parse(opts, args);

    if (args.length == 0) {
        throw new IllegalArgumentException("No args specified for client to initialize");
    }

    if (cliParser.hasOption("log_properties")) {
        String log4jPath = cliParser.getOptionValue("log_properties");
        try {
            Log4jPropertyHelper.updateLog4jConfiguration(Client.class, log4jPath);
        } catch (Exception e) {
            LOG.warn("Can not set up custom log4j properties. " + e);
        }
    }

    if (cliParser.hasOption("help")) {
        printUsage();
        return false;
    }

    if (cliParser.hasOption("debug")) {
        debugFlag = true;

    }

    if (fileExist("log4j.properties")) {
        try {
            Log4jPropertyHelper.updateLog4jConfiguration(ApplicationMaster.class, "log4j.properties");
        } catch (Exception e) {
            LOG.warn("Can not set up custom log4j properties. " + e);
        }
    } else {
        LOG.warn("No Log4j found");

    }

    yarnClient = YarnClient.createYarnClient();
    String configPath = cliParser.getOptionValue("config", "");
    if (configPath.compareTo("") == 0) {
        conf = new YarnConfiguration();
    } else {
        conf = new YarnConfiguration();
    }
    yarnClient.init(conf);

    appName = cliParser.getOptionValue("appname", appName);
    amPriority = Integer.parseInt(cliParser.getOptionValue("priority", "0"));
    amQueue = cliParser.getOptionValue("queue", "default");
    amMemory = Integer.parseInt(cliParser.getOptionValue("master_memory", "10"));
    amVCores = Integer.parseInt(cliParser.getOptionValue("master_vcores", "1"));
    serviceUrl = cliParser.getOptionValue("service_url", "yarn://localhost?fs=hdfs://localhost:9000");

    try {
        org.apache.commons.httpclient.URI url = new org.apache.commons.httpclient.URI(serviceUrl, false);
        //YARN URL
        String host = url.getHost();
        int port = 8032;
        if (url.getPort() != -1) {
            port = url.getPort();
        }
        ;
        String yarnRM = host + ":" + port;
        LOG.info("Connecting to YARN at: " + yarnRM);
        conf.set("yarn.resourcemanager.address", yarnRM);

        //Hadoop FS/HDFS URL
        String query = url.getQuery();
        if (query.startsWith("fs=")) {
            dfsUrl = query.substring(3, query.length());
            LOG.info("Connect to Hadoop FS: " + dfsUrl);
            conf.set("fs.defaultFS", dfsUrl);
        }

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

    if (amMemory < 0) {
        throw new IllegalArgumentException(
                "Invalid memory specified for application master, exiting." + " Specified memory=" + amMemory);
    }
    if (amVCores < 0) {
        throw new IllegalArgumentException("Invalid virtual cores specified for application master, exiting."
                + " Specified virtual cores=" + amVCores);
    }

    if (!cliParser.hasOption("jar")) {
        throw new IllegalArgumentException("No jar file specified for application master");
    }

    appMasterJar = cliParser.getOptionValue("jar");

    if (!cliParser.hasOption("shell_command") && !cliParser.hasOption("shell_script")) {
        throw new IllegalArgumentException(
                "No shell command or shell script specified to be executed by application master");
    } else if (cliParser.hasOption("shell_command") && cliParser.hasOption("shell_script")) {
        throw new IllegalArgumentException(
                "Can not specify shell_command option " + "and shell_script option at the same time");
    } else if (cliParser.hasOption("shell_command")) {
        shellCommand = cliParser.getOptionValue("shell_command");
    } else {
        shellScriptPath = cliParser.getOptionValue("shell_script");
    }
    if (cliParser.hasOption("shell_args")) {
        shellArgs = cliParser.getOptionValues("shell_args");
    }
    if (cliParser.hasOption("shell_env")) {
        String envs[] = cliParser.getOptionValues("shell_env");
        for (String env : envs) {
            env = env.trim();
            int index = env.indexOf('=');
            if (index == -1) {
                shellEnv.put(env, "");
                continue;
            }
            String key = env.substring(0, index);
            String val = "";
            if (index < (env.length() - 1)) {
                val = env.substring(index + 1);
            }
            shellEnv.put(key, val);
        }
    }
    shellCmdPriority = Integer.parseInt(cliParser.getOptionValue("shell_cmd_priority", "0"));

    containerMemory = Integer.parseInt(cliParser.getOptionValue("container_memory", "10"));
    containerVirtualCores = Integer.parseInt(cliParser.getOptionValue("container_vcores", "1"));
    numContainers = Integer.parseInt(cliParser.getOptionValue("num_containers", "1"));

    if (containerMemory < 0 || containerVirtualCores < 0 || numContainers < 1) {
        throw new IllegalArgumentException("Invalid no. of containers or container memory/vcores specified,"
                + " exiting." + " Specified containerMemory=" + containerMemory + ", containerVirtualCores="
                + containerVirtualCores + ", numContainer=" + numContainers);
    }

    clientTimeout = Integer.parseInt(cliParser.getOptionValue("timeout", "600000"));

    log4jPropFile = cliParser.getOptionValue("log_properties", "");

    return true;
}

From source file:de.rrze.idmone.utils.jpwgen.PwGenerator.java

/**
 * This method parses the command line options, initializes the needed
 * objects and generates the required number of passwords by calling
 * <em>generatePassword()</em>. When not used as a stand-alone program this
 * method is to be preferred instead of the main(String[]).
 * //ww  w.  j av a 2s. co  m
 * @param args
 *            the arguments used to initialize the generation process
 * @return a list of passwords or <em>null</em> if no suitable passwords
 *         could be generated.
 */

public static synchronized List<String> process(String[] args) {
    int passwordFlags = initDefaultFlags();

    // The length of the password to be generated
    int passwordLength = DEFAULT_PASSWORD_LENGTH;

    int numberOfPasswords = DEFAULT_NUMBER_OF_PASSWORDS;

    int maxAttempts = DEFAULT_MAX_ATTEMPTS;

    log(Messages.getString("PwGenerator.PASSWORD_GENERATOR"), //$NON-NLS-1$
            false);

    ArrayList<String> passwords = new ArrayList<String>();
    BasicParser parser = new BasicParser();
    try {
        CommandLine commandLine = parser.parse(options, args);

        parser.parse(options, args);
        if (commandLine.hasOption(CL_HELP)) {
            printUsage();
            log(Messages.getString("PwGenerator.SEPARATOR"), //$NON-NLS-1$
                    false);
            return passwords;
        }

        parser.parse(options, args);
        if (commandLine.hasOption(CL_SR_PROVIDERS)) {
            Set<String> serviceProviders = RandomFactory.getInstance()
                    .getServiceProviderFor(IRandomFactory.TYPE_SECURE_RANDOM);
            log(Messages.getString("PwGenerator.SERVICES_PROVIDERS_FOR") //$NON-NLS-1$
                    + IRandomFactory.TYPE_SECURE_RANDOM + Messages.getString("PwGenerator.NEW_LINE"), false); //$NON-NLS-1$
            for (Iterator<String> iter = serviceProviders.iterator(); iter.hasNext();) {
                String element = (String) iter.next();
                log(Messages.getString("PwGenerator.SERVICE_PROVIDERS") + element //$NON-NLS-1$
                        + Messages.getString("PwGenerator.NEW_LINE"), false); //$NON-NLS-1$
                log(Messages.getString("PwGenerator.SEPARATOR"), //$NON-NLS-1$
                        false);
                return passwords;
            }
        }

        parser.parse(options, args);
        if (commandLine.hasOption(CL_PROVIDERS)) {
            log(Messages.getString("PwGenerator.ALL_SEC_PROVIDERS") //$NON-NLS-1$
                    + IRandomFactory.TYPE_SECURE_RANDOM + Messages.getString("PwGenerator.NEW_LINE"), false); //$NON-NLS-1$
            Provider[] serviceProviders = RandomFactory.getInstance().getProviders();
            for (int i = 0; i < serviceProviders.length; i++) {
                log(Messages.getString("PwGenerator.SEPARATOR"), //$NON-NLS-1$
                        false);
                log(Messages.getString("PwGenerator.PROVIDER") + serviceProviders[i].getName() //$NON-NLS-1$
                        + Messages.getString("PwGenerator.NEW_LINE"), //$NON-NLS-1$
                        false);
                Set<Provider.Service> services = serviceProviders[i].getServices();
                log(Messages.getString("PwGenerator.SERVICES") + Messages.getString("PwGenerator.NEW_LINE"), //$NON-NLS-1$//$NON-NLS-2$
                        false);
                log(services.toString(), false);
                log(Messages.getString("PwGenerator.SEPARATOR"), //$NON-NLS-1$
                        false);
            }
            log(Messages.getString("PwGenerator.SEPARATOR"), //$NON-NLS-1$
                    false);
            return passwords;
        }

        if (commandLine.hasOption(CL_NUMBER_PASSWORD)) {
            String sNumberOfPasswords = commandLine.getOptionValue(CL_NUMBER_PASSWORD);
            if (sNumberOfPasswords != null)
                numberOfPasswords = Integer.parseInt(sNumberOfPasswords);
            log(Messages.getString("PwGenerator.NUM_PASSWORDS") + numberOfPasswords, false); //$NON-NLS-1$
        }

        commandLine = parser.parse(options, args);
        if (commandLine.hasOption(CL_PASSWORD_LENGTH)) {
            String sPasswordLength = commandLine.getOptionValue(CL_PASSWORD_LENGTH);
            if (sPasswordLength != null)
                passwordLength = Integer.parseInt(sPasswordLength);
            log(Messages.getString("PwGenerator.PASSWORD_LENGTH") + passwordLength, false); //$NON-NLS-1$
        }

        parser.parse(options, args);
        if (commandLine.hasOption(CL_COLUMN)) {
            doColumns = true;
            log(Messages.getString("PwGenerator.COLUMNS_ENABLED"), false); //$NON-NLS-1$
        }

        parser.parse(options, args);
        if (commandLine.hasOption(CL_TERM_WIDTH)) {
            String sTermWidth = commandLine.getOptionValue(CL_TERM_WIDTH);
            if (sTermWidth != null)
                termWidth = Integer.parseInt(sTermWidth);
            log(Messages.getString("PwGenerator.TERMINAL_LENGTH") + termWidth, false); //$NON-NLS-1$
        }

        Random random = null;
        parser.parse(options, args);
        if (commandLine.hasOption(CL_RANDOM)) {
            random = RandomFactory.getInstance().getRandom();
            log(Messages.getString("PwGenerator.NORMAL_RANDOM"), false); //$NON-NLS-1$
        } else {
            try {
                random = RandomFactory.getInstance().getSecureRandom();
            } catch (NoSuchAlgorithmException e) {
                e.printStackTrace();
                random = RandomFactory.getInstance().getRandom();
            } catch (NoSuchProviderException e) {
                e.printStackTrace();
                random = RandomFactory.getInstance().getRandom();
            }
        }

        parser.parse(options, args);
        if (commandLine.hasOption(CL_SR_ALGORITHM)) {
            String[] data = commandLine.getOptionValues(CL_SR_ALGORITHM);
            if (data.length == 2) {
                try {
                    random = RandomFactory.getInstance().getSecureRandom(data[0], data[1]);

                    log(Messages.getString("PwGenerator.SEC_ALG") + data[0] //$NON-NLS-1$
                            + Messages.getString("PwGenerator.PROV") + data[1] //$NON-NLS-1$
                            + Messages.getString("PwGenerator.DOR"), false); //$NON-NLS-1$
                } catch (NoSuchAlgorithmException e) {
                    log(Messages.getString("PwGenerator.ERROR") + e.getMessage() //$NON-NLS-1$
                            + Messages.getString("PwGenerator.NEW_LINE"), true); //$NON-NLS-1$
                    log(Messages.getString("PwGenerator.DEFAUL_RANDOM"), true); //$NON-NLS-1$
                } catch (NoSuchProviderException e) {
                    log(Messages.getString("PwGenerator.ERROR") + e.getMessage() //$NON-NLS-1$
                            + Messages.getString("PwGenerator.NEW_LINE"), true); //$NON-NLS-1$
                    log(Messages.getString("PwGenerator.DEFAUL_RANDOM"), true); //$NON-NLS-1$
                }
            }
        }

        if (commandLine.hasOption(CL_NUMERALS)) {
            passwordFlags |= PW_DIGITS;
            log(Messages.getString("PwGenerator.DIGITS_ON"), false); //$NON-NLS-1$
        }

        if (commandLine.hasOption(CL_NO_NUMERALS)) {
            passwordFlags &= ~PW_DIGITS;
            log(Messages.getString("PwGenerator.DIGITS_OFF"), false); //$NON-NLS-1$
        }

        if (commandLine.hasOption(CL_CAPITALIZE)) {
            passwordFlags |= PW_UPPERS;
            log(Messages.getString("PwGenerator.UPPERCASE_ON"), false); //$NON-NLS-1$
        }

        if (commandLine.hasOption(CL_NO_CAPITALIZE)) {
            passwordFlags &= ~PW_UPPERS;
            log(Messages.getString("PwGenerator.UPPERCASE_OFF"), false); //$NON-NLS-1$
        }

        if (commandLine.hasOption(CL_AMBIGOUS)) {
            passwordFlags |= PW_AMBIGUOUS;
            log(Messages.getString("PwGenerator.AMBIGOUS_ON"), false); //$NON-NLS-1$
        }

        if (commandLine.hasOption(CL_NO_AMBIGOUS)) {
            passwordFlags &= ~PW_AMBIGUOUS;
            log(Messages.getString("PwGenerator.AMBIGOUS_OFF"), false); //$NON-NLS-1$
        }

        if (commandLine.hasOption(CL_SYMBOLS)) {
            passwordFlags |= PW_SYMBOLS;
            log(Messages.getString("PwGenerator.SYMBOLS_ON"), false); //$NON-NLS-1$
        }

        if (commandLine.hasOption(CL_SYMBOLS_REDUCED)) {
            passwordFlags |= PW_SYMBOLS_REDUCED;
            log(Messages.getString("PwGenerator.SYMBOLS_REDUCED_ON"), false); //$NON-NLS-1$
        }

        if (commandLine.hasOption(CL_NO_SYMBOLS)) {
            passwordFlags &= ~PW_SYMBOLS;
            passwordFlags &= ~PW_SYMBOLS_REDUCED;
            log(Messages.getString("PwGenerator.SYMBOLS_OFF"), false); //$NON-NLS-1$
        }

        if (commandLine.hasOption(CL_MAX_ATTEMPTS)) {
            String sMaxAttempts = commandLine.getOptionValue(CL_MAX_ATTEMPTS);
            if (sMaxAttempts != null)
                maxAttempts = Integer.parseInt(sMaxAttempts);
            log(Messages.getString("PwGenerator.MAX_ATTEMPTS") + maxAttempts, false); //$NON-NLS-1$
        }

        if (commandLine.hasOption(CL_REGEX_STARTS_NO_SMALL_LETTER))
            passwordFlags |= REGEX_STARTS_NO_SMALL_LETTER_FLAG;

        if (commandLine.hasOption(CL_REGEX_ENDS_NO_SMALL_LETTER))
            passwordFlags |= REGEX_STARTS_NO_SMALL_LETTER_FLAG;

        if (commandLine.hasOption(CL_REGEX_STARTS_NO_UPPER_LETTER))
            passwordFlags |= REGEX_STARTS_NO_UPPER_LETTER_FLAG;

        if (commandLine.hasOption(CL_REGEX_ENDS_NO_UPPER_LETTER))
            passwordFlags |= REGEX_ENDS_NO_UPPER_LETTER_FLAG;

        if (commandLine.hasOption(CL_REGEX_ENDS_NO_DIGIT))
            passwordFlags |= REGEX_ENDS_NO_DIGIT_FLAG;

        if (commandLine.hasOption(CL_REGEX_STARTS_NO_DIGIT))
            passwordFlags |= REGEX_STARTS_NO_DIGIT_FLAG;

        if (commandLine.hasOption(CL_REGEX_STARTS_NO_SYMBOL))
            passwordFlags |= REGEX_STARTS_NO_SYMBOL_FLAG;

        if (commandLine.hasOption(CL_REGEX_ENDS_NO_SYMBOL))
            passwordFlags |= REGEX_ENDS_NO_SYMBOL_FLAG;

        if (commandLine.hasOption(CL_REGEX_ONLY_1_CAPITAL))
            passwordFlags |= REGEX_ONLY_1_CAPITAL_FLAG;

        if (commandLine.hasOption(CL_REGEX_ONLY_1_SYMBOL))
            passwordFlags |= REGEX_ONLY_1_SYMBOL_FLAG;

        if (commandLine.hasOption(CL_REGEX_AT_LEAST_2_SYMBOLS))
            passwordFlags |= REGEX_AT_LEAST_2_SYMBOLS_FLAG;

        if (commandLine.hasOption(CL_REGEX_ONLY_1_DIGIT))
            passwordFlags |= REGEX_ONLY_1_DIGIT_FLAG;

        if (commandLine.hasOption(CL_REGEX_AT_LEAST_2_DIGITS))
            passwordFlags |= REGEX_AT_LEAST_2_DIGITS_FLAG;
        // -------------------------------------------------------------------

        log(Messages.getString("PwGenerator.GENRIC_FLAGS"), //$NON-NLS-1$
                false);

        int res = passwordFlags & PW_DIGITS;
        log(Messages.getString("PwGenerator.DIGITS") + (res != 0), false); //$NON-NLS-1$
        res = passwordFlags & PW_AMBIGUOUS;
        log(Messages.getString("PwGenerator.AMBIGOUS") + (res != 0), false); //$NON-NLS-1$
        res = passwordFlags & PW_SYMBOLS;
        log(Messages.getString("PwGenerator.SYMBOLS") + (res != 0), false); //$NON-NLS-1$
        res = passwordFlags & PW_SYMBOLS_REDUCED;
        log(Messages.getString("PwGenerator.SYMBOLS_REDUCED") + (res != 0), false); //$NON-NLS-1$
        res = passwordFlags & PW_UPPERS;
        log(Messages.getString("PwGenerator.UPPERS") + (res != 0), false); //$NON-NLS-1$
        log(Messages.getString("PwGenerator.SEPARATOR"), //$NON-NLS-1$
                false);

        log(Messages.getString("PwGenerator.GENERATING") + numberOfPasswords //$NON-NLS-1$
                + Messages.getString("PwGenerator.PW_LENGTH") //$NON-NLS-1$
                + passwordLength, false);
        log(Messages.getString("PwGenerator.PW"), //$NON-NLS-1$
                false);

        int i;
        for (i = 0; i < numberOfPasswords; i++) {
            String password = generatePassword(passwordLength, passwordFlags, maxAttempts, random);
            if (password != null)
                passwords.add(password);
        }
    } catch (ParseException e) {
        log(Messages.getString("PwGenerator.PARAM_ERROR") + e.getLocalizedMessage(), true); //$NON-NLS-1$
        printUsage();
    } catch (NumberFormatException e) {
        log(Messages.getString("PwGenerator.NUM_FORM_ERROR") + e.getLocalizedMessage(), true); //$NON-NLS-1$
        printUsage();
    }

    return passwords;
}

From source file:com.hp.mqm.clt.CliParser.java

public Settings parse(String[] args) {
    Settings settings = new Settings();
    CommandLineParser parser = new DefaultParser();
    try {//from   w ww .  j a  v a  2  s  .c om
        CommandLine cmd = parser.parse(options, args);

        if (cmd.hasOption("h")) {
            printHelp();
            System.exit(ReturnCode.SUCCESS.getReturnCode());
        }

        if (cmd.hasOption("v")) {
            printVersion();
            System.exit(ReturnCode.SUCCESS.getReturnCode());
        }

        if (!areCmdArgsValid(cmd)) {
            printHelp();
            System.exit(ReturnCode.FAILURE.getReturnCode());
        }

        if (!addInputFilesToSettings(cmd, settings)) {
            printHelp();
            System.exit(ReturnCode.FAILURE.getReturnCode());
        }

        // load config
        String filename = null;
        if (cmd.hasOption("c")) {
            filename = cmd.getOptionValue("c");
        }
        try {
            settings.load(filename);
        } catch (NumberFormatException e) {
            System.out.println("Can not convert string from properties file to integer: " + e.getMessage());
            System.exit(ReturnCode.FAILURE.getReturnCode());
        } catch (IllegalArgumentException e) {
            // Inform user that loading was not successful
            // Configuration must be specified in arguments in this case
            System.out.println(e.getMessage());
        } catch (IOException e) {
            System.out.println("Can not read from properties file: " + filename);
            System.exit(ReturnCode.FAILURE.getReturnCode());
        }

        if (cmd.hasOption("i")) {
            settings.setInternal(true);
        }

        if (cmd.hasOption("e")) {
            settings.setSkipErrors(true);
        }

        if (cmd.hasOption("o")) {
            settings.setOutputFile(cmd.getOptionValue("o"));
        }

        if (cmd.hasOption("s")) {
            settings.setServer(cmd.getOptionValue("s"));
        }

        if (cmd.hasOption("d")) {
            settings.setSharedspace(((Long) cmd.getParsedOptionValue("d")).intValue());
        }

        if (cmd.hasOption("w")) {
            settings.setWorkspace(((Long) cmd.getParsedOptionValue("w")).intValue());
        }

        if (cmd.hasOption("u")) {
            settings.setUser(cmd.getOptionValue("u"));
        }

        if (settings.getOutputFile() == null) {
            if (cmd.hasOption("p")) {
                settings.setPassword(cmd.getOptionValue("p"));
            } else if (cmd.hasOption("password-file")) {
                try {
                    settings.setPassword(
                            FileUtils.readFileToString(new File(cmd.getOptionValue("password-file"))));
                } catch (IOException e) {
                    System.out
                            .println("Can not read the password file: " + cmd.getOptionValue("password-file"));
                    System.exit(ReturnCode.FAILURE.getReturnCode());
                }
            } else {
                System.out.println("Please enter your password if it's required and hit enter: ");
                settings.setPassword(new String(System.console().readPassword()));
            }
        }

        if (cmd.hasOption("proxy-host")) {
            settings.setProxyHost(cmd.getOptionValue("proxy-host"));
        }

        if (cmd.hasOption("proxy-port")) {
            settings.setProxyPort(((Long) cmd.getParsedOptionValue("proxy-port")).intValue());
        }

        if (cmd.hasOption("proxy-user")) {
            settings.setProxyUser(cmd.getOptionValue("proxy-user"));
        }

        if (settings.getOutputFile() == null && StringUtils.isNotEmpty(settings.getProxyUser())) {
            if (cmd.hasOption("proxy-password")) {
                settings.setProxyPassword(cmd.getOptionValue("proxy-password"));
            } else if (cmd.hasOption("proxy-password-file")) {
                try {
                    settings.setProxyPassword(
                            FileUtils.readFileToString(new File(cmd.getOptionValue("proxy-password-file"))));
                } catch (IOException e) {
                    System.out.println(
                            "Can not read the password file: " + cmd.getOptionValue("proxy-password-file"));
                    System.exit(ReturnCode.FAILURE.getReturnCode());
                }
            } else {
                System.out.println("Please enter your proxy password if it's required and hit enter: ");
                settings.setProxyPassword(new String(System.console().readPassword()));
            }
        }

        if (cmd.hasOption("check-result")) {
            settings.setCheckResult(true);
        }

        if (cmd.hasOption("check-result-timeout")) {
            settings.setCheckResultTimeout(
                    ((Long) cmd.getParsedOptionValue("check-status-timeout")).intValue());
        }

        if (cmd.hasOption("t")) {
            settings.setTags(Arrays.asList(cmd.getOptionValues("t")));
        }

        if (cmd.hasOption("f")) {
            settings.setFields(Arrays.asList(cmd.getOptionValues("f")));
        }

        if (cmd.hasOption("r")) {
            settings.setRelease(((Long) cmd.getParsedOptionValue("r")).intValue());
        }

        if (cmd.hasOption("started")) {
            settings.setStarted((Long) cmd.getParsedOptionValue("started"));
        }

        if (cmd.hasOption("a")) {
            settings.setProductAreas(cmd.getOptionValues("a"));
        }

        if (cmd.hasOption("b")) {
            settings.setBacklogItems(cmd.getOptionValues("b"));
        }

        if (!areSettingsValid(settings)) {
            System.exit(ReturnCode.FAILURE.getReturnCode());
        }

    } catch (ParseException e) {
        printHelp();
        System.exit(ReturnCode.FAILURE.getReturnCode());
    }
    return settings;
}