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

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

Introduction

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

Prototype

public final String getArgument() 

Source Link

Document

Retrieve argument to option if it takes arguments.

Usage

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

/**
 * Takes the command line arguments and uses them to determine how to
 * startup JMeter./*w  ww  . j a va 2s.  c om*/
 * 
 * Called reflectively by {@link NewDriver#main(String[])}
 * @param args The arguments for JMeter
 */
public void start(String[] args) {
    CLArgsParser parser = new CLArgsParser(args, options);
    String error = parser.getErrorString();
    if (error == null) {// Check option combinations
        boolean gui = parser.getArgumentById(NONGUI_OPT) == null;
        boolean nonGuiOnly = parser.getArgumentById(REMOTE_OPT) != null
                || parser.getArgumentById(REMOTE_OPT_PARAM) != null
                || parser.getArgumentById(REMOTE_STOP) != null;
        if (gui && nonGuiOnly) {
            error = "-r and -R and -X are only valid in non-GUI mode";
        }
    }
    if (null != error) {
        System.err.println("Error: " + error);
        System.out.println("Usage");
        System.out.println(CLUtil.describeOptions(options).toString());
        // repeat the error so no need to scroll back past the usage to see it
        System.out.println("Error: " + error);
        return;
    }
    try {
        initializeProperties(parser); // Also initialises JMeter logging
        /*
         * The following is needed for HTTPClient.
         * (originally tried doing this in HTTPSampler2,
         * but it appears that it was done too late when running in GUI mode)
         * Set the commons logging default to Avalon Logkit, if not already defined
         */
        if (System.getProperty("org.apache.commons.logging.Log") == null) { // $NON-NLS-1$
            System.setProperty("org.apache.commons.logging.Log" // $NON-NLS-1$
                    , "org.apache.commons.logging.impl.LogKitLogger"); // $NON-NLS-1$
        }

        Thread.setDefaultUncaughtExceptionHandler(new UncaughtExceptionHandler() {
            @Override
            public void uncaughtException(Thread t, Throwable e) {
                if (!(e instanceof ThreadDeath)) {
                    log.error("Uncaught exception: ", e);
                    System.err.println("Uncaught Exception " + e + ". See log file for details.");
                }
            }
        });

        log.info(JMeterUtils.getJMeterCopyright());
        log.info("Version " + JMeterUtils.getJMeterVersion());
        logProperty("java.version"); //$NON-NLS-1$
        logProperty("java.vm.name"); //$NON-NLS-1$
        logProperty("os.name"); //$NON-NLS-1$
        logProperty("os.arch"); //$NON-NLS-1$
        logProperty("os.version"); //$NON-NLS-1$
        logProperty("file.encoding"); // $NON-NLS-1$
        log.info("Max memory     =" + Runtime.getRuntime().maxMemory());
        log.info("Available Processors =" + Runtime.getRuntime().availableProcessors());
        log.info("Default Locale=" + Locale.getDefault().getDisplayName());
        log.info("JMeter  Locale=" + JMeterUtils.getLocale().getDisplayName());
        log.info("JMeterHome=" + JMeterUtils.getJMeterHome());
        logProperty("user.dir", "  ="); //$NON-NLS-1$
        log.info("PWD       =" + new File(".").getCanonicalPath());//$NON-NLS-1$
        log.info("IP: " + JMeterUtils.getLocalHostIP() + " Name: " + JMeterUtils.getLocalHostName()
                + " FullName: " + JMeterUtils.getLocalHostFullName());
        setProxy(parser);

        updateClassLoader();
        if (log.isDebugEnabled()) {
            String jcp = System.getProperty("java.class.path");// $NON-NLS-1$
            String[] bits = jcp.split(File.pathSeparator);
            log.debug("ClassPath");
            for (String bit : bits) {
                log.debug(bit);
            }
        }

        // Set some (hopefully!) useful properties
        long now = System.currentTimeMillis();
        JMeterUtils.setProperty("START.MS", Long.toString(now));// $NON-NLS-1$
        Date today = new Date(now); // so it agrees with above
        // TODO perhaps should share code with __time() function for this...
        JMeterUtils.setProperty("START.YMD", new SimpleDateFormat("yyyyMMdd").format(today));// $NON-NLS-1$ $NON-NLS-2$
        JMeterUtils.setProperty("START.HMS", new SimpleDateFormat("HHmmss").format(today));// $NON-NLS-1$ $NON-NLS-2$

        if (parser.getArgumentById(VERSION_OPT) != null) {
            displayAsciiArt();
        } else if (parser.getArgumentById(HELP_OPT) != null) {
            displayAsciiArt();
            System.out.println(JMeterUtils.getResourceFileAsText("org/apache/jmeter/help.txt"));// $NON-NLS-1$
        } else if (parser.getArgumentById(OPTIONS_OPT) != null) {
            displayAsciiArt();
            System.out.println(CLUtil.describeOptions(options).toString());
        } else if (parser.getArgumentById(SERVER_OPT) != null) {
            // Start the server
            try {
                RemoteJMeterEngineImpl.startServer(JMeterUtils.getPropDefault("server_port", 0)); // $NON-NLS-1$
            } catch (Exception ex) {
                System.err.println("Server failed to start: " + ex);
                log.error("Giving up, as server failed with:", ex);
                throw ex;
            }
            startOptionalServers();
        } else {
            String testFile = null;
            CLOption testFileOpt = parser.getArgumentById(TESTFILE_OPT);
            if (testFileOpt != null) {
                testFile = testFileOpt.getArgument();
                if (USE_LAST_JMX.equals(testFile)) {
                    testFile = LoadRecentProject.getRecentFile(0);// most recent
                }
            }
            CLOption testReportOpt = parser.getArgumentById(REPORT_GENERATING_OPT);
            if (testReportOpt != null) { // generate report from existing file
                String reportFile = testReportOpt.getArgument();
                extractAndSetReportOutputFolder(parser);
                ReportGenerator generator = new ReportGenerator(reportFile, null);
                generator.generate();
            } else if (parser.getArgumentById(NONGUI_OPT) == null) { // not non-GUI => GUI
                startGui(testFile);
                startOptionalServers();
            } else { // NON-GUI must be true
                extractAndSetReportOutputFolder(parser);

                CLOption rem = parser.getArgumentById(REMOTE_OPT_PARAM);
                if (rem == null) {
                    rem = parser.getArgumentById(REMOTE_OPT);
                }
                CLOption jtl = parser.getArgumentById(LOGFILE_OPT);
                String jtlFile = null;
                if (jtl != null) {
                    jtlFile = processLAST(jtl.getArgument(), ".jtl"); // $NON-NLS-1$
                }
                CLOption reportAtEndOpt = parser.getArgumentById(REPORT_AT_END_OPT);
                if (reportAtEndOpt != null) {
                    if (jtlFile == null) {
                        throw new IllegalUserActionException("Option -" + ((char) REPORT_AT_END_OPT)
                                + " requires -" + ((char) LOGFILE_OPT) + " option");
                    }
                }
                startNonGui(testFile, jtlFile, rem, reportAtEndOpt != null);
                startOptionalServers();
            }
        }
    } catch (IllegalUserActionException e) {
        System.out.println("Incorrect Usage:" + e.getMessage());
        System.out.println(CLUtil.describeOptions(options).toString());
    } catch (Throwable e) {
        log.fatalError("An error occurred: ", e);
        System.out.println("An error occurred: " + e.getMessage());
        System.exit(1); // TODO - could this be return?
    }
}

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

private void startNonGui(String testFile, String logFile, CLOption remoteStart, boolean generateReportDashboard)
        throws IllegalUserActionException, ConfigurationException {
    // add a system property so samplers can check to see if JMeter
    // is running in NonGui mode
    System.setProperty(JMETER_NON_GUI, "true");// $NON-NLS-1$
    JMeter driver = new JMeter();// TODO - why does it create a new instance?
    driver.remoteProps = this.remoteProps;
    driver.remoteStop = this.remoteStop;
    driver.parent = this;
    PluginManager.install(this, false);

    String remoteHostsString = null;
    if (remoteStart != null) {
        remoteHostsString = remoteStart.getArgument();
        if (remoteHostsString == null) {
            remoteHostsString = JMeterUtils.getPropDefault("remote_hosts", //$NON-NLS-1$
                    "127.0.0.1");//$NON-NLS-1$
        }//w  w w  .  j  a  v a2  s.  c o m
    }
    if (testFile == null) {
        throw new IllegalUserActionException("Non-GUI runs require a test plan");
    }
    driver.runNonGui(testFile, logFile, remoteStart != null, remoteHostsString, generateReportDashboard);
}

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

public void startGui(CLOption testFile) {
    PluginManager.install(this, true);
    ReportTreeModel treeModel = new ReportTreeModel();
    ReportTreeListener treeLis = new ReportTreeListener(treeModel);
    treeLis.setActionHandler(ReportActionRouter.getInstance());
    ReportGuiPackage.getInstance(treeLis, treeModel);
    org.apache.jmeter.gui.ReportMainFrame main = new org.apache.jmeter.gui.ReportMainFrame(treeModel, treeLis);
    ComponentUtil.centerComponentInWindow(main, 80);
    main.setVisible(true);//www . j a v a2 s  . co  m

    ReportActionRouter.getInstance().actionPerformed(new ActionEvent(main, 1, ReportCheckDirty.ADD_ALL));
    if (testFile != null) {
        FileInputStream reader = null;
        try {
            File f = new File(testFile.getArgument());
            log.info("Loading file: " + f);
            reader = new FileInputStream(f);
            HashTree tree = SaveService.loadTree(reader);

            ReportGuiPackage.getInstance().setReportPlanFile(f.getAbsolutePath());

            new ReportLoad().insertLoadedTree(1, tree);
        } catch (Exception e) {
            log.error("Failure loading test file", e);
            JMeterUtils.reportErrorToUser(e.toString());
        } finally {
            JOrphanUtils.closeQuietly(reader);
        }
    }
}