1 |
|
|
2 |
|
|
3 |
|
|
4 |
|
|
5 |
|
|
6 |
|
|
7 |
|
|
8 |
|
|
9 |
|
|
10 |
|
|
11 |
|
|
12 |
|
|
13 |
|
|
14 |
|
|
15 |
|
|
16 |
|
package mnemosyne.archiver; |
17 |
|
|
18 |
|
import mnemosyne.util.Util; |
19 |
|
import mnemosyne.util.PersistenceRuntimeException; |
20 |
|
import org.apache.commons.logging.Log; |
21 |
|
import org.apache.commons.logging.LogFactory; |
22 |
|
|
23 |
|
import java.io.*; |
24 |
|
import java.text.DecimalFormat; |
25 |
|
import java.util.SortedSet; |
26 |
|
import java.util.TreeSet; |
27 |
|
|
28 |
|
|
29 |
|
|
30 |
|
|
31 |
|
public abstract class ArchiveDirectory |
32 |
|
{ |
33 |
8 |
private static final Log log = LogFactory.getLog(ArchiveDirectory.class); |
34 |
|
|
35 |
|
private static final String IN_PROGRESS_SUFFIX = "_"; |
36 |
|
private static final String SEQUENCE_FORMAT = "0000000000000000000"; |
37 |
4 |
private static final DecimalFormat sequenceFormatter = new DecimalFormat(SEQUENCE_FORMAT); |
38 |
|
|
39 |
|
private File directory; |
40 |
|
protected SortedSet archiveFiles; |
41 |
|
private ArchiveSequence sequence; |
42 |
|
|
43 |
|
protected ArchiveDirectory(String directoryName, ArchiveSequence sequence) |
44 |
30 |
{ |
45 |
30 |
if (!Util.ensureDirectoryExists(directoryName)) |
46 |
|
{ |
47 |
0 |
throw new PersistenceRuntimeException("Unable to create archive directory"); |
48 |
|
} |
49 |
30 |
this.directory = new File(directoryName); |
50 |
30 |
this.sequence = sequence; |
51 |
30 |
scanDirectory(); |
52 |
30 |
} |
53 |
|
|
54 |
|
protected abstract String prefix(); |
55 |
|
protected abstract String postfix(); |
56 |
|
protected abstract String extension(); |
57 |
|
|
58 |
|
protected ObjectOutputStream createOutputStream(OutputStream output) throws IOException |
59 |
|
{ |
60 |
12 |
return new ArchiveOutputStream(output); |
61 |
|
} |
62 |
|
|
63 |
|
protected ObjectInputStream createInputStream(InputStream input) throws IOException |
64 |
|
{ |
65 |
12 |
return new ArchiveInputStream(input); |
66 |
|
} |
67 |
|
|
68 |
|
private void scanDirectory() |
69 |
|
{ |
70 |
30 |
log.debug("Scanning archive directory " + directory.getPath()); |
71 |
30 |
File[] files = directory.listFiles(new FilenameFilter() { |
72 |
|
public boolean accept(File dir, String name) |
73 |
|
{ |
74 |
|
return isValidArchiveFile(name); |
75 |
|
} |
76 |
|
}); |
77 |
|
|
78 |
30 |
initializeFileCollection(files); |
79 |
30 |
} |
80 |
|
|
81 |
|
protected boolean isValidArchiveFile(String filename) |
82 |
|
{ |
83 |
12 |
boolean isValid = |
84 |
|
filename.startsWith(prefix()) && |
85 |
|
filename.endsWith(postfix() + "." + extension()) && |
86 |
|
filename.length() == prefix().length() + SEQUENCE_FORMAT.length() + postfix().length() + ".".length() + extension().length(); |
87 |
|
|
88 |
12 |
return isValid; |
89 |
|
} |
90 |
|
|
91 |
|
private void initializeFileCollection(File[] files) |
92 |
|
{ |
93 |
30 |
archiveFiles = new TreeSet(); |
94 |
30 |
long maxSequenceNumber = -1; |
95 |
36 |
for (int i = 0; i < files.length; i++) |
96 |
|
{ |
97 |
6 |
File file = files[i]; |
98 |
|
try |
99 |
|
{ |
100 |
6 |
long sequenceNumber = extractFileSequenceNumber(file.getName()); |
101 |
5 |
log.debug("Found archive file " + file.getName()); |
102 |
5 |
if (sequenceNumber > maxSequenceNumber) |
103 |
|
{ |
104 |
5 |
maxSequenceNumber = sequenceNumber; |
105 |
|
} |
106 |
5 |
archiveFiles.add(files[i].getName()); |
107 |
5 |
} |
108 |
|
catch (NumberFormatException e) |
109 |
|
{ |
110 |
1 |
log.debug("Invalid archive file found in archive directory: " + file.getName()); |
111 |
1 |
} |
112 |
|
} |
113 |
|
|
114 |
30 |
sequence.advanceValue(maxSequenceNumber + 1); |
115 |
30 |
} |
116 |
|
|
117 |
|
protected boolean isEmpty() |
118 |
|
{ |
119 |
3 |
return archiveFiles.isEmpty(); |
120 |
|
} |
121 |
|
|
122 |
|
protected long extractFileSequenceNumber(String filename) throws NumberFormatException |
123 |
|
{ |
124 |
13 |
int startSequenceIndex = prefix().length(); |
125 |
13 |
int finishSequenceIndex = filename.indexOf(".") - postfix().length(); |
126 |
13 |
String sequenceNumber = filename.substring(startSequenceIndex, finishSequenceIndex); |
127 |
13 |
long retval = Long.parseLong(sequenceNumber); |
128 |
11 |
log.debug("Extracted sequence " + retval + " from filename " + filename); |
129 |
11 |
return retval; |
130 |
|
} |
131 |
|
|
132 |
|
protected File allocateNewInProgressArchiveFile() |
133 |
|
{ |
134 |
17 |
return new File(directory, buildArchiveFilename(sequence.getNext()) + IN_PROGRESS_SUFFIX); |
135 |
|
} |
136 |
|
|
137 |
|
protected File constructArchiveFile(String archiveFilename) |
138 |
|
{ |
139 |
16 |
return new File(directory, archiveFilename); |
140 |
|
} |
141 |
|
|
142 |
|
protected String buildArchiveFilename(long sequenceNumber) |
143 |
|
{ |
144 |
27 |
return prefix() + sequenceFormatter.format(sequenceNumber) + postfix() + "." + extension(); |
145 |
|
} |
146 |
|
|
147 |
|
protected Object readArchiveFile(File archiveFile) throws ArchiverException |
148 |
|
{ |
149 |
15 |
if (log.isDebugEnabled()) |
150 |
0 |
log.debug("Loading file " + archiveFile.getPath()); |
151 |
|
|
152 |
15 |
FileInputStream fileInput = null; |
153 |
15 |
ObjectInputStream input = null; |
154 |
15 |
Object retval = null; |
155 |
|
try |
156 |
|
{ |
157 |
15 |
fileInput = new FileInputStream(archiveFile); |
158 |
14 |
input = createInputStream(new BufferedInputStream(fileInput)); |
159 |
13 |
retval = input.readObject(); |
160 |
|
} |
161 |
|
catch (ClassNotFoundException e) |
162 |
|
{ |
163 |
0 |
throw new ArchiverException("Exception occurred while trying to load archive " + archiveFile.getPath(), e); |
164 |
|
} |
165 |
|
catch (IOException e) |
166 |
|
{ |
167 |
2 |
throw new ArchiverException("Exception occurred while trying to load archive" + archiveFile.getPath(), e); |
168 |
|
} |
169 |
|
finally |
170 |
|
{ |
171 |
2 |
try |
172 |
|
{ |
173 |
15 |
if (input != null) |
174 |
|
{ |
175 |
13 |
input.close(); |
176 |
|
} |
177 |
2 |
else if (fileInput != null) |
178 |
|
{ |
179 |
1 |
fileInput.close(); |
180 |
|
} |
181 |
15 |
} |
182 |
|
catch (IOException e) |
183 |
|
{ |
184 |
0 |
log.warn("Unable to close archive file " + archiveFile.getPath()); |
185 |
0 |
} |
186 |
|
} |
187 |
13 |
return retval; |
188 |
|
} |
189 |
|
|
190 |
|
public void writeArchiveFile(Object obj) throws ArchiverException |
191 |
|
{ |
192 |
15 |
File outputFile = allocateNewInProgressArchiveFile(); |
193 |
15 |
if (log.isDebugEnabled()) |
194 |
0 |
log.debug("Writing archive to " + outputFile.getPath()); |
195 |
|
|
196 |
15 |
boolean removeOutputFile = false; |
197 |
15 |
FileOutputStream fileOutput = null; |
198 |
15 |
ObjectOutputStream output = null; |
199 |
|
try |
200 |
|
{ |
201 |
15 |
fileOutput = new FileOutputStream(outputFile); |
202 |
15 |
output = createOutputStream(new BufferedOutputStream(fileOutput)); |
203 |
14 |
output.writeObject(obj); |
204 |
|
} |
205 |
|
catch (IOException e) |
206 |
|
{ |
207 |
1 |
removeOutputFile = true; |
208 |
1 |
throw new ArchiverException("Exception occurred while trying to save archive", e); |
209 |
|
} |
210 |
|
finally |
211 |
|
{ |
212 |
1 |
try |
213 |
|
{ |
214 |
15 |
if (output != null) |
215 |
|
{ |
216 |
14 |
output.flush(); |
217 |
14 |
output.close(); |
218 |
|
} |
219 |
1 |
else if (fileOutput != null) |
220 |
|
{ |
221 |
1 |
fileOutput.flush(); |
222 |
1 |
fileOutput.close(); |
223 |
|
} |
224 |
15 |
} |
225 |
|
catch (IOException e) |
226 |
|
{ |
227 |
0 |
log.warn("Unable to close archive file " + outputFile.getPath(), e); |
228 |
0 |
} |
229 |
|
|
230 |
15 |
if (removeOutputFile && outputFile.exists()) |
231 |
|
{ |
232 |
1 |
outputFile.delete(); |
233 |
|
} |
234 |
|
} |
235 |
|
|
236 |
14 |
onArchiveComplete(outputFile); |
237 |
14 |
} |
238 |
|
|
239 |
|
protected void onArchiveComplete(File inProgressFile) throws ArchiverException |
240 |
|
{ |
241 |
16 |
String inProgressPath = inProgressFile.getPath(); |
242 |
16 |
String completeFileName = inProgressPath.substring(0, inProgressPath.length() - IN_PROGRESS_SUFFIX.length()); |
243 |
16 |
File completeFile = new File(completeFileName); |
244 |
16 |
if (log.isDebugEnabled()) |
245 |
0 |
log.debug("Renaming in progress archive file from " + inProgressPath + " to " + completeFile.getPath()); |
246 |
16 |
if (!inProgressFile.renameTo(completeFile)) |
247 |
|
{ |
248 |
1 |
throw new ArchiverException("Unable to rename in progress archive file"); |
249 |
|
} |
250 |
|
|
251 |
15 |
archiveFiles.add(completeFile.getName()); |
252 |
15 |
} |
253 |
|
|
254 |
|
} |