comparison src/org/prevayler/implementation/NumberFileFinder.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.util.Arrays;
41 import java.text.*;
42
43 /** Finds the last .snapshot file by number and finds all the subsequent pending .log files.
44 */
45 class NumberFileFinder {
46
47 private File directory;
48 private File lastSnapshot;
49 private long fileNumber;
50 private NumberFileCreator fileCreator;
51
52 /** @throws IOException if location does not exist and cannot be created as a directory.
53 * @throws IllegalArgumentException If location exists and is not a directory.
54 */
55 public NumberFileFinder(String directoryName) throws IOException {
56 this.directory = new File(directoryName);
57 if (!directory.exists() && !directory.mkdirs()) throw new IOException("Directory doesn't exist and could not be created: " + directoryName);
58 if (!directory.isDirectory()) throw new IllegalArgumentException("Path exists but is not a directory: " + directoryName);
59
60 init();
61 }
62
63 /** Returns the last snapshot file. Returns null if there are no snapshots.
64 */
65 public File lastSnapshot() {
66 return lastSnapshot;
67 }
68
69 /** @throws EOFException if there are no more pending .log files.
70 */
71 public File nextPendingLog() throws EOFException {
72 File log = new File(directory, NumberFileCreator.LOG_FORMAT.format(fileNumber + 1));
73 if (!log.exists()) {
74 fileCreator = new NumberFileCreator(directory, fileNumber + 1);
75 throw new EOFException();
76 }
77 ++fileNumber;
78 return log;
79 }
80
81 /** Returns a NumberFileCreator that will start off with the first number after the last .log file number.
82 * @return null if there are still .log files pending.
83 */
84 public NumberFileCreator fileCreator() {
85 return fileCreator;
86 }
87
88 private void init() throws IOException {
89 findLastSnapshot();
90 fileNumber = lastSnapshot == null
91 ? 0
92 : number(lastSnapshot);
93 }
94
95 private long number(File snapshot) throws NumberFormatException { //NumberFomatException is a RuntimeException.
96 String name = snapshot.getName();
97 if (!name.endsWith("." + NumberFileCreator.SNAPSHOT_SUFFIX)) throw new NumberFormatException();
98 return Long.parseLong(name.substring(0,name.indexOf('.'))); // "00000.snapshot" becomes "00000".
99 //The following doesn't work! It throws ParseException (UnparseableNumber): return (NumberFileCreator.SNAPSHOT_FORMAT.parse(snapshot.getName())).longValue();
100 }
101
102 private void findLastSnapshot() throws IOException {
103 File[] snapshots = directory.listFiles(new SnapshotFilter());
104 if (snapshots == null) throw new IOException("Error reading file list from directory " + directory);
105
106 Arrays.sort(snapshots);
107
108 lastSnapshot = snapshots.length > 0
109 ? snapshots[snapshots.length - 1]
110 : null;
111 }
112
113
114 private class SnapshotFilter implements FileFilter {
115
116 public boolean accept(File file) {
117 try {
118 number(file);
119 } catch (NumberFormatException nfx) {
120 return false;
121 }
122 return true;
123 }
124 }
125
126 }