package org.ow2.jonas.lib.management.extensions.clusterdaemon;

import java.util.ArrayList;
import java.util.List;

import javax.management.MalformedObjectNameException;
import javax.management.ObjectName;

import org.objectweb.util.monolog.api.BasicLevel;
import org.ow2.jonas.lib.management.extensions.base.BaseManagement;
import org.ow2.jonas.lib.management.tools.ManagementException;
import org.ow2.jonas.lib.management.tools.ManagementRepr;
import org.ow2.jonas.lib.util.JonasObjectName;
import org.ow2.jonas.management.extensions.clusterdaemon.api.IClusterdaemon;

/**
 * Implements clusterdaemon management functions.
 * @author Adriana Danes
 * @author Oualaa Hani
 */
    public class ClusterdaemonManagement extends BaseManagement implements IClusterdaemon {

        /* ======================= ClusterDaemonProxy MBean attributes ==================== */
        /* ======================= domainName:type=ClusterDaemonProxy,name=clusterdaemonName ==================== */
        private final static String state = "State";
        private final static String controlledServersNames = "ControlledServersNames";
        private final static String runningState = "RUNNING";

        /**
         * Constructor.
         */
        public ClusterdaemonManagement() {
        }

        /**
         * Get the ClusterDaemonProxy MBean ObjectName for a given cluster daemon.
         * @param clusterdaemonName the name of the cluster daemon
         * @return ClusterDaemonProxy MBean ObjectName
         */
        private ObjectName getClusterdaemonOn(final String clusterdaemonName) {
            String domain = getDomainName();
            ObjectName clusterdaemonOn = null;
            try {
                clusterdaemonOn = JonasObjectName.clusterDaemonProxy(domain, clusterdaemonName);
            } catch (MalformedObjectNameException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            return clusterdaemonOn;
        }

        /**
         * @return the state of a cluster daemon in the domain
         * @param clusterdaemonName name of the cluster daemon to manage
         */
        public String getClusterdaemonState(final String clusterdaemonName) {
            ObjectName clusterdaemonOn = getClusterdaemonOn(clusterdaemonName);
            String clusterdaemonState = null;
            try {
                clusterdaemonState = (String) ManagementRepr.getAttribute(clusterdaemonOn, state, getServerName());
            } catch (ManagementException e) {
                logger.log(BasicLevel.ERROR, "Cannot get state for cluster daemon " + clusterdaemonName + " :" + e);
            }
            return clusterdaemonState;
        }

        /**
         * @param clusterdaemonName name of the cluster daemon to manage.
         * @return true if the cluster daemon is in RUNNING state, false otherwise
         */
        public boolean isRunning(final String clusterdaemonName) {
            boolean result = false;
            ObjectName clusterdaemonOn = getClusterdaemonOn(clusterdaemonName);
            try {
                String clusterdaemonState = (String) ManagementRepr.getAttribute(clusterdaemonOn, state, getServerName());
                if (runningState.equals(clusterdaemonState)) {
                    result = true;
                }
            } catch (ManagementException e) {
                logger.log(BasicLevel.ERROR, "Cannot get state for cluster daemon " + clusterdaemonName + " :" + e);
            }
            return result;
        }

        /**
         * @param clusterdaemonName name of the cluster daemon to manage
         * @return list of servers controlled by a clusterdaemon
         */
        public String[] getControlledServersNames(final String clusterdaemonName) {
            ObjectName clusterdaemonOn = getClusterdaemonOn(clusterdaemonName);
            List<String> servers = new ArrayList<String>();
            try {
                servers = (ArrayList<String>) ManagementRepr.getAttribute(clusterdaemonOn, controlledServersNames, getServerName());
                if (servers == null) {
                    return new String[0];
                }
            } catch (ManagementException e) {
                logger.log(BasicLevel.ERROR, "Cannot get controlled server names for cluster daemon " + clusterdaemonName + " :" + e);
                return null;
            }
            String[] controlledServers = new String [servers.size()];
            for (int i = 0; i < servers.size(); i++) {
                controlledServers[i] = servers.get(i);
            }
            return controlledServers;
        }
    }
