view launcher/src/main/java/com/redhat/thermostat/launcher/internal/PluginInfoSource.java @ 2593:767b2627c92d

Implement cli/shell command-group support Reviewed-by: jerboaa Review-thread: http://icedtea.classpath.org/pipermail/thermostat/2016-November/021748.html Review-thread: http://icedtea.classpath.org/pipermail/thermostat/2016-December/021824.html Review-thread: http://icedtea.classpath.org/pipermail/thermostat/2017-January/021934.html Review-thread: http://icedtea.classpath.org/pipermail/thermostat/2017-February/022139.html
author Andrew Azores <aazores@redhat.com>
date Tue, 14 Feb 2017 11:26:33 -0500
parents e7fd8adb5f9c
children
line wrap: on
line source

/*
 * Copyright 2012-2017 Red Hat, Inc.
 *
 * This file is part of Thermostat.
 *
 * Thermostat is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published
 * by the Free Software Foundation; either version 2, or (at your
 * option) any later version.
 *
 * Thermostat is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with Thermostat; see the file COPYING.  If not see
 * <http://www.gnu.org/licenses/>.
 *
 * Linking this code with other modules is making a combined work
 * based on this code.  Thus, the terms and conditions of the GNU
 * General Public License cover the whole combination.
 *
 * As a special exception, the copyright holders of this code give
 * you permission to link this code with independent modules to
 * produce an executable, regardless of the license terms of these
 * independent modules, and to copy and distribute the resulting
 * executable under terms of your choice, provided that you also
 * meet, for each linked independent module, the terms and conditions
 * of the license of that module.  An independent module is a module
 * which is not derived from or based on this code.  If you modify
 * this code, you may extend this exception to your version of the
 * library, but you are not obligated to do so.  If you do not wish
 * to do so, delete this exception statement from your version.
 */

package com.redhat.thermostat.launcher.internal;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Properties;
import java.util.Map.Entry;
import java.util.Set;
import java.util.logging.Level;
import java.util.logging.Logger;

import com.redhat.thermostat.common.config.experimental.ConfigurationInfoSource;
import com.redhat.thermostat.common.utils.LoggingUtils;
import com.redhat.thermostat.launcher.BundleInformation;
import com.redhat.thermostat.launcher.internal.PluginConfiguration.CommandExtensions;
import com.redhat.thermostat.launcher.internal.PluginConfiguration.CommandGroupMetadata;
import com.redhat.thermostat.launcher.internal.PluginConfiguration.Configurations;
import com.redhat.thermostat.launcher.internal.PluginConfiguration.NewCommand;
import com.redhat.thermostat.launcher.internal.PluginConfiguration.PluginID;
import com.redhat.thermostat.plugin.validator.PluginConfigurationValidatorException;
import com.redhat.thermostat.plugin.validator.ValidationErrorsFormatter;

/**
 * Searches for plugins under <code>$THERMOSTAT_HOME/plugins/</code> and
 * provides information about commands and configurations specified by them.
 * <p>
 * Each plugin is located under
 * <code>$THERMOSTAT_HOME/plugins/$PLUGIN_NAME/</code> and must have a
 * <code>thermostat-plugin.xml</code> file in the main plugin directory.
 *
 * @see PluginConfigurationParser how the thermostat-plugin.xml file is parsed
 */
public class PluginInfoSource implements CommandInfoSource, ConfigurationInfoSource, CommandGroupMetadataSource {

    private static final String PLUGIN_CONFIG_FILE = "thermostat-plugin.xml";

    private static final Logger logger = LoggingUtils.getLogger(PluginInfoSource.class);

    private final UsageStringBuilder usageBuilder;

    private Map<String, BasicCommandInfo> allNewCommands = new HashMap<>();
    private Map<CommandGroupMetadata, Set<String>> pluginMetadataMap = new HashMap<>();
    private Map<String, CommandGroupMetadata> commandGroupMetadataMap = new HashMap<>();
    private Map<String, List<BundleInformation>> additionalBundlesForExistingCommands = new HashMap<>();
    private Map<PluginID, Configurations> allConfigs = new HashMap<>();

    private final File userConfRootdir;
    private final File sysConfRootDir;

    public PluginInfoSource(String internalJarRoot, String systemPluginRootDir, String userPluginRootDir, String sysConfRootDir, String userConfRootDir) {
        this(new File(internalJarRoot), new File(systemPluginRootDir), new File(userPluginRootDir),
                new File(sysConfRootDir), new File(userConfRootDir),
                new PluginConfigurationParser(), new UsageStringBuilder());
    }

