/*
 * 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.eviction;
import java.util.TimerTask;
import org.jboss.logging.Logger;

/**
 * Timer threads to do periodic node clean up by running the eviction policy.
 *
 * @author Ben Wang 2-2004
 */
public class EvictionTimerTask extends TimerTask
{
   private EvictionPolicy policy_;
   private Logger log_ = Logger.getLogger(EvictionTimerTask.class);

   public EvictionTimerTask(EvictionPolicy policy) {
      policy_ = policy;
   }

   public void run()
   {
      // TODO Let's safeguard timer comes in too often. But this can potentially overflow.
      synchronized(policy_) {
         Region[] regions = policy_.getRegions();
         for(int i=0; i < regions.length; i++) {
            Region region = regions[i];
            EvictionAlgorithm algo = region.getEvictionAlgorithm();
            try {
               algo.process(region);
            } catch (EvictionException e) {
               log_.error("run(): error processing eviction with exception: " +e.toString()
                     +" will reset the eviction queue list.");
               region.resetEvictionQueues();
               e.printStackTrace();
            }
         }
      }
   }
}
