package examples.applets;

/**
 * This class may be used to pre-load all the classes
 * used to do multitier WebLogic JDBC for an applet, in the interest
 * of improving applet responsiveness. It is recommended
 * that it be run in its own thread launched from an
 * applet's <tt>init()</tt> method. This way, the applet's UI
 * can come up, and by the time it is ready to do some
 * JDBC access (after garnering information like a login
 * from the user) all the classes will be already loaded.
 *
 * @author Copyright (c) 1998-2000 by BEA Systems, Inc. All Rights Reserved.
 */
public class WebLogicPreLoader implements Runnable {
  
  static final String[] JDBCClasses = {
    "weblogic.jdbc.t3.Connection",
    "weblogic.jdbc.t3.Statement",
    "weblogic.jdbc.t3.PreparedStatement",
    "weblogic.jdbc.t3.CallableStatement",
    "weblogic.common.ParamSet",
    "weblogic.common.ParamSetException",
    "weblogic.common.internal.VectorTable",
    "weblogic.common.ParamValue",
    "weblogic.jdbc.common.internal.ConnectionState",
    "weblogic.jdbc.common.internal.DriverMsg",
    "weblogic.db.jdbc.DataSetException",
    "weblogic.jdbc.common.internal.ConnectionMsg",
    "weblogic.jdbc.common.internal.StatementMsg",
    "weblogic.jdbc.t3.ResultSet",
    "weblogic.jdbc.t3.internal.T3InputStream",
    "weblogic.jdbc.t3.internal.UnicodeInputStream",
    "weblogic.jdbc.common.internal.ResultSetMsg",
    "weblogic.db.jdbc.Schema",
    "weblogic.jdbc.t3.ResultSetMetaData",
    "weblogic.jdbc.common.internal.ResultSetMetaDataMsg",
    "weblogic.jdbc.common.internal.ResultSetMetaDataInfo",
    "weblogic.db.jdbc.Column",
    "weblogic.db.jdbc.Record",
    "weblogic.db.jdbc.Value",
  };
  
  boolean isAlive = true;
  java.net.URL base;
  
  /**
   * Constructs a preloader for the WebLogic
   * Server specified by the URL
   */
  public WebLogicPreLoader(java.net.URL u) {
    base = u;
  }

  /**
   * Implements the applet <tt>stop()</tt> method.
   */
  public void stop() {
    isAlive = false;
  }

  /**
   * Connects to the WebLogic Server, and loads
   * classes in the background.
   */
  public void run() {
    try {
      int port = base.getPort();
      if (port == -1) port = 80;
      weblogic.common.T3Client t3 =
        new weblogic.common.T3Client("t3://"+base.getHost()+":"+port);
      t3.connect();
      t3.disconnect();
    }
    catch (Exception e) {}
    
    try {
      if (!isAlive) {
        return;
      }
      try {
        Class.forName("weblogic.jdbc.t3.Driver").newInstance();
      }
      catch (Exception e) {
        e.printStackTrace();
      }
      for (int i = 0; isAlive && i < JDBCClasses.length; i++) {
        try {
          Class.forName(JDBCClasses[i]);
        }
        catch (Exception e) {
          e.printStackTrace();
        }
      }
    }
    catch (Exception e) {
      e.printStackTrace();
    }
  }
}
