/**
 * JOnAS: Java(TM) Open Application Server
 * Copyright (C) 2006 Bull S.A.
 * Contact: jonas-team@objectweb.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.objectweb.jonas.management.monitoring;

import java.util.Collection;

import javax.management.ObjectName;

import org.objectweb.jonas.jmx.JonasObjectName;

import org.objectweb.util.monolog.api.BasicLevel;

/**
 * ClusterDaemon proxy
 * It is created when a new element has been found in domain.xml
 * It holds all the necessary information to go to the ClusterDaemon
 * @author durieuxp
 */
public class ClusterDaemonProxy extends JMXProxy implements ClusterDaemonProxyMBean {

    /**
     * Constructor.
     * This is called when a ClusterDaemon element has been found in domain.xml.
     * @param dm the DomainMonitor object reference
     * @param name cluster daemon name
     * @param urls possible urls that can be used for connection with the CD
     */
    public ClusterDaemonProxy(DomainMonitor dm, String name, Collection urls) {
        super(dm, name, urls);
    }

    /**
     * Start a Remote JOnAS Server
     * @param serverName Name of the jonas server
     * @param otherParams additional parameters to start a server via the ClusterDaemon
     * @return true if operation succeded
     */
    public boolean startServer(String serverName, String otherParams) {
        logger.log(BasicLevel.DEBUG, "Start remote server: " + serverName);
        if (!checkConnection()) {
            // Not yet connected. Try to connect now.
            if (!connect(getUrls())) {
                logger.log(BasicLevel.ERROR, "Unable to get connection to cluster daemon " + name);
                return false;
            }
        }

        // Ask Cluster Daemon to start the Server.
        String opName = "startJOnAS";
        String[] opParams = {serverName, getDomain(), otherParams};
        String[] opSignature = {"java.lang.String", "java.lang.String", "java.lang.String"};
        try {
            ObjectName on = JonasObjectName.clusterDaemon();
            connection.invoke(on, opName, opParams, opSignature);
        } catch (Exception e) {
            logger.log(BasicLevel.ERROR, "Cannot start server: " + e);
            return false;
        }
        return true;
    }

    /**
     * Stop a Remote JOnAS Server
     * @param serverName Name of the jonas server
     * @param otherParams additional parameters that could be used to stop the server
     * @return true if operation succeded
     */
    public boolean stopServer(String serverName, String otherParams) {
        logger.log(BasicLevel.DEBUG, "Stop remote server: " + serverName);
        if (!checkConnection()) {
            // Not yet connected. Try to connect now.
            if (!connect(getUrls())) {
                logger.log(BasicLevel.ERROR, "Unable to get connection to cluster daemon " + name);
                return false;
            }
        }

        // Ask Cluster Daemon to stop the Server.
        String opName = "stopJOnAS";
        String[] opParams = {serverName};
        String[] opSignature = {"java.lang.String"};
        try {
            ObjectName on = JonasObjectName.clusterDaemon();
            connection.invoke(on, opName, opParams, opSignature);
        } catch (Exception e) {
            logger.log(BasicLevel.ERROR, "Cannot stop server: " + e);
            return false;
        }
        return true;
    }

    public void startAllServers(String otherParams) {
        logger.log(BasicLevel.DEBUG, "");
        if (!checkConnection()) {
            // Not yet connected. Try to connect now.
            if (!connect(getUrls())) {
                logger.log(BasicLevel.ERROR, "Unable to get connection to cluster daemon " + name);
                return;
            }
        }

        // Ask Cluster Daemon to start all the Servers.
        String opName = "startAllJOnAS";
        String[] opParams = {getDomain(), otherParams};
        String[] opSignature = {"java.lang.String", "java.lang.String"};
        try {
            ObjectName on = JonasObjectName.clusterDaemon();
            connection.invoke(on, opName, opParams, opSignature);
        } catch (Exception e) {
            logger.log(BasicLevel.ERROR, "Cannot start server: " + e);
            // TODO
        }
    }

    public void stopAllServers(String otherParams) {
        logger.log(BasicLevel.DEBUG, "");
        if (!checkConnection()) {
            // Not yet connected. Try to connect now.
            if (!connect(getUrls())) {
                logger.log(BasicLevel.ERROR, "Unable to get connection to cluster daemon " + name);
                return;
            }
        }

        // Ask Cluster Daemon to start all the Servers.
        String opName = "stopAllJOnAS";
        String[] opParams = {};
        String[] opSignature = {};
        try {
            ObjectName on = JonasObjectName.clusterDaemon();
            connection.invoke(on, opName, opParams, opSignature);
        } catch (Exception e) {
            logger.log(BasicLevel.ERROR, "Cannot stop server: " + e);
            // TODO
        }
    }

    /**
     * Check the MBean server connection and possibly change state
     */
    public void checkit() {
        logger.log(BasicLevel.DEBUG, name);

        switch (state) {
            case INITIAL:
            case UNREACHABLE:
                if (checkConnection()) {
                    state = RUNNING;
                } else {
                    if (!connect(getUrls())) {
                        state = UNREACHABLE;
                    } else {
                        state = RUNNING;
                    }
                }
                break;
        }
    }

}