KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > tirsen > nanning > samples > prevayler > PrevaylerInterceptor


1 package com.tirsen.nanning.samples.prevayler;
2
3 import java.lang.reflect.Method JavaDoc;
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 /**
12  * TODO document PrevaylerInterceptor
13  *
14  * @author <a HREF="mailto:jon_tirsen@yahoo.com">Jon Tirs?n</a>
15  * @version $Revision: 1.29 $
16  */

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 JavaDoc interfaceClass) {
25         return Attributes.hasAttribute(interfaceClass, "entity");
26     }
27
28     public boolean interceptsMethod(Method JavaDoc method) {
29         return Attributes.hasAttribute(method, "transaction");
30     }
31
32     public Object JavaDoc invoke(Invocation invocation) throws Throwable JavaDoc {
33         /*
34         only first call on objects already in Prevayler should result in a command in the log
35         ie. the following conditions should be met:
36         1. there should be an active Prevayler
37         2. there should NOT be a current transaction
38         3. the target of the call has an object id in the current prevayler OR if the target is a service
39         */

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 JavaDoc method) {
59         return isTransactional(method.getReturnType());
60     }
61
62     public static boolean isTransactional(Class JavaDoc aClass) {
63         if (aClass == null) {
64             return false;
65         }
66
67         Method JavaDoc[] methods = aClass.getMethods();
68         for (int i = 0; i < methods.length; i++) {
69             Method JavaDoc method = methods[i];
70             if (Attributes.hasAttribute(method, "transaction")) {
71                 return true;
72             }
73         }
74         Class JavaDoc[] interfaces = aClass.getInterfaces();
75         for (int i = 0; i < interfaces.length; i++) {
76             Class JavaDoc 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