/**
 * JOnAS: Java(TM) Open Application Server
 * Copyright (C) 2006 Bull S.A.S.
 * 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.cluster.daemon;

import javax.management.MBeanServerConnection;
import javax.management.ObjectInstance;

import org.objectweb.jonas.jmx.commons.JMXRemoteHelper;

/**
 * This class implements a JMX client to administer the cluster daemon.
 *
 * @author Benoit Pelletier
 * @author S. Ali Tokmen
 */
public class ClusterDaemonAdmin {
    /**
     * STOP command
     */
    private static final int STOP_CMD = 0;

    /**
     * Connection to the MBean server
     */
    private MBeanServerConnection cnx = null;

    /**
     * Object Instance
     */
    private ObjectInstance oi = null;

    /**
     * clusterd.xml filename (by default $JONAS_ROOT/conf/clusterd.xml)
     */
    private String confFile = null;

    /**
     * User name to use for JMX connections.
     */
    private String username = null;

    /**
     * Password to use for JMX connections.
     */
    private String password = null;

    /**
     * Private constructor
     */
    private ClusterDaemonAdmin() {
        // Nothing: this is a utility class
    }

    /**
     * Main function
     * @param args arguments
     */
    public void start(String[] args) {
        int cmd = -1;

        // Get command args
        for (int argn = 0; argn < args.length; argn++) {
            String arg = args[argn];

            if (arg.equals("-stop")) {
                cmd = STOP_CMD;
                continue;
            }

            if (arg.equals("-confFile")) {
                confFile = args[++argn];
                continue;
            }

            if (arg.equals("-username")) {
                username = args[++argn];
                continue;
            }

            if (arg.equals("-password")) {
                password = args[++argn];
                continue;
            }
        }

        // load the clusterd.xml file
        try {
            ClusterDaemonTools.loadClusterDaemonConfiguration(confFile);
        } catch (Exception e) {
            System.err.println("Error loading the configuration file : " + e);
            System.exit(2);
        }

        // Initialize the JMX connection with the cluster daemon
        try {
            cnx = JMXRemoteHelper.connect(
                    ClusterDaemonTools.getJmxUrl(ClusterDaemonTools.getCurrentConfiguration().getClusterDaemon().getName()),
                    username, password);
            oi = JMXRemoteHelper.getInstance(cnx, "*:type=ClusterDaemon");
        } catch (SecurityException e) {
            e.printStackTrace(System.err);
            System.err.println("This cluster daemon requires credentials.");
            System.err.println("Please use the -username and -password options to provide them.");
            System.exit(2);
        } catch (Exception e) {
            System.err.println("Cannot initialize MBean server connection : " + e);
            System.exit(2);
        }

        // execute the command
        switch (cmd) {
            case STOP_CMD:
                try {
                    // call the stop() operation
                    Object[] params = new Object[] {};
                    String[] sig = new String[] {};
                    cnx.invoke(oi.getObjectName(), "stopClusterDaemon", params, sig);
                    System.out.println("Cluster daemon stopped");
                } catch (Exception e) {
                    System.err.println("MBean invocation exception : " + e);
                    System.exit(2);
                }
                break;
            default:
                System.err.println("Bad command: " + cmd);
                System.err.println("Please make sure you use the batch/shell script to execute this class");
                System.exit(2);
                break;
        }
    }

    /**
     * Main method
     * @param args arguments
     */
    public static void main(String[] args)  {
        new ClusterDaemonAdmin().start(args);
    }
}