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

import java.lang.reflect.Method;
import javax.ejb.TransactionAttribute;
import javax.ejb.TransactionAttributeType;
import javax.ejb.TransactionManagementType;
import org.jboss.aop.Advisor;
import org.jboss.aop.joinpoint.Joinpoint;
import org.jboss.aop.joinpoint.MethodJoinpoint;

/**
 * This interceptor handles transactions for AOP
 *
 * @author <a href="mailto:bill@jboss.org">Bill Burke</a>
 * @version $Revision: 1.3.2.3 $
 */
public class TxInterceptorFactory extends org.jboss.aspects.tx.TxInterceptorFactory
{
   protected String resolveTxType(Advisor advisor, Joinpoint jp)
   {
      Method method = ((MethodJoinpoint) jp).getMethod();
      TransactionAttribute tx = (TransactionAttribute) advisor.resolveAnnotation(method, TransactionAttribute.class);

      String value = "REQUIRED";
      if (tx != null)
      {
         TransactionAttributeType type = tx.value();

         if (type == null)
         {
            value = "REQUIRED";
         }
         else if (type == TransactionAttributeType.NOT_SUPPORTED)
         {
            value = "NOTSUPPORTED";
         }
         else if (type == TransactionAttributeType.REQUIRES_NEW)
         {
            value = "REQUIRESNEW";
         }
         else
         {
            value = type.name();
         }
      }

      return value;
   }

   protected void initializePolicy()
   {
      policy = new Ejb3TxPolicy();
   }

   public Object createPerJoinpoint(Advisor advisor, Joinpoint jp)
   {
      // We have to do this until AOP supports matching based on annotation attributes
      TransactionManagementType type = TxUtil.getTransactionManagementType(advisor);
      if (type == TransactionManagementType.BEAN) return new NullInterceptor();
      return super.createPerJoinpoint(advisor, jp);
   }


   public String getName()
   {
      return getClass().getName();
   }
}
