import javax.naming.*;
import javax.naming.directory.*;
import java.util.Hashtable;

/**
  * Demonstrates how to update a referral entry.
  *
  * usage: java UpdateReferral
  */
class UpdateReferral {
    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.ldap.LdapCtxFactory");
	env.put(Context.PROVIDER_URL, "ldap://localhost:489/o=JNDITutorial");

	// Set referral property; optional because "ignore" is the default
	env.put(Context.REFERRAL, "ignore");

	// Uncomment this line when using 1.0.1 
	// env.put("java.naming.ldap.control.manageReferral", "true");

	try {
	    // Create the initial context
	    DirContext ctx = new InitialDirContext(env);

	    // Set up new referral attribute
	    Attributes attrs = new BasicAttributes("ref",
		"ldap://localhost:389/cn=C. User, ou=NewHires, o=JNDITutorial",
		true); // case-ignore

	    // Update the "ref" attribute
	    ctx.modifyAttributes(
		"cn=NewReferral", DirContext.REPLACE_ATTRIBUTE, attrs);

	} catch (NamingException e) {
	    System.out.println("Update failed: " + e);
	}
    }
}
