1 22 package org.jboss.ejb3.interceptor; 23 24 import java.lang.reflect.InvocationTargetException ; 25 import java.lang.reflect.Method ; 26 import java.util.HashMap ; 27 28 import javax.interceptor.InvocationContext; 29 import javax.ejb.PostActivate ; 30 import javax.annotation.PostConstruct; 31 import javax.annotation.PreDestroy; 32 import javax.ejb.PrePassivate ; 33 34 import org.jboss.ejb3.BeanContext; 35 36 41 public abstract class LifecycleInvocationContextImpl implements InvocationContext 42 { 43 44 private int currentInterceptor; 45 private int currentMethod; 46 BeanContext beanContext; 47 InterceptorInfo[] interceptorInfos; 48 Object [] instances; 49 private Method [] beanMethods; 50 private HashMap metadata; 51 52 protected LifecycleInvocationContextImpl() 53 { 54 } 55 56 public static InvocationContext getLifecycleInvocationContext(Class type, BeanContext beanContext, InterceptorInfo[] interceptorInfos, Method [] beanMethods) 57 { 58 LifecycleInvocationContextImpl ic = null; 59 if (type == PostConstruct.class) ic = new PostConstructICtxImpl(); 60 else if (type == PostActivate .class) ic = new PostActivateICtxImpl(); 61 else if (type == PrePassivate .class) ic = new PrePassivateICtxImpl(); 62 else if (type == PreDestroy.class) ic = new PreDestroyICtxImpl(); 63 else throw new RuntimeException ("Unsupported lifecycle event: " + type); 64 65 ic.instances = beanContext.getInterceptorInstances(interceptorInfos); 66 if (interceptorInfos.length != ic.instances.length) 67 { 68 throw new RuntimeException ("interceptorInfos and instances have different length"); 69 } 70 71 ic.beanContext = beanContext; 72 ic.beanMethods = beanMethods; 73 ic.interceptorInfos = interceptorInfos; 74 75 return ic; 76 } 77 78 public Object getTarget() 79 { 80 return beanContext.getInstance(); 81 } 82 83 public Method getMethod() 84 { 85 return null; 86 } 87 88 public Object [] getParameters() 89 { 90 return new Object [0]; 91 } 92 93 public void setParameters(Object [] params) 94 { 95 } 97 98 99 public java.util.Map getContextData() 100 { 101 if (metadata == null) { 102 metadata = new HashMap (); 103 } 104 return metadata; 105 } 106 107 public Object proceed() throws Exception 108 { 109 if (currentInterceptor < interceptorInfos.length) 110 { 111 int oldInterceptor = currentInterceptor; 112 int oldMethod = currentMethod; 113 try 114 { 115 int curr = currentInterceptor; 116 int currMethod = currentMethod++; 117 InterceptorInfo info = interceptorInfos[curr]; 118 if (currMethod == getLifecycleMethods(info).length) 119 { 120 curr = ++currentInterceptor; 121 currentMethod = 0; 122 currMethod = currentMethod++; 123 info = (curr < interceptorInfos.length) ? interceptorInfos[curr] : null; 124 } 125 126 if (info != null) 127 { 128 try 129 { 130 Method [] methods = getLifecycleMethods(info); 131 return methods[currMethod].invoke(instances[curr], this); 132 } 133 catch (InvocationTargetException e) 134 { 135 if (e.getTargetException() instanceof Exception ) 136 { 137 throw ((Exception ) e.getCause()); 138 } 139 else 140 { 141 throw new RuntimeException (e.getCause()); 142 } 143 } 144 } 145 } 146 finally 147 { 148 currentInterceptor = oldInterceptor; 150 currentMethod = oldMethod; 151 } 152 } 153 if (beanMethods != null) 154 { 155 try 156 { 157 for (Method beanMethod : beanMethods) 158 { 159 beanMethod.invoke(getTarget(), new Object [0]); 160 } 161 } 162 catch (InvocationTargetException e) 163 { 164 if (e.getTargetException() instanceof Exception ) 165 { 166 throw ((Exception ) e.getCause()); 167 } 168 else 169 { 170 throw new RuntimeException (e.getCause()); 171 } 172 } 173 finally 174 { 175 } 176 } 177 178 return null; 179 } 180 181 abstract Method [] getLifecycleMethods(InterceptorInfo info); 182 183 public static class PostConstructICtxImpl extends LifecycleInvocationContextImpl 184 { 185 Method [] getLifecycleMethods(InterceptorInfo info) 186 { 187 return info.getPostConstructs(); 188 } 189 } 190 191 public static class PostActivateICtxImpl extends LifecycleInvocationContextImpl 192 { 193 Method [] getLifecycleMethods(InterceptorInfo info) 194 { 195 return info.getPostActivates(); 196 } 197 } 198 199 public static class PrePassivateICtxImpl extends LifecycleInvocationContextImpl 200 { 201 Method [] getLifecycleMethods(InterceptorInfo info) 202 { 203 return info.getPrePassivates(); 204 } 205 } 206 207 public static class PreDestroyICtxImpl extends LifecycleInvocationContextImpl 208 { 209 Method [] getLifecycleMethods(InterceptorInfo info) 210 { 211 return info.getPreDestroys(); 212 } 213 } 214 } 215 | Popular Tags |