/*
 * JBoss, Home of Professional Open Source
 * Copyright 2005, JBoss Inc., and individual contributors as indicated
 * by the @authors tag. See the copyright.txt in the distribution for a
 * full listing of individual contributors.
 *
 * This 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 (at your option) any later version.
 *
 * This software 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 software; if not, write to the Free
 * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
 * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
 */
package org.jboss.metadata;

import java.util.HashMap;

import org.w3c.dom.Element;
import org.w3c.dom.NodeList;

import org.jboss.deployment.DeploymentException;

/** The meta data information specific to session beans.
 *
 *   @author <a href="mailto:sebastien.alborini@m4x.org">Sebastien Alborini</a>
 *   @author <a href="mailto:Scott_Stark@displayscape.com">Scott Stark</a>.
 *   @version $Revision: 1.14.2.1 $
 */
public class SessionMetaData extends BeanMetaData 
{
   // Constants -----------------------------------------------------
   public static final String DEFAULT_STATEFUL_INVOKER = "stateful-rmi-invoker";
   public static final String DEFAULT_CLUSTERED_STATEFUL_INVOKER = "clustered-stateful-rmi-invoker";
   public static final String DEFAULT_STATELESS_INVOKER = "stateless-rmi-invoker";
   public static final String DEFAULT_CLUSTERED_STATELESS_INVOKER = "clustered-stateless-rmi-invoker";
    
   // Attributes ----------------------------------------------------
   private boolean stateful;

   // Static --------------------------------------------------------
    
   // Constructors --------------------------------------------------
   public SessionMetaData(ApplicationMetaData app)
   {
      super(app, BeanMetaData.SESSION_TYPE);
   }
	
   // Public --------------------------------------------------------
   public boolean isStateful() { return stateful; }
   public boolean isStateless() { return !stateful; }
		
   public String getDefaultConfigurationName()
   {
      if (isStateful()) {
         if (this.isClustered())
            return ConfigurationMetaData.CLUSTERED_STATEFUL_13;
         else
            return ConfigurationMetaData.STATEFUL_13;
      } else {
         if (this.isClustered())
            return ConfigurationMetaData.CLUSTERED_STATELESS_13;
         else
            return ConfigurationMetaData.STATELESS_13;
      }
   }
	

   protected void defaultInvokerBindings()
   {
      invokerBindings = new HashMap();
      if (isClustered())
      {
         if (stateful)
         {
            invokerBindings.put(DEFAULT_CLUSTERED_STATEFUL_INVOKER, getJndiName());
         }
         else
         {
            invokerBindings.put(DEFAULT_CLUSTERED_STATELESS_INVOKER, getJndiName());
         }
      }
      else
      {
         if (stateful)
         {
            invokerBindings.put(DEFAULT_STATEFUL_INVOKER, getJndiName());
         }
         else
         {
            invokerBindings.put(DEFAULT_STATELESS_INVOKER, getJndiName());
         }
      }
   }

   public void importEjbJarXml(Element element) throws DeploymentException {
      super.importEjbJarXml(element);
		
      // set the session type 
      String sessionType = getElementContent(getUniqueChild(element, "session-type"));
      if (sessionType.equals("Stateful")) {
         stateful = true;
      } else if (sessionType.equals("Stateless")) {
         stateful = false;
      } else {
         throw new DeploymentException("session type should be 'Stateful' or 'Stateless'");
      }
			
      // set the transaction type
      String transactionType = getElementContent(getUniqueChild(element, "transaction-type"));
      if (transactionType.equals("Bean")) {
         containerManagedTx = false;
      } else if (transactionType.equals("Container")) {
         containerManagedTx = true;
      } else {
         throw new DeploymentException("transaction type should be 'Bean' or 'Container'");
      }
   }

   // Package protected ---------------------------------------------
    
   // Protected -----------------------------------------------------
    
   // Private -------------------------------------------------------
    
   // Inner classes -------------------------------------------------
}
