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 import java.util.Map ; 28 29 import javax.interceptor.InvocationContext; 30 31 import org.jboss.ejb3.EJBContainerInvocation; 32 33 37 public class InvocationContextImpl implements InvocationContext 38 { 39 private int currentInterceptor; 40 private int currentMethod; 41 private int currentBeanMethod; 42 InterceptorInfo[] interceptorInfos; 43 Object [] instances; 44 private Method [] beanAroundInvokes; 45 private Map metadata; 46 47 EJBContainerInvocation wrapped; 48 49 public InvocationContextImpl(EJBContainerInvocation inv, InterceptorInfo[] interceptorInfos, Object [] instances, Method [] beanAroundInvokes) 50 { 51 wrapped = inv; 52 this.beanAroundInvokes = beanAroundInvokes; 53 54 if (interceptorInfos.length != instances.length) 55 { 56 throw new RuntimeException ("interceptorInfos and instances have different length"); 57 } 58 59 this.interceptorInfos = interceptorInfos; 60 this.instances = instances; 61 } 62 63 public Object getTarget() 64 { 65 return wrapped.getTargetObject(); 66 } 67 68 public Method getMethod() 69 { 70 return wrapped.getMethod(); 71 } 72 73 public Object [] getParameters() 74 { 75 return wrapped.getArguments(); 76 } 77 78 public void setParameters(Object [] params) 79 { 80 wrapped.setArguments(params); 81 } 82 83 public java.util.Map getContextData() 84 { 85 if (metadata == null) { 86 metadata = ClientInterceptorUtil.getClientMetadataMap(wrapped); 87 if (metadata == null) 88 { 89 metadata = new HashMap (); 90 } 91 } 92 return metadata; 93 } 94 95 public Object proceed() throws Exception 96 { 97 if (currentInterceptor < interceptorInfos.length) 98 { 99 int oldInterceptor = currentInterceptor; 100 int oldMethod = currentMethod; 101 try 102 { 103 int curr = currentInterceptor; 104 int currMethod = currentMethod++; 105 106 InterceptorInfo info = interceptorInfos[curr]; 107 if (currMethod == info.getAroundInvokes().length) 108 { 109 curr = ++currentInterceptor; 110 currentMethod = 0; 111 currMethod = currentMethod++; 112 info = (curr < interceptorInfos.length) ? interceptorInfos[curr] : null; 113 } 114 if (info != null) 115 { 116 try 117 { 118 return info.getAroundInvokes()[currMethod].invoke(instances[curr], this); 119 } 120 catch (InvocationTargetException e) 121 { 122 if (e.getTargetException() instanceof Exception ) 123 { 124 throw ((Exception ) e.getCause()); 125 } 126 else 127 { 128 throw new RuntimeException (e.getCause()); 129 } 130 } 131 } 132 } 133 finally 134 { 135 currentInterceptor = oldInterceptor; 137 currentMethod = oldMethod; 138 } 139 } 140 141 if (beanAroundInvokes != null && currentBeanMethod < beanAroundInvokes.length) 142 { 143 try 144 { 145 int curr = currentBeanMethod++; 146 return beanAroundInvokes[curr].invoke(getTarget(), this); 147 } 148 catch (InvocationTargetException e) 149 { 150 if (e.getTargetException() instanceof Exception ) 151 { 152 throw ((Exception ) e.getCause()); 153 } 154 else 155 { 156 throw new RuntimeException (e.getCause()); 157 } 158 } 159 finally 160 { 161 currentBeanMethod--;; 162 } 163 } 164 try 165 { 166 return wrapped.invokeNext(); 167 } 168 catch (Exception e) 169 { 170 throw e; 171 } 172 catch (Throwable t) 173 { 174 throw new RuntimeException (t); 175 } 176 } 177 } 178 | Popular Tags |