Previous | Next | Trail Map | The Basics | Directory Operations

Hybrid Naming and Directory Operations

In the naming lesson (in the Basics trail) you learned about using the bind(), rebind(), and createSubcontext() methods in the Context(in the API reference documentation) interface to create bindings and subcontexts. The DirContext(in the API reference documentation) interface contains overloaded versions of these methods that accept attributes. You can use these DirContext methods to associate attributes with the object at the time the binding or subcontext is added to the namespace. For example, you might create a "person" object and bind it to the namespace and at the same time associate attributes about that "person" object.


Before you go on:

The examples in this lesson require additions to the schema. You must either turn schema-checking off in the LDAP server, or add the schema that accompanies this tutorial to the server. Both of these tasks are typically performed by the directory server's administrator.


Creating a Context with Attributes

To create a context with attributes, you supply the name of the context you want to create and its attributes to the DirContext.createSubcontext()(in the API reference documentation) method:
// Create attributes to be associated with new context
Attributes attrs = new BasicAttributes(true); // case-ignore
Attribute objclass = new BasicAttribute("objectclass");
objclass.add("top");
objclass.add("extensibleObject");
attrs.put(objclass);

// Create the context
Context result = ctx.createSubcontext("cn=Fruits", attrs);
This example creates a new context called "cn=Fruits" with an attribute "objectclass" with two values: "top" and "extensibleObject", in the context ctx. If you list the context ctx, you will see that there is now an entry for "cn=Fruits".
# java Create
ou=Groups: javax.naming.directory.DirContext
ou=People: javax.naming.directory.DirContext
cn=Fruits: javax.naming.directory.DirContext

Adding a Binding with Attributes

The DirContext.bind()(in the API reference documentation) method is used to add a binding with attributes to a context. It accepts as arguments the name of the object, the object to be bound, and a set of attributes.
// Create object to be bound
Fruit fruit = new Fruit("orange");

// Create attributes to be associated with object
Attributes attrs = new BasicAttributes(true); // case-ignore
Attribute objclass = new BasicAttribute("objectclass");
objclass.add("top");
objclass.add("extensibleObject");
attrs.put(objclass);
attrs.put("color", "orange");
attrs.put("flavor", "sweet");

// Perform bind
ctx.bind("cn=favorite, cn=Fruits", fruit, attrs);
This example creates an object of class Fruit and binds it to the name "cn=favorite" into the context named "cn=Fruits", relative to ctx. This binding has three attributes ("objectclass", "color", and "flavor"). If you were to subsequently look up the name "cn=favorite, cn=Fruits" in ctx, you would get the fruit object. If you were to subsequently get the attributes of "cn=favorite, cn=Fruits", you would get those attributes with which the object was created. The following shows this example's output.

The extra attributes and attribute values that you see are the used to stored information about the object (fruit). Details about these extra attributes are discussed in the Java Objects and the Directory (in the Java Objects and the Directory trail) trail.

# java Bind
orange
attribute: flavor
value: sweet
attribute: color
value: orange
attribute: objectclass
value: top
value: extensibleObject
value: javaObject
value: javaNamingReference
attribute: javaclassname
value: Fruit
attribute: javafactory
value: FruitFactory
attribute: javareferenceaddress
value: #0#fruit#orange
attribute: cn
value: favorite
If you run this example twice, the second attempt will fail with a NameAlreadyBoundException(in the API reference documentation) because the name "cn=favorite" is already bound in the "cn=Fruits" context. In order for the second attempt to succeed, you will have to use rebind().

Overwriting a Binding with Attributes

The DirContext.rebind()(in the API reference documentation) method is used to add or replace a binding and its attributes. It accepts the same arguments as bind() but the semantics of rebind() is that if the name is already bound, it will be unbound and the newly given object and attributes bound:
// Create object to be bound
Fruit fruit = new Fruit("lemon");

// Create attributes to be associated with object
Attributes attrs = new BasicAttributes(true); // case-ignore
Attribute objclass = new BasicAttribute("objectclass");
objclass.add("top");
objclass.add("extensibleObject");
attrs.put(objclass);
attrs.put("color", "yellow");
attrs.put("flavor", "sour");

// Perform bind
ctx.rebind("cn=favorite, cn=Fruits", fruit, attrs);
When you run this example, it will replace the binding created by the bind() example:
# java Rebind
lemon
attribute: flavor
value: sour
attribute: color
value: yellow
attribute: objectclass
value: top
value: extensibleObject
value: javaObject
value: javaNamingReference
attribute: javaclassname
value: Fruit
attribute: javafactory
value: FruitFactory
attribute: javareferenceaddress
value: #0#fruit#lemon
attribute: cn
value: favorite


Previous | Next | Trail Map | The Basics | Directory Operations