KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > thoughtworks > xstream > converters > reflection > ExternalizableConverter


1 package com.thoughtworks.xstream.converters.reflection;
2
3 import com.thoughtworks.xstream.converters.ConversionException;
4 import com.thoughtworks.xstream.converters.Converter;
5 import com.thoughtworks.xstream.converters.MarshallingContext;
6 import com.thoughtworks.xstream.converters.UnmarshallingContext;
7 import com.thoughtworks.xstream.core.util.CustomObjectInputStream;
8 import com.thoughtworks.xstream.core.util.CustomObjectOutputStream;
9 import com.thoughtworks.xstream.io.HierarchicalStreamReader;
10 import com.thoughtworks.xstream.io.HierarchicalStreamWriter;
11 import com.thoughtworks.xstream.mapper.Mapper;
12
13 import java.io.Externalizable JavaDoc;
14 import java.io.IOException JavaDoc;
15 import java.io.NotActiveException JavaDoc;
16 import java.io.ObjectInput JavaDoc;
17 import java.io.ObjectInputValidation JavaDoc;
18 import java.io.ObjectOutput JavaDoc;
19 import java.util.Map JavaDoc;
20
21 /**
22  * Converts any object that implements the java.io.Externalizable interface, allowing compatability with native Java
23  * serialization.
24  *
25  * @author Joe Walnes
26  */

27 public class ExternalizableConverter implements Converter {
28
29     private Mapper mapper;
30
31     public ExternalizableConverter(Mapper mapper) {
32         this.mapper = mapper;
33     }
34
35     public boolean canConvert(Class JavaDoc type) {
36         return Externalizable JavaDoc.class.isAssignableFrom(type);
37     }
38
39     public void marshal(Object JavaDoc source, final HierarchicalStreamWriter writer, final MarshallingContext context) {
40         try {
41             Externalizable JavaDoc externalizable = (Externalizable JavaDoc) source;
42             CustomObjectOutputStream.StreamCallback callback = new CustomObjectOutputStream.StreamCallback() {
43                 public void writeToStream(Object JavaDoc object) {
44                     if (object == null) {
45                         writer.startNode("null");
46                         writer.endNode();
47                     } else {
48                         writer.startNode(mapper.serializedClass(object.getClass()));
49                         context.convertAnother(object);
50                         writer.endNode();
51                     }
52                 }
53
54                 public void writeFieldsToStream(Map JavaDoc fields) {
55                     throw new UnsupportedOperationException JavaDoc();
56                 }
57
58                 public void defaultWriteObject() {
59                     throw new UnsupportedOperationException JavaDoc();
60                 }
61
62                 public void flush() {
63                     writer.flush();
64                 }
65
66                 public void close() {
67                     throw new UnsupportedOperationException JavaDoc("Objects are not allowed to call ObjecOutput.close() from writeExternal()");
68                 }
69             };
70             ObjectOutput JavaDoc objectOutput = CustomObjectOutputStream.getInstance(context, callback);
71             externalizable.writeExternal(objectOutput);
72         } catch (IOException JavaDoc e) {
73             throw new ConversionException("Cannot serialize " + source.getClass().getName() + " using Externalization", e);
74         }
75     }
76
77     public Object JavaDoc unmarshal(final HierarchicalStreamReader reader, final UnmarshallingContext context) {
78         final Class JavaDoc type = context.getRequiredType();
79         try {
80             Externalizable JavaDoc externalizable = (Externalizable JavaDoc) type.newInstance();
81             CustomObjectInputStream.StreamCallback callback = new CustomObjectInputStream.StreamCallback() {
82                 public Object JavaDoc readFromStream() {
83                     reader.moveDown();
84                     Object JavaDoc streamItem = context.convertAnother(type, mapper.realClass(reader.getNodeName()));
85                     reader.moveUp();
86                     return streamItem;
87                 }
88
89                 public Map JavaDoc readFieldsFromStream() {
90                     throw new UnsupportedOperationException JavaDoc();
91                 }
92
93                 public void defaultReadObject() {
94                     throw new UnsupportedOperationException JavaDoc();
95                 }
96
97                 public void registerValidation(ObjectInputValidation JavaDoc validation, int priority) throws NotActiveException JavaDoc {
98                     throw new NotActiveException JavaDoc("stream inactive");
99                 }
100
101                 public void close() {
102                     throw new UnsupportedOperationException JavaDoc("Objects are not allowed to call ObjectInput.close() from readExternal()");
103                 }
104             };
105             ObjectInput JavaDoc objectInput = CustomObjectInputStream.getInstance(context, callback);
106             externalizable.readExternal(objectInput);
107             return externalizable;
108         } catch (InstantiationException JavaDoc e) {
109             throw new ConversionException("Cannot construct " + type.getClass(), e);
110         } catch (IllegalAccessException JavaDoc e) {
111             throw new ConversionException("Cannot construct " + type.getClass(), e);
112         } catch (IOException JavaDoc e) {
113             throw new ConversionException("Cannot externalize " + type.getClass(), e);
114         } catch (ClassNotFoundException JavaDoc e) {
115             throw new ConversionException("Cannot externalize " + type.getClass(), e);
116         }
117     }
118 }
119
Popular Tags