-
Notifications
You must be signed in to change notification settings - Fork 4
/
AuditPacker.java
186 lines (172 loc) · 7.64 KB
/
AuditPacker.java
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
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
/*
* #%L
* Bitrepository Audit Trail Service
* %%
* Copyright (C) 2010 - 2012 The State and University Library, The Royal Library and The State Archives, Denmark
* %%
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as
* published by the Free Software Foundation, either version 2.1 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Lesser Public License for more details.
*
* You should have received a copy of the GNU General Lesser Public
* License along with this program. If not, see
* <http://www.gnu.org/licenses/lgpl-2.1.html>.
* #L%
*/
package org.bitrepository.audittrails.preserver;
import java.io.*;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.bitrepository.audittrails.store.AuditEventIterator;
import org.bitrepository.audittrails.store.AuditTrailStore;
import org.bitrepository.bitrepositoryelements.AuditTrailEvent;
import org.bitrepository.common.utils.FileUtils;
import org.bitrepository.common.utils.SettingsUtils;
import org.bitrepository.settings.referencesettings.AuditTrailPreservation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
* Performs the extraction and packaging of audit trails for preservation for a specific collection.
* Only packs the audit trails with a larger sequence number than the reached sequence number for the given
* contributor.
*/
public class AuditPacker {
/** The log.*/
private Logger log = LoggerFactory.getLogger(getClass());
/** The audit trail store.*/
private final AuditTrailStore store;
/** The id of the collection to pack audits for.*/
private final String collectionID;
/** The list of contributors for this collection. */
private final List<String> contributors = new ArrayList<String>();
/** The directory where the temporary files are stored.*/
private final File directory;
/** Map between the contributor id and the reached preservation sequence number. */
private Map<String, Long> seqReached = new HashMap<String, Long>();
/** Whether the output stream should be appended to the file.*/
private static final boolean APPEND = true;
/**
* Constructor.
* @param store The audit trail store
* @param settings the settings
* @param collectionID the collection ID
*/
public AuditPacker(AuditTrailStore store, AuditTrailPreservation settings, String collectionID) {
this.store = store;
this.collectionID = collectionID;
this.directory = FileUtils.retrieveDirectory(settings.getAuditTrailPreservationTemporaryDirectory());
this.contributors.addAll(SettingsUtils.getAuditContributorsForCollection(collectionID));
initialiseReachedSequenceNumbers();
}
/**
* Makes a copy of the map, which is returned.
* @return A mapping between the contributor ids and their preservation sequence numbers.
*/
public Map<String, Long> getSequenceNumbersReached() {
return new HashMap<String, Long>(seqReached);
}
/**
* Retrieves the preservation sequence number for each contributor and inserts it into the map.
*/
private void initialiseReachedSequenceNumbers() {
for(String contributor : contributors) {
Long seq = store.getPreservationSequenceNumber(contributor, collectionID);
seqReached.put(contributor, seq);
}
}
/**
* Creates a new package with all the newest audit trails from all contributors, e.g. all the audit trails with a
* larger sequence number than the reached.
* Cleans up the temporary container afterwards.
* @return A compressed file with all the audit trails.
*/
public synchronized File createNewPackage() {
File container = new File(directory, collectionID + "-audit-trails-" + System.currentTimeMillis());
try {
container.createNewFile();
packContributors(container);
return createCompressedFile(container);
} catch (IOException e) {
throw new IllegalStateException("Cannot package the newest audit trails.", e);
} finally {
// cleaning up.
if(container.exists()) {
FileUtils.delete(container);
}
}
}
/**
* Packs all newest audit trails from every contributor into the given file.
* @param container The file where the audit trails should be written.
* @throws IOException If writing to the file somehow fails.
*/
private void packContributors(File container) throws IOException {
PrintWriter writer = null;
try {
writer = new PrintWriter(
new OutputStreamWriter(new FileOutputStream(container, APPEND), StandardCharsets.UTF_8));
for(String contributor : contributors) {
packContributor(contributor, writer);
}
} finally {
if(writer != null) {
writer.flush();
writer.close();
}
}
}
/**
* Writes all the newest audit trails for a single contributor to the PrintWriter.
* @param contributorId The id of the contributor to write the files for.
* @param writer The PrinterWriter where the output will be written.
*/
private void packContributor(String contributorId, PrintWriter writer) {
long nextSeqNumber = store.getPreservationSequenceNumber(contributorId, collectionID);
long largestSeqNumber = -1;
long numPackedAudits = 0;
log.debug("Starting to pack audittrails for contributor: " + contributorId + " for collection: " + collectionID);
AuditEventIterator iterator = store.getAuditTrailsByIterator(null, collectionID, contributorId, nextSeqNumber,
null, null, null, null, null, null, null);
Long timeStart = System.currentTimeMillis();
long logInterval = 1000;
AuditTrailEvent event;
log.debug("AuditEventIterator created");
while((event = iterator.getNextAuditTrailEvent()) != null) {
numPackedAudits++;
if(largestSeqNumber < event.getSequenceNumber().longValue()) {
largestSeqNumber = event.getSequenceNumber().longValue();
}
writer.println(event.toString());
if((numPackedAudits % logInterval) == 0) {
log.debug("Packed " + numPackedAudits + " audittrails in: " + (System.currentTimeMillis() - timeStart) + " ms");
}
}
log.debug("Packed a total of: " + numPackedAudits + " audittrails in: " + (System.currentTimeMillis() - timeStart) + " ms");
if(numPackedAudits > 0) {
seqReached.put(contributorId, largestSeqNumber);
}
}
/**
* Compresses the given file into a zip-file.
* It will have the same name as the file to compress, just with the extension '.zip', and it will
* be placed in the directory defined in settings.
*
* @param fileToCompress The file to compress.
* @return The compressed file.
* @throws IOException If anything goes wrong.
*/
private File createCompressedFile(File fileToCompress) throws IOException {
File zippedFile = new File(directory, fileToCompress.getName() + ".zip");
FileUtils.zipFile(fileToCompress, zippedFile);
return zippedFile;
}
}