KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > hudson > util > XStream2


1 package hudson.util;
2
3 import com.thoughtworks.xstream.XStream;
4 import com.thoughtworks.xstream.converters.Converter;
5 import com.thoughtworks.xstream.converters.DataHolder;
6 import com.thoughtworks.xstream.converters.MarshallingContext;
7 import com.thoughtworks.xstream.converters.UnmarshallingContext;
8 import com.thoughtworks.xstream.io.HierarchicalStreamDriver;
9 import com.thoughtworks.xstream.io.HierarchicalStreamReader;
10 import com.thoughtworks.xstream.io.HierarchicalStreamWriter;
11 import hudson.model.Hudson;
12
13 /**
14  * {@link XStream} enhanced for retroweaver support.
15  * @author Kohsuke Kawaguchi
16  */

17 public class XStream2 extends XStream {
18     public XStream2() {
19         init();
20     }
21
22     public XStream2(HierarchicalStreamDriver hierarchicalStreamDriver) {
23         super(hierarchicalStreamDriver);
24         init();
25     }
26
27     @Override JavaDoc
28     public Object JavaDoc unmarshal(HierarchicalStreamReader reader, Object JavaDoc root, DataHolder dataHolder) {
29         // init() is too early to do this
30
// defensive because some use of XStream happens before plugins are initialized.
31
Hudson h = Hudson.getInstance();
32         if(h!=null && h.pluginManager!=null && h.pluginManager.uberClassLoader!=null) {
33             setClassLoader(h.pluginManager.uberClassLoader);
34         }
35
36         return super.unmarshal(reader,root,dataHolder);
37     }
38
39     private void init() {
40         registerConverter(new RobustCollectionConverter(getClassMapper()),10);
41         registerConverter(new RetroweaverEnumConverter(),10);
42         registerConverter(new RetrotranslatorEnumConverter(),10);
43         registerConverter(new CopyOnWriteList.ConverterImpl(getClassMapper()),10);
44         registerConverter(new DescribableList.ConverterImpl(getClassMapper()),10);
45
46         // this should come after all the XStream's default simpler converters,
47
// but before reflection-based one kicks in.
48
registerConverter(new AssociatedConverterImpl(),-10);
49     }
50
51     private static final class AssociatedConverterImpl implements Converter {
52         private Converter findConverter(Class JavaDoc t) {
53             try {
54                 if(t==null || t.getClassLoader()==null)
55                     return null;
56                 Class JavaDoc<?> cl = t.getClassLoader().loadClass(t.getName() + "$ConverterImpl");
57                 return (Converter)cl.newInstance();
58             } catch (ClassNotFoundException JavaDoc e) {
59                 return null;
60             } catch (IllegalAccessException JavaDoc e) {
61                 IllegalAccessError JavaDoc x = new IllegalAccessError JavaDoc();
62                 x.initCause(e);
63                 throw x;
64             } catch (InstantiationException JavaDoc e) {
65                 InstantiationError JavaDoc x = new InstantiationError JavaDoc();
66                 x.initCause(e);
67                 throw x;
68             }
69         }
70
71         public boolean canConvert(Class JavaDoc type) {
72             return findConverter(type)!=null;
73         }
74
75         public void marshal(Object JavaDoc source, HierarchicalStreamWriter writer, MarshallingContext context) {
76             findConverter(source.getClass()).marshal(source,writer,context);
77         }
78
79         public Object JavaDoc unmarshal(HierarchicalStreamReader reader, UnmarshallingContext context) {
80             return findConverter(context.getRequiredType()).unmarshal(reader,context);
81         }
82     }
83 }
84
Popular Tags