/**
 * 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 1any 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: Christophe Ney
 * --------------------------------------------------------------------------
 * $Id$
 * --------------------------------------------------------------------------
 */

package org.objectweb.jonas_lib.deployment.api;

import org.objectweb.jonas_lib.deployment.xml.ResourceRef;
import org.objectweb.jonas_lib.deployment.xml.JonasResource;


/**
 * This class represents the description of a ResourceRef object
 * @author Christophe Ney
 * @author Florent Benoit
 */
public class ResourceRefDesc {

    /**
     * To indicate that the application manage the authentication.
     */
    public static final int APPLICATION_AUTH = 0;

    /**
     * To indicate that the container manage the authentication.
     */
    public static final int CONTAINER_AUTH   = 1;

    /**
     * List of all possible authentication.
     */
    private static final String[] AUTH = {"APPLICATION_AUTH", "CONTAINER_AUTH"};

    /**
     * The resource ref name.
     */
    private String name;

    /**
     * The resource ref type name.
     */
    private String typeName;

    /**
     * The resource ref authentication.
     */
    private int authentication;

    /**
     * The resource ref jndi name.
     */
    private String jndiName;

    /**
     * Construct a descriptor for the resource-ref and jonas-resource tags.
     * @param classLoader the classloader for the classes.
     * @param res the resource-ref resulting of the web.xml parsing.
     * @param jRes the jonas-resource resulting of the jonas-web.xml parsing.
     * @throws DeploymentDescException when missing information for
     * creating the ResourceRefDesc.
     */
    public ResourceRefDesc(ClassLoader classLoader,
                           ResourceRef res, JonasResource jRes)
        throws DeploymentDescException {
        name = res.getResRefName();
        typeName = new String(res.getResType());
        String auth = res.getResAuth();

        if (auth.equals("Application")) {
            authentication = APPLICATION_AUTH;
        } else if (auth.equals("Container")) {
            authentication = CONTAINER_AUTH;
        } else {
            throw new DeploymentDescException("res-auth not valid for resource-ref " + name);
        }
        jndiName = jRes.getJndiName();
    }

    /**
     * Get resource ref. name
     * @return Name of the resource ref.
     */
    public String getName() {
        return name;
    }

    /**
     * Get resource ref. type name
     * @return Class name of the resource ref.
     */
    public String getTypeName() {
        return typeName;
    }

    /**
     * Get the authentication of the resource ref.
     * @return Authentication value within APPLICATION_AUTH, CONTAINER_AUTH
     */
    public int getAuthentication() {
        return authentication;
    }

    /**
     * Assessor for JDBC resource
     * @return true if the resource is Jdbc compliant
     */
    public boolean isJdbc() {
        return "javax.sql.DataSource".equals(typeName);
    }

    /**
     * Get the jndi name of the resource ref.
     * @return String representation of the JNDI name
     */
    public String getJndiName() {
        return jndiName;
    }

    /**
     * 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("\ngetTypeName()=" + getTypeName());
        ret.append("\ngetAuthentication()=" + AUTH[getAuthentication()]);
        ret.append("\nisJdbc()=" + new Boolean(isJdbc()).toString());
        ret.append("\ngetJndiName()=" + getJndiName());
        return ret.toString();
    }
}
