/*-
 * ~~~~~~licensing~~~~~~
 * entaxy-audit
 * ==========
 * Copyright (C) 2020 - 2025 EmDev LLC
 * ==========
 * You may not use this file except in accordance with the License Terms of the Copyright
 * Holder located at: https://entaxy.ru/eula . All copyrights, all intellectual property
 * rights to the Software and any copies are the property of the Copyright Holder. Unless
 * it is explicitly allowed the Copyright Holder, the User is prohibited from using the
 * Software for commercial purposes to provide services to third parties.
 * 
 * The Copyright Holder hereby declares that the Software is provided on an "AS IS".
 * Under no circumstances does the Copyright Holder guarantee or promise that the
 * Software provided by him will be suitable or not suitable for the specific purposes
 * of the User, that the Software will meet all commercial and personal subjective
 * expectations of the User, that the Software will work properly, without technical
 * errors, quickly and uninterruptedly.
 * 
 * Under no circumstances shall the Copyright Holder or its Affiliates is not liable
 * to the User for any direct or indirect losses of the User, his expenses or actual
 * damage, including, downtime; loss of bussines; lost profit; lost earnings; loss
 * or damage to data, property, etc.
 * ~~~~~~/licensing~~~~~~
 */
package ru.entaxy.audit.service.osgi;

import java.util.Dictionary;
import java.util.Hashtable;

import org.osgi.framework.BundleActivator;
import org.osgi.framework.BundleContext;
import org.osgi.framework.ServiceRegistration;
import org.osgi.service.event.EventConstants;
import org.osgi.service.event.EventHandler;

import ru.entaxy.audit.service.AuditService;
import ru.entaxy.audit.service.AuditServiceFactory;
import ru.entaxy.audit.service.EventSubscriber;
import ru.entaxy.audit.service.filter.EventRouter;
import ru.entaxy.audit.service.impl.AuditServiceImpl;

public class Activator implements BundleActivator {

    private static final String LOGS_TOPIC = "org/osgi/service/*";
    private static final String COMMANDS_TOPIC = "org/apache/karaf/shell/console/*";
    private static final String JMX_AUDIT_TOPIC = "javax/management/MBeanServer/*";
    private static final String HAWTIO_AUDIT_TOPIC = "entaxy/hawtio/audit";

    private ServiceRegistration<?> listenerRegistration = null;
    private ServiceRegistration<?> serviceRegistration = null;
    private ServiceRegistration<?> commandRegistration = null;
    private ServiceRegistration<?> jmxRegistration = null;
    private ServiceRegistration<?> hawtioRegistration = null;

    private ServiceRegistration<?> eventRouterRegistration = null;

    private EventRouter eventRouter;

    @Override
    public void start(BundleContext bundleContext) throws Exception {
        // System.err.println("I'm activating!");
        AuditService auditService = AuditServiceFactory.getAuditService();
        serviceRegistration =
                bundleContext.registerService(AuditService.class.getName(), new AuditServiceImpl(), new Hashtable<>());
        eventRouter = new EventRouter(auditService);
        eventRouterRegistration = bundleContext.registerService(EventRouter.class, eventRouter, new Hashtable<>());

        EventHandler logSubscriber = new EventSubscriber(eventRouter);
        EventHandler commandsSubscriber = new EventSubscriber(eventRouter);
        EventHandler jmxSubscriber = new EventSubscriber(eventRouter);
        EventHandler hawtioSubscriber = new EventSubscriber(eventRouter);

        Dictionary<String, Object> events = new Hashtable<>();
        events.put(EventConstants.EVENT_TOPIC, LOGS_TOPIC);
        listenerRegistration = bundleContext.registerService(EventHandler.class.getName(), logSubscriber, events);

        Dictionary<String, Object> commandEvents = new Hashtable<>();
        commandEvents.put(EventConstants.EVENT_TOPIC, COMMANDS_TOPIC);
        commandRegistration =
                bundleContext.registerService(EventHandler.class.getName(), commandsSubscriber, commandEvents);

        Dictionary<String, Object> jmxEvents = new Hashtable<>();
        jmxEvents.put(EventConstants.EVENT_TOPIC, JMX_AUDIT_TOPIC);
        jmxRegistration =
                bundleContext.registerService(EventHandler.class.getName(), jmxSubscriber, jmxEvents);

        Dictionary<String, Object> hawtioEvents = new Hashtable<>();
        hawtioEvents.put(EventConstants.EVENT_TOPIC, HAWTIO_AUDIT_TOPIC);
        hawtioRegistration =
                bundleContext.registerService(EventHandler.class.getName(), hawtioSubscriber, hawtioEvents);
    }

    @Override
    public void stop(BundleContext bundleContext) throws Exception {
        if (listenerRegistration != null) {
            listenerRegistration.unregister();
        }
        if (commandRegistration != null) {
            commandRegistration.unregister();
        }
        if (jmxRegistration != null) {
            jmxRegistration.unregister();
        }
        if (serviceRegistration != null) {
            serviceRegistration.unregister();
        }
        if (eventRouterRegistration != null) {
            eventRouterRegistration.unregister();
        }
        if (eventRouter != null) {
            eventRouter.stop();
        }
    }
}
