view agent/core/src/main/java/com/redhat/thermostat/agent/BackendConfigurationListener.java @ 2776:6ea7021a74d6

Added framework for configurable Backend activation/deactivation Reviewed-By: jkang, jerboaa Review-Thread: http://icedtea.classpath.org/pipermail/thermostat/2017-August/024612.html http://icedtea.classpath.org/pipermail/thermostat/2017-September/024925.html http://icedtea.classpath.org/pipermail/thermostat/2017-October/025247.html
author Joshua Matsuoka <jmatsuok@redhat.com>
date Thu, 19 Oct 2017 11:44:23 -0400
parents
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.agent;

import java.util.Hashtable;
import java.util.logging.Level;
import java.util.logging.Logger;

import org.apache.felix.scr.annotations.Activate;
import org.apache.felix.scr.annotations.Component;
import org.apache.felix.scr.annotations.Reference;
import org.apache.felix.scr.annotations.Service;
import org.osgi.service.cm.Configuration;
import org.osgi.service.cm.ConfigurationAdmin;

import com.redhat.thermostat.backend.BackendConfiguration;
import com.redhat.thermostat.common.ActionEvent;
import com.redhat.thermostat.common.ActionListener;
import com.redhat.thermostat.common.ThermostatExtensionRegistry;
import com.redhat.thermostat.common.utils.LoggingUtils;

@Component
@Service(value = BackendConfigurationListener.class)
public class BackendConfigurationListener implements ActionListener<ThermostatExtensionRegistry.Action> {

    @Reference
    private ConfigurationAdmin configurationAdmin;

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

    // Default constructor provided for DS
    public BackendConfigurationListener() { }

    @Override
    public void actionPerformed(ActionEvent<ThermostatExtensionRegistry.Action> actionEvent) {
        BackendConfiguration configurator = (BackendConfiguration) actionEvent.getPayload();
        switch (actionEvent.getActionId()) {

            case SERVICE_ADDED: {
                // When we receive a BackendConfiguration we need to know if the plugin services
                // should be enabled, as well as what the plugin ID is. The plugin ID is used as
                // the ConfigurationPid for the Configuration Admin.
                try {
                    if (configurator.isEnabled()) {
                        logger.info("Plugin " + configurator.getPluginId() + " is enabled, activating services.");
                        Configuration configuration = configurationAdmin.getConfiguration(configurator.getPluginId());
                        Hashtable<String, Object> config = new Hashtable<>();
                        // When configuration is created it is bound to the calling bundle's location. We should
                        // assign the configuration to the plugin bundle instead.
                        configuration.setBundleLocation(configurator.getLocation());
                        logger.config("Sending configuration to " + configurator.getPluginId());
                        configuration.update(config);

                    } else {
                        logger.info("Plugin " + configurator.getPluginId() + " is disabled");
                    }
                } catch (Exception e) {
                    logger.warning(e.getMessage());
                }
                break;
            }

            case SERVICE_REMOVED: {
                try {
                    if (configurator.isEnabled()) {
                        logger.info("Deactivating plugin " + configurator.getPluginId());
                        Configuration configuration = configurationAdmin.getConfiguration(configurator.getPluginId());
                        configuration.delete();
                    }
                } catch (Exception e) {
                    logger.warning(e.getMessage());
                }
                break;
            }

            default: {
                logger.log(Level.WARNING, "received unknown event from BackendConfigurationRegistry: " + actionEvent.getActionId());
                break;
            }
        }
    }

    @Activate
    public void activate() {}

    public void bindConfigurationAdmin(ConfigurationAdmin admin) {
        this.configurationAdmin = admin;
    }

}