package org.motechproject.tasks.osgi;

import org.eclipse.gemini.blueprint.service.exporter.OsgiServiceRegistrationListener;
import org.motechproject.tasks.service.ChannelService;
import org.osgi.framework.BundleContext;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.Map;

/**
 * A listener for {@link ChannelService}, effectively serves as a tasks-bundle start handler, that sets up tracking of start/stop events of all other bundles
 */
public class ChannelServiceRegistrationListener implements OsgiServiceRegistrationListener {

    private static final Logger LOG = LoggerFactory.getLogger(ChannelServiceRegistrationListener.class);

    private BundleContext bundleContext;
    private TasksBlueprintApplicationContextTracker tracker;

    @Autowired
    public ChannelServiceRegistrationListener(BundleContext bundleContext) {
        LOG.info("Starting ChannelService registration listener");
        this.bundleContext = bundleContext;
    }

    @Override
    public void registered(Object service, Map serviceProperties) {
        if (service instanceof ChannelService && tracker == null) {
            LOG.info("ChannelService registered, starting TasksBlueprintApplicationContextTracker");

            ChannelService channelService = (ChannelService) service;

            tracker = new TasksBlueprintApplicationContextTracker(bundleContext, channelService);
            tracker.open(true);
        }
    }

    @Override
    public void unregistered(Object service, Map serviceProperties) {
        if (service instanceof ChannelService && tracker != null) {
            tracker.close();
        }
    }
}

package org.motechproject.tasks.osgi;

import org.eclipse.gemini.blueprint.service.exporter.OsgiServiceRegistrationListener;
import org.motechproject.tasks.service.ChannelService;
import org.osgi.framework.BundleContext;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.Map;

/**
 * A listener for {@link ChannelService}, effectively serves as a tasks-bundle start handler, that sets up tracking of start/stop events of all other bundles
 */
public class ChannelServiceRegistrationListener implements OsgiServiceRegistrationListener {

    private static final Logger LOG = LoggerFactory.getLogger(ChannelServiceRegistrationListener.class);

    private BundleContext bundleContext;
    private TasksBlueprintApplicationContextTracker tracker;

    @Autowired
    public ChannelServiceRegistrationListener(BundleContext bundleContext) {
        LOG.info("Starting ChannelService registration listener");
        this.bundleContext = bundleContext;
    }

    @Override
    public void registered(Object service, Map serviceProperties) {
        if (service instanceof ChannelService && tracker == null) {
            LOG.info("ChannelService registered, starting TasksBlueprintApplicationContextTracker");

            ChannelService channelService = (ChannelService) service;

            tracker = new TasksBlueprintApplicationContextTracker(bundleContext, channelService);
            tracker.open(true);
        }
    }

    @Override
    public void unregistered(Object service, Map serviceProperties) {
        if (service instanceof ChannelService && tracker != null) {
            tracker.close();
            tracker = null;
        }
    }
}

Fix task channel registration when installing module from UI
Fix task channel registration when installing module from UI

I noticed that when we install a new module via UI, its
channel does not get registered. The reason for that is that when
entities bundle is getting restarted, what unregisters the ChannelService,
the ChannelService did not get registered back on, due to the info
that tracker is already present. This change should fix this and the
channels should get properly registered now.

Change-Id: I4d3aa3e5f24a6e6fa380fc726cb2b9f8ac597b94
