1 25 26 package org.objectweb.jonas_ejb.container; 27 28 import java.util.Iterator ; 29 import java.util.LinkedList ; 30 import javax.naming.Context ; 31 import javax.transaction.Synchronization ; 32 import javax.transaction.Transaction ; 33 34 import org.objectweb.jonas_ejb.container.jorm.JormFactory; 35 import org.objectweb.jorm.api.PMapper; 36 import org.objectweb.medor.eval.prefetch.api.PrefetchCache; 37 import org.objectweb.util.monolog.api.BasicLevel; 38 39 46 public class TxListener implements Synchronization { 47 48 private JEntityFactory bf; 49 private Transaction tx; 50 51 54 private LinkedList ctxList = new LinkedList (); 55 private LinkedList additionalContexts = new LinkedList (); 56 private boolean inStoreInstances = false; 57 58 63 public TxListener(JEntityFactory bf, Transaction tx) { 64 if (TraceEjb.isDebugTxlistener()) { 65 TraceEjb.txlistener.log(BasicLevel.DEBUG, ""); 66 } 67 this.bf = bf; 68 this.tx = tx; 69 } 70 71 77 public synchronized void storeInstances() { 78 if (TraceEjb.isDebugTxlistener()) { 79 TraceEjb.txlistener.log(BasicLevel.DEBUG, ""); 80 } 81 inStoreInstances = true; 82 storeInstances(ctxList); 83 while (!additionalContexts.isEmpty()) { 84 LinkedList lst = new LinkedList (); 85 lst.addAll(additionalContexts); 86 additionalContexts.clear(); 87 storeInstances(lst); 88 ctxList.addAll(lst); 89 } 90 inStoreInstances = false; 91 } 92 93 97 public synchronized void addInstance(JEntityContext ec) { 98 if (TraceEjb.isDebugTxlistener()) { 99 TraceEjb.txlistener.log(BasicLevel.DEBUG, ""); 100 } 101 if (inStoreInstances) { 102 additionalContexts.addLast(ec); 103 } else { 104 ctxList.addLast(ec); 105 } 106 } 107 108 112 120 public void beforeCompletion() { 121 if (TraceEjb.isDebugTxlistener()) { 122 TraceEjb.txlistener.log(BasicLevel.DEBUG, ""); 123 } 124 125 ClassLoader old = Thread.currentThread().getContextClassLoader(); 127 Thread.currentThread().setContextClassLoader(bf.myClassLoader()); 128 129 Context bnctx = bf.setComponentContext(); 130 storeInstances(); 131 bf.resetComponentContext(bnctx); 132 Thread.currentThread().setContextClassLoader(old); 133 } 134 135 142 public void afterCompletion(int status) { 143 if (TraceEjb.isDebugTxlistener()) { 144 TraceEjb.txlistener.log(BasicLevel.DEBUG, ""); 145 } 146 147 synchronized (this) { 149 for (Iterator i = ctxList.iterator(); i.hasNext(); ) { 150 JEntityContext ec = (JEntityContext) i.next(); 151 ec.afterCompletion(status); 152 } 153 } 154 155 if (bf instanceof JormFactory) { 157 PMapper mapper = ((JormFactory) bf).getMapper(); 158 PrefetchCache pc = mapper.getPrefetchCache(); 159 if (pc != null) { 160 pc.invalidatePrefetchBuffer(tx); 161 } 162 } 163 164 bf.removeTxListener(tx); 166 } 167 168 private void storeInstances(LinkedList contexts) { 169 for (Iterator i = contexts.iterator(); i.hasNext(); ) { 170 JEntityContext ec = (JEntityContext) i.next(); 171 ec.beforeCompletion(); 172 } 173 } 174 } 175 | Popular Tags |