comparison src/org/prevayler/implementation/CommandOutputStream.java @ 0:3dc0c5604566

Initial checkin of blitz 2.0 fcs - no installer yet.
author Dan Creswell <dan.creswell@gmail.com>
date Sat, 21 Mar 2009 11:00:06 +0000
parents
children a77f0a9ed93c
comparison
equal deleted inserted replaced
-1:000000000000 0:3dc0c5604566
1 /*
2 The copyright of all source code included in this Prevayler distribution is
3 held by Klaus Wuestefeld, except the files that specifically state otherwise.
4 All rights are reserved. "PREVAYLER" is a trademark of Klaus Wuestefeld.
5
6
7 BSD License:
8
9 Redistribution and use in source and binary forms, with or without
10 modification, are permitted provided that the following conditions are met:
11
12 - Redistributions of source code must retain the above copyright notice, this
13 list of conditions and the following disclaimer.
14
15 - Redistributions in binary form must reproduce the above copyright notice,
16 this list of conditions and the following disclaimer in the documentation
17 and/or other materials provided with the distribution.
18
19 - Neither the name of Prevayler nor the names of its contributors may be used
20 to endorse or promote products derived from this software without specific
21 prior written permission.
22
23
24 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
25 AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26 IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27 ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
28 LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
29 CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
30 SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
31 INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
32 CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
33 ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
34 POSSIBILITY OF SUCH DAMAGE.
35 */
36
37 package org.prevayler.implementation;
38
39 import org.prevayler.*;
40 import java.io.*;
41
42 /** Provides a simple API for writing commands and snapshots.
43 */
44 class CommandOutputStream {
45
46 /** This number determines the size of the log files produces by the system.
47 */
48 public static final long LOG_FILE_SIZE = 100L * 1024L * 1024L;
49 private final NumberFileCreator fileCreator;
50 private ObjectOutputStream logStream;
51 private DelegatingByteCountStream2 fileStream;
52 private boolean doReset;
53 private boolean shouldClean;
54
55 private int theBufferSize = 0;
56
57 public CommandOutputStream(NumberFileCreator fileCreator,
58 boolean shouldReset, boolean shouldClean) {
59 this(fileCreator, shouldReset, shouldClean, 0);
60 }
61
62 public CommandOutputStream(NumberFileCreator fileCreator,
63 boolean shouldReset, boolean doClean,
64 int bufferSize) {
65 this.fileCreator = fileCreator;
66 doReset = shouldReset;
67 theBufferSize = bufferSize;
68 shouldClean = doClean;
69 }
70
71 public void writeCommand(Command command) throws IOException{
72 ObjectOutputStream oos = logStream();
73 try {
74 // long myStart = System.currentTimeMillis();
75
76 oos.writeObject(command);
77
78 if (doReset)
79 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.
80 oos.flush();
81
82 // long myEnd = System.currentTimeMillis();
83
84 // System.err.println("Log command time: " + (myEnd - myStart));
85 } catch (IOException iox) {
86 closeLogStream();
87 throw iox;
88 }
89 }
90
91 public void writeCommand(Command command, boolean doSync)
92 throws IOException{
93
94 ObjectOutputStream oos = logStream();
95 try {
96 // long myStart = System.currentTimeMillis();
97
98 oos.writeObject(command);
99
100 if (doReset)
101 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.
102
103 if (doSync)
104 oos.flush();
105
106 // long myEnd = System.currentTimeMillis();
107
108 // System.err.println("Log command time: " + (myEnd - myStart));
109
110 } catch (IOException iox) {
111 closeLogStream();
112 throw iox;
113 }
114 }
115
116 public synchronized Snapshotter writeSnapshot(PrevalentSystem system)
117 throws IOException{
118
119 closeLogStream(); //After every snapshot, a new commandLog file must be started.
120
121 SnapshotterImpl mySnapper = new SnapshotterImpl(fileCreator,
122 shouldClean);
123 mySnapper.cacheSnapshot(system);
124
125 return mySnapper;
126 }
127
128 private ObjectOutputStream logStream() throws IOException{
129 if(logStream == null) {
130 fileStream =
131 new DelegatingByteCountStream2(fileCreator.newLog(),
132 theBufferSize);
133 logStream = new ObjectOutputStream(fileStream);
134 }
135
136 if(fileStream.bytesWritten() >= LOG_FILE_SIZE) {
137 closeLogStream();
138 return logStream(); //Recursive call.
139 }
140
141 return logStream;
142 }
143
144 private void closeLogStream() throws IOException {
145 if (logStream == null) return;
146
147 logStream.close();
148 logStream = null;
149 }
150
151 }