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.ow2.jonas.lib.management.extensions.base.BaseManagement;
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{

        private ObjectName clusterdaemonOn = null;

        public ClusterdaemonManagement() {

        }

        /**
         * @param clusterDaemon name
         * @return list of servers controlled by a clusterdaemon
         */
        public String[] getControlledServersNames(final String clusterdaemonName) {
            String domain = getDomainName();
            String server = getServerName();
            try {
                clusterdaemonOn = JonasObjectName.clusterDaemonProxy(domain, clusterdaemonName);
            } catch (MalformedObjectNameException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            List<String> servers = new ArrayList<String>();
            servers = (ArrayList<String>) ManagementRepr.getAttribute(clusterdaemonOn,"ControlledServersNames", server);
            String[] controlledServers = new String [servers.size()];
            for(int i=0; i<servers.size();i++) {
                controlledServers[i] = servers.get(i);
            }
            return controlledServers;
        }
    }