/*
 * 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.List;
import javax.ejb.TransactionManagementType;
import org.jboss.logging.Logger;

/**
 * Represents an EJB element of the ejb-jar.xml deployment descriptor for the
 * 1.4 schema
 *
 * @author <a href="mailto:bdecoste@jboss.com">William DeCoste</a>
 * @version <tt>$Revision: 1.10.2.4 $</tt>
 */
public abstract class EnterpriseBean
{

   private static final Logger log = Logger.getLogger(EnterpriseBean.class);

   // ejb-jar.xml
   private String ejbName = null;

   private String home = null;

   private String remote = null;

   private String localHome = null;

   private String local = null;

   private String ejbClass = null;

   private List<EjbLocalRef> ejbLocalRefs = new ArrayList();

   private List<EjbRef> ejbRefs = new ArrayList();

   private List<EnvEntry> envEntries = new ArrayList();

   private List<ResourceRef> resourceRefs = new ArrayList();
   
   private List<Invoker> invokers = new ArrayList();

   private SecurityIdentity securityIdentity;

   protected TransactionManagementType tmType = null;

   private List callbackListeners = new ArrayList();

   private List interceptors = new ArrayList();

   private List messageDestinationRefs = new ArrayList();

   // jboss.xml
   private String jndiName;

   private String localJndiName;

   private String securityDomain;

   private boolean callByValue = false;

   private ClusterConfig clusterConfig = null;

   private String aopDomainName = null;
   
   public void addInvoker(Invoker invoker)
   {
      invokers.add(invoker);
   }
   
   public List getInvokers()
   {
      return invokers;
   }

   public void updateEjbRef(EjbRef updatedRef)
   {
      for (EjbRef ref : ejbRefs)
      {
         if (ref.getEjbRefName().equals(updatedRef.getEjbRefName()))
            ref.setJndiName(updatedRef.getJndiName());
      }
   }

   public void updateEjbLocalRef(EjbLocalRef updatedRef)
   {
      for (EjbLocalRef ref : ejbLocalRefs)
      {
         if (ref.getEjbRefName().equals(updatedRef.getEjbRefName()))
            ref.setJndiName(updatedRef.getJndiName());
      }
   }

   public void updateResourceRef(ResourceRef updatedRef)
   {
      for (ResourceRef ref : resourceRefs)
      {
         if (ref.getResRefName().equals(updatedRef.getResRefName()))
         {
            ref.setJndiName(updatedRef.getJndiName());
            ref.setResourceName(updatedRef.getResourceName());
         }
      }
   }

   public void setAopDomainName(String aopDomainName)
   {
      this.aopDomainName = aopDomainName;
   }

   public String getAopDomainName()
   {
      return aopDomainName;
   }

   public void setClusterConfig(ClusterConfig clusterConfig)
   {
      this.clusterConfig = clusterConfig;
   }

   public boolean isClustered()
   {
      return clusterConfig != null;
   }

   public ClusterConfig getClusterConfig()
   {
      return clusterConfig;
   }

   public void setRunAsPrincipal(String principal)
   {
      if (securityIdentity != null)
         securityIdentity.setRunAsPrincipal(principal);
   }

   public void setCallByValue(boolean callByValue)
   {
      this.callByValue = callByValue;
   }

   public boolean isCallByValue()
   {
      return callByValue;
   }

   public String getSecurityDomain()
   {
      return securityDomain;
   }

   public void setSecurityDomain(String securityDomain)
   {
      this.securityDomain = securityDomain;
   }

   public List getMessageDestinationRefs()
   {
      return messageDestinationRefs;
   }

   public void addMessageDestinationRef(MessageDestinationRef messageDestinationRef)
   {
      messageDestinationRefs.add(messageDestinationRef);
   }

   public List getInterceptors()
   {
      return interceptors;
   }

   public void addInterceptor(String interceptor)
   {
      interceptors.add(interceptor);
   }

   public List getCallbackListeners()
   {
      return callbackListeners;
   }

   public void addCallbackListener(String callbackListener)
   {
      callbackListeners.add(callbackListener);
   }

   public String getJndiName()
   {
      return jndiName;
   }

   public void setJndiName(String jndiName)
   {
      this.jndiName = jndiName;
   }

   public String getLocalJndiName()
   {
      return localJndiName;
   }

   public void setLocalJndiName(String localJndiName)
   {
      this.localJndiName = localJndiName;
   }

   public TransactionManagementType getTransactionManagementType()
   {
      return tmType;
   }

   public boolean isSessionBean()
   {
      return this instanceof SessionEnterpriseBean;
   }

   public boolean isEntityBean()
   {
      return this instanceof EntityEnterpriseBean;
   }

   public boolean isMessageDrivenBean()
   {
      return this instanceof MessageDrivenBean;
   }

   public String getEjbName()
   {
      return ejbName;
   }

   public void setEjbName(String ejbName)
   {
      this.ejbName = ejbName;
   }

   public String getHome()
   {
      return home;
   }

   public void setHome(String home)
   {
      this.home = home;
   }

   public String getRemote()
   {
      return remote;
   }

   public void setRemote(String remote)
   {
      this.remote = remote;
   }

   public String getLocalHome()
   {
      return localHome;
   }

   public void setLocalHome(String localHome)
   {
      this.localHome = localHome;
   }

   public String getLocal()
   {
      return local;
   }

   public void setLocal(String local)
   {
      this.local = local;
   }

   public String getEjbClass()
   {
      return ejbClass;
   }

   public void setEjbClass(String ejbClass)
   {
      this.ejbClass = ejbClass;
   }

   public List getEjbLocalRefs()
   {
      return ejbLocalRefs;
   }

   public void setEjbLocalRefs(List ejbLocalRefs)
   {
      this.ejbLocalRefs = ejbLocalRefs;
   }

   public void addEjbLocalRef(EjbLocalRef ref)
   {
      ejbLocalRefs.add(ref);
   }

   public List getEjbRefs()
   {
      return ejbRefs;
   }

   public void setEjbRefs(List ejbRefs)
   {
      this.ejbRefs = ejbRefs;
   }

   public void addEjbRef(EjbRef ref)
   {
      ejbRefs.add(ref);
   }

   public List getEnvEntries()
   {
      return envEntries;
   }

   public void addEnvEntry(EnvEntry entry)
   {
      envEntries.add(entry);
   }

   public List getResourceRefs()
   {
      return resourceRefs;
   }

   public void setResourceRefs(List ejbLocalRefs)
   {
      this.resourceRefs = ejbLocalRefs;
   }

   public void addResourceRef(ResourceRef ref)
   {
      resourceRefs.add(ref);
   }

   public SecurityIdentity getSecurityIdentity()
   {
      return securityIdentity;
   }

   public void setSecurityIdentity(SecurityIdentity securityIdentity)
   {
      this.securityIdentity = securityIdentity;
   }

   public String toString()
   {
      StringBuffer sb = new StringBuffer(100);
      sb.append("ejbName=").append(ejbName);
      sb.append(",jndiName=").append(jndiName);
      sb.append(",local=").append(local);
      sb.append(",remote=").append(remote);
      sb.append(",home=").append(home);
      sb.append(",localHome=").append(localHome);
      sb.append(",ejbClass=").append(ejbClass);
      return sb.toString();
   }
}
