KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > thoughtworks > xstream > converters > extended > DynamicProxyConverter


1 package com.thoughtworks.xstream.converters.extended;
2
3 import com.thoughtworks.xstream.alias.ClassMapper;
4 import com.thoughtworks.xstream.converters.ConversionException;
5 import com.thoughtworks.xstream.converters.Converter;
6 import com.thoughtworks.xstream.converters.MarshallingContext;
7 import com.thoughtworks.xstream.converters.UnmarshallingContext;
8 import com.thoughtworks.xstream.io.HierarchicalStreamReader;
9 import com.thoughtworks.xstream.io.HierarchicalStreamWriter;
10
11 import java.lang.reflect.InvocationHandler JavaDoc;
12 import java.lang.reflect.Proxy JavaDoc;
13 import java.util.ArrayList JavaDoc;
14 import java.util.List JavaDoc;
15
16 /**
17  * Converts a dynamic proxy to XML, storing the implemented
18  * interfaces and handler.
19  *
20  * @author Joe Walnes
21  */

22 public class DynamicProxyConverter implements Converter {
23
24     private ClassLoader JavaDoc classLoader;
25     private ClassMapper classMapper;
26
27     public DynamicProxyConverter(ClassMapper classMapper) {
28         this(classMapper, DynamicProxyConverter.class.getClassLoader());
29     }
30
31     public DynamicProxyConverter(ClassMapper classMapper, ClassLoader JavaDoc classLoader) {
32         this.classLoader = classLoader;
33         this.classMapper = classMapper;
34     }
35
36     public boolean canConvert(Class JavaDoc type) {
37         return type.equals(ClassMapper.DynamicProxy.class) || Proxy.isProxyClass(type);
38     }
39
40     public void marshal(Object JavaDoc source, HierarchicalStreamWriter writer, MarshallingContext context) {
41         InvocationHandler JavaDoc invocationHandler = Proxy.getInvocationHandler(source);
42         addInterfacesToXml(source, writer);
43         writer.startNode("handler");
44         writer.addAttribute("class", classMapper.lookupName(invocationHandler.getClass()));
45         context.convertAnother(invocationHandler);
46         writer.endNode();
47     }
48
49     private void addInterfacesToXml(Object JavaDoc source, HierarchicalStreamWriter writer) {
50         Class JavaDoc[] interfaces = source.getClass().getInterfaces();
51         for (int i = 0; i < interfaces.length; i++) {
52             Class JavaDoc currentInterface = interfaces[i];
53             writer.startNode("interface");
54             writer.setValue(classMapper.lookupName(currentInterface));
55             writer.endNode();
56         }
57     }
58
59     public Object JavaDoc unmarshal(HierarchicalStreamReader reader, UnmarshallingContext context) {
60         List JavaDoc interfaces = new ArrayList JavaDoc();
61         InvocationHandler JavaDoc handler = null;
62         while (reader.hasMoreChildren()) {
63             reader.moveDown();
64             String JavaDoc elementName = reader.getNodeName();
65             if (elementName.equals("interface")) {
66                 interfaces.add(classMapper.lookupType(reader.getValue()));
67             } else if (elementName.equals("handler")) {
68                 Class JavaDoc handlerType = classMapper.lookupType(reader.getAttribute("class"));
69                 handler = (InvocationHandler JavaDoc) context.convertAnother(null, handlerType);
70             }
71             reader.moveUp();
72         }
73         if (handler == null) {
74             throw new ConversionException("No InvocationHandler specified for dynamic proxy");
75         }
76         Class JavaDoc[] interfacesAsArray = new Class JavaDoc[interfaces.size()];
77         interfaces.toArray(interfacesAsArray);
78         return Proxy.newProxyInstance(classLoader, interfacesAsArray, handler);
79     }
80 }
81
Popular Tags