/*-
 * ~~~~~~licensing~~~~~~
 * system-commons
 * ==========
 * Copyright (C) 2020 - 2021 EmDev LLC
 * ==========
 * 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.
 * ~~~~~~/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;
    }

}
