Mercurial > hg > blitz_condensed
comparison src/org/prevayler/implementation/NumberFileCleaner.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 package org.prevayler.implementation; | |
2 | |
3 import org.dancres.blitz.txn.TxnManager; | |
4 | |
5 import java.io.File; | |
6 import java.io.FileFilter; | |
7 | |
8 import java.util.Arrays; | |
9 import java.util.logging.Level; | |
10 | |
11 /** | |
12 <p>Cleans up old log files and snapshots. Helper class produced from a | |
13 NumberFileCreator which can be used by a programmer to clean up no longer | |
14 required log files and snapshots. Not all deployments will want to use this | |
15 as some will wish to archive all such logs and snapshots for recovery | |
16 purposes or to satisfy certain legal requirements.</p> | |
17 */ | |
18 class NumberFileCleaner { | |
19 File theDirectory; | |
20 | |
21 NumberFileCleaner(File aDirectory) { | |
22 theDirectory = aDirectory; | |
23 } | |
24 | |
25 void clean() { | |
26 File[] myFiles = theDirectory.listFiles(new SnapshotFilter()); | |
27 | |
28 Arrays.sort(myFiles); | |
29 | |
30 /* | |
31 for (int i = (myFiles.length -1); i >-1; i--) { | |
32 TxnManager.theLogger.log(Level.SEVERE, "Ordered: " + myFiles[i]); | |
33 } | |
34 */ | |
35 | |
36 int myLastSnapshot = findLastSnapshot(myFiles); | |
37 | |
38 if (myLastSnapshot != -1) { | |
39 for (int i = (myLastSnapshot - 1); i > -1; i--) { | |
40 // TxnManager.theLogger.log(Level.SEVERE, "Delete: " + myFiles[i]); | |
41 myFiles[i].delete(); | |
42 } | |
43 } | |
44 } | |
45 | |
46 private int findLastSnapshot(File[] aFiles) { | |
47 for (int i = (aFiles.length - 1); i > -1; i--) { | |
48 File myFile = aFiles[i]; | |
49 if (myFile.getName().endsWith("." + | |
50 NumberFileCreator.SNAPSHOT_SUFFIX)) { | |
51 return i; | |
52 } | |
53 } | |
54 | |
55 return -1; | |
56 } | |
57 | |
58 private static class SnapshotFilter implements FileFilter { | |
59 public boolean accept(File aFile) { | |
60 String myName = aFile.getName(); | |
61 | |
62 if ((aFile.isDirectory()) || | |
63 ((!myName.endsWith("." + | |
64 NumberFileCreator.SNAPSHOT_SUFFIX)) && | |
65 (!myName.endsWith("." + | |
66 NumberFileCreator.LOGFILE_SUFFIX)))) | |
67 return false; | |
68 | |
69 try { | |
70 Long.parseLong(myName.substring(0,myName.indexOf('.'))); | |
71 | |
72 return true; | |
73 } catch (NumberFormatException nfx) { | |
74 return false; | |
75 } | |
76 } | |
77 } | |
78 | |
79 | |
80 } |