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.Command;
08: import org.prevayler.PrevalentSystem;
09:
10: import java.io.*;
11:
12: /** Provides an easy API for reading commands and snapshots.
13: */
14: class CommandInputStream {
15:
16: public CommandInputStream(String directory) throws IOException {
17: fileFinder = new NumberFileFinder(directory);
18: out("Recovering system state...");
19: }
20:
21: public PrevalentSystem readLastSnapshot() throws IOException,
22: ClassNotFoundException {
23: File snapshotFile = fileFinder.lastSnapshot();
24: if (snapshotFile == null)
25: return null;
26: out(snapshotFile);
27:
28: ObjectInputStream ois = new ObjectInputStream(
29: new FileInputStream(snapshotFile));
30: try {
31: return (PrevalentSystem) ois.readObject();
32: } finally {
33: ois.close();
34: }
35: }
36:
37: public Command readCommand() throws IOException,
38: ClassNotFoundException {
39: if (currentLogStream == null)
40: currentLogStream = newLogStream(); //Throws EOFException if there are no more log streams.
41:
42: try {
43: return (Command) currentLogStream.readObject();
44: } catch (EOFException eof) {
45: //No more commands in this file.
46: } catch (ObjectStreamException osx) {
47: logStreamExceptionMessage(osx);
48: } catch (RuntimeException rx) {
49: logStreamExceptionMessage(rx); //Some stream corruptions cause runtime exceptions!
50: }
51:
52: currentLogStream.close();
53: currentLogStream = null;
54: return readCommand();
55: }
56:
57: public CommandOutputStream commandOutputStream() {
58: return new CommandOutputStream(fileFinder.fileCreator());
59: }
60:
61: private ObjectInputStream newLogStream() throws IOException {
62: File logFile = fileFinder.nextPendingLog(); //Throws EOFException if there are no more pending log files.
63: out(logFile);
64: return new ObjectInputStream(new FileInputStream(logFile));
65: }
66:
67: private void logStreamExceptionMessage(Exception exception) {
68: out(" " + exception);
69: out(" Some commands might have been lost. Looking for the next file...");
70: }
71:
72: private static void out(File file) {
73: out("Reading " + file + "...");
74: }
75:
76: private static void out(Object obj) {
77: System.out.println(obj);
78: }
79:
80: private NumberFileFinder fileFinder;
81: private ObjectInputStream currentLogStream;
82: }
|