<commit_before>/**
 * File:   SignalReceiver.java
 * Author: <NAME>
 * Date:   23 sept. 2011
 */
package org.psem2m.signals;

import java.util.HashMap;
import java.util.HashSet;
import java.util.LinkedHashSet;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

import org.apache.felix.ipojo.annotations.Bind;
import org.apache.felix.ipojo.annotations.Component;
import org.apache.felix.ipojo.annotations.Instantiate;
import org.apache.felix.ipojo.annotations.Invalidate;
import org.apache.felix.ipojo.annotations.Provides;
import org.apache.felix.ipojo.annotations.Requires;
import org.apache.felix.ipojo.annotations.ServiceProperty;
import org.apache.felix.ipojo.annotations.Unbind;
import org.apache.felix.ipojo.annotations.Validate;
import org.osgi.framework.BundleException;
import org.psem2m.isolates.base.IIsolateLoggerSvc;
import org.psem2m.isolates.base.Utilities;
import org.psem2m.isolates.base.activators.CPojoBase;
import org.psem2m.isolates.services.remote.signals.ISignalData;
import org.psem2m.isolates.services.remote.signals.ISignalListener;
import org.psem2m.isolates.services.remote.signals.ISignalReceiver;
import org.psem2m.isolates.services.remote.signals.ISignalReceptionProvider;

/**
 * Base signal receiver logic
 * 
 * @author <NAME>
 */
