| |
"Designing for Object Serialization"
Vol. 6, Issue 7, p. 64
Listing 1
java.io.NotSerializableException: bw.dm.impl.FieldAspectAdapter
at java.io.ObjectOutputStream.outputObject(ObjectOutputStream.java:845)
at java.io.ObjectOutputStream.writeObject(ObjectOutputStream.java:342)
at java.io.ObjectOutputStream.outputArray(ObjectOutputStream.java:811)
at java.io.ObjectOutputStream.checkSubstitutableSpecialClasses(...:432)
at java.io.ObjectOutputStream.writeObject(ObjectOutputStream.java:337)
at java.io.ObjectOutputStream.outputClassFields(ObjectOutputStream.java:1567)
at java.io.ObjectOutputStream.defaultWriteObject(ObjectOutputStream.java:453)
at java.io.ObjectOutputStream.outputObject(ObjectOutputStream.java:911)
at java.io.ObjectOutputStream.writeObject(ObjectOutputStream.java:342)
. . .
Listing 2
package bw.dm.test;
import java.io.*;
/**
* Serializes and deserializes an object. Used to verify
* that the object can be successfully serialized (and
* deserialized).
*
* Simulates the typical serialization process of
* serializing an object, persisting the data somewhere
* (pehaps in a file), retreiving the persistent data,
* and deserializing it to get the object back.
*
* DataMapper examples do not <EM>have</EM> to use
* serialization unless they need it. But format maps
* are expected to be serializible, and this is a good
* way to test one to make sure that it is.
*
* @see java.io.Serializable
* @see java.io.ObjectInput
* @see java.io.ObjectOutput
*/
public class ObjectSerializer {
public static Object serializeAndDeserialize(Serializable obj) {
try {
byte[] bytes = ObjectSerializer.serialize(obj);
return ObjectSerializer.deserialize(bytes);
}
catch (IOException ex) {
throw new RuntimeException("Serialization failed: "
+ ex.getClass().getName() + " - " + ex.getMessage());
}
catch (ClassNotFoundException ex) {
throw new RuntimeException("Serialization failed: "
+ ex.getClass().getName() + " - " + ex.getMessage());
}
}
protected static byte[] serialize(Serializable obj)
throws IOException {
ByteArrayOutputStream stream = new ByteArrayOutputStream();
ObjectOutput serialStream = new ObjectOutputStream(stream);
serialStream.writeObject(obj);
return stream.toByteArray();
}
protected static Object deserialize(byte[] bytes)
throws IOException, ClassNotFoundException {
InputStream stream = new ByteArrayInputStream(bytes);
ObjectInput serialStream = new ObjectInputStream(stream);
return serialStream.readObject();
}
}
Listing 3
public class FieldAspectAdapterProxy extends FieldAdapterProxy {
protected AspectSpec spec;
transient protected ProtocolAdapter adapter = null;
protected void setupDomainAdapter() {
if (adapter == null) {
try {
this.initializeAdapter();
}
catch . . .
}
}
protected void initializeAdapter() throws . . . {
adapter = new FieldAspectAdapter(field, spec);
}
. . .
}
architectures using various J2EE technologies and embeddable tools.
|
|