Search in sources :

Example 1 with FileTriggerRouter

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;
}
Also used : LastEventType(org.jumpmind.symmetric.model.FileSnapshot.LastEventType) FileTriggerRouter(org.jumpmind.symmetric.model.FileTriggerRouter) IFileSyncService(org.jumpmind.symmetric.service.IFileSyncService) FileTriggerRouter(org.jumpmind.symmetric.model.FileTriggerRouter) Router(org.jumpmind.symmetric.model.Router) TriggerRouter(org.jumpmind.symmetric.model.TriggerRouter) HashSet(java.util.HashSet) IRouterService(org.jumpmind.symmetric.service.IRouterService)

Example 2 with FileTriggerRouter

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());
}
Also used : FileTriggerRouter(org.jumpmind.symmetric.model.FileTriggerRouter) IFileSyncService(org.jumpmind.symmetric.service.IFileSyncService) OutgoingBatches(org.jumpmind.symmetric.model.OutgoingBatches) OutgoingBatch(org.jumpmind.symmetric.model.OutgoingBatch) File(java.io.File)

Example 3 with FileTriggerRouter

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));
}
Also used : FileTriggerRouter(org.jumpmind.symmetric.model.FileTriggerRouter) IFileSyncService(org.jumpmind.symmetric.service.IFileSyncService) File(java.io.File)

Example 4 with FileTriggerRouter

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;
        }
    }
}
Also used : FileSnapshot(org.jumpmind.symmetric.model.FileSnapshot) Table(org.jumpmind.db.model.Table) FileTriggerRouter(org.jumpmind.symmetric.model.FileTriggerRouter) FileTrigger(org.jumpmind.symmetric.model.FileTrigger) ArrayList(java.util.ArrayList) Data(org.jumpmind.symmetric.model.Data) CsvData(org.jumpmind.symmetric.io.data.CsvData) DataMetaData(org.jumpmind.symmetric.model.DataMetaData) File(java.io.File)

Example 5 with FileTriggerRouter

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);
}
Also used : FileSnapshot(org.jumpmind.symmetric.model.FileSnapshot) FileTriggerRouter(org.jumpmind.symmetric.model.FileTriggerRouter) FileTrigger(org.jumpmind.symmetric.model.FileTrigger) FileTriggerRouter(org.jumpmind.symmetric.model.FileTriggerRouter) Router(org.jumpmind.symmetric.model.Router) Test(org.junit.Test)

Aggregations

FileTriggerRouter (org.jumpmind.symmetric.model.FileTriggerRouter)15 FileTrigger (org.jumpmind.symmetric.model.FileTrigger)8 File (java.io.File)6 Router (org.jumpmind.symmetric.model.Router)6 FileSnapshot (org.jumpmind.symmetric.model.FileSnapshot)5 Test (org.junit.Test)5 IFileSyncService (org.jumpmind.symmetric.service.IFileSyncService)4 IOException (java.io.IOException)3 ArrayList (java.util.ArrayList)3 IoException (org.jumpmind.exception.IoException)3 Table (org.jumpmind.db.model.Table)2 SymmetricException (org.jumpmind.symmetric.SymmetricException)2 DirectorySnapshot (org.jumpmind.symmetric.file.DirectorySnapshot)2 FileConflictException (org.jumpmind.symmetric.file.FileConflictException)2 CsvData (org.jumpmind.symmetric.io.data.CsvData)2 Data (org.jumpmind.symmetric.model.Data)2 LastEventType (org.jumpmind.symmetric.model.FileSnapshot.LastEventType)2 OutgoingBatch (org.jumpmind.symmetric.model.OutgoingBatch)2 OutgoingBatches (org.jumpmind.symmetric.model.OutgoingBatches)2 NoContentException (org.jumpmind.symmetric.transport.NoContentException)2