Mercurial > hg > blitz_stable
comparison src/org/prevayler/implementation/CommandInputStream.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 |
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 java.io.*; | |
40 import org.prevayler.*; | |
41 | |
42 /** Provides an easy API for reading commands and snapshots. | |
43 */ | |
44 class CommandInputStream { | |
45 | |
46 public CommandInputStream(String directory) throws IOException { | |
47 fileFinder = new NumberFileFinder(directory); | |
48 } | |
49 | |
50 public PrevalentSystem readLastSnapshot() throws IOException, ClassNotFoundException { | |
51 File snapshotFile = fileFinder.lastSnapshot(); | |
52 if (snapshotFile == null) return null; | |
53 out(snapshotFile); | |
54 | |
55 ObjectInputStream ois = new ObjectInputStream(new FileInputStream(snapshotFile)); | |
56 try { | |
57 return (PrevalentSystem)ois.readObject(); | |
58 } finally { | |
59 ois.close(); | |
60 } | |
61 } | |
62 | |
63 public Command readCommand() throws IOException, ClassNotFoundException { | |
64 if (currentLogStream == null) currentLogStream = newLogStream(); //Throws EOFException if there are no more log streams. | |
65 | |
66 try { | |
67 return (Command)currentLogStream.readObject(); | |
68 } catch (EOFException eof) { | |
69 //No more commands in this file. | |
70 } catch (ObjectStreamException osx) { | |
71 logStreamExceptionMessage(osx); | |
72 } catch (RuntimeException rx) { | |
73 logStreamExceptionMessage(rx); //Some stream corruptions cause runtime exceptions! | |
74 } | |
75 | |
76 currentLogStream.close(); | |
77 currentLogStream = null; | |
78 return readCommand(); | |
79 } | |
80 | |
81 public CommandOutputStream commandOutputStream(boolean shouldReset, | |
82 boolean shouldClean, | |
83 int aBufferSize) { | |
84 return new CommandOutputStream(fileFinder.fileCreator(), shouldReset, | |
85 shouldClean, aBufferSize); | |
86 } | |
87 | |
88 private ObjectInputStream newLogStream() throws IOException { | |
89 File logFile = fileFinder.nextPendingLog(); //Throws EOFException if there are no more pending log files. | |
90 out(logFile); | |
91 return new ObjectInputStream(new BufferedInputStream(new FileInputStream(logFile))); | |
92 } | |
93 | |
94 private void logStreamExceptionMessage(Exception exception) { | |
95 out(" " + exception); | |
96 out(" Some commands might have been lost. Looking for the next file..." ); | |
97 } | |
98 | |
99 private static void out(File file) { | |
100 out("Reading: " + file + "..."); | |
101 } | |
102 | |
103 private static void out(Object obj) { | |
104 System.out.println(obj); | |
105 } | |
106 | |
107 private NumberFileFinder fileFinder; | |
108 private ObjectInputStream currentLogStream; | |
109 } |