KickJava   Java API By Example, From Geeks To Geeks.

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


1 //Prevayler(TM) - The Free-Software Prevalence Layer.
2
//Copyright (C) 2001-2003 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: Alexandre Nodari
5

6 package org.prevayler.implementation.snapshot;
7 import java.io.*;
8
9 import javax.xml.transform.stream.*;
10
11 import com.skaringa.javaxml.*;
12
13 /**
14  * Writes and reads snapshots to/from XML files.
15  *
16  * <p>This implementation requires the <a HREF="http://www.skaringa.com/">Skaringa</a>
17  * Java and XML language binding framework which provides for Java object XML serialization.</p>
18  *
19  * <p>Note that Skaringa has some dependencies of its own. It requires the standard XML API's
20  * (xml-apis.jar from the <a HREF="http://xml.apache.org/xerces2-j/">Apache Xerces-j</a> project or j2sdk1.4.x),
21  * an XML Transformation engine (<a HREF="http://xml.apache.org/xalan-j/">Apache Xalan-j</a>),
22  * and a logging api (<a HREF="http://jakarta.apache.org/commons/logging.html">Apache Jakarta Commons Logging</a>),
23  * and, if desired, a logging implementation (<a HREF="http://jakarta.apache.org/log4j/">Apache Jakarta Log4j</a> or j2sdk1.4.x logging).</p>
24  *
25  * <p>One more quick note. j2sdk1.4.x comes with an old buggy version of Xalan which you should override using the
26  * endorsed package override mechanism. To do this, add your preferred version of xalan.jar to JAVA_HOME/jre/lib/endorsed.
27  * You will need to create the 'endorsed' directory if it doesn't already exist. That is the *only* way to override packages
28  * that the JDK already provides in j2sdk1.4.x and above. In JDK1.3.x, this isn't a problem, although you will need to supply
29  * everything mentioned above on the classpath.</p>
30  *
31  * @see org.prevayler.implementation.snapshot.SnapshotManager
32  */

33 public class XmlSnapshotManager extends SnapshotManager {
34
35
36     public XmlSnapshotManager(Object JavaDoc newPrevalentSystem, String JavaDoc snapshotDirectoryName) throws ClassNotFoundException JavaDoc, IOException {
37         super(newPrevalentSystem, snapshotDirectoryName);
38     }
39
40
41     /**
42      * @see org.prevayler.implementation.snapshot.SnapshotManager#readSnapshot(InputStream)
43      */

44     public Object JavaDoc readSnapshot(InputStream in) throws IOException {
45         StreamSource source = new StreamSource(in);
46         try {
47             return transformer().deserialize(source);
48         } catch (DeserializerException de) {
49             throw new IOException("Unable to deserialize with Skaringa: " + de.getMessage());
50         } finally {
51             source.getInputStream().close();
52         }
53     }
54
55
56     /**
57      * @see org.prevayler.implementation.snapshot.SnapshotManager#suffix()
58      */

59     protected String JavaDoc suffix() {
60         return "xml";
61     }
62
63
64     /**
65      * @see org.prevayler.implementation.snapshot.SnapshotManager#writeSnapshot(Object, OutputStream)
66      */

67     public void writeSnapshot(Object JavaDoc prevalentSystem, OutputStream out) throws IOException {
68         StreamResult result = new StreamResult(out);
69         try {
70             transformer().serialize(prevalentSystem, result);
71         } catch (SerializerException se) {
72             throw new IOException("Unable to serialize with Skaringa: " + se.getMessage());
73         } finally {
74             result.getOutputStream().close();
75         }
76     }
77
78
79     private ObjectTransformer transformer() throws IOException {
80         try {
81             return ObjectTransformerFactory.getInstance().getImplementation();
82 // Other options you can try:
83
// trans.setProperty(javax.xml.transform.OutputKeys.INDENT, "yes");
84
// trans.setProperty(javax.xml.transform.OutputKeys.ENCODING, "ISO-8859-1");
85
// trans.setProperty(com.skaringa.javaxml.PropertyKeys.OMIT_XSI_TYPE, "true");
86
}
87         catch (NoImplementationException nie) {
88             throw new IOException("Unable to start Skaringa: " + nie.getMessage());
89         }
90     }
91 }
92
Popular Tags