ZipUtil.walkZipFile(changeSetZip, new ZipUtil.ZipEntryVisitor() {
@Override
public boolean visit(ZipEntry zipEntry, ZipInputStream stream) throws Exception {
ChangeSetReader reader = new ChangeSetReaderImpl(new BufferedReader(new InputStreamReader(stream)));
Headers headers = reader.getHeaders();
List<DriftFileDTO> missingContent = new LinkedList<DriftFileDTO>();
MongoDBChangeSet changeSet = new MongoDBChangeSet();
changeSet.setCategory(headers.getType());
changeSet.setResourceId(resourceId);
changeSet.setDriftDefinitionId(headers.getDriftDefinitionId());
changeSet.setDriftDefinitionName(headers.getDriftDefinitionName());
changeSet.setDriftHandlingMode(DriftHandlingMode.normal);
changeSet.setVersion(headers.getVersion());
summary.setCategory(headers.getType());
summary.setResourceId(resourceId);
summary.setDriftDefinitionName(headers.getDriftDefinitionName());
summary.setCreatedTime(changeSet.getCtime());
for (FileEntry fileEntry : reader) {
String path = FileUtil.useForwardSlash(fileEntry.getFile());
MongoDBChangeSetEntry entry = new MongoDBChangeSetEntry(path, fileEntry.getType());
switch (fileEntry.getType()) {
case FILE_ADDED:
entry.setNewFileHash(fileEntry.getNewSHA());
if (fileDAO.findOne(fileEntry.getNewSHA()) == null) {
missingContent.add(newDriftFile(fileEntry.getNewSHA()));
}
break;
case FILE_CHANGED:
entry.setOldFileHash(fileEntry.getOldSHA());
entry.setNewFileHash(fileEntry.getNewSHA());
if (fileDAO.findOne(fileEntry.getNewSHA()) == null) {
missingContent.add(newDriftFile(fileEntry.getNewSHA()));
}
if (fileDAO.findOne(fileEntry.getOldSHA()) == null) {
missingContent.add(newDriftFile(fileEntry.getNewSHA()));
}
break;
default: // FILE_REMOVED
entry.setOldFileHash(fileEntry.getOldSHA());
if (fileDAO.findOne(fileEntry.getOldSHA()) == null) {
missingContent.add(newDriftFile(fileEntry.getOldSHA()));
}
}
changeSet.add(entry);
// we are taking advantage of the fact that we know the summary is only used by the server
// if the change set is a DRIFT report. If its a coverage report, it is not used (we do
// not alert on coverage reports) - so don't waste memory by collecting all the paths
// when we know they aren't going to be used anyway.
if (headers.getType() == DriftChangeSetCategory.DRIFT) {
summary.addDriftPathname(path);
}
}
ds.save(changeSet);
// The following section of code really should not be part of the plugin
// implementation, but it is currently required due to a flaw in the design
// of the drift plugin framework. Two things are done here. First, if we
// successfully persist the change set, then we need to send an
// acknowledgement to the agent. This is critical because the agent will
// in effect suspend drift detection (for the particular definition) until
// it receives the ACK. Secondly, we need to tell the agent to send the
// actual file bits for any change set content we do not have.
DriftAgentService driftService = getDriftAgentService(resourceId);
driftService.ackChangeSet(headers.getResourceId(), headers.getDriftDefinitionName());
if (!missingContent.isEmpty()) {
driftService.requestDriftFiles(resourceId, headers, missingContent);
}
return true;