    PluginInfoSource(File internalJarRoot, File systemPluginRootDir, File userPluginRootDir,
            File sysConfRootDir, File userConfRootDir,
            PluginConfigurationParser parser, UsageStringBuilder usageBuilder) {
        this.usageBuilder = usageBuilder;
        this.userConfRootdir = userConfRootDir;
        this.sysConfRootDir = sysConfRootDir;

        List<File> pluginDirectories = new ArrayList<>();

        addPluginDirectory(pluginDirectories, systemPluginRootDir);
        addPluginDirectory(pluginDirectories, userPluginRootDir);

        for (File pluginDir : pluginDirectories) {
            try {
                File configurationFile = new File(pluginDir, PLUGIN_CONFIG_FILE);
                PluginConfiguration pluginConfig = parser.parse(configurationFile);
                loadNewAndExtendedCommands(internalJarRoot, pluginDir, pluginConfig);
                processCommandGroupMetadata(pluginConfig, pluginDir);
                if (allConfigs.containsKey(pluginConfig.getPluginID())) {
                    logger.log(Level.WARNING, "Plugin with ID: " + pluginConfig.getPluginID() + " conflicts with a previous plugin's ID and the config file will not be overwritten.");
                } else if (pluginConfig.hasValidID() && pluginConfig.hasConfigurations()) {
                    allConfigs.put(pluginConfig.getPluginID(), pluginConfig.getConfigurations());
                }

            } catch (PluginConfigurationParseException exception) {
                logger.log(Level.WARNING, "unable to parse plugin configuration", exception);

            } catch (PluginConfigurationValidatorException pcve) {
                ValidationErrorsFormatter formatter = new ValidationErrorsFormatter();
                logger.log(Level.INFO, formatter.format(pcve.getAllErrors()));
                logger.log(Level.INFO, "unable to validate " + pcve.getXmlFile().getAbsolutePath() + " file\n");

            } catch (FileNotFoundException exception) {
                logger.log(Level.INFO, "file not found", exception);
            }
        }
        combineCommands();
        validateCommandGroupMetadataSources();
    }

    private void addPluginDirectory(List<File> allPluginDirectories, File aPluginRoot) {
        File[] pluginDirs = aPluginRoot.listFiles();

        if (pluginDirs != null) {
            allPluginDirectories.addAll(Arrays.asList(pluginDirs));
        }
    }

    private void loadNewAndExtendedCommands(File coreJarRoot, File pluginDir,
            PluginConfiguration pluginConfig) {

        for (CommandExtensions extension : pluginConfig.getExtendedCommands()) {
            String commandName = extension.getCommandName();
            List<BundleInformation> bundles = extension.getBundles();
            logger.finest("plugin at " + pluginDir + " needs " +
                    bundles.size() + " bundles for comamnd '" + commandName + "'");

            List<BundleInformation> bundlePaths = additionalBundlesForExistingCommands.get(commandName);
            if (bundlePaths == null) {
                bundlePaths = new LinkedList<>();
            }

            bundlePaths.addAll(bundles);

            additionalBundlesForExistingCommands.put(commandName, bundlePaths);
        }

        for (NewCommand command : pluginConfig.getNewCommands()) {
            String commandName = command.getCommandName();
            logger.finest("plugin at " + pluginDir + " contributes new command '" + commandName + "'");

            if (allNewCommands.containsKey(commandName)) {
                throw new IllegalStateException("multiple plugins are providing the command " + commandName);
            }

            String usage = command.getUsage();
            if (usage == null) {
                usage = usageBuilder.getUsage(commandName, !command.getSubcommands().isEmpty(), command.getOptions(),
                        command.getPositionalArguments().toArray(new String[0]));
            }
            BasicCommandInfo info = new BasicCommandInfo(commandName,
                    command.getSummary(),
                    command.getDescription(),
                    command.getCommandGroups(),
                    usage,
                    command.getOptions(), command.getSubcommands(),
                    command.getEnvironments(),
                    command.getBundles()
            );

            allNewCommands.put(commandName, info);
        }

    }

    private void processCommandGroupMetadata(PluginConfiguration pluginConfiguration, File pluginDir) {
        List<CommandGroupMetadata> commandGroupMetadata = pluginConfiguration.getCommandGroupMetadata();
        for (CommandGroupMetadata metadata : commandGroupMetadata) {
            if (!pluginMetadataMap.containsKey(metadata)) {
                pluginMetadataMap.put(metadata, new HashSet<String>());
            }
            pluginMetadataMap.get(metadata).add(pluginDir.getName());
            String name = metadata.getName();
            if (commandGroupMetadataMap.containsKey(name)) {
                logger.warning("Already found metadata for command group \"" + name + "\"");
                continue;
            }
            commandGroupMetadataMap.put(name, metadata);
        }
    }

