Developing Java Beans private void readObject(ObjectInputStream stream) throws

Developing Java Beans We synchronize on mappingTable itself in order to prevent another thread from accessing the hash table. This is sufficient because all of the access routines on the Hashtable class are synchronized. The first step is to save the number of elements in mappingTable. We call the size() method on the hash table to determine how many elements it has, and store this value in the stream by calling writeInt(). We need to iterate over the keys and values of the hash table, so we invoke the keys() and elements() methods to get instances of java.util.Enumeration for each. Now we can loop through the objects in the two enumerations together. We retrieve a java.awt.Buttonfrom one enumeration and store it to the stream using writeObject(). The other enumeration contains instances of java.lang.re-flect.Method. This is the object that gives us a little trouble; we can’t directly serialize an instance of java.lang.reflect.Method. But all we really need is the method name, because we can use our addMapping() method to reconstruct the method object later. We get the name of the method by invoking its getName() method, which returns an instance of java.lang.String. Finally, we pass the name to the stream using writeObject(). For reconstructing the adapter during deserialization, we implement readObject(). The defaultReadObject() method is called on the stream to read the non-transient data members of the adapter. Next, the number of mappings are read from the stream by invoking the readInt() method. Finally we run a loop over the number of mappings to read back the stored instances of java.awt.Button and java.lang.String. For each pair of objects read from the stream, the addMapping() method is called in order to reconstruct the elements of mappingTable. The most important aspect of this example is that it is possible to add additional data to a stream that can be used during deserialization to reconstruct objects that are not serializable. Here’s the code for the modified GenericButtonAdapter class: package BeansBook.util; import java.awt.*; import java.awt.event.*; import java.lang.reflect.*; import java.util.*; import java.io.*; public class GenericButtonAdapter implements ActionListener, // the adapter receives the events Serializable { // the target object protected Object theTarget; // the class of the target object protected Class theTargetClass; // the class array for the parameters used for // the target method protected final static Class paramClasses[] = { java.awt.event.ActionEvent.class }; // the mapping of source objects to callback methods protected transient Hashtable mappingTable; // constructor public GenericButtonAdapter(Object target) { theTarget = target; theTargetClass = target.getClass(); page 84
Note: If you are looking for cheap and reliable provider to host and run your servlet application check Vision servlet hosting services

Comments are closed.