Previous | Next | Trail Map | Java Objects and the Directory | Object Factories

Custom Object Example

The custom object example (in the Java Objects and the Directory trail) illustrates how an instance of a user-defined class Person is stored and subsequently looked up from the directory. When the Person object's attributes are looked up from the directory, the Context.lookup() (in the API reference documentation) method turned these attributes into an instance of Person.
Person john2 = (Person) ctx.lookup("cn=John Smith");
This was made possible because: PersonObjectFactory.getObjectInstance() first verifies that the object is intended for its factory. It does this by checking that the "objectclass" attribute has a value of "person". If this verification fails, it returns null. Otherwise, it constructs an instance of Person by supplying the constructor values obtained from the entry's other attributes, such as the "surname" and "commonname" attributes.

The definition of PersonObjectFactory.getObjectInstance() is as follows:

public Object getObjectInstance(
    Object obj, Name name, Context ctx, Hashtable env, Attributes attrs)
    throws Exception {

    // Only interested in Attributes with person objectclass
    // System.out.println("object factory: " + attrs);
    Attribute oc = (attrs != null ? attrs.get("objectclass") : null);
    if (oc != null && oc.contains("person")) {
        Attribute attr;
        Person per = new Person(
          (String)attrs.get("sn").get(),
          (String)attrs.get("cn").get(),
          (attr=attrs.get("userPassword")) != null ? (String)attr.get() : null,
          (attr=attrs.get("telephoneNumber")) != null ? (String)attr.get() : null,
          (attr=attrs.get("seealso")) != null ? (String)attr.get() : null,
          (attr=attrs.get("description")) != null ? (String)attr.get() : null);
        return per;
    }
    return null;
}

Object Factories: End of Lesson

What's next? Now you can:


Previous | Next | Trail Map | Java Objects and the Directory | Object Factories