package examples.workspace.shared;

import weblogic.common.*;
import weblogic.workspace.common.*;

/**
 *
 *<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% SharedSpace.java</b></font>
 *
 *
 *<p>	
 *<li>Start WebLogic Server in a separate shell.
 *
 *</ol>
 * 
 *<p>
 *<dl>
 *This example illustrates how a user can create a workspace
 * that can be shared among multiple clients. In this example,
 * the user supplies a workspace name, a key string, and a value.
 * The named workspace is created, and an arbitrary name=value pair
 * is stored in the workspace. Another client can then extract
 * the value from the named workspace by supplying the workspace
 * name, and the key string. For example, the command: 
 *
 * <p>
 *  <font face="Courier New"><b>$ java examples.workspace.shared.SharedSpace t3://<i>hostname:port</i> NASDAQ SUNW 34</b></font>
 *
 *  <p><p>Where <i>hostname:port</i> is the host name and port number of your WebLogic Server.  
 *
 *  <p>
 * creates a workspace called NASDAQ on the WebLogic Server localhost
 * listening on port 7001, and stores the the name=value pair SUNW=34 in
 * the workspace.
 *<p>
 * This command allows a different client to retrieve the value for SUNW:
 *
 * <p>
 *  <font face="Courier New"><b>$ java examples.workspace.shared.SharedSpace t3://<i>hostname:port</i> NASDAQ SUNW</b></font>
 *<p>Where <i>hostname:port</i> is the host name and port number of your WebLogic Server.  
 *
 *<p>
 * @author                    Copyright (c) 1997-2000 by BEA Systems, Inc.
 *                            All Rights Reserved.
 */
public class SharedSpace {

  /**
   * Default constructor.
   */
  public SharedSpace() {}

  /**
   * Runs this example from the command line. This example takes the
   * following arguments to retrieve a value:
   * <ul>
   * <li>WebLogic Server URL (host and port)
   * <li>Name of the Workspace
   * <li>Key to retrieve a value from the workspace
   * </ul>
   * Add one additional argument to store a value rather than retrieve it:
   * <ul>
   * <li>Value to be stored
   * </ul>
   */
  public static void main(String argv[]) {
    
    if (argv.length < 3 || argv.length > 4) {
      showUsage();
      return;
    }
    
    // Get command-line arguments that will be used for attaching to or creating
    // the workspace, and setting or getting a value.
    String url    = argv[0];
    String wsname = argv[1];
    String key    = argv[2];
    String val;
    WorkspaceDef workspace;
    
    T3Client t3 = null;
    
    try {
      // Connect to the WebLogic Server using the URL supplied as the first argument.
      t3 = new T3Client(url);
      t3.connect();
      
      WorkspaceDef defaultWS = t3.services.workspace().getWorkspace();
      if (argv.length == 4) {
	val = argv[3];
	// Create the workspace
	workspace =
          defaultWS.getWorkspace(wsname, WorkspaceDef.OPEN, WorkspaceServicesDef.SCOPE_SERVER);
	// Add the key & value
	addToWorkspace(t3, workspace, key, val);
        System.out.println("T3Client " + t3.services.workspace().getID() +
                           " added the key/value pair " + key +
                           "=" + val + " to the Workspace named " + wsname);
      }
      else {
	// Attach to the workspace and retrieve the value
	// Verify that the named workspace exists
	try {
	  workspace =
            defaultWS.getWorkspace(wsname, WorkspaceDef.ATTACH, WorkspaceDef.SCOPE_SERVER);
	}
        catch (T3Exception e) {
	  System.out.println("Error attaching to workspace: "+e);
	  return;
	}
	
	// Fetch the named value from the workspace
	try {
	  val = ((ParamSet) workspace.fetch(key)).getParam(key).asString();
          System.out.println("T3Client " + t3.services.workspace().getID() +
                             " fetched the key/value pair " + key +
                             "=" + val + " from the Workspace named " + wsname);
          
	}
	catch (Exception e) {
	  System.out.println("Error fetching " + key + " from Workspace " + workspace.getName());
	  return;
        }
      }
    }
    catch (Exception e) {
      System.out.println(e);
    }
    finally {
      try {t3.disconnect();} catch (Exception e) {;}
    }
  }
  
  static boolean addToWorkspace(T3Client t3, WorkspaceDef workspace, String key, String value) {
    ParamSet ps = new ParamSet();
    try {
      ps.setParam(key, value);
      // Store the user-supplied key and integer value in the workspace.
      workspace.store(key, ps);
      System.out.println("Value " + value + " stored for key " + key);
    }
    catch(ParamSetException pse) {
      System.out.println("Error setting param"); 
      return false;
    }
    catch(T3Exception t3e) {
      System.out.println(t3e); 
      return false;
    }
    return true;
  }
  
  static void showUsage() {
    System.out.println("Usage:   java examples.workspace.shared.SharedSpace " +
		       "WebLogicURL WkspName KeyForValue [ValueToBeSet]\n" +
		       "This sets the value:\njava examples.workspace.shared.SharedSpace " +
		       "t3://localhost:7001 NASDAQ SUNW 34\n\n" + 
		       
		       "This retrieves the value:\njava examples.workspace.shared.SharedSpace " +
		       "t3://localhost:7001 NASDAQ SUNW\n\n");
  }
}



