package examples.t3client;

import java.util.*;

import weblogic.common.*;
import weblogic.remote.common.*;
import weblogic.workspace.common.*;
import java.io.*;
import java.net.*;

/**
 * The StartupQueryClient connects to the T3Servlet defined in the 
 * StartupQuery class, that is running on the server. 
 *
 * @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 StartupQueryClient {
  /**
   * Sets the name of this T3Servlet.
   */
  public static final String SERVLET_NAME = "examples.t3client.StartupQuery";

  /**
   * Runs this example from the command line.
   */
  public static void main(String argv[]) {
    if (argv.length != 2) {
      System.out.println("Usage:   " +
			 "java examples.t3client.StartupQueryClient " +
			 "WebLogicURL registeredStartupName");
      System.out.println("Example: " +
			 "java examples.t3client.StartupQueryClient " +
			 "t3://localhost:7001 doquery");
      return;
    }
    
    T3Client t3 = null;
    String URL = argv[0];
    String name = argv[1];
    try {
      t3 = new T3Client(URL);
      t3.connect();
      
      ParamSet paramSet = new ParamSet();
      try {
	paramSet.setParam("name", name);
      }
      catch (ParamSetException pse) {
	System.out.println("Error while setting parameter of 'name' to " + name);
      }
      
      // Fetch the cached DataSet with a T3Servlet
      T3ServletDef servlet = null;
      try {
        System.out.println("__________________________________"+
                           "Retrieving dataset from T3Servlet:\n");
        servlet = t3.services.remote().getT3Servlet(SERVLET_NAME);
        servlet.execute(paramSet);
        
        weblogic.db.jdbc.DataSet dataSet =  null;
        dataSet = (weblogic.db.jdbc.DataSet)paramSet.getParam("dataset").asObject();
        
        System.out.println(dataSet.toString());
      } catch (ParamSetException pse) {
        System.out.println("Error while getting parameter of 'dataset'");
      }
      catch (T3ServletException t3e) {
        System.out.println("Servlet named "+ SERVLET_NAME + " was not found");
      }
      
      
      // Fetch the cached DataSet with a fetch call
      try {
        System.out.println("\n\n"+
                           "__________________________________"+
                           "Retrieving dataset from workspace:");
        ParamSet wsps = (ParamSet)
             t3.services.workspace().fetch(name, WorkspaceDef.SCOPE_SERVER);
        
        weblogic.db.jdbc.DataSet dataSet =  null;
        dataSet = (weblogic.db.jdbc.DataSet)wsps.getParam("dataset").asObject();
        System.out.println(dataSet);
      }
      catch (ParamSetException pse) {
        System.out.println("Error while getting parameter of 'dataset'");
      }
      
      t3.disconnect();
    
    } catch (UnknownHostException uke) {
      System.out.println("UnknownHostException: " +
                         "URL " + URL + " is probably incorrect");
    } catch (IOException ioe) {
      System.out.println("IOException: " +
                         "WebLogic may not be available at  " + URL);
    } catch (T3ExecuteException t3ee) {
      System.out.println("T3ExecuteException: " +
                         "An error has occurred in internal " +
                         "WebLogic Server execution: " + t3ee);
    } catch (SecurityException se) {
      System.out.println("SecurityException: " +
                         "T3User lacks privileges to access WebLogic Server: " + se);
    } catch (Exception e) {
      System.out.println("General Exception occurred: " + e);
    }
  }
}

