For example, the java.awt.Button class implements the Serializable interface, so you can serialize a java.awt.Button object and store that serialized state in a file. Later, you can read back the serialized state and deserialize into a java.awt.Button object.
The Java platform specifies a default way by which serializable objects are serialized. A (Java) class can 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 how to locate and load the necessary class files. For example, a Java application might include in its classpath a JAR file that contains the class files of the serialized object(s) or load the class definitions by using information stored in the directory, as explained later in this lesson.
The following example invokes
Context.bind
to bind an AWT button to the name "cn=Button".
To associate attributes with the new binding, you use
DirContext.bind
.
To overwrite an existing binding, use
Context.rebind
and
DirContext.rebind
.
// Create the object to be bound Button b = new Button("Push me"); // Perform the bind ctx.bind("cn=Button", b);
Context.lookup
, as follows.
// 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]
Alternatively, you can record a codebase with the serialized
object in the directory, either when you bind the object or subsequently
by adding an attribute by using
DirContext.modifyAttributes
.
You can use any attribute to record this codebase and have your application
read that attribute from the directory and use it appropriately.
Or you can use the "javaCodebase" attribute specified in .
In the latter case, Sun's LDAP service provider will automatically
use the attribute to load the class definitions as needed.
"javaCodebase" should contain the URL of a codebase directory
or a JAR file.
If the codebase contains more than one URL,
then each URL must be separated
by a space character.
The following example resembles the one for binding a java.awt.Button. It differs in that it uses a user-defined Serializable class, Flower, and supplies a "javaCodebase" attribute that contains the location of Flower's class definition. Here's the code that does the binding.
String codebase = ...; // Create the object to be bound Flower f = new Flower("rose", "pink"); // Perform the bind and specify the codebase ctx.bind("cn=Flower", f, new BasicAttributes("javaCodebase", codebase));
# java SerObjWithCodebase http://web1/example/classes/ pink rose