/**
 * 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.jonasbase;

import org.objectweb.jonas.ant.JOnASBaseTask;

/**
 * Defines properties for carol.properties
 * @author Florent Benoit
 */
public class Carol extends Tasks {

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

    /**
     * Protocols property
     */
    private static final String PROTOCOLS_PROPERTY = "carol.protocols";

    /**
     * Default protocols
     */
    private static final String DEFAULT_PROTOCOLS = "jrmp";

    /**
     * Default Host
     */
    private static final String DEFAULT_HOST = "localhost";

    /**
     * Default protocol header
     */
    private static final String PROTOCOL_HEADER = "://";

    /**
     * Default jrmp port number
     */
    private static final String DEFAULT_JRMP_PORT = "1099";

    /**
     * Default jeremie port number
     */
    private static final String DEFAULT_JEREMIE_PORT = "2000";

    /**
     * Default iiop port number
     */
    private static final String DEFAULT_IIOP_PORT = "2001";

    /**
     * Default cmi port number
     */
    private static final String DEFAULT_CMI_PORT = "2002";

    /**
     * Default irmi port number
     */
    private static final String DEFAULT_IRMI_PORT = "1098";

    /**
     * Local Registry
     */
    private static final String LOCALREGISTRY_PROPERTY = "carol.jvm.rmi.local.registry=";

    /**
     * Local Registry(off)
     */
    private static final String LOCALREGISTRY_OFF = LOCALREGISTRY_PROPERTY
            + "false";

    /**
     * Local Registry (on)
     */
    private static final String LOCALREGISTRY_ON = LOCALREGISTRY_PROPERTY
            + "true";

    /**
     * Default cmi mcast addr
     */
    private static final String DEFAULT_CMI_MCASTADDR = "224.0.0.35";

    /**
     * Default cmi mcast port
     */
    private static final String DEFAULT_CMI_MCASTPORT = "35467";

    /**
     * Optimization
     */
    private static final String OPTIMIZATION_PROPERTY = "carol.jvm.rmi.local.call=";

    /**
     * Optimization (off)
     */
    private static final String OPTIMIZATION_OFF = OPTIMIZATION_PROPERTY + "false";

    /**
     * Optimization (on)
     */
    private static final String OPTIMIZATION_ON = OPTIMIZATION_PROPERTY + "true";

    /**
     * CMI mcast addr attribut
     */
    private static final String CMI_MCASTADDR_ATTR = "mcast_addr";

    /**
     * CMI mcast port attribut
     */
    private static final String CMI_MCASTPORT_ATTR = "mcast_port";

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

    /**
     * Set the port for a protocol
     * @param protocolName name of the protocol
     * @param hostName for the protocol
     * @param portNumber port for a given protocol
     * @param protocolUrl URL for the protocol (if any)
     * @param token of the protocol to replace
     */
    private void setURL(String protocolName, String hostName, String portNumber, String protocolUrl, String token) {
        String url = null;

        // Token to replace
        token = protocolName + PROTOCOL_HEADER + DEFAULT_HOST + ":" + token;

        if (protocolUrl != null) {
            url = protocolUrl;
        } else {
            url = protocolName + PROTOCOL_HEADER + hostName + ":" + portNumber;
        }
        JReplace propertyReplace = new JReplace();
        propertyReplace.setLogInfo(INFO + "Setting URL for protocol '" + protocolName + "' to : " + url);
        propertyReplace.setConfigurationFile(JOnASBaseTask.CAROL_CONF_FILE);
        propertyReplace.setToken(token);
        propertyReplace.setValue(url);
        addTask(propertyReplace);
    }

    /**
     * Set the port for JRMP
     * @param portNumber port for JRMP
     */
    public void setJrmpPort(String portNumber) {
        setURL("rmi", DEFAULT_HOST, portNumber, null, DEFAULT_JRMP_PORT);
    }

    /**
     * Set the port for JEREMIE
     * @param portNumber port for JEREMIE
     */
    public void setJeremiePort(String portNumber) {
        setURL("jrmi", DEFAULT_HOST, portNumber, null, DEFAULT_JEREMIE_PORT);
    }

    /**
     * Set the port for IIOP
     * @param portNumber port for IIOP
     */
    public void setIiopPort(String portNumber) {
        setURL("iiop", DEFAULT_HOST, portNumber, null, DEFAULT_IIOP_PORT);
    }