    private void validateCommandGroupMetadataSources() {
        for (Map.Entry<CommandGroupMetadata, Set<String>> entry : pluginMetadataMap.entrySet()) {
            CommandGroupMetadata metadata = entry.getKey();
            Set<String> providingPlugins = entry.getValue();
            if (providingPlugins.size() > 1) {
                logger.warning(String.format("Metadata for command group \"%s\" is provided by multiple plugins: %s",
                        metadata.getName(), providingPlugins));
            }
        }
    }

    private void combineCommands() {
        Iterator<Entry<String, List<BundleInformation>>> iter = additionalBundlesForExistingCommands.entrySet().iterator();
        while (iter.hasNext()) {
            Map.Entry<String, List<BundleInformation>> entry = iter.next();
            if (allNewCommands.containsKey(entry.getKey())) {
                BasicCommandInfo old = allNewCommands.get(entry.getKey());
                List<BundleInformation> updatedBundles = new ArrayList<>();
                updatedBundles.addAll(old.getBundles());
                updatedBundles.addAll(entry.getValue());
                BasicCommandInfo updated = new BasicCommandInfo(old.getName(),
                        old.getSummary(),
                        old.getDescription(),
                        old.getCommandGroups(),
                        old.getUsage(),
                        old.getOptions(), old.getSubcommands(),
                        old.getEnvironments(),
                        updatedBundles
                );
                allNewCommands.put(entry.getKey(), updated);
                iter.remove();
            }
        }
    }

    @Override
    public CommandInfo getCommandInfo(String name) throws CommandInfoNotFoundException {
        if (allNewCommands.containsKey(name)) {
            return allNewCommands.get(name);
        }
        List<BundleInformation> bundles = additionalBundlesForExistingCommands.get(name);
        if (bundles != null) {
            return createCommandInfo(name, bundles);
        }
        throw new CommandInfoNotFoundException(name);
    }

    @Override
    public Collection<CommandInfo> getCommandInfos() {
        List<CommandInfo> result = new ArrayList<>();
        result.addAll(allNewCommands.values());
        for (Entry<String, List<BundleInformation>> entry : additionalBundlesForExistingCommands.entrySet()) {
            result.add(createCommandInfo(entry.getKey(), entry.getValue()));
        }
        return result;
    }

    @Override
    public Map<String, CommandGroupMetadata> getCommandGroupMetadata() {
        return new HashMap<>(commandGroupMetadataMap);
    }

    private BasicCommandInfo createCommandInfo(String name, List<BundleInformation> bundles) {
        return new BasicCommandInfo(name, null, null, Collections.<String>emptyList(), null, null, null, null, bundles);
    }

    public Map<String, String> getConfiguration(String pluginID, String fileName) throws IOException {
        Configurations config = this.allConfigs.get(new PluginID(pluginID));
        if (config != null && config.containsFile(fileName)) {
            String filePath = config.getFullFilePath(fileName);
            File file = new File(filePath);
            Map<String, String> confMap = loadConfMap(file);
            return Collections.unmodifiableMap(confMap);
        } else {
            Map<String, String> sysMap = new HashMap<>();
            Map<String, String> userMap = new HashMap<>();

            String sysPath = this.sysConfRootDir + "/" + pluginID + "/" + fileName;
            String userPath = this.userConfRootdir + "/" + pluginID + "/" + fileName;

            File sysFile = new File(sysPath);
            File userFile = new File(userPath);

            if (sysFile.exists()) {
                sysMap = loadConfMap(sysFile);
            }
            if (userFile.exists()) {
                userMap = loadConfMap(userFile);
            }
            return Collections.unmodifiableMap(combineMap(userMap, sysMap));
        }
    }

    private Map<String, String> loadConfMap(File confFile) {
        try (FileInputStream stream = new FileInputStream(confFile)) {
            Properties properties = new Properties();
            properties.load(stream);

            Map<String, String> returnMap = new HashMap<>();
            for (Entry<Object, Object> entry : properties.entrySet()) {
                returnMap.put((String)entry.getKey(), (String)entry.getValue());
            }
            return returnMap;
        } catch (IOException e) {
            logger.warning("Plugin config file: " + confFile.getAbsolutePath() + " could not be parsed. ");
            return Collections.emptyMap();
        }
    }

    private Map<String, String> combineMap(Map<String, String> baseMap, Map<String, String> topMap) {
        // Writes top onto base and returns base.
        for (String key : topMap.keySet()) {
            if (!baseMap.containsKey(key)) {
                baseMap.put(key, topMap.get(key));
            }
        }
        return baseMap;
    }
}