1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25 package org.archive.io;
26
27 import java.io.File;
28 import java.io.FileNotFoundException;
29 import java.io.IOException;
30 import java.util.LinkedList;
31 import java.util.List;
32 import java.util.logging.FileHandler;
33
34
35 /***
36 * FileHandler with support for rotating the current file to
37 * an archival name with a specified integer suffix, and
38 * provision of a new replacement FileHandler with the current
39 * filename.
40 *
41 * @author gojomo
42 */
43 public class GenerationFileHandler extends FileHandler {
44 private LinkedList<String> filenameSeries = new LinkedList<String>();
45 private boolean shouldManifest = false;
46
47 /***
48 * @return Returns the filenameSeries.
49 */
50 public List getFilenameSeries() {
51 return filenameSeries;
52 }
53
54 /***
55 * Constructor.
56 * @param pattern
57 * @param append
58 * @param shouldManifest
59 * @throws IOException
60 * @throws SecurityException
61 */
62 public GenerationFileHandler(String pattern, boolean append,
63 boolean shouldManifest)
64 throws IOException, SecurityException {
65 super(pattern, append);
66 filenameSeries.addFirst(pattern);
67 this.shouldManifest = shouldManifest;
68 }
69
70 /***
71 * @param filenameSeries
72 * @param shouldManifest
73 * @throws IOException
74 */
75 public GenerationFileHandler(LinkedList<String> filenameSeries,
76 boolean shouldManifest)
77 throws IOException {
78 super((String)filenameSeries.getFirst(), false);
79 this.filenameSeries = filenameSeries;
80 this.shouldManifest = shouldManifest;
81 }
82
83 /***
84 * Move the current file to a new filename with the storeSuffix in place
85 * of the activeSuffix; continuing logging to a new file under the
86 * original filename.
87 *
88 * @param storeSuffix Suffix to put in place of <code>activeSuffix</code>
89 * @param activeSuffix Suffix to replace with <code>storeSuffix</code>.
90 * @return GenerationFileHandler instance.
91 * @throws IOException
92 */
93 public GenerationFileHandler rotate(String storeSuffix,
94 String activeSuffix)
95 throws IOException {
96 close();
97 String filename = (String)filenameSeries.getFirst();
98 if (!filename.endsWith(activeSuffix)) {
99 throw new FileNotFoundException("Active file does not have" +
100 " expected suffix");
101 }
102 String storeFilename = filename.substring(0,
103 filename.length() - activeSuffix.length()) +
104 storeSuffix;
105 File activeFile = new File(filename);
106 File storeFile = new File(storeFilename);
107 if (!activeFile.renameTo(storeFile)) {
108 throw new IOException("Unable to move " + filename + " to " +
109 storeFilename);
110 }
111 filenameSeries.add(1, storeFilename);
112 GenerationFileHandler newGfh =
113 new GenerationFileHandler(filenameSeries, shouldManifest);
114 newGfh.setFormatter(this.getFormatter());
115 return newGfh;
116 }
117
118 /***
119 * @return True if should manifest.
120 */
121 public boolean shouldManifest() {
122 return this.shouldManifest;
123 }
124 }