/**
 * JOnAS: Java(TM) Open Application Server
 * Copyright (C) 2007 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 (at your option) 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., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA
 *
 * --------------------------------------------------------------------------
 * $Id$
 * --------------------------------------------------------------------------
 */

/**
 *
 */
package org.objectweb.jonas.ant.jonasbase;

import java.io.File;

import org.objectweb.jonas.ant.JOnASBaseTask;

import org.apache.tools.ant.BuildException;


/**
 * Allow to configure the global properties in jonas.properties file:
 * services
 * jonas.security.manager
 * jonas.security.propagation
 * jonas.csiv2.propagation
 * jonas.transaction.propagation
 * jonas.log.configfile
 * @author coqp
 *
 */
public class JonasProperties extends JTask implements BaseTaskItf {

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

    /**
     * Services property
     */
    private static final String SERVICES_PROPERTY = "jonas.services";

    /**
     * SecurityManager property
     */
    private static final String SECURITYMANAGER_PROPERTY = "jonas.security.manager";

    /**
     * Security Propagation property
     */
    private static final String SECURITYPROPAGATIONPROPERTY = "jonas.security.propagation";

    /**
     * Csiv2 Propagation property
     */
    private static final String CSIV2PROPAGATIONPROPERTY = "jonas.csiv2.propagation";

    /**
     * Transaction Propagation property
     */
    private static final String TRANSACTIONPROPAGATIONPROPERTY = "jonas.transaction.propagation";

    /**
     * JONAS Log Config file property
     */
    private static final String LOGCONFIGFILEPROPERTY = "jonas.log.configfile";

    /**
     * List of services names
     */
    private String services = null;

    /**
     * Enable the Security manager
     */
    private Boolean securitymanager = Boolean.TRUE;

    /**
     * Enable the Security propagation
     */
    private Boolean securitypropagation = Boolean.TRUE;

    /**
     * Enable the Csiv2 propagation
     */
    private Boolean csiv2propagation = Boolean.TRUE;

    /**
     * Enable the transaction propagation
     */
    private Boolean transactionpropagation = Boolean.TRUE;

    /**
     * JOnAS log config file  name
     */
    private String configfile = "trace";

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

    /**
     * Set the names of the services
     * @param services list of services
     */
    public void setServices(String services) {
        this.services = services;
    }

    /**
     * Enable or disable SecurityManager
     * @param enabled (true or false)
     */
    public void setSecurityManager(boolean securitymanager) {
        this.securitymanager = Boolean.valueOf(securitymanager);
    }

    /**
     * Enable or disable Security propagation
     * @param enabled (true or false)
     */
    public void setSecurityPropagation(boolean securitypropagation) {
        this.securitypropagation = Boolean.valueOf(securitypropagation);
    }

    /**
     * Enable or disable Csiv2 propagation
     * @param enabled (true or false)
     */
    public void setCsiv2Propagation(boolean csiv2propagation) {
        this.csiv2propagation = Boolean.valueOf(csiv2propagation);
    }

    /**
     * Enable or disable Transaction propagation
     * @param enabled (true or false)
     */
    public void setTransactionPropagation(boolean transactionpropagation) {
        this.transactionpropagation = Boolean.valueOf(transactionpropagation);
    }

    /**
     * Set the name of the JOnAS log config file
     * @param configfile log config file name
     */
    public void setConfigfile(String configfile) {
        this.configfile = configfile;
    }
    /**
     * Check the properties
     */
    private void checkProperties() {
        if (services == null) {
            throw new BuildException(INFO + "Property 'services' is missing.");
        }
    }

    /**
     * Execute this task
     */
    public void execute() {
        checkProperties();

        // Path to JONAS_BASE
        String jBaseConf = getDestDir().getPath() + File.separator + "conf";
        changeValueForKey(INFO, jBaseConf, JOnASBaseTask.JONAS_CONF_FILE, SERVICES_PROPERTY, services, false);
        changeValueForKey(INFO, jBaseConf, JOnASBaseTask.JONAS_CONF_FILE, SECURITYMANAGER_PROPERTY, securitymanager.toString(), false);
        changeValueForKey(INFO, jBaseConf, JOnASBaseTask.JONAS_CONF_FILE, SECURITYPROPAGATIONPROPERTY, securitypropagation.toString(), false);
        changeValueForKey(INFO, jBaseConf, JOnASBaseTask.JONAS_CONF_FILE, CSIV2PROPAGATIONPROPERTY, csiv2propagation.toString(), false);
        changeValueForKey(INFO, jBaseConf, JOnASBaseTask.JONAS_CONF_FILE, TRANSACTIONPROPAGATIONPROPERTY, transactionpropagation.toString(), false);
        changeValueForKey(INFO, jBaseConf, JOnASBaseTask.JONAS_CONF_FILE, LOGCONFIGFILEPROPERTY, configfile, false);
    }


}
