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

import java.util.Date;

import javax.management.InstanceNotFoundException;
import javax.management.MBeanServerInvocationHandler;
import javax.management.ObjectName;
import javax.management.timer.TimerMBean;

import org.jboss.system.ServiceMBeanSupport;
import org.jboss.util.Strings;

/**
 * A simple service used to configure the periodic emition of notifications
 * by a standard JMX Timer.
 *  
 * @jmx:mbean
 *    extends="org.jboss.system.ServiceMBean"
 * 
 * @author <a href="mailto:dimitris@jboss.org">Dimitris Andreadis</a>
 * @version $Revision: 1.1.2.2 $
 */
public class TimerService extends ServiceMBeanSupport
   implements TimerServiceMBean
{
   // Private Data --------------------------------------------------
   
   /** Notification type */
   private String notificationType;
   
   /** Notification message */
   private String notificationMessage;
   
   /** Timer period string */
   private String timerPeriodString;
   
   /** Number of occurences */
   private long repeatitions;
   
   /** TimerMBean name */
   private ObjectName timerObjectName;
   
   /** Timer period as long */
   private long timerPeriod;
   
   /** Proxy to the TimerMBean */
   private TimerMBean timerProxy;

   /** The timer subscription id */
   private Integer id;
   
   // Constructors --------------------------------------------------
   
   /**
    * CTOR
    */
   public TimerService()
   {
      // empty
   }
   
   // Attributes -----------------------------------------------------
   
   /**
    * @jmx:managed-attribute
    */
   public void setNotificationType(String type)
   {
      this.notificationType = type;
   }
   
   /**
    * @jmx:managed-attribute
    */   
   public String getNotificationType()
   {
      return notificationType;
   }

   /**
    * @jmx:managed-attribute
    */ 
   public void setNotificationMessage(String message)
   {
      this.notificationMessage = message;
   }
   
   /**
    * @jmx:managed-attribute
    */ 
   public String getNotificationMessage()
   {
      return notificationMessage;
   }
   
   /**
    * @jmx:managed-attribute
    */ 
   public void setTimerPeriod(String timerPeriod)
   {
      this.timerPeriod = Strings.parsePositiveTimePeriod(timerPeriod);
      this.timerPeriodString = timerPeriod;
   }
   
   /**
    * @jmx:managed-attribute
    */ 
   public String getTimerPeriod()
   {
      return this.timerPeriodString;
   }
   
   /**
    * @jmx:managed-attribute
    */    
   public void setRepeatitions(long repeatitions)
   {
      this.repeatitions = repeatitions;
   }
   
   /**
    * @jmx:managed-attribute
    */    
   public long getRepeatitions()
   {
      return repeatitions;
   }
   
   /**
    * @jmx:managed-attribute
    */ 
   public void setTimerMBean(ObjectName timerMBean)
   {
      this.timerProxy = (TimerMBean)MBeanServerInvocationHandler
         .newProxyInstance(getServer(), timerMBean, TimerMBean.class, false);
      this.timerObjectName = timerMBean;
   }
   
   /**
    * @jmx:managed-attribute
    */ 
   public ObjectName getTimerMBean()
   {
      return timerObjectName;
   }
   
   // Lifecycle control (ServiceMBeanSupport) -----------------------
   
   /**
    * Start 
    */
   public void startService() throws Exception
   {
      if (timerProxy != null)
      {
         id = timerProxy.addNotification(
               notificationType,
               notificationMessage,
               null, // UserData
               new Date(), // now
               timerPeriod,
               repeatitions);
         
         log.debug("Added timer notification(" + id
               + ") : type=" + notificationType
               + ", message=" + notificationMessage
               + ", period=" + timerPeriodString
               + ", repeatitions=" + repeatitions
               + ", to timer '" + timerObjectName + "'");
      }
      else
      {
         log.warn("TimerMBean not set");
      }
   }
   
   /**
    * Stop
    */
   public void stopService()
   {
      if (id != null)
      {
         try
         {
            timerProxy.removeNotification(id);
            log.debug("Removed timer notification(" + id + ") from timer '" + timerObjectName + "'");
         }
         catch (InstanceNotFoundException ignore)
         {
            // after a fixed number of repeatitions
            // the notification is removed from the timer
         }
         id = null;
      }
   }
   
}
