use of org.jumpmind.symmetric.model.FileTriggerRouter 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.FileTriggerRouter in project symmetric-ds by JumpMind.
the class FileSyncTest method testUpdateManual.
protected void testUpdateManual(ISymmetricEngine rootServer, ISymmetricEngine clientServer) throws Exception {
OutgoingBatches batchesInError = rootServer.getOutgoingBatchService().getOutgoingBatchErrors(10);
List<OutgoingBatch> batches = batchesInError.getBatchesForChannel(Constants.CHANNEL_FILESYNC);
assertEquals(0, batches.size());
IFileSyncService fileSyncService = rootServer.getFileSyncService();
FileTriggerRouter fileTriggerRouter = fileSyncService.getFileTriggerRouter("all", "server_2_client");
fileTriggerRouter.setConflictStrategy(FileConflictStrategy.MANUAL);
fileSyncService.saveFileTriggerRouter(fileTriggerRouter);
pull("client");
File allFile1 = new File(allSvrSourceDir, "manual/test2.txt");
allFile1.getParentFile().mkdirs();
FileUtils.write(allFile1, "base value");
File allFile1Target = new File(allClntTargetDir, allFile1.getParentFile().getName() + "/" + allFile1.getName());
allFile1Target.getParentFile().mkdirs();
pullFiles();
assertEquals("base value", FileUtils.readFileToString(allFile1Target));
batchesInError = rootServer.getOutgoingBatchService().getOutgoingBatchErrors(10);
batches = batchesInError.getBatchesForChannel(Constants.CHANNEL_FILESYNC);
assertEquals(0, batches.size());
FileUtils.write(allFile1, "new value", true);
pullFiles();
assertEquals("base valuenew value", FileUtils.readFileToString(allFile1Target));
batchesInError = rootServer.getOutgoingBatchService().getOutgoingBatchErrors(10);
batches = batchesInError.getBatchesForChannel(Constants.CHANNEL_FILESYNC);
assertEquals(0, batches.size());
}
use of org.jumpmind.symmetric.model.FileTriggerRouter in project symmetric-ds by JumpMind.
the class FileSyncTest method testTargetWins.
protected void testTargetWins(ISymmetricEngine rootServer, ISymmetricEngine clientServer) throws Exception {
IFileSyncService fileSyncService = rootServer.getFileSyncService();
FileTriggerRouter fileTriggerRouter = fileSyncService.getFileTriggerRouter("all", "server_2_client");
fileTriggerRouter.setConflictStrategy(FileConflictStrategy.TARGET_WINS);
fileSyncService.saveFileTriggerRouter(fileTriggerRouter);
pull("client");
File allFile1 = new File(allSvrSourceDir, "tgt_wins/test.txt");
allFile1.getParentFile().mkdirs();
FileUtils.write(allFile1, "server value");
File allFile1Target = new File(allClntTargetDir, allFile1.getParentFile().getName() + "/" + allFile1.getName());
allFile1Target.getParentFile().mkdirs();
FileUtils.write(allFile1Target, "client value");
pullFiles();
assertEquals("client value", FileUtils.readFileToString(allFile1Target));
}
use of org.jumpmind.symmetric.model.FileTriggerRouter in project symmetric-ds by JumpMind.
the class AbstractFileParsingRouter method deleteFileIfNecessary.
public void deleteFileIfNecessary(DataMetaData dataMetaData) {
Data data = dataMetaData.getData();
Table snapshotTable = dataMetaData.getTable();
if (data.getDataEventType() == DataEventType.INSERT || data.getDataEventType() == DataEventType.UPDATE) {
List<File> filesToDelete = new ArrayList<File>();
Map<String, String> columnData = data.toColumnNameValuePairs(snapshotTable.getColumnNames(), CsvData.ROW_DATA);
FileSnapshot fileSnapshot = new FileSnapshot();
fileSnapshot.setTriggerId(columnData.get("TRIGGER_ID"));
fileSnapshot.setRouterId(columnData.get("ROUTER_ID"));
fileSnapshot.setFileModifiedTime(Long.parseLong(columnData.get("FILE_MODIFIED_TIME")));
fileSnapshot.setFileName(columnData.get("FILE_NAME"));
fileSnapshot.setRelativeDir(columnData.get("RELATIVE_DIR"));
fileSnapshot.setLastEventType(LastEventType.fromCode(columnData.get("LAST_EVENT_TYPE")));
FileTriggerRouter triggerRouter = getEngine().getFileSyncService().getFileTriggerRouter(fileSnapshot.getTriggerId(), fileSnapshot.getRouterId());
if (triggerRouter != null) {
FileTrigger fileTrigger = triggerRouter.getFileTrigger();
if (fileTrigger.isDeleteAfterSync()) {
File file = fileTrigger.createSourceFile(fileSnapshot);
if (!file.isDirectory()) {
filesToDelete.add(file);
if (fileTrigger.isSyncOnCtlFile()) {
File ctlFile = getEngine().getFileSyncService().getControleFile(file);
filesToDelete.add(ctlFile);
}
}
} else if (getEngine().getParameterService().is(ParameterConstants.FILE_SYNC_DELETE_CTL_FILE_AFTER_SYNC, false)) {
File file = fileTrigger.createSourceFile(fileSnapshot);
if (!file.isDirectory()) {
if (fileTrigger.isSyncOnCtlFile()) {
File ctlFile = getEngine().getFileSyncService().getControleFile(file);
filesToDelete.add(ctlFile);
}
}
}
}
if (filesToDelete != null && filesToDelete.size() > 0) {
for (File file : filesToDelete) {
if (file != null && file.exists()) {
log.debug("Deleting the '{}' file", file.getAbsolutePath());
boolean deleted = FileUtils.deleteQuietly(file);
if (!deleted) {
log.warn("Failed to 'delete on sync' the {} file", file.getAbsolutePath());
}
}
file = null;
}
filesToDelete = null;
}
}
}
use of org.jumpmind.symmetric.model.FileTriggerRouter in project symmetric-ds by JumpMind.
the class FileTriggerTrackerTest method testTakeSnapshotRecursiveTestDelete.
@Test
public void testTakeSnapshotRecursiveTestDelete() throws Exception {
FileTrigger fileTrigger = new FileTrigger(directory.getAbsolutePath(), true, null, null);
Router router = new Router();
FileTriggerRouter fileTriggerRouter = new FileTriggerRouter(fileTrigger, router);
FileTriggerTracker tracker = new FileTriggerTracker(fileTriggerRouter, null, null, false, null);
tracker.trackChanges();
FileUtils.deleteQuietly(fileInDirectory1);
DirectorySnapshot snapshot = tracker.trackChanges();
assertEquals(1, snapshot.size());
FileSnapshot change = snapshot.get(0);
assertEquals(change.getFileName(), FileSyncUtils.getRelativePath(fileInDirectory1, directory));
assertEquals(change.getLastEventType(), LastEventType.DELETE);
}
Aggregations