/*-
 * ~~~~~~licensing~~~~~~
 * system-commons
 * ==========
 * 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.esb.system.common.osgi.impl;

import org.osgi.framework.Bundle;
import org.osgi.framework.ServiceRegistration;
import org.osgi.service.blueprint.container.BlueprintContainer;
import ru.entaxy.esb.system.common.osgi.BundleMarkerService;

import java.util.*;

/**
 * @author starovoitenkov_sv
 *
 */
public class BundleMarkerServiceImpl implements BundleMarkerService {

    protected Bundle bundle = null;
    protected BlueprintContainer container = null;
    protected List<String> markers = new ArrayList<String>();
    protected String category = null;
    protected ServiceRegistration serviceRegistration = null;
    protected Map<String, String> serviceProperties = new HashMap<String, String>();

    /* (non-Javadoc)
     * @see ru.entaxy.esb.system.common.osgi.BundleMarkerService#getBundleId()
     */
    @Override
    public long getBundleId() {
        return (bundle == null ? -1 : bundle.getBundleId());
    }

    /* (non-Javadoc)
     * @see ru.entaxy.esb.system.common.osgi.BundleMarkerService#getMarkerCategory()
     */
    @Override
    public String getMarkerCategory() {
        return category;
    }

    /* (non-Javadoc)
     * @see ru.entaxy.esb.system.common.osgi.BundleMarkerService#getMarkers()
     */
    @Override
    public List<String> getMarkers() {
        return markers;
    }

    // LOCAL

    @SuppressWarnings({"rawtypes", "unchecked"})
    public void init() throws Exception {
        if (bundle == null)
            throw new Exception("Bundle not set");
        if (container == null)
            throw new Exception("Container not set");
        Dictionary properties = new Hashtable();

        for (Map.Entry<String, String> entry : serviceProperties.entrySet())
            properties.put(entry.getKey(), entry.getValue());
        properties.put(BundleMarkerService.CATEGORY_PROPERTY_NAME, category);
        serviceRegistration = bundle.getBundleContext().registerService(BundleMarkerService.class.getName(), this, properties);
    }

    public void destroy() {
        if (serviceRegistration != null)
            serviceRegistration.unregister();
    }

    // ACCESSORS

    public void setBundle(Bundle bundle) {
        this.bundle = bundle;
    }

    public void setContainer(BlueprintContainer container) {
        this.container = container;
    }

    public void setMarkers(List<String> markers) {
        this.markers = markers;
    }

    public void setCategory(String category) {
        this.category = category;
    }

    public Map<String, String> getServiceProperties() {
        return serviceProperties;
    }

    public void setServiceProperties(Map<String, String> serviceProperties) {
        this.serviceProperties = serviceProperties;
    }

}
