/*
 * 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.jgroups.blocks.MethodCall;
import org.jboss.cache.TreeCache;
import org.jboss.cache.GlobalTransaction;
import org.jboss.cache.TransactionTable;

import javax.transaction.Transaction;
import javax.transaction.TransactionManager;

/**
 * Always at the end of the chain, directly in front of the cache. Simply calls into the cache using reflection.
 * If the call resulted in a modification, add the Modification to the end of the modification list
 * keyed by the current transaction
 * @author Bela Ban
 * @version $Id: CallInterceptor.java,v 1.1.4.5 2005/12/01 19:48:41 dimitris Exp $
 */
public class CallInterceptor extends Interceptor {
   TransactionManager tx_mgr=null;
   TransactionTable   tx_table=null;


   public void setCache(TreeCache cache) {
      super.setCache(cache);
      tx_mgr=cache.getTransactionManager();
      tx_table=cache.getTransactionTable();
   }

   public Object invoke(MethodCall m) throws Throwable {
      Object retval;

      try {
         retval=m.invoke(cache);
      }
      catch(Throwable t) {
         retval=t;
      }

      Transaction tx=null;
      if(tx_mgr != null)
         tx=tx_mgr.getTransaction();
      if(tx != null && (isActive(tx) || isPreparing(tx))) {
         if(retval != null && retval instanceof Throwable) {
            tx.setRollbackOnly(); // no exception, regular return
         }
         else {
            if(TreeCache.isCrudMethod(m.getMethod())) {
               // if method is a CRUD (Create/Remove/Update/Delete) method: add it to the modification
               // list, otherwise skip (e.g. get() is not added)
               // add the modification to the TX's modification list. this is used to later
               // (on TX commit) send all modifications done in this TX to all members
               GlobalTransaction gtx=tx_table.get(tx);
               if(gtx == null) {
                  if(log.isDebugEnabled())
                     log.debug("didn't find GlobalTransaction for " + tx + "; won't add modification to transaction list");
               }
               else {
                  tx_table.addModification(gtx, m);
               }
            }
         }
      }
      if(retval != null && retval instanceof Throwable)
         throw (Throwable)retval;
      return retval;
   }
}
