1 17 package org.apache.geronimo.connector; 18 19 import java.util.Map ; 20 import java.util.HashMap ; 21 import java.util.Iterator ; 22 23 import javax.transaction.Transaction ; 24 import javax.transaction.Synchronization ; 25 import javax.transaction.RollbackException ; 26 import javax.transaction.SystemException ; 27 import javax.transaction.Status ; 28 29 import edu.emory.mathcs.backport.java.util.concurrent.ConcurrentHashMap; 30 import org.apache.geronimo.connector.outbound.TransactionCachingInterceptor; 31 32 35 public class ConnectorTransactionContext { 36 private static final ConcurrentHashMap DATA_INDEX = new ConcurrentHashMap(); 37 38 public static ConnectorTransactionContext get(Transaction transaction) { 39 if (transaction == null) { 40 throw new NullPointerException ("transaction is null"); 41 } 42 43 ConnectorTransactionContext ctx = (ConnectorTransactionContext) DATA_INDEX.get(transaction); 44 if (ctx == null) { 45 ctx = new ConnectorTransactionContext(); 46 47 try { 48 if (transaction.getStatus() == Status.STATUS_ACTIVE) { 49 transaction.registerSynchronization(new ConnectorSynchronization(ctx, transaction)); 50 DATA_INDEX.putIfAbsent(transaction, ctx); 55 } 56 } catch (RollbackException e) { 57 throw (IllegalStateException ) new IllegalStateException ("Transaction is already rolled back").initCause(e); 58 } catch (SystemException e) { 59 throw new RuntimeException ("Unable to register ejb transaction synchronization callback", e); 60 } 61 62 } 63 return ctx; 64 } 65 66 public static TransactionCachingInterceptor.ManagedConnectionInfos get(Transaction transaction, ConnectionReleaser key) { 67 ConnectorTransactionContext ctx = get(transaction); 68 TransactionCachingInterceptor.ManagedConnectionInfos infos = ctx.getManagedConnectionInfo(key); 69 if (infos == null) { 70 infos = new TransactionCachingInterceptor.ManagedConnectionInfos(); 71 ctx.setManagedConnectionInfo(key, infos); 72 } 73 return infos; 74 } 75 76 private static void remove(Transaction transaction) { 77 DATA_INDEX.remove(transaction); 78 } 79 80 private Map managedConnections; 81 82 private synchronized TransactionCachingInterceptor.ManagedConnectionInfos getManagedConnectionInfo(ConnectionReleaser key) { 83 if (managedConnections == null) { 84 return null; 85 } 86 return (TransactionCachingInterceptor.ManagedConnectionInfos) managedConnections.get(key); 87 } 88 89 private synchronized void setManagedConnectionInfo(ConnectionReleaser key, TransactionCachingInterceptor.ManagedConnectionInfos info) { 90 if (managedConnections == null) { 91 managedConnections = new HashMap (); 92 } 93 managedConnections.put(key, info); 94 } 95 96 private static class ConnectorSynchronization implements Synchronization { 97 private final ConnectorTransactionContext ctx; 98 private final Transaction transaction; 99 100 public ConnectorSynchronization(ConnectorTransactionContext ctx, Transaction transaction) { 101 this.ctx = ctx; 102 this.transaction = transaction; 103 } 104 105 public void beforeCompletion() { 106 } 107 108 public void afterCompletion(int status) { 109 try { 110 synchronized (ctx) { 111 if (ctx.managedConnections != null) { 112 for (Iterator entries = ctx.managedConnections.entrySet().iterator(); entries.hasNext();) { 113 Map.Entry entry = (Map.Entry ) entries.next(); 114 ConnectionReleaser key = (ConnectionReleaser) entry.getKey(); 115 key.afterCompletion(entry.getValue()); 116 } 117 ctx.managedConnections.clear(); 120 } 121 } 122 } finally { 123 remove(transaction); 124 } 125 } 126 } 127 } 128 | Popular Tags |