package examples.workspace.monitor;

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

/**
 * This code example shows how a Workspace Monitor can be used to
 * mirror a workspace value in another workspace. In this class,
 * we implement both the SetMonitor and the DestroyMonitor interfaces,
 * since we want to mirror both updates (sets) and deletes (destroys)
 * of the mirrored Workspace values.
 * <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% MirrorMonitor.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 MirrorMonitor implements SetMonitor, DestroyMonitor {
  
  public T3ServicesDef services;
  String mirrorName;
  WorkspaceDef mirror = null;
  
  /**
   * 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 {
    mirrorName = params.getParam("mirror").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's <tt>set()</tt> method is called. The target
   * object must implement the <tt>weblogic.common.Setable</tt> interface.
   *
   * @param target            Object to be set
   * @param callbackData      ParamSet of potential data for callback
   * @exception               weblogic.common.MonitorException if a
   *                          Monitor vetoes the operation
   */
  public void preSet(Setable target, ParamSet callbackData) throws MonitorException {
    if (mirror == null) {
      try {
        mirror = services.workspace().getWorkspace()
          .getWorkspace(mirrorName, WorkspaceDef.OPEN, WorkspaceDef.SCOPE_SERVER);
      }
      catch (T3Exception t3e) {
        try {
          services.log().error("monitorInit", t3e);
        }
        catch (T3Exception t3ee) {}
      }
    }
  }
  
  /**
   * Implement this method to carry out some arbitrary operations after
   * the target object's <tt>set()</tt> method is called. The target
   * object must implement the <tt>weblogic.common.Setable</tt> interface.
   *
   * @param target            Object to be set
   * @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 postSet(Setable target, ParamSet callbackData, Exception e) {
    if (e == null && target instanceof WorkspaceValue) {
      try {
        mirror.store(((WorkspaceValue)target).getKey(), target.newValue());
      }
      catch(T3Exception t3e) {}
    }
  }
  
  /**
   * Implement this method to carry out some arbitrary operations before
   * the target object is destroyed. The target object must implement the
   * <tt>weblogic.common.Destroyable</tt> interface.
   *
   * @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 remove the mirrored
   * value.
   *
   * @param target            Object to be mirrored
   * @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) {
    try {
      mirror.remove(((WorkspaceValue)target).getKey());
    }
    catch(T3Exception t3e) {}
  }
}


