use of org.jumpmind.symmetric.model.FileSnapshot.LastEventType in project symmetric-ds by JumpMind.
the class FileSyncDataRouter method routeToNodes.
public Set<String> routeToNodes(SimpleRouterContext context, DataMetaData dataMetaData, Set<Node> nodes, boolean initialLoad, boolean initialLoadSelectUsed, TriggerRouter triggerRouter) {
Set<String> nodeIds = new HashSet<String>();
IFileSyncService fileSyncService = engine.getFileSyncService();
IRouterService routerService = engine.getRouterService();
Map<String, String> newData = getNewDataAsString(null, dataMetaData, engine.getSymmetricDialect());
String triggerId = newData.get("TRIGGER_ID");
String routerId = newData.get("ROUTER_ID");
String sourceNodeId = newData.get("LAST_UPDATE_BY");
String lastEventType = newData.get("LAST_EVENT_TYPE");
if (triggerId == null) {
Map<String, String> oldData = getOldDataAsString(null, dataMetaData, engine.getSymmetricDialect());
triggerId = oldData.get("TRIGGER_ID");
routerId = oldData.get("ROUTER_ID");
sourceNodeId = oldData.get("LAST_UPDATE_BY");
lastEventType = oldData.get("LAST_EVENT_TYPE");
}
LastEventType eventType = LastEventType.fromCode(lastEventType);
FileTriggerRouter fileTriggerRouter = fileSyncService.getFileTriggerRouter(triggerId, routerId);
if (fileTriggerRouter != null && fileTriggerRouter.isEnabled()) {
if (fileTriggerRouter.getRouter().getNodeGroupLink().equals(triggerRouter.getRouter().getNodeGroupLink())) {
if (eventType == null || eventType == LastEventType.DELETE && fileTriggerRouter.getFileTrigger().isSyncOnDelete() || eventType == LastEventType.MODIFY && fileTriggerRouter.getFileTrigger().isSyncOnModified() || eventType == LastEventType.CREATE && fileTriggerRouter.getFileTrigger().isSyncOnCreate()) {
Router router = fileTriggerRouter.getRouter();
Map<String, IDataRouter> routers = routerService.getRouters();
IDataRouter dataRouter = null;
if (StringUtils.isNotBlank(router.getRouterType())) {
dataRouter = routers.get(router.getRouterType());
}
if (dataRouter == null) {
dataRouter = routers.get("default");
}
if (context instanceof ChannelRouterContext) {
((ChannelRouterContext) context).addUsedDataRouter(dataRouter);
}
dataMetaData.setRouter(router);
Set<String> dataRouterNodeIds = dataRouter.routeToNodes(context, dataMetaData, nodes, false, false, triggerRouter);
if (dataRouterNodeIds != null) {
nodeIds.addAll(dataRouterNodeIds);
}
nodeIds.remove(sourceNodeId);
}
}
} else {
log.error("Could not find a trigger router with a trigger_id of {} and a router_id of {}. The file snapshot will not be routed", triggerId, routerId);
}
return nodeIds;
}
use of org.jumpmind.symmetric.model.FileSnapshot.LastEventType in project symmetric-ds by JumpMind.
the class BashFileSyncZipScript method buildScriptFileSnapshot.
@Override
public void buildScriptFileSnapshot(Batch batch, FileSnapshot snapshot, FileTriggerRouter triggerRouter, FileTrigger fileTrigger, File file, String targetBaseDir, String targetFile) {
LastEventType eventType = snapshot.getLastEventType();
appendln("processFile=true");
appendln(String.format("sourceFileName=\"%s\"", snapshot.getFileName()));
append("targetRelativeDir=\"");
if (!snapshot.getRelativeDir().equals(".")) {
append(StringEscapeUtils.escapeJava(snapshot.getRelativeDir()));
}
appendln("\"");
appendln("targetFileName=$sourceFileName");
appendln(String.format("sourceFilePath=\"%s\"", StringEscapeUtils.escapeJava(snapshot.getRelativeDir())));
appendln(String.format("targetBaseDir=\"%s\"", targetBaseDir));
if (StringUtils.isNotBlank(fileTrigger.getBeforeCopyScript())) {
appendln(fileTrigger.getBeforeCopyScript());
}
appendln("if [ \"$processFile\" = true ] ; then ");
// This line guards against shell script syntax error caused by empty "if...fi" when the source
// file was removed before extraction.
appendln(" echo \"#Processing " + snapshot.getFileName() + "\" >> \"$outputFileName\"");
switch(eventType) {
case CREATE:
case MODIFY:
if (file.exists()) {
appendln(" mkdir -p \"$targetBaseDir\"");
append(" sourceFile=\"$batchDir/");
if (!snapshot.getRelativeDir().equals(".")) {
append("$sourceFilePath/");
}
append("$sourceFileName");
appendln("\"");
appendln("targetFile=\"$targetBaseDir/$targetRelativeDir/$targetFileName\"");
appendln("targetDir=\"$targetBaseDir/$targetRelativeDir\"");
// no need to copy directory if it already exists
appendln(" if [ -d \"$targetFile\" ]; then");
appendln(" processFile=false");
appendln(" fi");
// conflict resolution
// FileConflictStrategy conflictStrategy = triggerRouter.getConflictStrategy();
// if (conflictStrategy == FileConflictStrategy.TARGET_WINS ||
// conflictStrategy == FileConflictStrategy.MANUAL) {
// command.appendln(" if (targetFile.exists() && !targetFile.isDirectory()) {\n");
// command.appendln(" long targetChecksum = org.apache.commons.io.FileUtils.checksumCRC32(targetFile);\n");
// command.appendln(" if (targetChecksum != " + snapshot.getOldCrc32Checksum() + "L) {\n");
// if (conflictStrategy == FileConflictStrategy.MANUAL) {
// command.appendln(" throw new org.jumpmind.symmetric.file.FileConflictException(targetFileName + \" was in conflict \");\n");
// } else {
// command.appendln(" processFile = false;\n");
// }
// command.appendln(" }\n");
// command.appendln(" }\n");
// }
appendln(" if [ \"$processFile\" = true ] ; then");
appendln(" cp -a \"$sourceFile\" \"$targetDir\"");
appendln(" echo \"$targetBaseDir/$targetRelativeDir/$targetFileName=" + eventType.getCode() + "\" >> $outputFileName");
appendln(" fi ");
}
break;
case DELETE:
appendln(" if [ \"$processFile\" = true ] ; then");
appendln(" rm -rf \"$targetBaseDir/$targetRelativeDir/$targetFileName\"");
appendln(" echo \"$targetBaseDir/$targetRelativeDir/$targetFileName=D\" >> $outputFileName");
appendln(" fi ");
break;
default:
break;
}
if (StringUtils.isNotBlank(fileTrigger.getAfterCopyScript())) {
appendln(fileTrigger.getAfterCopyScript());
}
appendln("fi\n");
}
use of org.jumpmind.symmetric.model.FileSnapshot.LastEventType 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());
}
FileSyncZipScript script = createFileSyncZipScript(batch.getTargetNodeId());
script.buildScriptStart(batch);
Map<String, LastEventType> entries = new HashMap<String, LastEventType>();
for (FileSnapshot snapshot : snapshotEvents) {
FileTriggerRouter triggerRouter = fileSyncService.getFileTriggerRouter(snapshot.getTriggerId(), snapshot.getRouterId());
if (triggerRouter != null) {
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);
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("/");
}
String targetFile = "targetBaseDir + \"/\" + targetRelativeDir + \"/\" + targetFileName";
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());
}
}
script.buildScriptFileSnapshot(batch, snapshot, triggerRouter, fileTrigger, file, targetBaseDir, targetFile);
}
} 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.buildScriptEnd(batch);
ZipEntry entry = new ZipEntry(batch.getBatchId() + "/" + script.getScriptFileName(batch));
zos.putNextEntry(entry);
IOUtils.write(script.getScript().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);
}
}
use of org.jumpmind.symmetric.model.FileSnapshot.LastEventType 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);
}
Aggregations