KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > cache > interceptors > OrderedSynchronizationHandler


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 JavaDoc;
7 import javax.transaction.Synchronization JavaDoc;
8 import javax.transaction.SystemException JavaDoc;
9 import javax.transaction.Transaction JavaDoc;
10 import java.util.HashMap JavaDoc;
11 import java.util.Iterator JavaDoc;
12 import java.util.LinkedList JavaDoc;
13 import java.util.Map JavaDoc;
14
15 /**
16  * Maintains a list of Synchronization handlers. Reason is that we have to
17  * invoke certain handlers <em>before</em> others. See the description in
18  * SyncTxUnitTestCase.testConcurrentPuts(). For example, for synchronous
19  * replication, we have to execute the ReplicationInterceptor's
20  * afterCompletion() <em>before</em> the TransactionInterceptor's.
21  *
22  * @author Bela Ban
23  * @version $Id: OrderedSynchronizationHandler.java,v 1.4 2006/07/19 21:34:43 msurtani Exp $
24  */

25 public class OrderedSynchronizationHandler implements Synchronization JavaDoc {
26    Transaction JavaDoc tx=null;
27    LinkedList JavaDoc handlers=new LinkedList JavaDoc();
28
29    /** Map<Transaction,OrderedSynchronizationHandler> */
30    static Map JavaDoc instances=new HashMap JavaDoc();
31
32    static Log log=LogFactory.getLog(OrderedSynchronizationHandler.class);
33
34
35    private OrderedSynchronizationHandler(Transaction JavaDoc tx) {
36       this.tx=tx;
37    }
38
39    /**
40     * Creates a new instance of OrderedSynchronizationHandler, or fetches an existing instance. Key is the local
41     * transaction (tx). This instance registers with the TransactionManager automatically
42     * @param tx
43     */

44    public static OrderedSynchronizationHandler getInstance(Transaction JavaDoc tx) throws SystemException JavaDoc, RollbackException JavaDoc {
45       OrderedSynchronizationHandler retval=(OrderedSynchronizationHandler)instances.get(tx);
46       if(retval != null) return retval;
47       retval=new OrderedSynchronizationHandler(tx);
48       tx.registerSynchronization(retval);
49       instances.put(tx, retval);
50       return retval;
51    }
52
53
54    public void registerAtHead(Synchronization JavaDoc handler) {
55       register(handler, true);
56    }
57
58    public void registerAtTail(Synchronization JavaDoc handler) {
59       register(handler, false);
60    }
61
62    void register(Synchronization JavaDoc handler, boolean head) {
63       if(handler != null && !handlers.contains(handler)) {
64          if(head)
65             handlers.addFirst(handler);
66          else
67             handlers.addLast(handler);
68       }
69    }
70
71    public void beforeCompletion() {
72       for(Iterator JavaDoc it=handlers.iterator(); it.hasNext();) {
73          Synchronization JavaDoc sync=(Synchronization JavaDoc)it.next();
74          sync.beforeCompletion();
75       }
76    }
77
78    public void afterCompletion(int status) {
79       for(Iterator JavaDoc it=handlers.iterator(); it.hasNext();) {
80          Synchronization JavaDoc sync=(Synchronization JavaDoc)it.next();
81          try {
82             sync.afterCompletion(status);
83          }
84          catch(Throwable JavaDoc t) {
85             log.error("failed calling afterCompletion() on " + sync, t);
86          }
87       }
88
89       // finally unregister us from the hashmap
90
instances.remove(tx);
91    }
92
93    public String JavaDoc toString() {
94       StringBuffer JavaDoc sb=new StringBuffer JavaDoc();
95       sb.append("tx=" + tx + ", handlers=" + handlers);
96       return sb.toString();
97    }
98 }
99
Popular Tags