![]() ![]() ![]() ![]() |
Storing Objects in the Directory |
Definitions
To serialize an object means to convert its state into a byte stream in such a way that the byte stream can be reverted back into a copy of the object. A Java object is serializable if its class or any of its superclasses implements either the java.io.Serializable interface or its subinterface java.io.Externalizable. Deserialization is the process of converting the serialized form of an object back into a copy of the object.Because the java.awt.Button class implements the Serializable interface, for example, you can serialize a java.awt.Button object and store that serialized state in a file. Later, you read back the serialized state and convert it back (that is, "deserialize" it) into a java.awt.Button object.
The Java platform specifies a default way by which serializable objects are serialized. A (Java) class can also override this default serialization and define its own way of serializing objects of that class. The Object Serialization Specification describes object serialization in detail.
When an object is serialized, information that identifies its class is recorded in the serialized stream. However, the class's definition ("class file") itself is not recorded. It is the responsibility of the system that is deserializing the object to determine which mechanism to use for locating and loading the associated class files. For example, the Java application might include in its classpath a JAR file containing the class files of the serialized object(s).
Binding a Serializable Object
You can store a serializable object in the directory if the underlying service provider supports it. Sun's LDAP service provider supports storing serialized objects.The following example invokes the Context.bind()
method to bind an AWT button to the name "cn=Button". You can also use the DirContext.bind()
method if you want to associate attributes with the new binding. Context.rebind()
and DirContext.rebind()
can be used if you want to overwrite an existing binding.
You can then read it back using Context.lookup()// Create object to be bound Button b = new Button("Push me"); // Perform bind ctx.bind("cn=Button", b);as follows:
Running this example produces the following output:// Check that it is bound Button b2 = (Button)ctx.lookup("cn=Button"); System.out.println(b2);# java SerObj java.awt.Button[button0,0,0,0x0,invalid,label=Push me]
![]() ![]() ![]() ![]() |
Storing Objects in the Directory |