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 java.io.*;
08: import java.text.*;
09:
10: /** Creates .log and .snapshot files using a number sequence for the file name.
11: */
12: class NumberFileCreator {
13:
14: public static final String SNAPSHOT_SUFFIX = "snapshot";
15: public static final DecimalFormat SNAPSHOT_FORMAT = new DecimalFormat(
16: "000000000000000000000'.'" + SNAPSHOT_SUFFIX); //21 zeros (enough for a long number).
17: public static final DecimalFormat LOG_FORMAT = new DecimalFormat(
18: "000000000000000000000'.'commandLog"); //21 zeros (enough for a long number).
19:
20: private File directory;
21: private long nextFileNumber;
22:
23: public NumberFileCreator(File directory, long firstFileNumber) {
24: this .directory = directory;
25: this .nextFileNumber = firstFileNumber;
26: }
27:
28: File newLog() throws IOException {
29: File log = new File(directory, LOG_FORMAT
30: .format(nextFileNumber));
31: if (!log.createNewFile())
32: throw new IOException(
33: "Attempt to create command log file that already existed: "
34: + log);
35: ;
36:
37: ++nextFileNumber;
38: return log;
39: }
40:
41: File newSnapshot() throws IOException {
42: File snapshot = new File(directory, SNAPSHOT_FORMAT
43: .format(nextFileNumber - 1));
44: snapshot.delete(); //If no commands are logged, the same snapshot file will be created over and over.
45: return snapshot;
46: }
47:
48: File newTempSnapshot() throws IOException {
49: return File.createTempFile("temp", "generatingSnapshot",
50: directory);
51: }
52:
53: }
|