package examples.workspace;

import java.net.*;
import java.util.*;
import java.sql.*;
import weblogic.db.jdbc.*;
import weblogic.common.*;
import weblogic.workspace.common.*;

/**
 * This code example shows how a T3Client can cache arbitrary
 * data in its workspace on the WebLogic Server. This example uses
 * a very simple piece of data -- a cookie -- to show how to
 * set up data for caching on the WebLogic Server. Any data that you
 * can add to a ParamSet can be cached.
 * <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 %CLIENT_CLASSES% datacache.java</b></font>
 *
 *
 *<p>	
 *<li>Start WebLogic Server in a separate shell.
 *
 *<p>
 *<li>Run this example by executing the following command in your development shell:
 *
 *<p>
 *
 *<font face="Courier New"><b>$ java examples.workspace.datacache  t3://<i>hostname:port</i></b></font>
 *<p>Where <i>hostname:port</i> is the host name and port number of your WebLogic Server.
 *</ol>
 * 
 * @author                    Copyright (c) 1996-2000 by BEA Systems, Inc.
 *                            All Rights Reserved.
 */

public class datacache {
  
  /**
   * In the main, the T3Client connects to the WebLogic Server and sets its
   * soft disconnect timeout to "never." Setting the soft disconnect
   * to "never" prevents the WebLogic Server from cleaning up (or destroying)
   * a T3Client's resource in the WebLogic Server even if the T3Client goes
   * away. We get the T3Client's Workspace ID, which we will use as a
   * token to identify our Workspace when reconnecting after a
   * disconnect. Then the T3Client is disconnected.
   * <p>
   * The ParamSet that we will save into the T3Client's Workspace is
   * created. We use a loop to create, connect, and disconnect the T3Client,
   * each time connecting to the original T3Client Workspace by
   * supplying the Workspace ID when the T3Client is created.
   * Within the loop, we set a different ParamValue
   * and store the ParamSet in the Workspace; then we disconnect and
   * connect again, and retrieve the data that we stored in the
   * previous session.
   * <p>
   * Finally, we connect one last time and set the soft disconnect
   * timeout to zero. This forces an immediate clean up of the
   * T3Client's resources in the WebLogic Server after we've finished with
   * this client's Workspace.
   */

  public static void main(String argv[]) throws Exception {

    if (argv.length != 1) {
      System.out.println("Usage:   java examples.workspace.datacache WebLogicURL");
      System.out.println("Example: java examples.workspace datacache t3://localhost:7001");
      return;
    }

    String weblogicURL = argv[0];
    
    // Create T3Client
    T3Client t3 = null;
    String wsid = null;
    
    t3 = new T3Client(weblogicURL);
    
    // Connect and set the timeout to be unlimited
    t3.connect()
      .setSoftDisconnectTimeoutMins(T3Client.DISCONNECT_TIMEOUT_NEVER);
    
    System.out.println("Setting soft disconnect timeout to 'never'");

    WorkspaceDef defaultWS = t3.services.workspace().getWorkspace();
    wsid = defaultWS.getID();
    System.out.println("Workspace ID for this client is " + wsid);
    
    // Disconnect the client. Since the soft disconnect timeout interval 
    // is large, the server will preserve the session.
    t3.disconnect();
    
    System.out.println("T3Client disconnected");
    
    ParamSet cookie = new ParamSet();
    System.out.println("Created a ParamSet");
    
    for (int i = 0; i < 10; i++) {
      t3 = new T3Client(weblogicURL, wsid);
      t3.connect();
      System.out.println("T3Client " + i + " connected at Workspace ID " + wsid);
      
      // Send the data to the WebLogic Server Workspace
      cookie.setParam("CookieValue", i);
      defaultWS = t3.services.workspace().getWorkspace();
      defaultWS.store("mycookie", cookie);
      System.out.println("Stored the ParamSet: " + i);
      
      // Disconnect the client and reconnect.
      t3.disconnect();
      System.out.println("Disconnected");
      
      t3 = new T3Client(weblogicURL, wsid);
      t3.connect();
      
      // Retreive the data from the WebLogic Server Workspace
      defaultWS = t3.services.workspace().getWorkspace();
      int val = ((ParamSet)defaultWS.fetch("mycookie"))
	.getParam("CookieValue").asInt();
      
      System.out.println("T3Client " + i + " retrieved value: " + val);
      
      t3.disconnect();
    }
    
    // Sets the timeout interval to 0 to force an immediate
    // disconnect on the server.
    t3 = new T3Client(weblogicURL, wsid);
    t3.connect();
    t3.setSoftDisconnectTimeoutMins(0);
    t3.disconnect();
    System.out.println("Final disconnect -- Completed.");
  }
}


