1 package com.tirsen.nanning.samples.prevayler; 2 3 import java.lang.reflect.Method ; 4 5 import com.tirsen.nanning.FilterMethodsInterceptor; 6 import com.tirsen.nanning.Invocation; 7 import com.tirsen.nanning.MethodInterceptor; 8 import com.tirsen.nanning.attribute.Attributes; 9 import com.tirsen.nanning.definition.SingletonInterceptor; 10 11 17 public class PrevaylerInterceptor implements MethodInterceptor { 18 private boolean resolveEntities; 19 20 public PrevaylerInterceptor(boolean resolveEntities) { 21 this.resolveEntities = resolveEntities; 22 } 23 24 public boolean interceptsConstructor(Class interfaceClass) { 25 return Attributes.hasAttribute(interfaceClass, "entity"); 26 } 27 28 public boolean interceptsMethod(Method method) { 29 return Attributes.hasAttribute(method, "transaction"); 30 } 31 32 public Object invoke(Invocation invocation) throws Throwable { 33 40 41 if (CurrentPrevayler.hasPrevayler() 42 && !CurrentPrevayler.isInTransaction() 43 && (PrevaylerUtils.isService(invocation.getTargetInterface()) || 44 PrevaylerUtils.isEntity(invocation.getTargetInterface()))) 45 { 46 CurrentPrevayler.enterTransaction(); 47 try { 48 InvokeCommand command = new InvokeCommand(invocation, resolveEntities); 49 return command.executeUsing(CurrentPrevayler.getPrevayler()); 50 } finally { 51 CurrentPrevayler.exitTransaction(); 52 } 53 } else { 54 return invocation.invokeNext(); 55 } 56 } 57 58 static boolean transactionalReturnValue(Method method) { 59 return isTransactional(method.getReturnType()); 60 } 61 62 public static boolean isTransactional(Class aClass) { 63 if (aClass == null) { 64 return false; 65 } 66 67 Method [] methods = aClass.getMethods(); 68 for (int i = 0; i < methods.length; i++) { 69 Method method = methods[i]; 70 if (Attributes.hasAttribute(method, "transaction")) { 71 return true; 72 } 73 } 74 Class [] interfaces = aClass.getInterfaces(); 75 for (int i = 0; i < interfaces.length; i++) { 76 Class anInterface = interfaces[i]; 77 if (isTransactional(anInterface)) { 78 return true; 79 } 80 } 81 if (isTransactional(aClass.getSuperclass())) { 82 return true; 83 } 84 return false; 85 } 86 } 87 | Popular Tags |