Mercurial > hg > blitz_stable
comparison src/org/prevayler/implementation/NumberFileCreator.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 java.text.*; | |
41 | |
42 /** Creates .log and .snapshot files using a number sequence for the file name. | |
43 */ | |
44 class NumberFileCreator { | |
45 | |
46 public static final String SNAPSHOT_SUFFIX = "snapshot"; | |
47 public static final String LOGFILE_SUFFIX = "commandLog"; | |
48 public static final DecimalFormat SNAPSHOT_FORMAT = new DecimalFormat("000000000000000000000'.'" + SNAPSHOT_SUFFIX); //21 zeros (enough for a long number). | |
49 public static final DecimalFormat LOG_FORMAT = new DecimalFormat("000000000000000000000'.'" + LOGFILE_SUFFIX); //21 zeros (enough for a long number). | |
50 | |
51 private File directory; | |
52 private long nextFileNumber; | |
53 | |
54 public NumberFileCreator(File directory, long firstFileNumber) { | |
55 this.directory = directory; | |
56 this.nextFileNumber = firstFileNumber; | |
57 } | |
58 | |
59 File newLog() throws IOException { | |
60 File log = new File(directory, LOG_FORMAT.format(nextFileNumber)); | |
61 if(!log.createNewFile()) throw new IOException("Attempt to create command log file that already existed: " + log);; | |
62 | |
63 ++nextFileNumber; | |
64 return log; | |
65 } | |
66 | |
67 File newSnapshot() throws IOException { | |
68 File snapshot = new File(directory, SNAPSHOT_FORMAT.format(nextFileNumber - 1)); | |
69 snapshot.delete(); //If no commands are logged, the same snapshot file will be created over and over. | |
70 return snapshot; | |
71 } | |
72 | |
73 File newTempSnapshot() throws IOException { | |
74 return File.createTempFile("temp","generatingSnapshot",directory); | |
75 } | |
76 | |
77 NumberFileCleaner newCleaner() { | |
78 return new NumberFileCleaner(directory); | |
79 } | |
80 } |