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

import java.lang.reflect.Method;
import java.util.ArrayList;

import javax.ejb.EJBHome;
import javax.ejb.EJBMetaData;
import javax.ejb.EJBObject;
import javax.ejb.Handle;
import javax.ejb.HomeHandle;

import org.jboss.aop.advice.Interceptor;
import org.jboss.aop.joinpoint.MethodInvocation;
import org.jboss.aop.util.MethodHashing;
import org.jboss.aop.util.PayloadKey;
import org.jboss.aspects.asynch.AsynchMixin;
import org.jboss.aspects.asynch.AsynchProvider;
import org.jboss.aspects.asynch.AsynchProxyInterceptor;
import org.jboss.aspects.asynch.FutureHolder;
import org.jboss.ejb3.asynchronous.AsynchronousInterceptor;
import org.jboss.ejb3.remoting.Proxy;

/**
 * @author <a href="mailto:kabir.khan@jboss.org">Kabir Khan</a>
 * @version $Revision: 1.1.2.4 $
 */
public class ProxyUtils
{
   public final static Class ASYNCH_PROVIDER_CLASS = AsynchProvider.class;
   public final static long GET_ASYNCHRONOUS;
   public static final long TO_STRING;
   public static final long EQUALS;
   public static final long HASHCODE;
   public static final Method GET_PRIMARY_KEY;
   public static final Method GET_HANDLE;
   public static final Method GET_EJB_HOME;
   public static final Method IS_IDENTICAL;
   public static final Method GET_HOME_HANDLE;
   public static final Method GET_EJB_METADATA;
   public static final Method REMOVE;

   static
   {
      try
      {
         Method method = JBossProxy.class.getMethod("getAsynchronousProxy");
         GET_ASYNCHRONOUS = MethodHashing.calculateHash(method);
         TO_STRING = MethodHashing.calculateHash(Object.class.getDeclaredMethod("toString", new Class[0]));
         EQUALS = MethodHashing.calculateHash(Object.class.getDeclaredMethod("equals", new Class[]{Object.class}));
         HASHCODE = MethodHashing.calculateHash(Object.class.getDeclaredMethod("hashCode", new Class[0]));
         
         Class[] empty = {};
              
         GET_PRIMARY_KEY = EJBObject.class.getMethod("getPrimaryKey", empty);
         GET_HANDLE = EJBObject.class.getMethod("getHandle", empty);
         GET_EJB_HOME = EJBObject.class.getMethod("getEJBHome", empty);
         IS_IDENTICAL = EJBObject.class.getMethod("isIdentical", new Class[] { EJBObject.class });
         REMOVE = EJBObject.class.getMethod("remove", empty);
                 
         GET_HOME_HANDLE = EJBHome.class.getMethod("getHomeHandle", empty);
         GET_EJB_METADATA = EJBHome.class.getMethod("getEJBMetaData", empty);
      }
      catch (NoSuchMethodException e)
      {
         throw new RuntimeException(e);
      }
   }

   public static boolean isAsynchronous(Class[] infs)
   {
      for (int i = 0; i < infs.length; i++)
      {
         if (infs[i] == ASYNCH_PROVIDER_CLASS)
         {
            return true;
         }
      }
      return false;
   }

   public static Class[] addAsynchProviderInterface(Class[] infs)
   {
      ArrayList interfaces = new ArrayList();

      for (int i = 0; i < infs.length; i++)
      {
         if (infs[i] == ASYNCH_PROVIDER_CLASS)
         {
            //This should not happen
            continue;
         }
         interfaces.add(infs[i]);
      }

      interfaces.add(ASYNCH_PROVIDER_CLASS);
      return (Class[]) interfaces.toArray(new Class[interfaces.size()]);
   }

   public static Interceptor[] addAsynchProxyInterceptor(AsynchMixin mixin, Interceptor[] interceptors)
   {
      AsynchProxyInterceptor interceptor = new AsynchProxyInterceptor(mixin);
      Interceptor[] newInterceptors = null;
      newInterceptors = new Interceptor[interceptors.length + 1];
      newInterceptors[0] = interceptor;
      System.arraycopy(interceptors, 0, newInterceptors, 1, interceptors.length);
      return newInterceptors;
   }

   public static void addLocalAsynchronousInfo(MethodInvocation invocation, FutureHolder provider)
   {
      if (provider != null)
      {
         invocation.getMetaData().addMetaData(AsynchronousInterceptor.ASYNCH, AsynchronousInterceptor.INVOKE_ASYNCH, "YES", PayloadKey.AS_IS);
         invocation.getMetaData().addMetaData(AsynchronousInterceptor.ASYNCH, AsynchronousInterceptor.FUTURE_HOLDER, provider, PayloadKey.AS_IS);
      }
   }

   public static Object handleCallLocally(JBossProxy jbproxy, Proxy ih, Method m, Object[] args)
   {
      long hash = MethodHashing.calculateHash(m);
      return handleCallLocally(hash, jbproxy, ih, m, args);
   }

   public static Object handleCallLocally(long hash, JBossProxy jbproxy, Proxy ih, Method m, Object[] args)
   {
      if (hash == ProxyUtils.GET_ASYNCHRONOUS)
      {
         return ih.getAsynchronousProxy(jbproxy);
      }
      else if (hash == TO_STRING)
      {
         return ih.toString();
      }
      else if (hash == HASHCODE)
      {
         return ih.toString().hashCode();
      }
      else if (hash == EQUALS)
      {
         return ih.toString().equals(args[0].toString());
      } 
      return null;
   }
}
