use of org.jumpmind.symmetric.model.FileConflictStrategy in project symmetric-ds by JumpMind.
the class BeanShellFileSyncZipScript method buildScriptFileSnapshot.
@Override
public void buildScriptFileSnapshot(Batch batch, FileSnapshot snapshot, FileTriggerRouter triggerRouter, FileTrigger fileTrigger, File file, String targetBaseDir, String targetFile) {
LastEventType eventType = snapshot.getLastEventType();
StringBuilder command = new StringBuilder();
command.append("targetBaseDir = \"").append(targetBaseDir).append("\";\n");
command.append("if (targetBaseDir.startsWith(\"${androidBaseDir}\")) { \n");
command.append(" targetBaseDir = targetBaseDir.replace(\"${androidBaseDir}\", \"\"); \n");
command.append(" targetBaseDir = androidBaseDir + targetBaseDir; \n");
command.append("} else if (targetBaseDir.startsWith(\"${androidAppFilesDir}\")) { \n");
command.append(" targetBaseDir = targetBaseDir.replace(\"${androidAppFilesDir}\", \"\"); \n");
command.append(" targetBaseDir = androidAppFilesDir + targetBaseDir; \n");
command.append("} \n");
command.append("processFile = true;\n");
command.append("sourceFileName = \"").append(snapshot.getFileName()).append("\";\n");
command.append("targetRelativeDir = \"");
if (!snapshot.getRelativeDir().equals(".")) {
command.append(StringEscapeUtils.escapeJava(snapshot.getRelativeDir()));
command.append("\";\n");
} else {
command.append("\";\n");
}
command.append("targetFileName = sourceFileName;\n");
command.append("sourceFilePath = \"");
command.append(StringEscapeUtils.escapeJava(snapshot.getRelativeDir())).append("\";\n");
if (StringUtils.isNotBlank(fileTrigger.getBeforeCopyScript())) {
command.append(fileTrigger.getBeforeCopyScript()).append("\n");
}
command.append("if (processFile) {\n");
switch(eventType) {
case CREATE:
case MODIFY:
if (file.exists()) {
command.append(" File targetBaseDirFile = new File(targetBaseDir);\n");
command.append(" if (!targetBaseDirFile.exists()) {\n");
command.append(" targetBaseDirFile.mkdirs();\n");
command.append(" }\n");
command.append(" java.io.File sourceFile = new java.io.File(batchDir + \"/\"");
if (!snapshot.getRelativeDir().equals(".")) {
command.append(" + sourceFilePath + \"/\"");
}
command.append(" + sourceFileName");
command.append(");\n");
command.append(" java.io.File targetFile = new java.io.File(");
command.append(targetFile);
command.append(");\n");
// no need to copy directory if it already exists
command.append(" if (targetFile.exists() && targetFile.isDirectory()) {\n");
command.append(" processFile = false;\n");
command.append(" }\n");
// conflict resolution
FileConflictStrategy conflictStrategy = triggerRouter.getConflictStrategy();
if (conflictStrategy == FileConflictStrategy.TARGET_WINS || conflictStrategy == FileConflictStrategy.MANUAL) {
command.append(" if (targetFile.exists() && !targetFile.isDirectory()) {\n");
command.append(" long targetChecksum = org.apache.commons.io.FileUtils.checksumCRC32(targetFile);\n");
command.append(" if (targetChecksum != " + snapshot.getOldCrc32Checksum() + "L) {\n");
if (conflictStrategy == FileConflictStrategy.MANUAL) {
command.append(" throw new org.jumpmind.symmetric.file.FileConflictException(targetFileName + \" was in conflict \");\n");
} else {
command.append(" processFile = false;\n");
}
command.append(" }\n");
command.append(" }\n");
}
command.append(" if (processFile) {\n");
command.append(" if (sourceFile.isDirectory()) {\n");
command.append(" org.apache.commons.io.FileUtils.copyDirectory(sourceFile, targetFile, true);\n");
command.append(" } else {\n");
command.append(" org.apache.commons.io.FileUtils.copyFile(sourceFile, targetFile, true);\n");
command.append(" }\n");
command.append(" }\n");
command.append(" fileList.put(").append(targetFile).append(",\"");
command.append(eventType.getCode());
command.append("\");\n");
}
break;
case DELETE:
command.append(" org.apache.commons.io.FileUtils.deleteQuietly(new java.io.File(");
command.append(targetFile);
command.append("));\n");
command.append(" fileList.put(").append(targetFile).append(",\"");
command.append(eventType.getCode());
command.append("\");\n");
break;
default:
break;
}
if (StringUtils.isNotBlank(fileTrigger.getAfterCopyScript())) {
command.append(fileTrigger.getAfterCopyScript()).append("\n");
}
command.append("}\n\n");
getScript().append(command);
}
use of org.jumpmind.symmetric.model.FileConflictStrategy in project symmetric-ds by JumpMind.
the class FileSyncZipDataWriter method end.
public void end(Batch batch, boolean inError) {
try {
if (!inError) {
if (zos == null) {
zos = new ZipOutputStream(stagedResource.getOutputStream());
}
Map<String, LastEventType> entries = new HashMap<String, LastEventType>();
StringBuilder script = new StringBuilder("fileList = new HashMap();\n");
for (FileSnapshot snapshot : snapshotEvents) {
FileTriggerRouter triggerRouter = fileSyncService.getFileTriggerRouter(snapshot.getTriggerId(), snapshot.getRouterId());
if (triggerRouter != null) {
StringBuilder command = new StringBuilder("\n");
LastEventType eventType = snapshot.getLastEventType();
FileTrigger fileTrigger = triggerRouter.getFileTrigger();
String targetBaseDir = ((triggerRouter.getTargetBaseDir() == null) ? null : triggerRouter.getTargetBaseDir().replace('\\', '/'));
if (StringUtils.isBlank(targetBaseDir)) {
targetBaseDir = ((fileTrigger.getBaseDir() == null) ? null : fileTrigger.getBaseDir().replace('\\', '/'));
}
targetBaseDir = StringEscapeUtils.escapeJava(targetBaseDir);
command.append("targetBaseDir = \"").append(targetBaseDir).append("\";\n");
command.append("processFile = true;\n");
command.append("sourceFileName = \"").append(snapshot.getFileName()).append("\";\n");
command.append("targetRelativeDir = \"");
if (!snapshot.getRelativeDir().equals(".")) {
command.append(StringEscapeUtils.escapeJava(snapshot.getRelativeDir()));
command.append("\";\n");
} else {
command.append("\";\n");
}
command.append("targetFileName = sourceFileName;\n");
command.append("sourceFilePath = \"");
command.append(StringEscapeUtils.escapeJava(snapshot.getRelativeDir())).append("\";\n");
StringBuilder entryName = new StringBuilder(Long.toString(batch.getBatchId()));
entryName.append("/");
if (!snapshot.getRelativeDir().equals(".")) {
entryName.append(snapshot.getRelativeDir()).append("/");
}
entryName.append(snapshot.getFileName());
File file = fileTrigger.createSourceFile(snapshot);
if (file.isDirectory()) {
entryName.append("/");
}
if (StringUtils.isNotBlank(fileTrigger.getBeforeCopyScript())) {
command.append(fileTrigger.getBeforeCopyScript()).append("\n");
}
command.append("if (processFile) {\n");
String targetFile = "targetBaseDir + \"/\" + targetRelativeDir + \"/\" + targetFileName";
switch(eventType) {
case CREATE:
case MODIFY:
if (file.exists()) {
command.append(" File targetBaseDirFile = new File(targetBaseDir);\n");
command.append(" if (!targetBaseDirFile.exists()) {\n");
command.append(" targetBaseDirFile.mkdirs();\n");
command.append(" }\n");
command.append(" java.io.File sourceFile = new java.io.File(batchDir + \"/\"");
if (!snapshot.getRelativeDir().equals(".")) {
command.append(" + sourceFilePath + \"/\"");
}
command.append(" + sourceFileName");
command.append(");\n");
command.append(" java.io.File targetFile = new java.io.File(");
command.append(targetFile);
command.append(");\n");
// no need to copy directory if it already exists
command.append(" if (targetFile.exists() && targetFile.isDirectory()) {\n");
command.append(" processFile = false;\n");
command.append(" }\n");
// conflict resolution
FileConflictStrategy conflictStrategy = triggerRouter.getConflictStrategy();
if (conflictStrategy == FileConflictStrategy.TARGET_WINS || conflictStrategy == FileConflictStrategy.MANUAL) {
command.append(" if (targetFile.exists() && !targetFile.isDirectory()) {\n");
command.append(" long targetChecksum = org.apache.commons.io.FileUtils.checksumCRC32(targetFile);\n");
command.append(" if (targetChecksum != " + snapshot.getOldCrc32Checksum() + "L) {\n");
if (conflictStrategy == FileConflictStrategy.MANUAL) {
command.append(" throw new org.jumpmind.symmetric.file.FileConflictException(targetFileName + \" was in conflict \");\n");
} else {
command.append(" processFile = false;\n");
}
command.append(" }\n");
command.append(" }\n");
}
command.append(" if (processFile) {\n");
command.append(" if (sourceFile.isDirectory()) {\n");
command.append(" org.apache.commons.io.FileUtils.copyDirectory(sourceFile, targetFile, true);\n");
command.append(" } else {\n");
command.append(" org.apache.commons.io.FileUtils.copyFile(sourceFile, targetFile, true);\n");
command.append(" }\n");
command.append(" }\n");
command.append(" fileList.put(").append(targetFile).append(",\"");
command.append(eventType.getCode());
command.append("\");\n");
}
break;
case DELETE:
command.append(" org.apache.commons.io.FileUtils.deleteQuietly(new java.io.File(");
command.append(targetFile);
command.append("));\n");
command.append(" fileList.put(").append(targetFile).append(",\"");
command.append(eventType.getCode());
command.append("\");\n");
break;
default:
break;
}
if (StringUtils.isNotBlank(fileTrigger.getAfterCopyScript())) {
command.append(fileTrigger.getAfterCopyScript()).append("\n");
}
LastEventType previousEventForEntry = entries.get(entryName.toString());
boolean process = true;
if (previousEventForEntry != null) {
if ((previousEventForEntry == eventType) || (previousEventForEntry == LastEventType.CREATE && eventType == LastEventType.MODIFY)) {
process = false;
}
}
if (process) {
if (eventType != LastEventType.DELETE) {
if (file.exists()) {
byteCount += file.length();
ZipEntry entry = new ZipEntry(entryName.toString());
entry.setSize(file.length());
entry.setTime(file.lastModified());
zos.putNextEntry(entry);
if (file.isFile()) {
FileInputStream fis = new FileInputStream(file);
try {
IOUtils.copy(fis, zos);
} finally {
IOUtils.closeQuietly(fis);
}
}
zos.closeEntry();
entries.put(entryName.toString(), eventType);
} else {
log.warn("Could not find the {} file to package for synchronization. Skipping it.", file.getAbsolutePath());
}
}
command.append("}\n\n");
script.append(command.toString());
}
} else {
log.error("Could not locate the file trigger ({}) router ({}) to process a snapshot event. The event will be ignored", snapshot.getTriggerId(), snapshot.getRouterId());
}
}
script.append("return fileList;\n");
ZipEntry entry = new ZipEntry(batch.getBatchId() + "/sync.bsh");
zos.putNextEntry(entry);
IOUtils.write(script.toString(), zos);
zos.closeEntry();
entry = new ZipEntry(batch.getBatchId() + "/batch-info.txt");
zos.putNextEntry(entry);
IOUtils.write(batch.getChannelId(), zos);
zos.closeEntry();
}
} catch (IOException e) {
throw new IoException(e);
}
}
Aggregations