package examples.t3client;

import weblogic.common.*;

/**
 * This example illustrates a simple mechanism for shutting
 * down a WebLogic Server from within a client application.
 * Note that calling the <tt>shut()</tt> method can only
 * succeed if the T3User is system, and the system user's
 * password is supplied.
 * <p>
 * Note also that the final client call to disconnect from
 * the WebLogic Server may fail, since the server is shutting
 * down. You should wrap the disconnect in a try block and
 * catch the potential exception.
 * 
 * @author Copyright (c) 1998 WebLogic, Inc. All Rights Reserved.
 * @author Copyright (c) 1999-2000 by BEA Systems, Inc. All Rights Reserved.
 */
public class SimpleShutdown {
  
  public static void main(String[] argv) {
    // Defaults for command-line values; note the system password must
    // be supplied
    String sysPassword = "";
    String URL         = "";
    String lockMsg     = "";
    int shutSecs       = 0;
    T3Client t3        = null;
    T3Client t32       = null;
    
    // Check user input; must supply at least a password
    if (argv.length < 1 | argv.length > 4) {
      System.out.println("You must supply at least the system password to " +
                         "use this example\n" +
                         "Usage:   java examples.t3client.SimpleShutdown PSWD [URL MSG SECS]");
      System.out.println("Example: java examples.t3client.Simpleshutdown 2sKf*fA");
      return;
    }
    // Use the first command line argument as the URL, if it exists
    else if (argv.length == 1) {
      sysPassword = argv[0];
      URL         = "t3://localhost:7001";
      System.out.println("Password supplied, using URL " + URL);
      lockMsg     = "Shutting down";
      shutSecs    = 0;
    }
    else if (argv.length == 4) {
      sysPassword = argv[0];
      URL         = argv[1];
      lockMsg     = argv[2];
      shutSecs    = Integer.parseInt(argv[3]);
    }
    
    try {
      // Set up a system user to shutdown the server
      t3 = new T3Client(URL, new T3User("system", sysPassword));
      t3.connect();
      t3.getT3Services().admin().shut(lockMsg, shutSecs);
      // WebLogic imposes a 20-second shutdown wait on thread death
      // Add this to the configurable seconds
      System.out.println("Server shutdown initiated; expect in " + (shutSecs + 20) + " seconds");
      Thread.currentThread().sleep(1000 * (shutSecs < 2 ? 2 : shutSecs));
      try {
        System.out.println("Trying second client");
        t32 = new T3Client(URL);
      }
      catch (Throwable t) {
        System.out.println("Client couldn't connect. Server has been shut down.");
      }
      Thread.currentThread().sleep(1000 * (shutSecs + 20));
    }
    catch (Exception e) {
      System.out.println("There was a connection problem. Check password and "+
                         "make sure the Server \nis running on the expected port.");
    }
    finally {
      try {
        t3.disconnect();
        t32.disconnect();
      }
      catch (Exception ee) {}
    }
  }
}

