/*-
 * ~~~~~~licensing~~~~~~
 * cluster-node-management
 * ==========
 * Copyright (C) 2020 - 2022 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.core.management.cluster.node.impl;

import java.util.Iterator;
import java.util.List;

import javax.management.NotCompliantMBeanException;

import org.osgi.framework.BundleContext;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.hazelcast.core.Member;

import ru.entaxy.esb.platform.base.management.core.ManagementCore;
import ru.entaxy.esb.platform.base.management.core.utils.ManagedObjectsListener;
import ru.entaxy.esb.platform.core.management.node.ClusterNodeMBean;

public class MemberMBeanManager extends ManagedObjectsListener<List<Member>, Member, ClusterNodeMBean, ClusterNodeMBeanImpl> {

	private static final Logger log = LoggerFactory.getLogger(MemberMBeanManager.class);
	
	public MemberMBeanManager(BundleContext bundleContext) {
		super(bundleContext, ClusterNodeMBean.class);
	}

	@Override
	protected Iterator<Member> getIterator(List<Member> managedObject) {
		return managedObject.iterator();
	}

	@Override
	protected String getKeyValue(Member object) {
		return object.getUuid();
	}

	@Override
	protected ClusterNodeMBeanImpl createService(Member managedObject) {
		try {
			return new ClusterNodeMBeanImpl(managedObject);
		} catch (NotCompliantMBeanException e) {
			log.error("Error creating [ClusterNodeMBeanImpl]", e);
			return null;
		}
	}

	@Override
	protected String getJmxObjectName(Member managedObject) {
		return ManagementCore.ENTAXY_JMX_DOMAIN + ":" + ManagementCore.REMOTE_NODE_KEY + "=" 
				+ managedObject.getAddress().getHost()+"-"+managedObject.getAddress().getPort();
	}

}
