/* JFox, the OpenSource J2EE Application Server
 *
 * Copyright (C) 2002 huihoo.com
 * Distributable under GNU LGPL license
 * See the GNU Lesser General Public License for more details.
 */

package org.jfox.mx;

import java.util.EventObject;
import java.io.ObjectInputStream;
import java.io.IOException;
import java.io.ObjectOutputStream;

/**
 * 
 * @author <a href="mailto:young_yy@hotmail.com">Young Yang</a>
 */

/**
 * The Notification class represents a notification emitted by an MBean.
 * It contains a reference to the source MBean: if the notification has been forwarded through the MBean server,
 * this is the object name of the MBean. If the listener has registered directly with the MBean, this is a direct reference
 * to the MBean.
 *
 * @author <a href="mailto:young_yy@hotmail.org">Young Yang</a>
 */

public class Notification extends EventObject {

  /**
   * The notification type. A string expressed in a dot notation similar to Java properties.
   * An example of a notification type is network.alarm.router
   */
  private String type;

  /**
   * The notification sequence number. A serial number which identify particular instance
   * of notification in the context of the notification source.
   */
  private long sequenceNumber;

  /**
   * The notification timestamp. Indicating when the notification was generated
   */
  private long timeStamp;

  /**
   * The notification user data.  Used for whatever other data the notification
   * source wishes to communicate to its consumers
   */
  private Object userData = null;

  /**
   * The notification message.
   */
  private String message  = new String();

  /**
   * The ObjectName source
   */
  private ObjectName sourceObjectName = null;

  /**
   * The object on which the notification initially occurred.
   */
  protected Object source = null;


  /**
   * Creates a Notification object.
   * The notification timeStamp is set to the curent date.
   *
   * @param type The notification type.
   * @param source The notification source.
   * @param sequenceNumber The notification sequence number within the source object.
   *
   */
  public Notification(String type, Object source, long sequenceNumber) {
    super (source) ;
    this.source = source;
    this.type = type;
    this.sequenceNumber = sequenceNumber ;
    this.timeStamp = (new java.util.Date()).getTime() ;
  }

  /**
   * Creates a Notification object.
   * The notification timeStamp is set to the curent date.
   *
   * @param type The notification type.
   * @param source The notification source.
   * @param sequenceNumber The notification sequence number within the source object.
   * @param message The detailed message.
   *
   */
  public Notification(String type, Object source, long sequenceNumber, String message) {
    super (source) ;
    this.source = source;
    this.type = type;
    this.sequenceNumber = sequenceNumber ;
    this.timeStamp = (new java.util.Date()).getTime() ;
    this.message = message ;
  }

  /**
   * Creates a Notification object.
   *
   * @param type The notification type.
   * @param source The notification source.
   * @param sequenceNumber The notification sequence number within the source object.
   * @param timeStamp The notification emission date.
   *
   */
  public Notification(String type, Object source, long sequenceNumber, long timeStamp) {
    super (source) ;
    this.source = source;
    this.type = type ;
    this.sequenceNumber = sequenceNumber ;
    this.timeStamp = timeStamp ;
  }

  /**
   * Creates a Notification object.
   *
   * @param type The notification type.
   * @param source The notification source.
   * @param sequenceNumber The notification sequence number within the source object.
   * @param timeStamp The notification emission date.
   * @param message The detailed message.
   *
   */
  public Notification(String type, Object source, long sequenceNumber, long timeStamp, String message) {
    super (source) ;
    this.source = source;
    this.type = type ;
    this.sequenceNumber = sequenceNumber ;
    this.timeStamp = timeStamp ;
    this.message = message ;
  }

  /**
   * Get the source object name
   *
   *@return The MBean object name on which the notification initially occurred.
   *
   */
  public Object getSource() {
    if (sourceObjectName == null) {
      return source ;
    }
    else {
      return sourceObjectName ;
    }
  }

  /**
   * Set the source object name
   *
   * @exception java.lang.IllegalArgumentException The source is not a ObjectName
   *
   */
  public void setSource(Object source) throws java.lang.IllegalArgumentException {
    if (!(source instanceof ObjectName)) {
      throw new java.lang.IllegalArgumentException() ;
    }
    this.sourceObjectName = (ObjectName) source ;
    this.source =  source ;
  }

  /**
   * Get the notification sequence number.
   *
   * @return The notification sequence number within the source object. It's a serial number
   * identifying a particular instance of notification in the context of the notification source.
   * The notification model does not assume that notifications will be received in the same order
   * that they are sent. The sequence number helps listeners to sort received notifications.
   *
   */
  public long getSequenceNumber() {
    return sequenceNumber ;
  }

  /**
   * Set the notification sequence number.
   *
   * @param sequenceNumber The notification sequence number within the source object. It is
   * a serial number identifying a particular instance of notification in the
   * context of the notification source.
   *
   */
  public void setSequenceNumber(long sequenceNumber) {
    this.sequenceNumber = sequenceNumber;
  }

  /**
   * Get the notification type.
   *
   * @return The notification type. It's a string expressed in a dot notation similar
   * to Java properties. An example of a notification type is network.alarm.router .
   *
   */
  public String getType() {
    return type ;
  }

  /**
   * Get the notification timestamp.
   *
   * @return The notification timestamp.
   *
   */
  public long getTimeStamp() {
    return timeStamp ;
  }

  /**
   * Set the notification timestamp.
   *
   * @param timeStamp The notification timestamp. It indicates when the notification was generated.
   *
   */
  public void setTimeStamp(long timeStamp) {
    this.timeStamp = timeStamp;
  }

  /**
   * Get the notification message.
   *
   * @return The message string of this notification object. It contains in a string,
   * which could be the explanation of the notification for displaying to a user
   *
   */
  public String getMessage() {
    return message ;
  }

  /**
   * Get the user data.
   *
   * @return The user data object. It is used for whatever data
   * the notification source wishes to communicate to its consumers.
   *
   */
  public Object getUserData() {
    return userData ;
  }

  /**
   * Set the user data.
   *
   * @param userData The user data object. It is used for whatever data
   * the notification source wishes to communicate to its consumers.
   *
   */
  public void setUserData(Object userData) {
    this.userData = userData ;
  }


  private void readObject(ObjectInputStream in) throws IOException, ClassNotFoundException {
    in.defaultReadObject();
    super.source = source;
  }

  private void writeObject(ObjectOutputStream out) throws IOException {
    out.defaultWriteObject();
  }

}