/* JFox, the OpenSource J2EE Application Server
 *
 * Distributable under GNU LGPL license by gun.org
 * more details please visit http://www.huihoo.org/jfox
 */
package org.jfox.ejb.meta;

import org.w3c.dom.Node;

/**
 * @author <a href="mailto:young_yy@hotmail.com">Young Yang</a>
 */

public class ResourceRefDescriptor extends Descriptor {

    public static final String APPLICATION_AUTHORIZATION = "Application";
    public static final String CONTAINER_AUTHORIZATION = "Container";
    private String refName;
    private String type;
    private String auth;
//  private String jndiName;

    public ResourceRefDescriptor() {

    }

    public void processXML(Node node) throws EJBDescriptionException {
        super.processXML(node);
        setRefName(XmlUtil.getChildNodeValueOf(node, "res-ref-name"));
        setType(XmlUtil.getChildNodeValueOf(node, "res-type"));
        setAuthorization(XmlUtil.getChildNodeValueOf(node, "res-auth"));
    }

    public boolean equals(Object obj) {
        if(obj instanceof ResourceRefDescriptor)
            return refName.equals(((ResourceRefDescriptor) obj).refName);
        else
            return false;
    }

    public String getAuthorization() {
        return auth;
    }

    /**
     * use refName as the jndi name for simpty
     *
     * @return
     */
    public String getJndiName() {
/*
    if (jndiName == null)
      jndiName = "";
    return jndiName;
*/
        return refName;
    }

    public String getRefName() {
        return refName;
    }

    public String getType() {
        return type;
    }

    public int hashCode() {
        return refName.hashCode();
    }

    public boolean isResolved() {
//    return jndiName != null && !"".equals(jndiName);
        return refName != null && !"".equals(refName);
    }

    public void setAuthorization(String s) {
        if(!"Application".equals(s) && !"Container".equals(s)) {
            throw new IllegalArgumentException("res-auth must be one of \"Application\" and \"Container\"");
        }
        else {
            auth = s;
            return;
        }
    }

/*
  public void setJndiName(String s) {
    jndiName = s;
  }
*/

    public void setRefName(String s) {
        refName = s;
    }

    public void setType(String s) {
        type = s;
    }

    public String toString() {
        return "res-ref-name: " + getRefName() + "\n" + "res-type: " + getType() + "\n" + "jndi-name: " + getJndiName() + "\n";
    }

    public static void main(String[] args) {

    }
}

