TxListener.java

00001 /*
00002  * JOnAS: Java(TM) Open Application Server
00003  * Copyright (C) 1999 Bull S.A.
00004  * Contact: jonas-team@objectweb.org
00005  *
00006  * This library is free software; you can redistribute it and/or
00007  * modify it under the terms of the GNU Lesser General Public
00008  * License as published by the Free Software Foundation; either
00009  * version 2.1 of the License, or any later version.
00010  *
00011  * This library is distributed in the hope that it will be useful,
00012  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00013  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00014  * Lesser General Public License for more details.
00015  *
00016  * You should have received a copy of the GNU Lesser General Public
00017  * License along with this library; if not, write to the Free Software
00018  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307
00019  * USA
00020  *
00021  * --------------------------------------------------------------------------
00022  * $Id: TxListener.java,v 1.8 2004/09/15 06:46:52 durieuxp Exp $
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     // Synchronization implementation
00104     // -------------------------------------------------------------------
00105 
00114     public void beforeCompletion() {
00115         TraceEjb.txlistener.log(BasicLevel.DEBUG, "");
00116 
00117         // Set classloader for getting the right component context
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         // Send afterCompletion on each Context.
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         // Invalid the prefetch cache
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         // remove this in the tx HashMap.
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 }

Generated on Tue Feb 15 15:05:43 2005 for JOnAS by  doxygen 1.3.9.1