<commit_before>/*
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.apache.karaf.cellar.config;

import org.apache.karaf.cellar.core.Configurations;
import org.apache.karaf.cellar.core.Group;
import org.apache.karaf.cellar.core.Node;
import org.apache.karaf.cellar.core.control.SwitchStatus;
import org.apache.karaf.cellar.core.event.EventProducer;
import org.apache.karaf.cellar.core.event.EventType;
import org.osgi.framework.InvalidSyntaxException;
import org.osgi.service.cm.Configuration;
import org.osgi.service.cm.ConfigurationEvent;
import org.osgi.service.cm.ConfigurationListener;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.IOException;
import java.util.*;

/**
 * Local configuration listener.
 */
public class LocalConfigurationListener extends ConfigurationSupport implements ConfigurationListener {

    private static final transient Logger LOGGER = LoggerFactory.getLogger(LocalConfigurationListener.class);

    private static final long SYNC_TIMEOUT = 200;

    private EventProducer eventProducer;

    /**
     * Handle local configuration events.
     * If the event is a pending event stop it. Else broadcast it to the cluster.
     *
     * @param event
     */
    public void configurationEvent(ConfigurationEvent event) {

        // check if the producer is ON
        if (eventProducer.getSwitch().getStatus().equals(SwitchStatus.OFF)) {
            LOGGER.warn("CELLAR CONFIG: cluster event producer is OFF");
            return;
        }

        String pid = event.getPid();
        Configuration conf = null;
        try {
            conf = configurationAdmin.getConfiguration(pid);
        } catch (Exception e) {
            LOGGER.error("CELLAR CONFIG: can't retrieve configuration with PID {}", pid, e);
            return;
        }

        Dictionary localDictionary = conf.getProperties();
        if (localDictionary.get(Constants.SYNC_PROPERTY) != null) {
            Long syncTimestamp = new Long((String) localDictionary.get(Constants.SYNC_PROPERTY));
            Long currentTimestamp = System.currentTimeMillis();
            if ((currentTimestamp - syncTimestamp) <= SYNC_TIMEOUT) {
                return;
            }
        }

        Set<Group> groups = groupManager.listLocalGroups();

        if (groups != null && !groups.isEmpty()) {
            for (Group group : groups) {
                // check if the pid is allowed for outbound.
                if (isAllowed(group, Constants.CATEGORY, pid, EventType.OUTBOUND)) {

                    // update the distributed map if needed
                    Map<String, Properties> configurationTable = clusterManager.getMap(Constants.CONFIGURATION_MAP + Configurations.SEPARATOR + group.getName());

                    try {
                        if (event.getType() == ConfigurationEvent.CM_DELETED) {
                            configurationTable.remove(pid);
                            // TODO broadcast the cluster event
                        } else {
                            //Configuration conf = configurationAdmin.getConfiguration(pid);
                            Properties localProperties = dictionaryToProperties(filter(localDictionary));
                            // update the distributed map
                            configurationTable.put(pid, localProperties);
                            // broadcast the cluster event
                            RemoteConfigurationEvent remoteConfigurationEvent = new RemoteConfigurationEvent(pid);
                            remoteConfigurationEvent.setSourceGroup(group);
                            eventProducer.produce(remoteConfigurationEvent);
                        }
                    } catch (Exception e) {
                        LOGGER.error("CELLAR CONFIG: failed to push configuration with PID {} to the distributed map", pid, e);
                    }<commit_msg>Fix issue with config delete event<commit_after>/*
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.apache.karaf.cellar.config;

import org.apache.karaf.cellar.core.Configurations;
import org.apache.karaf.cellar.core.Group;
import org.apache.karaf.cellar.core.Node;
import org.apache.karaf.cellar.core.control.SwitchStatus;
import org.apache.karaf.cellar.core.event.EventProducer;
import org.apache.karaf.cellar.core.event.EventType;
import org.osgi.framework.InvalidSyntaxException;
import org.osgi.service.cm.Configuration;
import org.osgi.service.cm.ConfigurationEvent;
import org.osgi.service.cm.ConfigurationListener;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.IOException;
import java.util.*;

/**
 * Local configuration listener.
 */
public class LocalConfigurationListener extends ConfigurationSupport implements ConfigurationListener {

    private static final transient Logger LOGGER = LoggerFactory.getLogger(LocalConfigurationListener.class);

    private static final long SYNC_TIMEOUT = 200;

    private EventProducer eventProducer;

    /**
     * Handle local configuration events.
     * If the event is a pending event stop it. Else broadcast it to the cluster.
     *
     * @param event
     */
    public void configurationEvent(ConfigurationEvent event) {

        // check if the producer is ON
        if (eventProducer.getSwitch().getStatus().equals(SwitchStatus.OFF)) {
            LOGGER.warn("CELLAR CONFIG: cluster event producer is OFF");
            return;
        }

        String pid = event.getPid();

        Dictionary localDictionary = null;
        if (event.getType() != ConfigurationEvent.CM_DELETED) {
            Configuration conf = null;
            try {
                conf = configurationAdmin.getConfiguration(pid);
            } catch (Exception e) {
                LOGGER.error("CELLAR CONFIG: can't retrieve configuration with PID {}", pid, e);
                return;
            }

            localDictionary = conf.getProperties();
            if (localDictionary.get(Constants.SYNC_PROPERTY) != null) {
                Long syncTimestamp = new Long((String) localDictionary.get(Constants.SYNC_PROPERTY));
                Long currentTimestamp = System.currentTimeMillis();
                if ((currentTimestamp - syncTimestamp) <= SYNC_TIMEOUT) {
                    return;
                }
            }
        }

        Set<Group> groups = groupManager.listLocalGroups();

        if (groups != null && !groups.isEmpty()) {
            for (Group group : groups) {
                // check if the pid is allowed for outbound.
                if (isAllowed(group, Constants.CATEGORY, pid, EventType.OUTBOUND)) {

                    // update the distributed map if needed
                    Map<String, Properties> configurationTable = clusterManager.getMap(Constants.CONFIGURATION_MAP + Configurations.SEPARATOR + group.getName());

                    try {
                        if (event.getType() == ConfigurationEvent.CM_DELETED) {
                            configurationTable.remove(pid);
                            // TODO broadcast the cluster event
                        } else {
                            //Configuration conf = configurationAdmin.getConfiguration(pid);
                            Properties localProperties = dictionaryToProperties(filter(localDictionary));
                            // update the distributed map
                            configurationTable.put(pid, localProperties);
                            // broadcast the cluster event
                            RemoteConfigurationEvent remoteConfigurationEvent = new RemoteConfigurationEvent(pid);
                            remoteConfigurationEvent.setSourceGroup(group);
                            eventProducer.produce(remoteConfigurationEvent);
                        }
                    } catch (Exception e) {
                        LOGGER.error("CELLAR CONFIG: failed to push configuration with PID {} to the distributed map", pid, e);
                    }
{'fraction_non_alphanumeric': 0.08283366644459249, 'fraction_numerical': 0.001998667554963358, 'mean_word_length': 2.253973988439306, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3232200', 'n_tokens_mistral': 2139, 'n_tokens_neox': 2098, 'n_words': 616}