import javax.naming.*;
import java.io.File;
import java.util.Hashtable;

/**
  * Demonstrates how to rename an object.
  *
  * usage: java Rename
  */

class Rename {
    public static void main(String[] args) {

	// Set up environment for creating initial context
	Hashtable env = new Hashtable(11);
	env.put(Context.INITIAL_CONTEXT_FACTORY, 
	    "com.sun.jndi.fscontext.RefFSContextFactory");
	env.put(Context.PROVIDER_URL, "file:/tmp/tutorial");

	try {
	    // Create the initial context
	    Context ctx = new InitialContext(env);

	    // Rename to old_report.txt
	    ctx.rename("report.txt", "old_report.txt");

	    // Check that it is there using new name
	    Object obj = ctx.lookup("old_report.txt");
	    System.out.println(obj);

	    // Rename back to report.txt
	    ctx.rename("old_report.txt", "report.txt");

	    // Check that it is there with original name
	    obj = ctx.lookup("report.txt");
	    System.out.println(obj);
	} catch (NamingException e) {
	    System.out.println("Rename failed: " + e);
	}
    }
}
