00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026 package org.objectweb.jonas_ejb.container;
00027
00028 import java.util.Iterator;
00029 import java.util.LinkedList;
00030 import javax.naming.Context;
00031 import javax.transaction.Synchronization;
00032 import javax.transaction.Transaction;
00033
00034 import org.objectweb.jonas_ejb.container.jorm.JormFactory;
00035 import org.objectweb.jorm.api.PMapper;
00036 import org.objectweb.medor.eval.prefetch.api.PrefetchCache;
00037 import org.objectweb.util.monolog.api.BasicLevel;
00038
00046 public class TxListener implements Synchronization {
00047
00048 private JEntityFactory bf;
00049 private Transaction tx;
00050
00054 private LinkedList ctxList = new LinkedList();
00055 private LinkedList additionalContexts = new LinkedList();
00056 private boolean inStoreInstances = false;
00057
00063 public TxListener(JEntityFactory bf, Transaction tx) {
00064 TraceEjb.txlistener.log(BasicLevel.DEBUG, "");
00065 this.bf = bf;
00066 this.tx = tx;
00067 }
00068
00075 public synchronized void storeInstances() {
00076 TraceEjb.txlistener.log(BasicLevel.DEBUG, "");
00077 inStoreInstances = true;
00078 storeInstances(ctxList);
00079 while (!additionalContexts.isEmpty()) {
00080 LinkedList lst = new LinkedList();
00081 lst.addAll(additionalContexts);
00082 additionalContexts.clear();
00083 storeInstances(lst);
00084 ctxList.addAll(lst);
00085 }
00086 inStoreInstances = false;
00087 }
00088
00093 public synchronized void addInstance(JEntityContext ec) {
00094 TraceEjb.txlistener.log(BasicLevel.DEBUG, "");
00095 if (inStoreInstances) {
00096 additionalContexts.addLast(ec);
00097 } else {
00098 ctxList.addLast(ec);
00099 }
00100 }
00101
00102
00103
00104
00105
00114 public void beforeCompletion() {
00115 TraceEjb.txlistener.log(BasicLevel.DEBUG, "");
00116
00117
00118 ClassLoader old = Thread.currentThread().getContextClassLoader();
00119 Thread.currentThread().setContextClassLoader(bf.myClassLoader());
00120
00121 Context bnctx = bf.setComponentContext();
00122 storeInstances();
00123 bf.resetComponentContext(bnctx);
00124 Thread.currentThread().setContextClassLoader(old);
00125 }
00126
00134 public void afterCompletion(int status) {
00135 TraceEjb.txlistener.log(BasicLevel.DEBUG, "");
00136
00137
00138 synchronized (this) {
00139 for (Iterator i = ctxList.iterator(); i.hasNext(); ) {
00140 JEntityContext ec = (JEntityContext) i.next();
00141 ec.afterCompletion(status);
00142 }
00143 }
00144
00145
00146 if (bf instanceof JormFactory) {
00147 PMapper mapper = ((JormFactory) bf).getMapper();
00148 PrefetchCache pc = mapper.getPrefetchCache();
00149 if (pc != null) {
00150 pc.invalidatePrefetchBuffer(tx);
00151 }
00152 }
00153
00154
00155 bf.removeTxListener(tx);
00156 }
00157
00158 private void storeInstances(LinkedList contexts) {
00159 for (Iterator i = contexts.iterator(); i.hasNext(); ) {
00160 JEntityContext ec = (JEntityContext) i.next();
00161 ec.beforeCompletion();
00162 }
00163 }
00164 }