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

package org.objectweb.jonas.ant.cluster;

import java.io.File;
import java.util.Iterator;

import org.objectweb.jonas.ant.jonasbase.BaseTaskItf;
import org.objectweb.jonas.ant.jonasbase.Carol;

/**
 * Define CarolCluster task
 * @author Benoit Pelletier
 */
public class CarolCluster extends ClusterTasks {

    /**
     * Info for the logger
     */
    private static final String INFO = "[CarolCluster] ";

    /**
     * ports range
     */
    private String[] portRange = null;

    /**
     * protocols
     */
    private String protocols = null;

    /**
     * multicast addr
     */
    private String mcastAddr = null;

    /**
     * multicast port
     */
    private String mcastPort = null;

    /**
     * jrmp optimization
     */
    private boolean jrmpOptimization = false;

    /**
     * Default constructor
     */
    public CarolCluster() {
        super();
    }

    /**
     * Set protocols
     * @param protocols protocols to set
     */
    public void setProtocols(String protocols) {
        this.protocols = protocols;
    }

    /**
     * Set mcastAddr
     * @param mcastAddr multicast address to set
     */
    public void setMcastAddr(String mcastAddr) {
        this.mcastAddr = mcastAddr;
    }

    /**
     * Set mcastPort
     * @param mcastPort multicast port to set
     */
    public void setMcastPort(String mcastPort) {
        this.mcastPort = mcastPort;
    }

    /**
     * Set ports range
     * @param portRange ports range
     */
    public void setPortRange(String portRange) {
        this.portRange = portRange.split(",");

    }

    /**
     * Set jrmp optimization
     * @param jrmpOptimization jrmp optimization
     */
    public void setJrmpOptimization(boolean jrmpOptimization) {
        this.jrmpOptimization = jrmpOptimization;

    }

    /**
     * Generates the carol tasks for each JOnAS's instances
     */
    public void generatesTasks() {

        int portInd = 0;

        for (int i = getDestDirSuffixIndFirst(); i <= getDestDirSuffixIndLast(); i++) {

            String destDir = getDestDir(getDestDirPrefix(), i);

            // creation of the Carol tasks
            Carol carol = new Carol();
            log(INFO + "tasks generation for " + destDir);
            carol.setDefaultPort(portRange[portInd]);
            carol.setProtocols(protocols);
            carol.setCmiMcastAddr(mcastAddr);
            carol.setCmiMcastPort(mcastPort);

            carol.setJrmpOptimization(jrmpOptimization);

            // set destDir for each carol task
            for (Iterator it = carol.getTasks().iterator(); it.hasNext();) {
                BaseTaskItf task = (BaseTaskItf) it.next();
                task.setDestDir(new File(destDir));
            }

            addTasks(carol);

            portInd++;

        }
    }
}