All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.jmeterplugins.repository.PluginManagerCMD Maven / Gradle / Ivy

package org.jmeterplugins.repository;

import kg.apc.cmdtools.AbstractCMDTool;
import org.apache.jorphan.logging.LoggingManager;
import org.apache.log.Logger;
import org.apache.log.Priority;

import java.io.IOException;
import java.io.PrintStream;
import java.util.HashMap;
import java.util.ListIterator;
import java.util.Map;

public class PluginManagerCMD extends AbstractCMDTool implements GenericCallback {
    private static final Logger log = LoggingManager.getLoggerForClass();

    @Override
    protected int processParams(ListIterator listIterator) throws UnsupportedOperationException, IllegalArgumentException {
        LoggingManager.setPriority(Priority.INFO);
        if (!listIterator.hasNext()) {
            throw new IllegalArgumentException("Command parameter is missing");
        }

        String command = listIterator.next().toString();
        log.info("Command is: " + command);
        try {
            switch (command) {
                case "status":
                    System.out.println(PluginManager.getAllPluginsStatus());
                    break;
                case "install":
                    process(listIterator, true);
                    break;
                case "uninstall":
                    process(listIterator, false);
                    break;
                default:
                    throw new UnsupportedOperationException("Wrong command: " + command);
            }
        } catch (IOException e) {
            throw new RuntimeException("Failed to perform cmdline operation: " + e.getMessage(), e);
        }

        return 0;
    }

    protected void process(ListIterator listIterator, boolean install) throws IOException {
        if (!listIterator.hasNext()) {
            throw new IllegalArgumentException("Plugins list parameter is missing");
        }

        Map params = parseParams(listIterator.next().toString());
        PluginManager mgr = new PluginManager();
        mgr.load();
        mgr.setDoRestart(false);

        for (Map.Entry pluginSpec : params.entrySet()) {
            Plugin plugin = mgr.getPluginByID(pluginSpec.getKey());
            if (pluginSpec.getValue() != null) {
                plugin.setCandidateVersion(pluginSpec.getValue());
            }
            mgr.toggleInstalled(plugin, install);
        }
        mgr.applyChanges(this);
    }

    private Map parseParams(String paramStr) {
        log.info("Params line is: " + paramStr);
        HashMap res = new HashMap<>();
        for (String part : paramStr.split(",")) {
            if (part.contains("=")) {
                String[] pieces = part.split("=");
                res.put(pieces[0].trim(), pieces[1].trim());
            } else {
                res.put(part.trim(), null);
            }
        }
        return res;
    }

    @Override
    protected void showHelp(PrintStream printStream) {
        printStream.println("Options for tool 'PluginManagerCMD':   "
                + " where  is one of: status, install, uninstall");
    }

    @Override
    public void notify(String s) {
        if (s.endsWith("%")) {
            log.debug(s);
        } else {
            log.info(s);
        }
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy