package examples.workspace.monitor;

import weblogic.common.*;
import weblogic.event.common.*;
import weblogic.workspace.server.WorkspaceValue;

/**
 * This example shows how a Workspace Monitor can be used to
 * trigger an event when a Workspace value is destroyed. It
 * implements the interface <tt>weblogic.common.DestroyMonitor</tt>,
 * in particular the <tt>preDestroy()</tt> and
 * <tt>postDestroy()</tt> methods. You use these methods to
 * operate on an object before and/or after it is destroyed. In
 * this example, we also use WebLogic Events; we submit an event
 * when the object is destroyed, potentially to notify any registrations
 * that are interested in the object of its demise.
 * <p>
 * <p>
 *<h3>To set up this example:</h3>
 *<ol>
 *<li>Set up your development shell as described in <a
 *href=../../../techstart/environment.html>Setting up your environment</a>.
 *<p>
 *<li>Compile this example by executing the following command in your development shell:
 *
 *<p>
 *<font face="Courier New"><b>$ javac -d %SERVER_CLASSES% DestructionMonitor.java</b></font>
 *<p>
 *
 *<p>	
 *
 *<p>
 *<li>You do not execute this example directly; it is called by the
 *<font face="Courier New" size=-1>MonitorDemo</font> class. You must
 *compile all the other examples in this package before running <font face="Courier New" size=-1><a href=examples.workspace.monitor.MonitorDemo.html>MonitorDemo</a></font>.
 *
 *</ol>
 * @author                    Copyright (c) 1997-2000 by BEA Systems, Inc.
 *                            All Rights Reserved.
 */
public class DestructionMonitor implements DestroyMonitor {
  
  private T3ServicesDef services;
  private String topic;

  /**
   * Initializes a monitor. This allows passing of initialization
   * parameters even when the object is instantiated remotely on
   * the WebLogic Server with a default constructor (which cannot
   * have arguments).
   *
   * @param params            ParamSet of initialization values
   * @param isMaster          true if this is a Master Monitor
   * @exception               weblogic.common.ParamSetException
   *                          if the Monitor cannot be initialized or
   *                          there is a problem with the ParamSet
   */
  public void monitorInit(ParamSet params, boolean isMaster) throws ParamSetException {
    topic = params.getParam("topic").asString();
  }

  /**
   * Sets the services stub for this class. It allows you to write a class that
   * can be instantiated by a client or a WebLogic Server -- how this class
   * accesses WebLogic services is determined by the services stub, which is
   * set at runtime.
   */
  public void setServices(T3ServicesDef services) {
    this.services = services;
  }

  /**
   * Implement this method to carry out some arbitrary operations before
   * the target object is destroyed.
   *
   * @param target            Object to be destroyed
   * @param callbackData      ParamSet of potential data for callback
   */
  public void preDestroy(Destroyable target, ParamSet callbackData) {}

  /**
   * Implement this method to carry out some arbitrary operations after
   * the target object is destroyed. Here we submit an event with
   * the target object as part of the event.
   *
   * @param target            Object to be destroyed
   * @param callbackData      ParamSet of potential data for callback
   * @param e                 Exception to be thrown if the monitor is vetoed
   *                          or does not complete
   */
  public void postDestroy(Destroyable target, ParamSet callbackData, Exception e) {
    if (e == null) {
      ParamSet ps = new ParamSet();
      try { 
        if (target instanceof WorkspaceValue) {
          ps.setParam("key", ((WorkspaceValue) target).getKey());
        }
        EventMessageDef em = services.events().getEventMessage(topic, ps);
        em.submit();
      }
      catch (ParamSetException pse) {
      }
      catch (EventGenerationException ege) {}
    }
  }
}
