package examples.io;

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

/**
 * This example illustrates the use of WebLogic File Services.  
 * HelloWorld looks for a file on the server, writes "Hello World" to
 * the file if it is NOT there, reads the first 11 bytes of the file, 
 * and displays them.
 * <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 HelloWorld with three arguments:
 * <ul>
 * <li>URL of the WebLogic Server
 * <li>Name of the file system
 * <li>Name of the target file
 * </ul>
 * You may also call HelloWorld 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 HelloWorld {
  
  /**
   * Runs the HelloWorld file example.
   */
  public static void main(String[] argv) {
    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.HelloWorld WebLogicURL fileSystemName fileName");
      System.out.println("Example: java examples.io.HelloWorld t3://localhost:7001 users test");
      return;
    }
    
    Context ctx = null;
    try {
      // Look-up T3Services context on the WebLogic Server...
      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");
      
      // Get the file system and the file
      System.out.println("Getting the file system " + fileSystemName);
      T3FileSystem fileSystem = t3s.io().getFileSystem(fileSystemName);
      System.out.println("Getting the file " + fileName);
      T3File file = fileSystem.getFile(fileName); 
      
      if (file.exists()) {
	// The file exists. Don't do anything. 
	System.out.println("The file already exists");
      }
      else {
	// The file does not exist. Create it.
	byte[] b = new String("Hello World").getBytes();

        // Get an OutputStream and write to the file
        System.out.println("Writing to the file");
	OutputStream os = file.getFileOutputStream();
	os.write(b);
	os.close();
      }
      
      // Get an InputStream and read from the file
      byte b[] = new byte[11];
      System.out.println("Reading from the file");
      InputStream is = file.getFileInputStream();
      is.read(b);
      is.close();
      
      // Report the result
      String result = new String(b);
      System.out.println("File contents: " + result);      
    }
    
    catch (Exception e) {
      System.out.println("The following exception occurred while " +
                         "running the HelloWorld 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) {}
    }
  }
}







