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

/**
 * Allow to define property with the value of my.${name}.property
 * @author Florent Benoit
 */
public class JProperty extends Property {

    /**
     * Internal value (to evaluate)
     */
    private String internalValue = null;

    /**
     * Default value if the key is not found
     */
    private String defaultValue = null;

    /**
     * The value of the property to set
     * @param value value to set
     */
    public void setValue(String value) {
        this.internalValue = value;
    }

    /**
     * The default value if the property cannot be found
     * @param defaultValue value to set
     */
    public void setDefaultValue(String defaultValue) {
        this.defaultValue = defaultValue;
    }

    /**
     * Execute the task. It sets the value by evaluating variable name
     * @throws BuildException if value is not set
     * @see org.apache.tools.ant.Task#execute()
     */
    public void execute() throws BuildException {

        if (internalValue == null) {
            throw new BuildException("The property '" + internalValue
                    + "' was not set.");
        }

        String valueEvaluated = getProject().getProperty(internalValue);

        // set default value
        if (valueEvaluated == null && defaultValue != null) {
            valueEvaluated = defaultValue;
        }

        // No value ? error
        if (valueEvaluated == null) {
            throw new BuildException("The property '" + internalValue
                    + "' cannot be evaluated in the current project.");
        }

        // Set the value with the value evaluated
        super.setValue(valueEvaluated);

        // Now execute the super method
        super.execute();
    }

}