@Component(name = "psem2m-signal-receiver-factory", publicFactory = false)
@Provides(specifications = ISignalReceiver.class)
@Instantiate(name = "psem2m-signal-receiver")
public class SignalReceiver extends CPojoBase implements ISignalReceiver,
        ISignalListener {

    /** Signal listeners */
    private final Map<String, Set<ISignalListener>> pListeners = new HashMap<String, Set<ISignalListener>>();

    /** Log service */
    @Requires
    private IIsolateLoggerSvc pLogger;

    /** Number of available providers */
    private int pNbProviders = 0;

    /** Notification thread pool */
    private ExecutorService pNotificationExecutor;

    /** On-line service property */
    @ServiceProperty(name = ISignalReceiver.PROPERTY_ONLINE, value = "false", mandatory = true)
    private boolean pPropertyOnline;

    /** Reception providers */
    @Requires(id = "receivers", optional = true)
    private ISignalReceptionProvider[] pReceivers;

    /**
     * Method called by iPOJO when a reception provider is bound
     * 
     * @param aProvider
     *            The new provider
     */
    @Bind(id = "receivers", aggregate = true, filter = "("
            + ISignalReceptionProvider.PROPERTY_READY + "=true)")
    protected void bindProvider(final ISignalReceptionProvider aProvider) {

        // Register to the provider
        aProvider.registerListener(this);

        // Increase the number of available providers
        pNbProviders++;

        // We're now on-line
        pPropertyOnline = true;
    }

    /*
     * (non-Javadoc)
     * 
     * @see org.psem2m.isolates.services.remote.signals.ISignalListener#
     * handleReceivedSignal(java.lang.String,
     * org.psem2m.isolates.services.remote.signals.ISignalData)
     */
    @Override
    public void handleReceivedSignal(final String aSignalName,
            final ISignalData aSignalData) {

        final Set<ISignalListener> signalListeners = new HashSet<ISignalListener>();

        // Get listeners set
        synchronized (pListeners) {

            for (final String signal : pListeners.keySet()) {
                // Take care of jokers ('*' and '?')
                if (Utilities.matchFilter(aSignalName, signal)) {
                    signalListeners.addAll(pListeners.get(signal));
                }
            }
        }

        // Notify listeners in another thread
        pNotificationExecutor.execute(new Runnable() {

            @Override
            public void run() {

                for (final ISignalListener listener : signalListeners) {
                    listener.handleReceivedSignal(aSignalName, aSignalData);
                }
            }
        });
    }

    /*
     * (non-Javadoc)
     * 
     * @see org.psem2m.isolates.base.activators.CPojoBase#invalidatePojo()
     */
    @Override
    @Invalidate<commit_msg>Log des erreurs des listeners de signaux<commit_after>/**
 * File:   SignalReceiver.java
 * Author: <NAME>
 * Date:   23 sept. 2011
 */
package org.psem2m.signals;

import java.util.HashMap;
import java.util.HashSet;
import java.util.LinkedHashSet;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

import org.apache.felix.ipojo.annotations.Bind;
import org.apache.felix.ipojo.annotations.Component;
import org.apache.felix.ipojo.annotations.Instantiate;
import org.apache.felix.ipojo.annotations.Invalidate;
import org.apache.felix.ipojo.annotations.Provides;
import org.apache.felix.ipojo.annotations.Requires;
import org.apache.felix.ipojo.annotations.ServiceProperty;
import org.apache.felix.ipojo.annotations.Unbind;
import org.apache.felix.ipojo.annotations.Validate;
import org.osgi.framework.BundleException;
import org.psem2m.isolates.base.IIsolateLoggerSvc;
import org.psem2m.isolates.base.Utilities;
import org.psem2m.isolates.base.activators.CPojoBase;
import org.psem2m.isolates.services.remote.signals.ISignalData;
import org.psem2m.isolates.services.remote.signals.ISignalListener;
import org.psem2m.isolates.services.remote.signals.ISignalReceiver;
import org.psem2m.isolates.services.remote.signals.ISignalReceptionProvider;

/**
 * Base signal receiver logic
 * 
 * @author <NAME>
 */
@Component(name = "psem2m-signal-receiver-factory", publicFactory = false)
@Provides(specifications = ISignalReceiver.class)
@Instantiate(name = "psem2m-signal-receiver")
public class SignalReceiver extends CPojoBase implements ISignalReceiver,
        ISignalListener {

    /** Signal listeners */
    private final Map<String, Set<ISignalListener>> pListeners = new HashMap<String, Set<ISignalListener>>();

    /** Log service */
    @Requires
    private IIsolateLoggerSvc pLogger;

    /** Number of available providers */
    private int pNbProviders = 0;

    /** Notification thread pool */
    private ExecutorService pNotificationExecutor;

    /** On-line service property */
    @ServiceProperty(name = ISignalReceiver.PROPERTY_ONLINE, value = "false", mandatory = true)
    private boolean pPropertyOnline;

    /** Reception providers */
    @Requires(id = "receivers", optional = true)
    private ISignalReceptionProvider[] pReceivers;

    /**
     * Method called by iPOJO when a reception provider is bound
     * 
     * @param aProvider
     *            The new provider
     */
    @Bind(id = "receivers", aggregate = true, filter = "("
            + ISignalReceptionProvider.PROPERTY_READY + "=true)")
    protected void bindProvider(final ISignalReceptionProvider aProvider) {

        // Register to the provider
        aProvider.registerListener(this);

        // Increase the number of available providers
        pNbProviders++;

        // We're now on-line
        pPropertyOnline = true;
    }

    /*
     * (non-Javadoc)
     * 
     * @see org.psem2m.isolates.services.remote.signals.ISignalListener#
     * handleReceivedSignal(java.lang.String,
     * org.psem2m.isolates.services.remote.signals.ISignalData)
     */
    @Override
    public void handleReceivedSignal(final String aSignalName,
            final ISignalData aSignalData) {

        final Set<ISignalListener> signalListeners = new HashSet<ISignalListener>();

        // Get listeners set
        synchronized (pListeners) {

            for (final String signal : pListeners.keySet()) {
                // Take care of jokers ('*' and '?')
                if (Utilities.matchFilter(aSignalName, signal)) {
                    signalListeners.addAll(pListeners.get(signal));
                }
            }
        }

        // Notify listeners in another thread
        pNotificationExecutor.execute(new Runnable() {

            @Override
            public void run() {

                for (final ISignalListener listener : signalListeners) {

                    try {
                        listener.handleReceivedSignal(aSignalName, aSignalData);

                    } catch (final Throwable throwable) {
                        pLogger.logWarn(this, "handleReceivedSignal",
                                "Error notifying a signal listener", throwable);
                    }
                }
            }
        });
    }

    /*
     * (non-Javadoc)
     * 
     * @see org.psem2m.isolates.base.activators.CPojoBase#invalidatePojo()
     */
    @Override
    @Invalidate
{'fraction_non_alphanumeric': 0.10288683602771363, 'fraction_numerical': 0.004618937644341801, 'mean_word_length': 2.885598923283984, 'pattern_counts': {'":': 0, '<': 19, '<?xml version=': 0, '>': 19, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, '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': '938477', 'n_tokens_mistral': 2636, 'n_tokens_neox': 2546, 'n_words': 492}