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 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 28 public Object unmarshal(HierarchicalStreamReader reader, Object root, DataHolder dataHolder) { 29 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 registerConverter(new AssociatedConverterImpl(),-10); 49 } 50 51 private static final class AssociatedConverterImpl implements Converter { 52 private Converter findConverter(Class t) { 53 try { 54 if(t==null || t.getClassLoader()==null) 55 return null; 56 Class <?> cl = t.getClassLoader().loadClass(t.getName() + "$ConverterImpl"); 57 return (Converter)cl.newInstance(); 58 } catch (ClassNotFoundException e) { 59 return null; 60 } catch (IllegalAccessException e) { 61 IllegalAccessError x = new IllegalAccessError (); 62 x.initCause(e); 63 throw x; 64 } catch (InstantiationException e) { 65 InstantiationError x = new InstantiationError (); 66 x.initCause(e); 67 throw x; 68 } 69 } 70 71 public boolean canConvert(Class type) { 72 return findConverter(type)!=null; 73 } 74 75 public void marshal(Object source, HierarchicalStreamWriter writer, MarshallingContext context) { 76 findConverter(source.getClass()).marshal(source,writer,context); 77 } 78 79 public Object unmarshal(HierarchicalStreamReader reader, UnmarshallingContext context) { 80 return findConverter(context.getRequiredType()).unmarshal(reader,context); 81 } 82 } 83 } 84 | Popular Tags |