/*-
 * ~~~~~~licensing~~~~~~
 * profile-management
 * ==========
 * 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.platform.base.management.core.utils;

import javax.management.AttributeChangeNotification;
import javax.management.ListenerNotFoundException;
import javax.management.MBeanNotificationInfo;
import javax.management.NotCompliantMBeanException;
import javax.management.Notification;
import javax.management.NotificationBroadcaster;
import javax.management.NotificationBroadcasterSupport;
import javax.management.NotificationEmitter;
import javax.management.NotificationFilter;
import javax.management.NotificationListener;

import org.osgi.framework.Bundle;
import org.osgi.framework.BundleContext;
import org.osgi.framework.FrameworkUtil;

import ru.entaxy.esb.platform.base.management.core.api.AnnotatedMBean;
import ru.entaxy.platform.base.support.osgi.tracker.BundleContextAware;

public class BundleAwareMBeanImpl<T extends BundleAwareManagedObject, I> 
		extends AnnotatedMBean<I> implements NotificationBroadcaster, NotificationEmitter, BundleAwareMBean, BundleContextAware {

	protected T managedObject; 

	protected BundleContext bundleContext;
	
	protected long sequenceNumber = 0;
	
	protected NotificationBroadcasterSupport emitter;
	
	public BundleAwareMBeanImpl(Class<I> clazz, T managedObject) throws NotCompliantMBeanException {
		super(clazz);
		this.managedObject = managedObject;
		this.emitter = new NotificationBroadcasterSupport(prepareNotificationInfo());
	} 
	
	// BEGIN: notification stuff
	
	protected MBeanNotificationInfo[] prepareNotificationInfo() { 
        String[] types = new String[] { 
            AttributeChangeNotification.ATTRIBUTE_CHANGE 
        }; 
        String name = AttributeChangeNotification.class.getName(); 
        String description = "An attribute of this MBean has changed"; 
        MBeanNotificationInfo info = 
            new MBeanNotificationInfo(types, name, description); 
        return new MBeanNotificationInfo[] {info}; 
	}
	
	@Override
	public void removeNotificationListener(NotificationListener listener, NotificationFilter filter, Object handback)
			throws ListenerNotFoundException {
		this.emitter.removeNotificationListener(listener, filter, handback);
	}

	@Override
	public void addNotificationListener(NotificationListener listener, NotificationFilter filter, Object handback)
			throws IllegalArgumentException {
		this.emitter.addNotificationListener(listener, filter, handback);
	}

	@Override
	public void removeNotificationListener(NotificationListener listener) throws ListenerNotFoundException {
		this.emitter.removeNotificationListener(listener);
	}

	@Override
	public MBeanNotificationInfo[] getNotificationInfo() {
		return this.emitter==null
			?prepareNotificationInfo()
			:emitter.getNotificationInfo();
	}	
	
	// END: notification stuff
	
	@Override
	public String getBundleState() {
		return managedObject.getBundleState();
	}

	@Override
	public long getBundleId() {
		return managedObject.getBundleId();
	}

	@Override
	public String getBundleSymbolicName() {
		return managedObject.getBundleSymbolicName();
	}
	
	@Override
	public BundleContext getBundleContext() {
		return bundleContext;
	}

	@Override
	public void setBundleContext(BundleContext bundleContext) {
		this.bundleContext = bundleContext;
	}

	@Override
	public void refreshBundleInfo() {
		Bundle b = FrameworkUtil.getBundle(getClass()).getBundleContext().getBundle(getBundleId());
		
		// track bundleState
		AttributeChangeTracker tracker = new AttributeChangeTracker();
		tracker.oldValue(this.managedObject.getBundleState());
		this.managedObject.bundleState(b.getState());
		if (tracker.newValue(this.managedObject.getBundleState()).isChanged()) {
			// send notification
			Notification n = new AttributeChangeNotification(
					this, 
					this.sequenceNumber++, 
					System.currentTimeMillis(), 
					"bundleState changed", 
					"bundleState", 
					"String", 
					tracker.oldValue, 
					tracker.newValue);
			this.emitter.sendNotification(n);
		}
		
	}


}
