Developing Java Beans private void readObject(ObjectInputStream stream) throws

Developing Java Beans When the ListeningPanel instance is serialized, the class hierarchy is walked upward. So when the data from the java.awt.Container class is serialized, the buttons are also serialized at that time. These buttons keep references to their action listeners, which in this case is an instance of the GenericButtonAdapter class. Since GenericButtonAdapter is not serializable, the process fails. It doesn’t help us to mark the instance of the adapter within the ListeningPanel as transient, because that isn’t the only reference to the adapter. So we need to find another solution. Let’s modify the GenericButtonAdapter class to be serializable. The first step is to declare that it implements the java.io.Serializable interface. However, declaring that the class is serializable isn’t enough. As I said previously, the real issue here is that the hash table is holding references to instances of java.lang.reflect.Method , which are not serializable. We’re going to have to do some extra work when the adapter is serialized and deserialized. We also have to mark the java.util.Hashtable data member, which is named mappingTable, as transient. This prevents Java from including the hash table when it serializes our adapter objects. However, this has significant implications for the design of our program both for the GenericButtonAdapter class itself, and also the adapter’s clients. The original implementation of the registerActionEventHandler() method added the method mappings to the hash table and registered the adapter as an action event listener for the button. Since we can’t serialize the hash table, we need to provide a way to rebuild the mappings as part of the deserialization process. Therefore, we need a method to add elements to the mappings but we don’t want to call the addActionListener() method on the button again, as this relationship already exists in the object stream being deserialized. To solve the problem, let’s introduce a new method called addMapping(), which adds elements to mappingTable. Now the registerActionEventHandler() method invokes addMapping() and addActionEventListener() on the button. The addMapping() method allows us to recreate the method mappings during deserialization. But we still need a mechanism to create an empty instance of the hash table. This can be handled by the addMapping() method. When it gets called, we can check to see if the hash table has been created. If it hasn’t, it can be created right there. We need to implement writeObject() and readObject() so that we can save and restore the data that is held in the hash table. The writeObject() method invokes defaultWriteObject() on the stream to store the non-transient data members, but now we need to store enough information from mappingTable so that it can be reconstructed later. First we have to consider the case where mappingTable has not yet been constructed. In this case we store a count of elements to the stream and return, since there isn’t anything else to do. Otherwise, we need to make a clone of the hash table within a synchronized block to ensure that no mappings are added while we are serializing. page 83
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.