Previous | Next | Trail Map | Tips for LDAP Users | Miscellaneous

Storing Objects

In the Objects (in the Java Objects and the Directory trail) trail, you saw how to store and read Java objects from the directory. Specifically, in the LDAP Directories (in the Java Objects and the Directory trail) section, you learned how Java objects are presented as attributes in an LDAP directory. If you have not gone through that trail yet, it is a good idea to at least read through it to understand some of the terminology before going further.

Sun's LDAP service provider supports storing objects that

  1. Are instances of Reference (in the API reference documentation)
  2. Implement the Referenceable (in the API reference documentation) interface
  3. Implement the Serializable interface
  4. Implement the DirContext (in the API reference documentation) interface
The LDAP Directories (in the Java Objects and the Directory trail) section describes the representation of a Reference as LDAP attributes. By default, the hash character ('#') is used to encode the RefAddr (in the API reference documentation) in the Reference. If this character already appears in the contents of the RefAddr, then you need to choose another character to use. You do this by setting the java.naming.ldap.ref.separator environment property to a string containing the separator character. Here's an example.

If you run the reference example (in the Java Objects and the Directory trail) , and then examine the "cn=favorite" entry in the directory, you will see the following attributes:

objectclass: top, javaContainer, javaNamingReference
javaclassname: Fruit 
javafactory: FruitFactory
javareferenceaddress: #0#fruit#orange
cn: favorite

You can modify that example to use the colon character (":") as the separator as follows:

// Ask to use ':' as the encoding character
env.put("java.naming.ldap.ref.separator", ":");

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

// Create object to be bound
Fruit fruit = new Fruit("orange");

// Perform bind
ctx.rebind("cn=favorite", fruit);
The modified program produces attributes that look as follows:
objectclass: top, javaContainer, javaNamingReference
javaclassname: Fruit
javafactory: FruitFactory
javareferenceaddress: :0:fruit:orange
cn: favorite


Previous | Next | Trail Map | Tips for LDAP Users | Miscellaneous