文档

Java™ 教程-Java Tutorials 中文版
存储和读取对象
Trail: Java Naming and Directory Interface
Lesson: Java Objects in the Directory

存储和读取对象

Applications and services can use the directory in different ways to store and locate objects:

In general terms, a Java object's serialized form contains the object's state and an object's reference in a compact representation of addressing information that can be used to contact the object. Some examples are given in the Lookup an Object lesson. An object's attributes are properties that are used to describe the object; attributes might include addressing and/or state information.

Which of these three ways to use depends on the application/system that is being built and how it needs to interoperate with other applications and systems that will share the objects stored in the directory. Another factor is the support provided by the service provider and the underlying directory service.

Programmatically, all applications use one of the following methods when storing objects in the directory:

The application passes the object that it wants to store to one of these methods. Then, depending on the types of objects that the service provider supports, the object will be transformed into a representation acceptable to the underlying directory service.

This lesson shows how to store serializable objects in the directory once the object is stored, you can simply use lookup()to get a copy of the object back from the directory, regardless of what type of information was actually stored.

You can get the object back not only by using lookup(), but also when you list a context and when you search a context or its subtree. In all of these cases, object factories might be involved. Object factories are discussed in detail in the JNDI Tutorial.

For storing below objects types, please refer to the JNDI Tutorial:


Before you go on:To run these examples successfully, you must either turn off schema-checking in the server or add the Java schema that accompany this tutorial to the server. This task is typically performed by the directory server's administrator. See the Software Setup lesson for more information.

Windows Active Directory:Context.rebind() and DirContext.rebind() do not work against Active Directory because these methods work by reading the attributes of the entry to be updated, removing the entry, and then adding a new entry that contains the modified attributes. Active Directory returns some attributes that cannot be set by the user, causing the final addition step to fail. The workaround for this problem is to use DirContext.getAttributes() to obtain and save the attributes that you want to keep. Then, remove the entry and add it back with the saved attributes (and any others that you want to add) using DirContext.bind().


Previous page: Java Objects in the Directory
Next page: Serializable Objects