/*
 * 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 org.objectweb.jonas_ejb.deployment.xml.ResourceRef;
import org.objectweb.jonas_ejb.deployment.xml.JonasResource;


/**
 * The ResourceRef class corresponds to the 'resource-ref' element in the XML DTD
 * for EJB 1.1 deployment descriptor.
 *
 * The resource-ref element contains a declaration of enterprise bean's reference
 * to an external resource.
 * It consists of:
 *   - an optional description (which is not taken into account here),
 *   - the resource manager connection factory reference name,
 *   - the indication of the resource manager connection factory type expected by the
 *     enterprise bean code,
 *   - and the type of authentication (bean or container).
 * @author Helene Joanin, Christophe Ney
 */
public class ResourceRefDesc {

    public static final int APPLICATION_AUTH = 0;
    public static final int CONTAINER_AUTH   = 1;
    private static final String[] AUTH = {"APPLICATION_AUTH","CONTAINER_AUTH"};

    String name;
    Class type;
    int authentication;
    String jndiName;

    /**
     * Package protected constructor used by API
     */
    ResourceRefDesc(ClassLoader classLoader,ResourceRef res, JonasResource jRes) 
            throws DeploymentDescException {
        name = res.getResRefName().getValue();
        try {
            type = classLoader.loadClass(res.getResType().getValue());
        } catch(ClassNotFoundException e) {
            throw new DeploymentDescException("res-type class not found for resource-ref "+name,e);
        }
        String auth = res.getResAuth().getValue();
        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().getValue();
    }

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

    /**
     * Get resource ref. type
     * @return Class of the resource ref.
     */
    public Class getType() {
            return type;
    }

    /**
     * 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() {
        Class jdbcClass = javax.sql.DataSource.class;
        return jdbcClass.isAssignableFrom(type);
    }

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

