/*
 * JOnAS: Java(TM) Open Application Server
 * Copyright (C) 1999 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(s): ____________________________________.
 * Contributor(s): Lutris Technologies Inc http://www.lutris.com
 *
 * --------------------------------------------------------------------------
 * $Id$
 * --------------------------------------------------------------------------
*/


package org.objectweb.jonas_ejb.deployment.api;


import java.lang.Class;
import java.lang.String;
import java.lang.Object;


/**
 * The EnvEntry class corresponds to the 'env-entry' element in the XML DTD
 * for EJB 1.1 deployment descriptor.
 *
 * The env-entry element contains the declaration of an enterprise bean's environment entry.
 * The declaration consists of:
 *   - an optional description (which is not taken into account here),
 *   - the name of the environment entry,
 *   - the type of the environment entry,
 *   - and an optional value.
 * @author Helene Joanin, Christophe Ney
 */
public class EnvEntryDesc {

    String name;
    Class type;
    Object value;

    /**
     * Package protected constructor used by API
     */
    EnvEntryDesc(org.objectweb.jonas_ejb.deployment.xml.EnvEntry env) 
            throws DeploymentDescException {
        name = env.getEnvEntryName().getValue();
        String t = env.getEnvEntryType().getValue();
        String v = (env.getEnvEntryValue()!=null) ? env.getEnvEntryValue().getValue() : null;
        try {        
            if (t.equals(Boolean.class.getName())) {
                type = Boolean.class;
                if (v!=null){
                    if (v.equalsIgnoreCase("true"))
                        value = Boolean.TRUE;
                    else if (v.equalsIgnoreCase("false")) 
                        value = Boolean.FALSE;
                    else 
                        throw new DeploymentDescException(v+" is not a valid value for env-entry "+name);
                }
            } else if (t.equals(String.class.getName())) {
                type = String.class;
                value = v;
            } else if (t.equals(Integer.class.getName())) {
                type = Integer.class;
                value = (v!=null)? new Integer(v):null;
            } else if (t.equals(Double.class.getName())) {
                type = Double.class;
                value = (v!=null)? new Double(v):null;
            } else if (t.equals(Byte.class.getName())) {
                type = Byte.class;
                value = (v!=null)? new Byte(v):null;
            } else if (t.equals(Short.class.getName())) {
                type = Short.class;
                value = (v!=null)? new Short(v):null;
            } else if (t.equals(Long.class.getName())) {
                type = Long.class;
                value = (v!=null)? new Long(v):null;
            } else if (t.equals(Float.class.getName())) {
                type = Float.class;
                value = (v!=null)? new Float(v):null;
            } else {
                throw new DeploymentDescException(t+" is not a valid type for env-entry "+name);
            }
        } catch (NumberFormatException e) {
            throw new DeploymentDescException(v+" is not a valid value for env-entry "+name,e);
        }
        
    }


    /**
     * Get the name of the environemt entry.
     * @return Name for environment entry
     */
    public String getName() {
            return name;
    }

    /**
     * Get the fully-qualified Java type of the environemt entry.
     * Type is needed since value is optional
     * The possibles values are:
     *   java.lang.Boolean
     *   java.lang.String
     *   java.lang.Integer
     *   java.lang.Double
     *   java.lang.Byte
     *   java.lang.Short
     *   java.lang.Long
     *   java.lang.Float
     */
    public Class getType() {
            return type;
    }

    /**
     * Assessor for existence of value for the descriptor
     * @return true if a value is available
     */
    public boolean hasValue() {
            return value!=null;
    }

    /**
     * Get the value of the environment entry.
     * @return value for the environment entry (must be set)
     */
    public Object getValue() {
        if (value==null)
            throw new Error("Value not set for env-entry "+name);
            return value;
    }

    /**
     * String representation of the object for test purpose
     * @return String representation of this object
     */
    public String toString() {
        StringBuffer ret = new StringBuffer();
        ret.append("\ngetName()="+getName());
        ret.append("\ngetType()="+getType());
        if (hasValue())
            ret.append("\ngetValue()="+getValue().toString());
        return ret.toString();
    }
    
}

