package examples.io;

import java.io.*;
import java.util.*;
import weblogic.common.*;
import weblogic.io.common.*;
import javax.naming.*;
import weblogic.jndi.*;

/**
 * This example illustrates the use of WebLogic File Services.  
 * SpeedTrap writes a 10 MByte file to the server, reads it back 
 * in again, and then reports the data transfer rates.
 * <p>
 * To run this example, first locate or start a WebLogic Server
 * that has a network-visible file system by setting the property
 * <tt>weblogic.io.fileSystem</tt> in your <tt>weblogic.properties</tt> file.
 * For example, to configure your WebLogic Server to offer
 * the native file system directory <i>/myserver/fullPath</i> as a network-visible 
 * file system named <i>myFiles</i>, set the property
 * <tt>weblogic.io.fileSystem.<i>myFiles</i>=<i>/myserver/fullPath</i>
 * in the WebLogic Server's <tt>weblogic.properties</tt> file.
 * <p>
 * After compiling this directory, call SpeedTrap with three arguments:
 * <ul>
 *     <li> The URL of the WebLogic Server
 *     <li> The name of the file system
 *     <li> The name of the target file to write and read from
 * </ul>
 * 
 * You may also call SpeedTrap with two arguments:
 * 
 * <ul>
 * <li>URL of your WebLogic Server
 * <li>Name of the target file
 * </ul>
 * 
 * in which case it will use the empty file system name. The empty file
 * system name refers to the local file system on the client. File names
 * are always absolute on a remote file system, but may be relative on
 * a local file system. 
 *
 * @author Copyright (c) 1997-1998 by WebLogic, Inc. All Rights Reserved.
 * @author Copyright (c) 1999-2000 by BEA Systems, Inc. All Rights Reserved.
 */
public class SpeedTrap {
  
  /**
   * Runs the SpeedTrap file example.
   */  
  public static void main(String argv[]) throws Exception {
    String url;
    String fileSystemName;
    String fileName;
    if (argv.length == 2) {
      url = argv[0];
      fileSystemName = "";  // Use the local file system on the client.
      fileName = argv[1];
    }
    else if (argv.length == 3) {
      url = argv[0];
      fileSystemName = argv[1];
      fileName = argv[2];
    }
    else {
      System.out.println("Usage:   java examples.io.SpeedTrap "+
                         "WebLogicURL fileSystemName fileName");
      System.out.println("Example: java examples.io.SpeedTrap "+
                         "t3://localhost:7001 users test");
      return;
    }
    
    // Look-up T3Services context on the WebLogic Server...
    Context ctx;
    Hashtable env = new Hashtable();
    env.put(Context.PROVIDER_URL, url);
    env.put(Context.INITIAL_CONTEXT_FACTORY, 
            weblogic.jndi.WLInitialContextFactory.class.getName());
    ctx = new InitialContext(env);
    T3ServicesDef t3s = (T3ServicesDef)ctx.lookup("weblogic.common.T3Services");
    
    try {
      int bufSize = 1024;
      int numBufs = 10000;
      byte buffer[] = new byte[bufSize];
      long start, total;
      
      // Obtain a T3File from the T3Services context... 
      System.out.println("Getting the file " + "//" + fileSystemName + "/" + fileName);
      T3File file = t3s.io().getFile("//" + fileSystemName + "/" + fileName);
      
      // Get an OutputStream and write to the file
      System.out.println("Writing to the file ...");
      OutputStream os = file.getFileOutputStream();      
      start = System.currentTimeMillis();
      for (int i = 0; i < numBufs; i++)
	os.write(buffer);
      os.close();
      total = System.currentTimeMillis() - start;
      if (total > 0)
        total = (100 * (long)bufSize * (long)numBufs)/(128 * total);
      System.out.println("Write speed = " + total/100.0 + " Mbits/sec");
      
      // Get an InputStream and read from the file
      System.out.println("Reading from the file ...");
      InputStream is = file.getFileInputStream();
      start = System.currentTimeMillis();
      for (int i = 0; i < numBufs; i++)
	is.read(buffer);
      is.close();
      total = System.currentTimeMillis() - start;
      if (total > 0)
	total = (100 * (long)bufSize * (long)numBufs)/(128 * total);       
      System.out.println("Read speed = " + total/100.0 + " Mbits/sec");
      
    } catch (Exception e) {
      System.out.println("The following exception occurred while " +
                         "running the SpeedTrap example.");
      e.printStackTrace();
      if (!fileSystemName.equals("")) {
	System.out.println("Make sure the WebLogic Server at " + url + " was started with");
	System.out.println("the property weblogic.io.fileSystem." + fileSystemName + " set.");
      }
    }
    finally {
      // Always disconnect from the WebLogic Server
      try {
        if (ctx != null) {
          ctx.close();
        }
      }
      catch (Exception e) {}
    }
  }
}

