1 6 package org.prevayler.implementation.snapshot; 7 8 import java.io.IOException ; 9 import java.io.InputStream ; 10 import java.io.InputStreamReader ; 11 import java.io.OutputStream ; 12 import java.io.OutputStreamWriter ; 13 14 import com.thoughtworks.xstream.XStream; 15 16 17 37 public class XStreamSnapshotManager extends SnapshotManager { 38 39 private ThreadLocal _xstreams = new ThreadLocal () { 40 protected Object initialValue() { 41 return createXStream(); 42 } 43 }; 44 45 private String _encoding; 46 47 50 public XStreamSnapshotManager(Object newPrevalentSystem, String snapshotDirectoryName) throws ClassNotFoundException , IOException { 51 this(newPrevalentSystem, snapshotDirectoryName, null); 52 } 53 54 63 public XStreamSnapshotManager(Object newPrevalentSystem, String snapshotDirectoryName, String encoding) throws ClassNotFoundException , IOException { 64 _encoding = encoding; 65 init(newPrevalentSystem, snapshotDirectoryName); 66 } 67 68 private XStream getXStream() { 69 return (XStream) _xstreams.get(); 70 } 71 72 75 public void writeSnapshot(Object prevalentSystem, OutputStream out) throws IOException { 76 OutputStreamWriter writer = _encoding == null ? new OutputStreamWriter (out) : new OutputStreamWriter (out, _encoding); 77 getXStream().toXML(prevalentSystem, writer); 78 writer.flush(); 79 } 80 81 84 public Object readSnapshot(InputStream in) throws IOException { 85 return getXStream().fromXML(_encoding == null ? new InputStreamReader (in) : new InputStreamReader (in, _encoding)); 86 } 87 88 91 protected String suffix() { 92 return "xstreamsnapshot"; 93 } 94 95 98 protected XStream createXStream() { 99 return new XStream(); 100 } 101 102 } | Popular Tags |