/*
* JBoss, the OpenSource EJB server
*
* Distributable under LGPL license.
* See terms of license at gnu.org.
*/
package javax.management.monitor;

import java.util.Iterator;

import javax.management.MBeanAttributeInfo;
import javax.management.MBeanNotificationInfo;
import javax.management.ObjectName;

/**
 * The string monitor service.
 *
 * @author <a href="mailto:Adrian.Brock@HappeningTimes.com">Adrian Brock</a>
 * @version $Revision: 1.1.8.2 $
 *
 */
public class StringMonitor
  extends Monitor
  implements StringMonitorMBean
{
  // Constants -----------------------------------------------------

  /**
   * The string match has been notified.
   */
  int STRING_MATCHES_NOTIFIED = 16;

  /**
   * The string differs has been notified.
   */
  int STRING_DIFFERS_NOTIFIED = 32;

  // Attributes ----------------------------------------------------

  /**
   * The comparison string.
   */
  String stringToCompare = "";

  /**
   * Notify Matches.
   */
  boolean notifyMatch = false;

  /**
   * Notify Differs.
   */
  boolean notifyDiffer = false;

  // Static --------------------------------------------------------

  // Constructors --------------------------------------------------

  /**
   * Default Constructor
   */
  public StringMonitor()
  {
  }

  // Public --------------------------------------------------------

  public MBeanNotificationInfo[] getNotificationInfo()
  {
    MBeanNotificationInfo[] result = new MBeanNotificationInfo[1];
    String[] types = new String[]
    {
      MonitorNotification.RUNTIME_ERROR,
      MonitorNotification.OBSERVED_OBJECT_ERROR,
      MonitorNotification.OBSERVED_ATTRIBUTE_ERROR,
      MonitorNotification.OBSERVED_ATTRIBUTE_TYPE_ERROR,
      MonitorNotification.STRING_TO_COMPARE_VALUE_MATCHED,
      MonitorNotification.STRING_TO_COMPARE_VALUE_DIFFERED
    };
    result[0] = new MBeanNotificationInfo(types,
      "javax.management.monitor.MonitorNotification",
      "Notifications sent by the String Monitor Service MBean");
    return result;
  }

  // StringMonitorMBean implementation -----------------------------

  public String getDerivedGauge()
  {
    ObservedObject object = getFirstObservedObject();
    if (object != null)
       return (String) object.getDerivedGauge();
    else
       return null;
  }

  public String getDerivedGauge(ObjectName name)
  {
     ObservedObject object = retrieveObservedObject(name);
    if (object != null)
       return (String) object.getDerivedGauge();
    else
       return null;
  }

  public long getDerivedGaugeTimeStamp()
  {
    ObservedObject object = getFirstObservedObject();
    if (object != null)
       return object.getDerivedGaugeTimeStamp();
    else
       return 0L;
  }

  public long getDerivedGaugeTimeStamp(ObjectName name)
  {
    ObservedObject object = retrieveObservedObject(name);
    if (object != null)
       return object.getDerivedGaugeTimeStamp();
    else
       return 0L;
  }

  public String getStringToCompare()
  {
    return stringToCompare;
  }

  public void setStringToCompare(String value)
    throws IllegalArgumentException
  {
    if (value == null)
      throw new IllegalArgumentException("Null string to compare.");
    this.stringToCompare = value;
  }

  public boolean getNotifyMatch()
  {
    return notifyMatch;
  }

  public void setNotifyMatch(boolean value)
  {
    notifyMatch = value;
  }

  public boolean getNotifyDiffer()
  {
    return notifyDiffer;
  }

  public void setNotifyDiffer(boolean value)
  {
    notifyDiffer = value;
  }

  // Package protected ---------------------------------------------

  void initObservedObject(ObservedObject object)
  {
     super.initObservedObject(object);
     object.setDerivedGauge("");
  }

  void monitor(ObservedObject object, MBeanAttributeInfo attributeInfo, Object value)
    throws Exception
  {
    // Must be a string.
    if (!(value instanceof String))
    {
      sendAttributeTypeErrorNotification(object, "Not a string attribute");
      return;
    }

    // Get the gauge and record when we got it.
    object.setDerivedGauge(value);
    object.setDerivedGaugeTimeStamp(System.currentTimeMillis());

    // Try to fire the relevant event.
    if (value.equals(stringToCompare))
      sendStringMatchesNotification(object, (String) value);
    else
      sendStringDiffersNotification(object, (String) value);
  }

  /**
   * Send an string matches notification.<p>
   *
   * This is only performed when requested and it has not already been sent.
   *
   * @param value the attribute value.
   */
  void sendStringMatchesNotification(ObservedObject object, String value)
  {
    if (notifyMatch && object.notAlreadyNotified(STRING_MATCHES_NOTIFIED))
      sendNotification(object, MonitorNotification.STRING_TO_COMPARE_VALUE_MATCHED,
        object.getDerivedGaugeTimeStamp(), "matches", observedAttribute, value, 
        stringToCompare);
    object.setNotAlreadyNotified(STRING_DIFFERS_NOTIFIED);
  }

  /**
   * Send an string differs notification.<p>
   *
   * This is only performed when requested and it has not already been sent.
   *
   * @param value the attribute value.
   */
  void sendStringDiffersNotification(ObservedObject object, String value)
  {
    if (notifyDiffer && object.notAlreadyNotified(STRING_DIFFERS_NOTIFIED))
      sendNotification(object, MonitorNotification.STRING_TO_COMPARE_VALUE_DIFFERED,
        object.getDerivedGaugeTimeStamp(), "differs", observedAttribute, value, 
        stringToCompare);
    object.setNotAlreadyNotified(STRING_MATCHES_NOTIFIED);
  }

  // Protected -----------------------------------------------------

  // Private -------------------------------------------------------

  // Inner classes -------------------------------------------------
}
