/*
 * JBoss, the OpenSource J2EE webOS
 *
 * Distributable under LGPL license.
 * See terms of license at gnu.org.
 */
package org.jboss.ejb3.dd;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import org.jboss.logging.Logger;

/**
 * Represents EJB elements of the ejb-jar.xml deployment descriptor for the 1.4
 * schema
 *
 * @author <a href="mailto:bdecoste@jboss.com">William DeCoste</a>
 *         ejbs.setCurrentEjbName(value); } else if
 *         (localName.equals("jndi-name")){ ejbs.setJndiName(value);
 * @version <tt>$Revision: 1.9.2.4 $</tt>
 */
public class EnterpriseBeans
{
   private static final Logger log = Logger.getLogger(EnterpriseBeans.class);

   private List enterpriseBeans = new ArrayList();

   private EnterpriseBean currentEjb;
   
   public void addInvoker(Invoker invoker)
   {
      currentEjb.addInvoker(invoker);
   }

   public void setCurrentEjbName(String currentEjbName)
   {
      currentEjb = findEjbByEjbName(currentEjbName);
   }

   public void updateEjbRef(EjbRef ref)
   {
      currentEjb.updateEjbRef(ref);
   }

   public void updateEjbLocalRef(EjbLocalRef ref)
   {
      currentEjb.updateEjbLocalRef(ref);
   }

   public void updateResourceRef(ResourceRef ref)
   {
      currentEjb.updateResourceRef(ref);
   }

   public void setAopDomainName(String aopDomainName)
   {
      currentEjb.setAopDomainName(aopDomainName);
   }

   public void setRunAsPrincipal(String principal)
   {
      currentEjb.setRunAsPrincipal(principal);
   }

   public void setClusterConfig(ClusterConfig config)
   {
      currentEjb.setClusterConfig(config);
   }

   public void setCallByValue(boolean callByValue)
   {
      currentEjb.setCallByValue(callByValue);
   }

   public void setJndiName(String jndiName)
   {
      currentEjb.setJndiName(jndiName);
   }

   public void setSecurityDomain(String securityDomain)
   {
      currentEjb.setSecurityDomain(securityDomain);
   }

   public void setLocalJndiName(String jndiName)
   {
      currentEjb.setLocalJndiName(jndiName);
   }

   public EnterpriseBean findEjbByEjbName(String ejbName)
   {
      Iterator ejbs = enterpriseBeans.iterator();
      while (ejbs.hasNext())
      {
         EnterpriseBean ejb = (EnterpriseBean) ejbs.next();
         if (ejb.getEjbName().equals(ejbName))
            return ejb;
      }
      return null;
   }

   public List<EnterpriseBean> findEjbsByClass(String className)
   {
      ArrayList<EnterpriseBean> result = new ArrayList<EnterpriseBean>();

      Iterator ejbs = enterpriseBeans.iterator();
      while (ejbs.hasNext())
      {
         EnterpriseBean ejb = (EnterpriseBean) ejbs.next();
         String ejbClassName = ejb.getEjbClass();
         if (ejbClassName != null && ejbClassName.equals(className))
            result.add(ejb);
      }
      return result;
   }

   public EnterpriseBean findEjbByRemote(String remote)
   {
      Iterator ejbs = enterpriseBeans.iterator();
      while (ejbs.hasNext())
      {
         EnterpriseBean ejb = (EnterpriseBean) ejbs.next();
         if (ejb.getRemote() != null && ejb.getRemote().equals(remote))
            return ejb;
      }
      return null;
   }

   public EnterpriseBean findEjbByLocal(String local)
   {
      Iterator ejbs = enterpriseBeans.iterator();
      while (ejbs.hasNext())
      {
         EnterpriseBean ejb = (EnterpriseBean) ejbs.next();
         if (ejb.getLocal() != null && ejb.getLocal().equals(local))
            return ejb;
      }
      return null;
   }

   public List getEnterpriseBeans()
   {
      return enterpriseBeans;
   }

   public void setEnterpriseBeans(List enterpriseBeans)
   {
      this.enterpriseBeans = enterpriseBeans;
   }

   public void addEnterpriseBean(EnterpriseBean ejb)
   {
      enterpriseBeans.add(ejb);
   }

   public String toString()
   {
      StringBuffer sb = new StringBuffer(100);
      sb.append("[");
      sb.append("enterpriseBeans=").append(enterpriseBeans);
      sb.append("]");
      return sb.toString();
   }

   public boolean equals(Object o)
   {
      if (this == o)
         return true;
      if (!(o instanceof EnterpriseBeans))
         return false;

      final EnterpriseBeans ejbs = (EnterpriseBeans) o;

      if (enterpriseBeans != null ? !enterpriseBeans
              .equals(ejbs.enterpriseBeans) : ejbs.enterpriseBeans != null)
         return false;

      return true;
   }

   public int hashCode()
   {
      int result;
      result = (enterpriseBeans != null ? enterpriseBeans.hashCode() : 0);
      // result = 29 * result + (version != null ? version.hashCode() : 0);
      return result;
   }
}
