KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > prevayler > implementation > snapshot > XStreamSnapshotManager


1 //Prevayler(TM) - The Free-Software Prevalence Layer.
2
//Copyright (C) 2001-2004 Klaus Wuestefeld
3
//This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
4
//Contributions: Jacob Kjome.
5

6 package org.prevayler.implementation.snapshot;
7
8 import java.io.IOException JavaDoc;
9 import java.io.InputStream JavaDoc;
10 import java.io.InputStreamReader JavaDoc;
11 import java.io.OutputStream JavaDoc;
12 import java.io.OutputStreamWriter JavaDoc;
13
14 import com.thoughtworks.xstream.XStream;
15
16
17 /**
18  * Writes and reads snapshots to/from XML files.
19  *
20  * <p>This implementation requires the <a HREF="http://xstream.codehaus.org/">XStream</a>
21  * Java and XML language binding framework which provides for Java object XML serialization.</p>
22  *
23  * <p>Note that XStream has some dependencies of its own. It requires the standard XML API's
24  * (xml-apis.jar from the <a HREF="http://xml.apache.org/xerces2-j/">Apache Xerces2-j</a> project or j2sdk1.4+)
25  * and an XML implementation (again, provided by Xerces2 or j2sdk1.4+).</p>
26  *
27  * <p>To make XStream up to 10x faster, add <a HREF="http://www.extreme.indiana.edu/xgws/xsoap/xpp/mxp1/">XPP3</a>
28  * to the classpath. XStream has the concept of a
29  * <a HREF="http://xstream.codehaus.org/javadoc/com/thoughtworks/xstream/io/HierarchicalStreamDriver.html">HierarchicalStreamDriver</a>
30  * and the default implementation for XStream is the highly performant XppDriver. However, XStream will fall back to the DomDriver if XPP3 is
31  * not found in the classpath making the XPP3 library entirely optional... well, not quite. See <a HREF="http://jira.codehaus.org/browse/XSTR-71">XSTR-71</a>.
32  * The current decision in that issue forces XPP3 to be a required runtime dependency when using XStream unless one specifically configures another driver, such as
33  * the DomDriver.</p>
34  *
35  * @see org.prevayler.implementation.snapshot.SnapshotManager
36  */

37 public class XStreamSnapshotManager extends SnapshotManager {
38
39     private ThreadLocal JavaDoc _xstreams = new ThreadLocal JavaDoc() {
40         protected Object JavaDoc initialValue() {
41             return createXStream();
42         }
43     };
44
45     private String JavaDoc _encoding;
46
47     /**
48      * @see #XStreamSnapshotManager(Object, String, String)
49      */

50     public XStreamSnapshotManager(Object JavaDoc newPrevalentSystem, String JavaDoc snapshotDirectoryName) throws ClassNotFoundException JavaDoc, IOException JavaDoc {
51         this(newPrevalentSystem, snapshotDirectoryName, null);
52     }
53
54     /**
55      * Creates a new XStreamSnapshotManager
56      *
57      * @param newPrevalentSystem the prevalent system to snapshot.
58      * @param snapshotDirectoryName the directory name where the snapshot must be stored.
59      * @param encoding optionally specify the character encoding for XML serialization. May be null.
60      * @throws ClassNotFoundException if some class from the system cannot be found.
61      * @throws IOException if there's a problem reading the latest snapshot.
62      */

63     public XStreamSnapshotManager(Object JavaDoc newPrevalentSystem, String JavaDoc snapshotDirectoryName, String JavaDoc encoding) throws ClassNotFoundException JavaDoc, IOException JavaDoc {
64         _encoding = encoding;
65         init(newPrevalentSystem, snapshotDirectoryName);
66     }
67
68     private XStream getXStream() {
69         return (XStream) _xstreams.get();
70     }
71
72     /**
73      * @see org.prevayler.implementation.snapshot.SnapshotManager#writeSnapshot(Object, OutputStream)
74      */

75     public void writeSnapshot(Object JavaDoc prevalentSystem, OutputStream JavaDoc out) throws IOException JavaDoc {
76         OutputStreamWriter JavaDoc writer = _encoding == null ? new OutputStreamWriter JavaDoc(out) : new OutputStreamWriter JavaDoc(out, _encoding);
77         getXStream().toXML(prevalentSystem, writer);
78         writer.flush();
79     }
80
81     /**
82      * @see org.prevayler.implementation.snapshot.SnapshotManager#readSnapshot(InputStream)
83      */

84     public Object JavaDoc readSnapshot(InputStream JavaDoc in) throws IOException JavaDoc {
85         return getXStream().fromXML(_encoding == null ? new InputStreamReader JavaDoc(in) : new InputStreamReader JavaDoc(in, _encoding));
86     }
87
88     /**
89      * @see org.prevayler.implementation.snapshot.SnapshotManager#suffix()
90      */

91     protected String JavaDoc suffix() {
92         return "xstreamsnapshot";
93     }
94
95     /**
96      * Create a new XStream instance. This must be a new instance because XStream instances are not threadsafe.
97      */

98     protected XStream createXStream() {
99         return new XStream();
100     }
101
102 }
Popular Tags