![]() ![]() ![]() ![]() |
Directory Operations |
In the naming lessonyou learned about using the bind(), rebind(), and createSubcontext() methods in the Context
interface to create bindings and subcontexts. The DirContext
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()method:
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".// 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);# java Create ou=Groups: javax.naming.directory.DirContext ou=People: javax.naming.directory.DirContext cn=Fruits: javax.naming.directory.DirContextAdding a Binding with Attributes
The DirContext.bind()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.
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.// 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);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
trail.
If you run this example twice, the second attempt will fail with a NameAlreadyBoundException# 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: favoritebecause 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()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:
When you run this example, it will replace the binding created by the bind() example:// 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);# 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
![]() ![]() ![]() ![]() |
Directory Operations |