    /**
     * Set the port for CMI
     * @param portNumber port for CMI
     */
    public void setCmiPort(String portNumber) {
        setURL("cmi", DEFAULT_HOST, portNumber, null, DEFAULT_CMI_PORT);
    }

    /**
     * Set the port for IRMI
     * @param portNumber port for IRMI
     */
    public void setIrmiPort(String portNumber) {
        setURL("rmi", DEFAULT_HOST, portNumber, null, DEFAULT_IRMI_PORT);
    }

    /**
     * Set mcastAddr for CMI
     * @param mcastAddr multicast address
     */
    public void setCmiMcastAddr(String mcastAddr) {

        // Token to replace the multicast addr
        String token = CMI_MCASTADDR_ATTR + "=" + "\"" + DEFAULT_CMI_MCASTADDR + "\"";
        String value = CMI_MCASTADDR_ATTR + "=" + "\"" + mcastAddr + "\"";
        JReplace mcastAddrReplace = new JReplace();
        mcastAddrReplace.setLogInfo(INFO + "Setting mcastaddr for protocol cmi");
        mcastAddrReplace.setConfigurationFile(JOnASBaseTask.JGROUPS_CMI_CONF_FILE);
        mcastAddrReplace.setToken(token);
        mcastAddrReplace.setValue(value);
        addTask(mcastAddrReplace);
    }

    /**
     * Set mcastPort for CMI
     * @param mcastPort multicast port
     */
    public void setCmiMcastPort(String mcastPort) {

        // Token to replace the multicast port
        String token = CMI_MCASTPORT_ATTR + "=" + "\"" + DEFAULT_CMI_MCASTPORT + "\"";
        String value = CMI_MCASTPORT_ATTR + "=" + "\"" + mcastPort + "\"";
        JReplace mcastPortReplace = new JReplace();
        mcastPortReplace.setLogInfo(INFO + "Setting mcastport for protocol cmi");
        mcastPortReplace.setConfigurationFile(JOnASBaseTask.JGROUPS_CMI_CONF_FILE);
        mcastPortReplace.setToken(token);
        mcastPortReplace.setValue(value);
        addTask(mcastPortReplace);

    }

    /**
     * Set the port for all protocols
     * @param portNumber port for all protocols
     */
    public void setDefaultPort(String portNumber) {
        setJrmpPort(portNumber);
        setJeremiePort(portNumber);
        setIiopPort(portNumber);
        setCmiPort(portNumber);
        setIrmiPort(portNumber);
    }

    /**
     * Set the initial protocols when JOnAS start
     * @param protocols comma separated list of protocols
     */
    public void setProtocols(String protocols) {
        JReplace propertyReplace = new JReplace();
        propertyReplace.setConfigurationFile(JOnASBaseTask.CAROL_CONF_FILE);
        propertyReplace.setToken(PROTOCOLS_PROPERTY + "=" + DEFAULT_PROTOCOLS);
        propertyReplace.setValue(PROTOCOLS_PROPERTY + "=" + protocols);
        propertyReplace.setLogInfo(INFO + "Setting protocols to : " + protocols);
        addTask(propertyReplace);
    }

    /**
     * Enable or disable optimization
     * @param enabled (true or false)
     */
    public void setJrmpOptimization(boolean enabled) {
        // Change only if needed
        if (!enabled) {
            return;
        }
        JReplace propertyReplace = new JReplace();
        propertyReplace.setConfigurationFile(JOnASBaseTask.CAROL_CONF_FILE);
        propertyReplace.setToken(OPTIMIZATION_OFF);
        propertyReplace.setValue(OPTIMIZATION_ON);
        propertyReplace.setLogInfo(INFO + "Enable JRMP optimization: "+enabled);
        addTask(propertyReplace);
    }

    /**
     * Enable or disable registry local
     *
     * @param enabled
     *            (true or false)
     */
    public void setLocalRegistry(boolean enabled) {
        // Change only if needed
        if (!enabled) {
            return;
        }
        JReplace propertyReplace = new JReplace();
        propertyReplace.setConfigurationFile(JOnASBaseTask.CAROL_CONF_FILE);
        propertyReplace.setToken(LOCALREGISTRY_OFF);
        propertyReplace.setValue(LOCALREGISTRY_ON);
        propertyReplace.setLogInfo(INFO + "Enable local registry  setting: "
                + enabled);
        addTask(propertyReplace);
    }

}