/*
 * JBoss, Home of Professional Open Source
 * Copyright 2005, JBoss Inc., and individual contributors as indicated
 * by the @authors tag. See the copyright.txt in the distribution for a
 * full listing of individual contributors.
 *
 * This is free software; you can redistribute it and/or modify it
 * under the terms of the GNU Lesser General Public License as
 * published by the Free Software Foundation; either version 2.1 of
 * the License, or (at your option) any later version.
 *
 * This software is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this software; if not, write to the Free
 * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
 * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
 */
package org.jboss.cache.interceptors;

import org.jboss.cache.TreeCache;
import org.jboss.cache.lock.IdentityLock;
import org.jgroups.blocks.MethodCall;

import javax.transaction.Transaction;
import javax.transaction.TransactionManager;
import java.util.HashMap;
import java.util.List;
import java.util.ListIterator;

/**
 * When a call returns, unlocks all locks held by the current thread in the LockTable. This is a no-op if a transaction
 * is used.
 * @author Bela Ban
 * @version $Id: UnlockInterceptor.java,v 1.4.10.2 2005/12/01 19:48:41 dimitris Exp $
 */
public class UnlockInterceptor extends Interceptor {
   HashMap lock_table=null;
   TransactionManager tx_mgr=null;

   public void setCache(TreeCache cache) {
      super.setCache(cache);
      tx_mgr=cache.getTransactionManager();
      lock_table=cache.getLockTable();
   }



   public Object invoke(MethodCall m) throws Throwable {
      Transaction       tx;

      try {
         return super.invoke(m);
      }
      finally {
         if(tx_mgr != null && (tx=tx_mgr.getTransaction()) != null && isValid(tx)) {
            ; // don't do anything; we have a transaction
         }
         else { // no TX
            List locks=(List)lock_table.get(Thread.currentThread());
            if(locks != null) {
               releaseLocks(locks);
               lock_table.remove(Thread.currentThread());
            }
         }
      }
   }





   private void releaseLocks(List locks) {
      IdentityLock lock;
      // Release the locks backwards (bottom-up in the tree)
      // Note that the lock could have been released already so don't panic
      if(locks != null) {
         for(ListIterator it=locks.listIterator(locks.size()); it.hasPrevious();) {
            lock=(IdentityLock)it.previous();
            if(log.isTraceEnabled())
               log.trace("releasing lock for " + lock.getFqn() + ": " + lock);
            lock.release(Thread.currentThread());
            // it.remove();
         }
         locks.clear();
      }
   }


}
