/**
 * JOnAS: Java(TM) Open Application Server
 * Copyright (C) 2004 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
 *
 * Initial developer: Florent BENOIT
 * --------------------------------------------------------------------------
 * $Id$
 * --------------------------------------------------------------------------
 */

package org.objectweb.jonas.ant;

import org.apache.tools.ant.BuildException;
import org.apache.tools.ant.taskdefs.Java;

/**
 * Uses to start/stop/... JOnAS
 * @author Florent Benoit
 */
public class JOnASTask extends BootstrapTask {

    /**
     * Start class (Server class)
     */
    private static final String START_CLASS = "org.objectweb.jonas.server.Server";

    /**
     * Stop class (Admin class)
     */
    private static final String ADMIN_CLASS = "org.objectweb.jonas.adm.JonasAdmin";

    /**
     * Check Environment class
     */
    private static final String CHECKENV_CLASS = "org.objectweb.jonas.tools.CheckEnv";

    /**
     * Version class
     */
    private static final String VERSION_CLASS = "org.objectweb.jonas_lib.version.Version";

    /**
     * (START) mode
     */
    private static final int START_MODE = 0;

    /**
     * (STOP) mode
     */
    private static final int STOP_MODE = 1;

    /**
     * (JNDI) mode (print jndi names of the registry)
     */
    private static final int JNDI_MODE = 2;

    /**
     * (CHECK) mode
     */
    private static final int CHECK_MODE = 3;

    /**
     * (VERSION) mode
     */
    private static final int VERSION_MODE = 4;

    /**
     * (PING) mode
     */
    private static final int PING_MODE = 5;

    /**
     * Default mode (START)
     */
    private static final int DEFAULT_MODE = START_MODE;

    /**
     * Mode (start or stop)
     */
    private String mode = null;

    /**
     * Run the task
     * @see org.apache.tools.ant.Task#execute()
     */
    public void execute() {
        int userMode = DEFAULT_MODE;

        if (mode.equalsIgnoreCase("start")) {
            userMode = START_MODE;
        } else if (mode.equalsIgnoreCase("stop")) {
            userMode = STOP_MODE;
        } else if (mode.equalsIgnoreCase("jndi")) {
            userMode = JNDI_MODE;
        } else if (mode.equalsIgnoreCase("check")) {
            userMode = CHECK_MODE;
        } else if (mode.equalsIgnoreCase("version")) {
            userMode = VERSION_MODE;
        } else if (mode.equalsIgnoreCase("ping")) {
            userMode = PING_MODE;
        } else {
            throw new BuildException("The mode '" + mode + "' is invalid.");
        }

        // Start the task
        Java bootstrapTask = null;
        switch (userMode) {
            default:
            case START_MODE:
                bootstrapTask = getBootstraptask(START_CLASS);
                break;
            case STOP_MODE:
                bootstrapTask = getBootstraptask(ADMIN_CLASS);
                bootstrapTask.createArg().setValue("-s");
                break;
            case JNDI_MODE:
                bootstrapTask = getBootstraptask(ADMIN_CLASS);
                bootstrapTask.createArg().setValue("-j");
                break;
            case CHECK_MODE:
                bootstrapTask = getBootstraptask(CHECKENV_CLASS);
                break;
            case VERSION_MODE:
                bootstrapTask = getBootstraptask(VERSION_CLASS);
                break;
            case PING_MODE:
                bootstrapTask = getBootstraptask(ADMIN_CLASS);
                bootstrapTask.createArg().setValue("-ping");
                break;
        }

        bootstrapTask.executeJava();

    }

    /**
     * @return the mode.
     */
    public String getMode() {
        return mode;
    }

    /**
     * Sets the mode for the server (start or stop)
     * @param mode The mode to set.
     */
    public void setMode(String mode) {
        this.mode = mode;
    }
}