01: // Prevayler(TM) - The Open-Source Prevalence Layer.
02: // Copyright (C) 2001 Klaus Wuestefeld.
03: // This library is free software; you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License version 2.1 as published by the Free Software Foundation. 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. See the GNU Lesser General Public License for more details. You should have received a copy of the GNU Lesser General Public License along with this library; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
04:
05: package org.prevayler.implementation;
06:
07: import org.prevayler.*;
08: import java.io.*;
09:
10: /** Provides a simple API for writing commands and snapshots.
11: */
12: class CommandOutputStream {
13:
14: /** This number determines the size of the log files produces by the system.
15: */
16: public static final long LOG_FILE_SIZE = 100000L;
17: private final NumberFileCreator fileCreator;
18: private ObjectOutputStream logStream;
19: private ByteCountStream fileStream;
20:
21: public CommandOutputStream(NumberFileCreator fileCreator) {
22: this .fileCreator = fileCreator;
23: }
24:
25: public void writeCommand(Command command) throws IOException {
26: ObjectOutputStream oos = logStream();
27: try {
28: oos.writeObject(command);
29: oos.reset(); //You can comment this line if your free RAM is large compared to the size of each commandLog file. If you comment this line, commands will occupy much less space in the log file because their class descriptions will only be written once. Your application will therefore produce much fewer log files. If you comment this line, you must make sure that no command INSTANCE is used more than once in your application with different internal values. "Reset will disregard the state of any objects already written to the stream. The state is reset to be the same as a new ObjectOutputStream. ... Objects previously written to the stream will not be refered to as already being in the stream. They will be written to the stream again." - JDK1.2.2 API documentation.
30: oos.flush();
31: } catch (IOException iox) {
32: closeLogStream();
33: throw iox;
34: }
35: }
36:
37: public synchronized void writeSnapshot(PrevalentSystem system)
38: throws IOException {
39: closeLogStream(); //After every snapshot, a new commandLog file must be started.
40:
41: File tempSnapshot = fileCreator.newTempSnapshot();
42:
43: ObjectOutputStream stream = new ObjectOutputStream(
44: new FileOutputStream(tempSnapshot));
45: stream.writeObject(system);
46: stream.close();
47:
48: File snapshot = fileCreator.newSnapshot();
49: if (!tempSnapshot.renameTo(snapshot))
50: throw new IOException("Unable to rename " + tempSnapshot
51: + " to " + snapshot);
52: }
53:
54: private ObjectOutputStream logStream() throws IOException {
55: if (logStream == null) {
56: fileStream = new ByteCountStream(fileCreator.newLog());
57: logStream = new ObjectOutputStream(fileStream);
58: }
59:
60: if (fileStream.bytesWritten() >= LOG_FILE_SIZE) {
61: closeLogStream();
62: return logStream(); //Recursive call.
63: }
64:
65: return logStream;
66: }
67:
68: private void closeLogStream() throws IOException {
69: if (logStream == null)
70: return;
71:
72: logStream.close();
73: logStream = null;
74: }
75:
76: }
|