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

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

import org.ow2.jonas.lib.management.extensions.base.BaseManagement;
import org.ow2.jonas.lib.management.extensions.domain.DomainManagement;
import org.ow2.jonas.lib.management.tools.ManagementRepr;
import org.ow2.jonas.lib.util.JonasObjectName;
import org.ow2.jonas.management.extensions.cluster.api.ICluster;

/**
 * Implements cluster management functions.
 * @author Adriana Danes
 * @author Oualaa Hani
 */
public class ClusterManagement extends BaseManagement implements ICluster {

    public ClusterManagement() {

    }

    /**
     * start all servers in the cluster.
     * @param clusterName
     */
    public void startAllServers(final String clusterName) {
        DomainManagement dm = new DomainManagement();
        String domainName = getDomainName();
        String clusterType = dm.getClusterType(clusterName);
        String serverName = getServerName();
        ObjectName on = null;
        try {
            on = JonasObjectName.cluster(domainName, clusterName, clusterType);
        } catch (MalformedObjectNameException e) {
            e.printStackTrace();
        }
        ManagementRepr.invoke(on, "startit", null, null, serverName);
    }

    /**
     * stop all servers in the cluster.
     * @param clusterName
     */
    public void stopAllServers(final String clusterName) {
        DomainManagement dm = new DomainManagement();
        String domainName = getDomainName();
        String clusterType = dm.getClusterType(clusterName);
        String serverName = getServerName();
        ObjectName on = null;
        try {
            on = JonasObjectName.cluster(domainName, clusterName, clusterType);
        } catch (MalformedObjectNameException e) {
            e.printStackTrace();
        }
        ManagementRepr.invoke(on, "stopit", null, null, serverName);
    }

    /**
     * @param clusterName
     * @return the multicast address
     */
    public String getMcastAddr(final String clusterName) {
        DomainManagement dm = new DomainManagement();
        String domainName = getDomainName();
        String clusterType = dm.getClusterType(clusterName);
        ObjectName on = null;
        try {
            on = JonasObjectName.cluster(domainName, clusterName, clusterType);
        } catch (MalformedObjectNameException e) {
            e.printStackTrace();
        }
        return getStringAttribute(on, "McastAddr");
    }

    /**
     * @param clusterName
     * @return the multicast port
     */
    public int getMcastPort(final String clusterName) {
        String domainName = getDomainName();
        DomainManagement dm = new DomainManagement();
        String clusterType = dm.getClusterType(clusterName);
        ObjectName on = null;
        try {
            on = JonasObjectName.cluster(domainName, clusterName, clusterType);
        } catch (MalformedObjectNameException e) {
            e.printStackTrace();
        }

        return getIntegerAttribute(on, "McastPort");
    }

    /**
     * @param clusterName
     * @return the protocol used
     */
    public String getProtocol(final String clusterName) {
        String domainName = getDomainName();
        ObjectName on = null;
        try {
            on = JonasObjectName.cluster(domainName, clusterName, "CmiCluster");
        } catch (MalformedObjectNameException e) {
            e.printStackTrace();
        }
        return getStringAttribute(on, "Protocol");
    }

    /**
     * @param clusterName
     * @return the delay used by clients to update their cluster view
     */
    public int getDelayToRefresh(final String clusterName) {
        String domainName = getDomainName();
        ObjectName on = null;
        try {
            on = JonasObjectName.cluster(domainName, clusterName, "CmiCluster");
        } catch (MalformedObjectNameException e) {
            e.printStackTrace();
        }
        return getIntegerAttribute(on, "DelayToRefresh");
    }
}
