1 17 18 package org.apache.geronimo.transaction.context; 19 20 import java.util.ArrayList ; 21 import java.util.HashMap ; 22 import java.util.Iterator ; 23 import java.util.Map ; 24 25 import org.apache.commons.logging.Log; 26 import org.apache.commons.logging.LogFactory; 27 import org.apache.geronimo.transaction.ConnectionReleaser; 28 import org.apache.geronimo.transaction.DoubleKeyedHashMap; 29 import org.apache.geronimo.transaction.InstanceContext; 30 import org.tranql.cache.InTxCache; 31 32 33 36 abstract class AbstractTransactionContext implements TransactionContext { 37 protected static final Log log = LogFactory.getLog(AbstractTransactionContext.class); 38 protected Map managedConnections; 39 40 private InstanceContext currentContext; 41 private final DoubleKeyedHashMap associatedContexts = new DoubleKeyedHashMap(); 42 private final DoubleKeyedHashMap dirtyContexts = new DoubleKeyedHashMap(); 43 private InTxCache inTxCache; 44 45 public final void associate(InstanceContext context) throws Throwable { 46 if (associatedContexts.put(context.getContainerId(), context.getId(), context) == null) { 47 context.associate(); 48 } 49 } 50 51 public final void unassociate(InstanceContext context) throws Throwable { 52 associatedContexts.remove(context.getContainerId(), context.getId()); 53 context.unassociate(); 54 } 55 56 public final void unassociate(Object containerId, Object id) throws Throwable { 57 InstanceContext context = (InstanceContext) associatedContexts.remove(containerId, id); 58 if (context != null) { 59 context.unassociate(); 60 } 61 } 62 63 public final InstanceContext getContext(Object containerId, Object id) { 64 return (InstanceContext) associatedContexts.get(containerId, id); 65 } 66 67 protected final ArrayList getAssociatedContexts() { 68 return new ArrayList (associatedContexts.values()); 69 } 70 71 protected final void unassociateAll() { 72 ArrayList toFlush = getAssociatedContexts(); 73 for (Iterator i = toFlush.iterator(); i.hasNext();) { 74 InstanceContext context = (InstanceContext) i.next(); 75 try { 76 context.unassociate(); 77 } catch (Throwable throwable) { 78 log.warn("Error while unassociating instance from transaction context: " + context, throwable); 79 } 80 } 81 } 82 83 public final InstanceContext beginInvocation(InstanceContext context) throws Throwable { 84 if (context.getId() != null) { 85 associate(context); 86 dirtyContexts.put(context.getContainerId(), context.getId(), context); 87 } 88 context.enter(); 89 InstanceContext caller = currentContext; 90 currentContext = context; 91 return caller; 92 } 93 94 public final void endInvocation(InstanceContext caller) { 95 if (currentContext != null) { 96 currentContext.exit(); 97 } 98 currentContext = caller; 99 } 100 101 public final void flushState() throws Throwable { 102 while (dirtyContexts.isEmpty() == false) { 103 ArrayList toFlush = new ArrayList (dirtyContexts.values()); 104 dirtyContexts.clear(); 105 for (Iterator i = toFlush.iterator(); i.hasNext();) { 106 InstanceContext context = (InstanceContext) i.next(); 107 if (!context.isDead()) { 108 context.flush(); 109 } 110 } 111 } 112 if (currentContext != null && currentContext.getId() != null) { 113 dirtyContexts.put(currentContext.getContainerId(), currentContext.getId(), currentContext); 114 } 115 if(inTxCache != null) { 116 inTxCache.flush(); 117 } 118 } 119 120 public final void setInTxCache(InTxCache inTxCache) { 121 this.inTxCache = inTxCache; 122 } 123 124 public final InTxCache getInTxCache() { 125 return inTxCache; 126 } 127 128 public void setManagedConnectionInfo(ConnectionReleaser key, Object info) { 129 if (managedConnections == null) { 130 managedConnections = new HashMap (); 131 } 132 managedConnections.put(key, info); 133 } 134 135 public Object getManagedConnectionInfo(ConnectionReleaser key) { 136 if (managedConnections == null) { 137 return null; 138 } 139 return managedConnections.get(key); 140 } 141 } 142 | Popular Tags |