/**
 * JOnAS: Java(TM) Open Application Server
 * Copyright (C) 2007 Bull S.A.
 * Contact: jonas-team@ow2.org
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307
 * USA
 *
 * --------------------------------------------------------------------------
 * $Id:
 * --------------------------------------------------------------------------
 */
package org.ow2.jonas.lib.management.extensions.domain;

import javax.management.ObjectName;
import org.ow2.jonas.lib.management.extensions.base.BaseManagement;
import org.ow2.jonas.lib.management.javaee.J2eeObjectName;
import org.ow2.jonas.lib.management.tools.ManagementRepr;
import org.ow2.jonas.management.api.extensions.domain.IDomain;

/**
 * Implement management operations to administer a JOnAS domain.
 * @author danesa
 *
 */
public class DomainManagement extends BaseManagement implements IDomain {
    private final static String masterAttribute = "master";
    private final static String serverNamesAttribute = "serverNames";
    private final static String serversAttribute = "serves";
    private final static String clustersAttribute = "clusters";
    private final static String clusterDaemonsAttribute = "clusterDaemons";

    /**
     * The J2EEDomain MBean's ObjectName.
     * All the servers in the domain have a J2EEDomain MBean registered in their JMX
     * server with the same ObjectName:
     * domainName:j2eeType=J2EEDomain,name=domainName
     */
    private ObjectName domainOn = null;

    /**
     * Constructor.
     */
    public DomainManagement() {
        super();
        domainOn = J2eeObjectName.J2EEDomain(getDomainName());
    }

    /**
     * Return True if the managed server is a master.
     * @param pServer managed server name
     * @return the "master" attribute of the corresponding J2EEDomain MBean
     */
    public boolean isMaster(final String pServer) {
        return ((Boolean) ManagementRepr.getAttribute(domainOn, masterAttribute, pServer)).booleanValue();
    }
    /**
     * Return True if the current server is a master.
     * @return the "master" attribute of the current J2EEDomain MBean
     */
    public boolean isMaster() {
        return ((Boolean) ManagementRepr.getAttribute(domainOn, masterAttribute, getServerName())).booleanValue();
    }

    /**
     * Return the type of a cluster in the current domain.
     * @param clusterName the cluster's name
     * @return the result of the getClusterType operation
     */
    public String getClusterType(final String clusterName) {
        String operation = "getClusterType";
        Object[] param = {clusterName};
        String[] signature = {"java.lang.String"};
        return (String) ManagementRepr.invoke(domainOn, operation, param, signature, getServerName());
    }

    /**
     * Return the server names that are belonging to the domain.
     * @return the server names that are belonging to the domain
     */
    public String[] getServerNames() {
        return (String[]) ManagementRepr.getAttribute(domainOn, serverNamesAttribute, getServerName());
    }

    /**
     * Return the server names that are belonging to a cluster.
     * @param clusterName the cluster name
     * @return the server names that are belonging to a cluster
     */
    public String[] getServerNames(final String clusterName) {
        String operation = "getServersInCluster";
        Object[] param = {clusterName};
        String[] signature = {"java.lang.String"};
        return (String[]) ManagementRepr.invoke(domainOn, operation, param, signature, getServerName());
    }

    /**
     * Return the clusters in the domain. A cluster in the domain has associated a cluster MBean having
     * in its ObjectName a key property 'type' with values in {LogicalCluster, TomcatCluster, JkCluster, EjbHa}
     * @return the OBJECT_NAMES of the Cluster MBeans.
     */
    public String[] getClusters() {
        return (String[]) ManagementRepr.getAttribute(domainOn, clustersAttribute, getServerName());
    }

    /**
     * Return the cluster daemons in the domain. A cluster daemon in the domain has associated a cluster daemon MBean having
     * in its ObjectName a key property 'type' with value equal to 'ClusterDaemon'.
     * @return the OBJECT_NAMES of the Cluster Daemon MBeans.
     */
    public String[] getclusterDaemons() {
        return (String[]) ManagementRepr.getAttribute(domainOn, clusterDaemonsAttribute, getServerName());
    }
    /**
     * Return the state of a server in the domain
     * @param serverName the server's name
     * @return the state of a server in the domain
     */
    public String getServerState(final String serverName) {
        String operation = "getServerState";
        Object[] param = {serverName};
        String[] signature = {"java.lang.String"};
        return (String) ManagementRepr.invoke(domainOn, operation, param, signature, getServerName());
    }

    /**
     * Return the cluster daemon of a server in the domain.
     * @param serverName the server's name
     * @return the name of the server's cluster daemon
     */
    public String getServerClusterdaemon(final String serverName) {
        String operation = "getServerClusterdaemon";
        Object[] param = {serverName};
        String[] signature = {"java.lang.String"};
        return (String) ManagementRepr.invoke(domainOn, operation, param, signature, getServerName());
    }

    /**
     * Return the state of a cluster in the domain.
     * @param clusterName the cluster's name
     * @return the state of a cluster in the domain
     */
    public String getClusterState(final String clusterName) {
        String operation = "getClusterState";
        Object[] param = {clusterName};
        String[] signature = {"java.lang.String"};
        return (String) ManagementRepr.invoke(domainOn, operation, param, signature, getServerName());
    }

    /**
     * Return the state of a cluster daemon in the domain.
     * @param clusterdaemonName the cluster daemon's name
     * @return the state of a cluster daemon in the domain
     */
    public String getClusterdaemonState(final String clusterdaemonName) {
        String operation = "getClusterdaemonState";
        Object[] param = {clusterdaemonName};
        String[] signature = {"java.lang.String"};
        return (String) ManagementRepr.invoke(domainOn, operation, param, signature, getServerName());
    }
}
