/*  
 * JBoss, Home of Professional Open Source
 *
 * Distributable under LGPL license.
 * See terms of license at gnu.org.
 */
package org.jboss.services.loggingmonitor;

import java.util.TimerTask;

import org.jboss.logging.Logger;


/**
 * This class provides a repeatable task for monitoring to be
 * executed by a timer.
 * 
 * @author <a href="mailto:jimmy.wilson@acxiom.com">James Wilson</a>
 * @author <a href="mailto:dimitris@jboss.org">Dimitris Andreadis</a>
 * @version $Revision: 1.1.2.2 $
 */
class LoggingMonitorTimerTask extends TimerTask
{
   private MonitoredMBean[] monitoredObjects;
   private Logger logger;

   /**
    * Constructor.
    *
    * @param monitoredObjects the objects to be monitored.
    * @param logger the logger to use for logging errors
    */
   public LoggingMonitorTimerTask(MonitoredMBean[] monitoredObjects, Logger logger)
   {
      this.monitoredObjects = monitoredObjects;
      this.logger = logger;
      
      for (int i = 0; i < monitoredObjects.length; ++i)
      {
         // log the header message
         monitoredObjects[i].logFormat();
      }
   }

   /**
    * TimerTask implementation.
    */
   public void run()
   {
      for (int i = 0; i < monitoredObjects.length; ++i)
      {
         try
         {
            monitoredObjects[i].logAttributes();
         }
         catch (Exception e)
         {
            logger.warn("Unable to log attributes for mbean: " + monitoredObjects[i].getObjectName(), e);
         }
      }
   }
}
