package examples.workspace;

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

/**
 * This short example demonstrates how to extend the workspace life
 * of a T3Client so that the client can disconnect, then reconnect to
 * continue work inside the same workspace. In this example, the
 * T3Client supplies a name for the workspace that will be used to
 * reenter the same workspace in the future.
 * <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% t3clientcache2.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.t3clientcache2  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 t3clientcache2 {
  
  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, and assign a name to the workspace
    T3Client t3 = new T3Client(weblogicURL, "myname");
    
    // Connect and set the timeout to be unlimited
    t3.connect()
      .setSoftDisconnectTimeoutMins(T3Client.DISCONNECT_TIMEOUT_NEVER);

    // Obtain a WorkspaceDef object to access the named workspace. 

    WorkspaceDef defaultWS = t3.services.workspace().getWorkspace();

    // Get the workspace ID and name
    String wsid = defaultWS.getID();
    String wsname = defaultWS.getName();
    System.out.println("Workspace ID = " +  wsid);

    // Disconnect the client. Since the soft disconnect timeout is NEVER,
    // the server will preserve the session.
    System.out.println("Disconnectiong from Workspace named \"" + wsname+ "\"");
    t3.disconnect();
    t3 = null;
    
    // Reconnect using the same workspace name
    T3Client newt3 = new T3Client(weblogicURL, "myname");
    newt3.connect();
    
    // Obtain a new WorkspaceDef object to access the named workspace
    WorkspaceDef newDefaultWS = newt3.services.workspace().getWorkspace();
   
    String new_wsid = newDefaultWS.getID();
    String new_wsname = newDefaultWS.getName();

    // Once reconnected, you can make JdbcT3, EventT3, or RemoteT3 calls
    System.out.println
      ("Reconnected client " + new_wsid + " to workspace named \"" + new_wsname + "\"");
    
    // Set the timeout internal to 0 to force an immediate disconnect
    // on the server.
    newt3.setSoftDisconnectTimeoutMins(0)
      .disconnect();
    System.out.println("Disconnected client");
  }
}

