package examples.t3client;

import java.util.*;
import java.io.*;
import weblogic.common.*;
import weblogic.remote.common.*;

/**
 * ShutdownTest is a simple example of the use of the T3ShutdownDef
 * interface for writing a shutdown class. This class logs the time
 * of the shutdown to a file.
 * <p>
 * To run this example, first set the following properties
 * by adding them to the <tt>weblogic.properties</tt> file:
 *
 * <pre>weblogic.system.shutdownClass.ShutdownTest=examples.t3client.ShutdownTest
 *weblogic.system.shutdownArgs.ShutdownTest=outfile=c:\temp\shutdown.log</pre>
 * <p>
 * Note that any class that implements T3ShutdownDef must have a
 * default constructor, as this example does.
 *
 * @author Copyright (c) 1996-98 by WebLogic, Inc. All Rights Reserved.
 * @author Copyright (c) 1999-2000 by BEA Systems, Inc. All Rights Reserved.
 */
public class ShutdownTest implements T3ShutdownDef {
  
  private T3ServicesDef services;
  
  /**
   * Sets the services access for this class.
   */
  public void setServices(T3ServicesDef services) {
    this.services = services;
  }
  
  /**
   * Default constructor. A default constructor is required of any
   * class that implements weblogic.common.T3ShutdownDef.
   */
  public void ShutdownTest() {
    // default constructor
  }
  
  /**
   * The shutdown action belongs in this method. The string
   * argument specifies the virtual name of this class by which it
   * is registered in the <tt>weblogic.properties</tt> file,
   * under the <tt>weblogic.system.shutdownClass</tt> property.
   * The hashtable argument is a set of name-value pairs that
   * are supplied to this method from the 
   * <tt>weblogic.system.shutdownArgs</tt> property.
   *
   * @param name             Virtual name under which this class is registered
   * @param args             Set of name-value pairs of arguments for this method
   * @exception              Exception if there is an error
   */
  public String shutdown(String name, Hashtable args) throws Exception {
    String outfile = (String)args.get("outfile");
    String msg = "Time of shutdown is " + (new Date()).toString();
    if (outfile != null) {
      try {
	FileOutputStream fout = new FileOutputStream(outfile);
	PrintWriter pout = new PrintWriter(fout);
        pout.println(msg);
        pout.close();
        fout.close();
	return "ok";
      }
      catch (IOException ioe) {
	System.out.println("Shutdown outfile '" + outfile + "' could not be opened");
      }
    } 
    System.out.println(msg);
    return "Failed to write to log file";
  }
}








