001: // SimpleCacheSerializer.java
002: // $Id: SimpleCacheSerializer.java,v 1.7 2000/08/16 21:38:04 ylafon Exp $
003: // (c) COPYRIGHT MIT, INRIA and Keio, 1999.
004: // Please first read the full copyright statement in file COPYRIGHT.html
005: package org.w3c.www.protocol.http.cache;
006:
007: import java.io.BufferedWriter;
008: import java.io.FileWriter;
009: import java.io.IOException;
010: import java.io.Reader;
011: import java.io.Writer;
012:
013: import java.util.Enumeration;
014: import java.util.Vector;
015:
016: import org.w3c.tools.resources.AttributeHolder;
017: import org.w3c.tools.resources.serialization.SerializationException;
018: import org.w3c.tools.resources.serialization.xml.XMLSerializer;
019:
020: import org.w3c.util.LookupTable;
021:
022: /**
023: * @version $Revision: 1.7 $
024: * @author Benoît Mahé (bmahe@w3.org)
025: */
026: public class SimpleCacheSerializer extends CacheSerializer {
027:
028: protected XMLSerializer serializer = null;
029:
030: /**
031: * Save a Generation, using a specified writer
032: * @param generation, a CacheGeneration, the generation to be saved
033: * @param writer, a Writer, the writer used to serialize this generation
034: */
035: public void writeGeneration(CacheGeneration generation,
036: Writer writer) throws IOException {
037: synchronized (generation) {
038: _writeGeneration(generation, writer);
039: }
040: }
041:
042: /**
043: * No synchronization on generation
044: */
045: private void _writeGeneration(CacheGeneration generation,
046: Writer writer) throws IOException {
047: Enumeration cgs = generation.getCachedResources();
048: Vector v = new Vector(10);
049: while (cgs.hasMoreElements()) {
050: v.addElement(cgs.nextElement());
051: }
052: CachedResource crs[] = new CachedResource[v.size()];
053: v.copyInto(crs);
054: try {
055: serializer.writeResources(crs, writer);
056: } catch (SerializationException ex) {
057: String msg = "XML Serialization, write failed : "
058: + ex.getMessage();
059: throw new IOException(msg);
060: }
061: }
062:
063: /**
064: * Save the list of generations (except the 'description' Generation)
065: * @param store, the Store to be dumped
066: * @param writer a Writer, used to dump
067: */
068: public void writeGenerationList(CacheStore store)
069: throws IOException {
070: synchronized (store) {
071: CacheGeneration cg = store.getMRUGeneration();
072: while ((cg != null) && (!cg.isSaved())) {
073: Writer writer = new BufferedWriter(new FileWriter(cg
074: .getGenerationFile()));
075: _writeGeneration(cg, writer);
076: cg = store.getNextGeneration(cg);
077: }
078: }
079: }
080:
081: /**
082: * Read a Generation, using a specified reader
083: * @param generation, a CacheGeneration, the generation to be loaded
084: * (or updated)
085: * @param reader, the Reader used to read this generation
086: */
087: public CacheGeneration readGeneration(CacheGeneration generation,
088: Reader reader) throws IOException {
089: synchronized (generation) {
090: generation.clean();
091: AttributeHolder ah[] = null;
092: try {
093: ah = serializer.readAttributeHolders(reader);
094: } catch (SerializationException ex) {
095: String msg = "XML Serialization, load failed : "
096: + ex.getMessage();
097: throw new IOException(msg);
098: }
099: for (int i = 0; i < ah.length; i++) {
100: generation.loadCachedResource((CachedResource) ah[i]);
101: }
102: generation.setLoaded(true);
103: generation.setSaved(true);
104: return generation;
105: }
106: }
107:
108: /**
109: * Read a Generation containing only CachedResourceDescription,
110: * using a specified reader.
111: * @param generation, a CacheGeneration, the generation to be 'loaded'
112: * @param reader, the Reader used to read this generation
113: */
114: public CacheGeneration readDescription(CacheGeneration generation,
115: Reader reader) throws IOException {
116: LookupTable tables[] = null;
117: try {
118: tables = serializer.readAttributes(reader,
119: CachedResource.ATTR_DESCR);
120: } catch (SerializationException ex) {
121: String msg = "XML Serialization, load failed : "
122: + ex.getMessage();
123: throw new IOException(msg);
124: }
125: synchronized (generation) {
126: generation.setDescription(tables);
127: }
128: return generation;
129: }
130:
131: /**
132: * Save an Attribute Holder
133: * @param holder, the attribute holder
134: * @param writer, a Writer, the writer used to serialize this generation
135: */
136: public void write(AttributeHolder holder, Writer writer)
137: throws IOException {
138: AttributeHolder holders[] = { holder };
139: try {
140: serializer.writeResources(holders, writer);
141: } catch (SerializationException ex) {
142: String msg = "XML Serialization, save failed : "
143: + ex.getMessage();
144: throw new IOException(msg);
145: }
146: }
147:
148: /**
149: * Read an Attribute Holder
150: * @param holder, the attribute holder
151: * @param reader, a Reader, the reader used to read the holder
152: */
153: public AttributeHolder read(Reader reader) throws IOException {
154: try {
155: AttributeHolder holders[] = serializer
156: .readAttributeHolders(reader);
157: if ((holders != null) && (holders.length > 0)) {
158: return holders[0];
159: } else {
160: throw new IOException("empty!");
161: }
162: } catch (SerializationException ex) {
163: String msg = "XML Serialization, load failed : "
164: + ex.getMessage();
165: throw new IOException(msg);
166: }
167: }
168:
169: public SimpleCacheSerializer() {
170: this .serializer = new XMLSerializer();
171: }
172: }
|