/* JFox, the OpenSource J2EE Application Server
 *
 * Copyright (C) 2002 huihoo.com
 * Distributable under GNU LGPL license
 * See the GNU Lesser General Public License for more details.
 */

package org.jfox.ejb.meta;

import java.net.URL;
import java.util.Collections;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.List;
import java.util.ArrayList;

import org.w3c.dom.Element;
import org.w3c.dom.Node;

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

public class EJBBundle extends Descriptor {
  private String displayName;
  private String smallIcon;
  private String largeIcon;
  // ejb-name => ejbDescriptor
  private Map ejbs = new HashMap();

  public EJBBundle(URL ejbJarUrl) throws EJBDescriptionException {
    Element docElement = EjbJarDocumentBuilder.getEjbJarDocument(ejbJarUrl);
    processXML(docElement);
  }

  /**
   * node is the Document element
   * @param node
   * @throws EJBDescriptionException
   */
  public void processXML(Node node) throws EJBDescriptionException {
    super.processXML(node);
    setDisplayName(XmlUtil.getChildNodeValueOf(node,"display-name"));
    setSmallIcon(XmlUtil.getChildNodeValueOf(node,"small-icon"));
    setLargeIcon(XmlUtil.getChildNodeValueOf(node,"large-icon"));

    // session bean
    Iterator sessionBeans = XmlUtil.getElementsByTagName((Element)node,"session");
    while(sessionBeans.hasNext()){
      Element session = (Element)sessionBeans.next();
      SessionBeanDescriptor sbd = new SessionBeanDescriptor();
      sbd.processXML(session);
      addEjb(sbd);
    }

    // entity bean not supported now
    // message driven bean not supported now
    // security-role not supported now
    // method-permission not supported now

    Iterator cmts = XmlUtil.getElementsByTagName((Element)node,"container-transaction");
    while(cmts.hasNext()){
      Element cmt = (Element)cmts.next();
      ContainerTransactionDescriptor ctd = new ContainerTransactionDescriptor(this);
      // will register cmt to EJBDescriptor
      ctd.processXML(cmt);
    }
  }

  public String getDisplayName() {
    return displayName;
  }

  public void setDisplayName(String displayName) {
    this.displayName = displayName;
  }

  public String getSmallIcon() {
    return smallIcon;
  }

  public void setSmallIcon(String smallIcon) {
    this.smallIcon = smallIcon;
  }

  public String getLargeIcon() {
    return largeIcon;
  }

  public void setLargeIcon(String largeIcon) {
    this.largeIcon = largeIcon;
  }

  private void addEjb(EJBDescriptor ejbDesc) {
    ejbDesc.setEJBBundle(this);
    ejbs.put(ejbDesc.getEjbName(),ejbDesc);
  }

  public EJBDescriptor getEjb(String ejbName) {
    return (EJBDescriptor)ejbs.get(ejbName);
  }

  public List getEjbs() {
    return Collections.unmodifiableList(new ArrayList(ejbs.values()));
  }

  public static void main(String[] args) {

  }

}