1 |
| package org.jboss.cache.interceptors; |
2 |
| |
3 |
| import org.apache.commons.logging.Log; |
4 |
| import org.apache.commons.logging.LogFactory; |
5 |
| |
6 |
| import javax.transaction.RollbackException; |
7 |
| import javax.transaction.Synchronization; |
8 |
| import javax.transaction.SystemException; |
9 |
| import javax.transaction.Transaction; |
10 |
| import java.util.HashMap; |
11 |
| import java.util.LinkedList; |
12 |
| import java.util.Map; |
13 |
| |
14 |
| |
15 |
| |
16 |
| |
17 |
| |
18 |
| |
19 |
| |
20 |
| |
21 |
| |
22 |
| |
23 |
| |
24 |
| public class OrderedSynchronizationHandler implements Synchronization |
25 |
| { |
26 |
| private Transaction tx = null; |
27 |
| private LinkedList<Synchronization> handlers = new LinkedList<Synchronization>(); |
28 |
| |
29 |
| |
30 |
| |
31 |
| |
32 |
| static Map instances = new HashMap(); |
33 |
| |
34 |
| static Log log = LogFactory.getLog(OrderedSynchronizationHandler.class); |
35 |
| |
36 |
| |
37 |
1121795
| private OrderedSynchronizationHandler(Transaction tx)
|
38 |
| { |
39 |
1121795
| this.tx = tx;
|
40 |
| } |
41 |
| |
42 |
| |
43 |
| |
44 |
| |
45 |
| |
46 |
| |
47 |
| |
48 |
1121831
| public static OrderedSynchronizationHandler getInstance(Transaction tx) throws SystemException, RollbackException
|
49 |
| { |
50 |
1121831
| OrderedSynchronizationHandler retval = (OrderedSynchronizationHandler) instances.get(tx);
|
51 |
36
| if (retval != null) return retval;
|
52 |
1121794
| retval = new OrderedSynchronizationHandler(tx);
|
53 |
1121795
| tx.registerSynchronization(retval);
|
54 |
1121795
| instances.put(tx, retval);
|
55 |
1121795
| return retval;
|
56 |
| } |
57 |
| |
58 |
| |
59 |
1121830
| public void registerAtHead(Synchronization handler)
|
60 |
| { |
61 |
1121830
| register(handler, true);
|
62 |
| } |
63 |
| |
64 |
1
| public void registerAtTail(Synchronization handler)
|
65 |
| { |
66 |
1
| register(handler, false);
|
67 |
| } |
68 |
| |
69 |
1121831
| void register(Synchronization handler, boolean head)
|
70 |
| { |
71 |
1121831
| if (handler != null && !handlers.contains(handler))
|
72 |
| { |
73 |
1121831
| if (head)
|
74 |
1121830
| handlers.addFirst(handler);
|
75 |
| else |
76 |
1
| handlers.addLast(handler);
|
77 |
| } |
78 |
| } |
79 |
| |
80 |
1121500
| public void beforeCompletion()
|
81 |
| { |
82 |
1121500
| for (Synchronization sync : handlers)
|
83 |
| { |
84 |
1121522
| sync.beforeCompletion();
|
85 |
| } |
86 |
| } |
87 |
| |
88 |
1121738
| public void afterCompletion(int status)
|
89 |
| { |
90 |
1121738
| for (Synchronization sync : handlers)
|
91 |
| { |
92 |
1121774
| try
|
93 |
| { |
94 |
1121774
| sync.afterCompletion(status);
|
95 |
| } |
96 |
| catch (Throwable t) |
97 |
| { |
98 |
8
| log.error("failed calling afterCompletion() on " + sync, t);
|
99 |
| } |
100 |
| } |
101 |
| |
102 |
| |
103 |
1121738
| instances.remove(tx);
|
104 |
| } |
105 |
| |
106 |
63
| public String toString()
|
107 |
| { |
108 |
63
| StringBuffer sb = new StringBuffer();
|
109 |
63
| sb.append("tx=" + getTxAsString() + ", handlers=" + handlers);
|
110 |
63
| return sb.toString();
|
111 |
| } |
112 |
| |
113 |
63
| private String getTxAsString()
|
114 |
| { |
115 |
| |
116 |
63
| if (tx == null)
|
117 |
0
| return null;
|
118 |
| |
119 |
63
| return tx.getClass().getName() + "@" + System.identityHashCode(tx);
|
120 |
| } |
121 |
| } |