Example usage for org.apache.commons.cli.avalon CLOption TEXT_ARGUMENT

List of usage examples for org.apache.commons.cli.avalon CLOption TEXT_ARGUMENT

Introduction

In this page you can find the example usage for org.apache.commons.cli.avalon CLOption TEXT_ARGUMENT.

Prototype

int TEXT_ARGUMENT

To view the source code for org.apache.commons.cli.avalon CLOption TEXT_ARGUMENT.

Click Source Link

Document

Value of CLOptionDescriptor#getId when the option is a text argument.

Usage

From source file:org.apache.jmeter.JMeter.java

private void initializeProperties(CLArgsParser parser) {
    if (parser.getArgumentById(PROPFILE_OPT) != null) {
        JMeterUtils.loadJMeterProperties(parser.getArgumentById(PROPFILE_OPT).getArgument());
    } else {/*ww  w. j a v  a 2 s .c  o m*/
        JMeterUtils.loadJMeterProperties(NewDriver.getJMeterDir() + File.separator + "bin" + File.separator // $NON-NLS-1$
                + "jmeter.properties");// $NON-NLS-1$
    }

    if (parser.getArgumentById(JMLOGFILE_OPT) != null) {
        String jmlogfile = parser.getArgumentById(JMLOGFILE_OPT).getArgument();
        jmlogfile = processLAST(jmlogfile, ".log");// $NON-NLS-1$
        JMeterUtils.setProperty(LoggingManager.LOG_FILE, jmlogfile);
    }

    JMeterUtils.initLogging();
    JMeterUtils.initLocale();
    // Bug 33845 - allow direct override of Home dir
    if (parser.getArgumentById(JMETER_HOME_OPT) == null) {
        JMeterUtils.setJMeterHome(NewDriver.getJMeterDir());
    } else {
        JMeterUtils.setJMeterHome(parser.getArgumentById(JMETER_HOME_OPT).getArgument());
    }

    Properties jmeterProps = JMeterUtils.getJMeterProperties();
    remoteProps = new Properties();

    // Add local JMeter properties, if the file is found
    String userProp = JMeterUtils.getPropDefault("user.properties", ""); //$NON-NLS-1$
    if (userProp.length() > 0) { //$NON-NLS-1$
        FileInputStream fis = null;
        try {
            File file = JMeterUtils.findFile(userProp);
            if (file.canRead()) {
                log.info("Loading user properties from: " + file.getCanonicalPath());
                fis = new FileInputStream(file);
                Properties tmp = new Properties();
                tmp.load(fis);
                jmeterProps.putAll(tmp);
                LoggingManager.setLoggingLevels(tmp);//Do what would be done earlier
            }
        } catch (IOException e) {
            log.warn("Error loading user property file: " + userProp, e);
        } finally {
            JOrphanUtils.closeQuietly(fis);
        }
    }

    // Add local system properties, if the file is found
    String sysProp = JMeterUtils.getPropDefault("system.properties", ""); //$NON-NLS-1$
    if (sysProp.length() > 0) {
        FileInputStream fis = null;
        try {
            File file = JMeterUtils.findFile(sysProp);
            if (file.canRead()) {
                log.info("Loading system properties from: " + file.getCanonicalPath());
                fis = new FileInputStream(file);
                System.getProperties().load(fis);
            }
        } catch (IOException e) {
            log.warn("Error loading system property file: " + sysProp, e);
        } finally {
            JOrphanUtils.closeQuietly(fis);
        }
    }

    // Process command line property definitions
    // These can potentially occur multiple times

    List<CLOption> clOptions = parser.getArguments();
    for (CLOption option : clOptions) {
        String name = option.getArgument(0);
        String value = option.getArgument(1);
        FileInputStream fis = null;

        switch (option.getDescriptor().getId()) {

        // Should not have any text arguments
        case CLOption.TEXT_ARGUMENT:
            throw new IllegalArgumentException("Unknown arg: " + option.getArgument());

        case PROPFILE2_OPT: // Bug 33920 - allow multiple props
            try {
                fis = new FileInputStream(new File(name));
                Properties tmp = new Properties();
                tmp.load(fis);
                jmeterProps.putAll(tmp);
                LoggingManager.setLoggingLevels(tmp);//Do what would be done earlier
            } catch (FileNotFoundException e) {
                log.warn("Can't find additional property file: " + name, e);
            } catch (IOException e) {
                log.warn("Error loading additional property file: " + name, e);
            } finally {
                JOrphanUtils.closeQuietly(fis);
            }
            break;
        case SYSTEM_PROPFILE:
            log.info("Setting System properties from file: " + name);
            try {
                fis = new FileInputStream(new File(name));
                System.getProperties().load(fis);
            } catch (IOException e) {
                log.warn("Cannot find system property file " + e.getLocalizedMessage());
            } finally {
                JOrphanUtils.closeQuietly(fis);
            }
            break;
        case SYSTEM_PROPERTY:
            if (value.length() > 0) { // Set it
                log.info("Setting System property: " + name + "=" + value);
                System.getProperties().setProperty(name, value);
            } else { // Reset it
                log.warn("Removing System property: " + name);
                System.getProperties().remove(name);
            }
            break;
        case JMETER_PROPERTY:
            if (value.length() > 0) { // Set it
                log.info("Setting JMeter property: " + name + "=" + value);
                jmeterProps.setProperty(name, value);
            } else { // Reset it
                log.warn("Removing JMeter property: " + name);
                jmeterProps.remove(name);
            }
            break;
        case JMETER_GLOBAL_PROP:
            if (value.length() > 0) { // Set it
                log.info("Setting Global property: " + name + "=" + value);
                remoteProps.setProperty(name, value);
            } else {
                File propFile = new File(name);
                if (propFile.canRead()) {
                    log.info("Setting Global properties from the file " + name);
                    try {
                        fis = new FileInputStream(propFile);
                        remoteProps.load(fis);
                    } catch (FileNotFoundException e) {
                        log.warn("Could not find properties file: " + e.getLocalizedMessage());
                    } catch (IOException e) {
                        log.warn("Could not load properties file: " + e.getLocalizedMessage());
                    } finally {
                        JOrphanUtils.closeQuietly(fis);
                    }
                }
            }
            break;
        case LOGLEVEL:
            if (value.length() > 0) { // Set category
                log.info("LogLevel: " + name + "=" + value);
                LoggingManager.setPriority(value, name);
            } else { // Set root level
                log.warn("LogLevel: " + name);
                LoggingManager.setPriority(name);
            }
            break;
        case REMOTE_STOP:
            remoteStop = true;
            break;
        default:
            // ignored
            break;
        }
    }

    String sampleVariables = (String) jmeterProps.get(SampleEvent.SAMPLE_VARIABLES);
    if (sampleVariables != null) {
        remoteProps.put(SampleEvent.SAMPLE_VARIABLES, sampleVariables);
    }
    jmeterProps.put("jmeter.version", JMeterUtils.getJMeterVersion());
}