FileDocCategorySizeDatePackage
LifecycleInvocationContextImpl.javaAPI DocJBoss 4.2.16341Fri Jul 13 20:53:52 BST 2007org.jboss.ejb3.interceptor

LifecycleInvocationContextImpl

public abstract class LifecycleInvocationContextImpl extends Object implements javax.interceptor.InvocationContext
author
Kabir Khan
version
$Revision: 60233 $

Fields Summary
private int
currentInterceptor
private int
currentMethod
org.jboss.ejb3.BeanContext
beanContext
InterceptorInfo[]
interceptorInfos
Object[]
instances
private Method[]
beanMethods
private HashMap
metadata
Constructors Summary
protected LifecycleInvocationContextImpl()

   
Methods Summary
public java.util.MapgetContextData()

      if (metadata == null) {
         metadata = new HashMap();
      }
      return metadata;
   
public static javax.interceptor.InvocationContextgetLifecycleInvocationContext(java.lang.Class type, org.jboss.ejb3.BeanContext beanContext, InterceptorInfo[] interceptorInfos, java.lang.reflect.Method[] beanMethods)

      LifecycleInvocationContextImpl ic = null;
      if (type == PostConstruct.class) ic = new PostConstructICtxImpl();
      else if (type == PostActivate.class) ic = new PostActivateICtxImpl();
      else if (type == PrePassivate.class) ic = new PrePassivateICtxImpl();
      else if (type == PreDestroy.class) ic = new PreDestroyICtxImpl();
      else throw new RuntimeException("Unsupported lifecycle event: " + type);
      
      ic.instances = beanContext.getInterceptorInstances(interceptorInfos);
      if (interceptorInfos.length != ic.instances.length)
      {
         throw new RuntimeException("interceptorInfos and instances have different length");
      }
      
      ic.beanContext = beanContext;
      ic.beanMethods = beanMethods;
      ic.interceptorInfos = interceptorInfos;
      
      return ic;
   
abstract java.lang.reflect.Method[]getLifecycleMethods(InterceptorInfo info)

public java.lang.reflect.MethodgetMethod()

      return null;
   
public java.lang.Object[]getParameters()

      return new Object[0];
   
public java.lang.ObjectgetTarget()

      return beanContext.getInstance();
   
public java.lang.Objectproceed()

      if (currentInterceptor < interceptorInfos.length)
      {
         int oldInterceptor = currentInterceptor;
         int oldMethod = currentMethod;
         try
         {
            int curr = currentInterceptor;
            int currMethod = currentMethod++;
            InterceptorInfo info = interceptorInfos[curr];
            if (currMethod == getLifecycleMethods(info).length)
            {
               curr = ++currentInterceptor;
               currentMethod = 0;
               currMethod = currentMethod++;
               info = (curr < interceptorInfos.length) ? interceptorInfos[curr] : null;
            }
            
            if (info != null)
            {
               try
               {
                  Method[] methods = getLifecycleMethods(info);
                  return methods[currMethod].invoke(instances[curr], this);
               }
               catch (InvocationTargetException e)
               {
                  if (e.getTargetException() instanceof Exception)
                  {
                     throw ((Exception) e.getCause());
                  }
                  else
                  {
                     throw new RuntimeException(e.getCause());
                  }
               }
            }
         }
         finally
         {
            // so that interceptors like clustering can reinvoke down the chain
            currentInterceptor = oldInterceptor;
            currentMethod = oldMethod;
         }
      }
      if (beanMethods != null)
      {
         try
         {
            for (Method beanMethod : beanMethods)
            {
               beanMethod.invoke(getTarget(), new Object[0]);
            }
         }
         catch (InvocationTargetException e)
         {
            if (e.getTargetException() instanceof Exception)
            {
               throw ((Exception) e.getCause());
            }
            else
            {
               throw new RuntimeException(e.getCause());
            }
         }
         finally
         {
         }
      }
      
      return null;
   
public voidsetParameters(java.lang.Object[] params)

      //Do nothing