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

import java.lang.reflect.Constructor;
import java.lang.reflect.Method;
import java.lang.reflect.Field;
import java.util.HashMap;
import org.jboss.aop.Advisor;
import org.jboss.aop.InstanceAdvisor;
import org.jboss.aop.joinpoint.ConstructorJoinpoint;
import org.jboss.aop.joinpoint.Joinpoint;
import org.jboss.aop.joinpoint.MethodJoinpoint;
import org.jboss.aop.joinpoint.FieldJoinpoint;
import org.jboss.tm.TxManager;

/**
 *  This interceptor handles transactions for AOP 
 *
 *  @author <a href="mailto:bill@jboss.org">Bill Burke</a>
 *  @version $Revision: 1.4.2.4 $
 */
public class TxInterceptorFactory implements org.jboss.aop.advice.AspectFactory
{
   protected TxPolicy policy;
   protected HashMap nameMap = new HashMap();

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

   public void initialize()
   {
      if (policy != null) return;
      initializePolicy();
      nameMap.put("NEVER", new TxInterceptor.Never(TxManager.getInstance(), policy));
      nameMap.put("NOTSUPPORTED", new TxInterceptor.NotSupported(TxManager.getInstance(), policy));
      nameMap.put("SUPPORTS", new TxInterceptor.Supports(TxManager.getInstance(), policy));
      nameMap.put("REQUIRED", new TxInterceptor.Required(TxManager.getInstance(), policy));
      nameMap.put("REQUIRESNEW", new TxInterceptor.RequiresNew(TxManager.getInstance(), policy));
      nameMap.put("MANDATORY", new TxInterceptor.Mandatory(TxManager.getInstance(), policy));
   }

   protected String resolveTxType(Advisor advisor, Joinpoint jp)
   {
      if (jp instanceof ConstructorJoinpoint)
      {
         Constructor con = ((ConstructorJoinpoint)jp).getConstructor();
         String txType = (String)advisor.getConstructorMetaData().getConstructorMetaData(con, "transaction", "trans-attribute");
         if (txType != null) return txType;

         txType = (String)advisor.getDefaultMetaData().getMetaData("transaction", "trans-attribute");
         if (txType != null) return txType;

         Tx tx = (Tx)advisor.resolveAnnotation(con, Tx.class);
         if (tx == null)
         {
            tx = (Tx)advisor.resolveAnnotation(Tx.class);
         }
         if (tx == null) return "REQUIRED";
         return tx.value().name();
      }
      else if (jp instanceof MethodJoinpoint)
      {
         Method con = ((MethodJoinpoint)jp).getMethod();
         String txType = (String)advisor.getMethodMetaData().getMethodMetaData(con, "transaction", "trans-attribute");
         if (txType != null) return txType;

         txType = (String)advisor.getDefaultMetaData().getMetaData("transaction", "trans-attribute");
         if (txType != null) return txType;

         Tx tx = (Tx)advisor.resolveAnnotation(con, Tx.class);
         if (tx == null)
         {
            tx = (Tx)advisor.resolveAnnotation(Tx.class);
         }
         if (tx == null) return "REQUIRED";
         return tx.value().name();
      }
      else
      {
         Field con = ((FieldJoinpoint)jp).getField();
         String txType = (String)advisor.getFieldMetaData().getFieldMetaData(con, "transaction", "trans-attribute");
         if (txType != null) return txType;

         txType = (String)advisor.getDefaultMetaData().getMetaData("transaction", "trans-attribute");
         if (txType != null) return txType;

         Tx tx = (Tx)advisor.resolveAnnotation(con, Tx.class);
         if (tx == null)
         {
            tx = (Tx)advisor.resolveAnnotation(Tx.class);
         }
         if (tx == null) return "REQUIRED";
         return tx.value().name();
      }
   }


   public Object createPerJoinpoint(Advisor advisor, Joinpoint jp)
   {
      initialize();
      String txType = resolveTxType(advisor, jp);
      Object rtn = nameMap.get(txType.toUpperCase());
      if (rtn == null) throw new RuntimeException("TX TYPE was null for: " + txType);
      return rtn;
   }

   public Object createPerVM()
   {
      throw new IllegalStateException("Scope not allowed");
   }

   public Object createPerClass(Advisor advisor)
   {
      throw new IllegalStateException("Scope not allowed");
   }

   public Object createPerInstance(Advisor advisor, InstanceAdvisor instanceAdvisor)
   {
      throw new IllegalStateException("Scope not allowed");
   }

   public Object createPerJoinpoint(Advisor advisor, InstanceAdvisor instanceAdvisor, Joinpoint jp)
   {
      throw new IllegalStateException("Scope not allowed");
   }

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