import javax.naming.*;
import javax.naming.directory.*;

import java.util.Hashtable;

/**
  * Demonstrates how to bind an object by supplying a set of attributes.
  * (Use Unbind to remove binding.)
  *
  * Need schema checking to be turned off on server (unless you add
  * a Drink objectclass to the schema).
  *
  * usage: java DirObj
  */

class DirObj {
    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:389/o=JNDITutorial");

	// So that object factory can be found
	env.put(Context.OBJECT_FACTORIES, "DrinkFactory");

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

	    // Create object to be bound
	    Drink dr = new Drink("water");

	    // Perform bind
	    // %%% workaround for LDAP Beta3
	    // ctx.bind("cn=favDrink", null, dr.getAttributes(""));
	    ctx.bind("cn=favDrink", dr);

	    // Read object back
	    Drink dr2 = (Drink) ctx.lookup("cn=favDrink");
	    System.out.println(dr2);

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