package org.argeo.sync.fs;
-import java.io.IOException;
-import java.nio.file.FileVisitResult;
-import java.nio.file.Files;
import java.nio.file.Path;
-import java.nio.file.SimpleFileVisitor;
-import java.nio.file.attribute.BasicFileAttributes;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
+import org.argeo.fs.BasicSyncFileVisitor;
/** Synchronises two directory structures. */
-public class SyncFileVisitor extends SimpleFileVisitor<Path> {
+public class SyncFileVisitor extends BasicSyncFileVisitor {
private final static Log log = LogFactory.getLog(SyncFileVisitor.class);
- private final Path sourceBasePath;
- private final Path targetBasePath;
-
- public SyncFileVisitor(Path sourceBasePath, Path targetBasePath) {
- this.sourceBasePath = sourceBasePath;
- this.targetBasePath = targetBasePath;
+ public SyncFileVisitor(Path sourceBasePath, Path targetBasePath, boolean delete) {
+ super(sourceBasePath, targetBasePath, delete);
}
@Override
- public FileVisitResult preVisitDirectory(Path dir, BasicFileAttributes attrs) throws IOException {
- Path targetPath = toTargetPath(dir);
- Files.createDirectories(targetPath);
- return FileVisitResult.CONTINUE;
+ protected void error(Object obj, Throwable e) {
+ log.error(obj, e);
}
@Override
- public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
- Path targetPath = toTargetPath(file);
- try {
- Files.copy(file, targetPath);
- if (log.isDebugEnabled())
- log.debug("Copied " + targetPath);
- } catch (Exception e) {
- log.error("Cannot copy " + file + " to " + targetPath, e);
- }
- return FileVisitResult.CONTINUE;
+ protected boolean isDebugEnabled() {
+ return log.isDebugEnabled();
}
@Override
- public FileVisitResult visitFileFailed(Path file, IOException exc) throws IOException {
- log.error("Cannot sync " + file, exc);
- return FileVisitResult.CONTINUE;
- }
-
- private Path toTargetPath(Path sourcePath) {
- Path relativePath = sourceBasePath.relativize(sourcePath);
- Path targetPath = targetBasePath.resolve(relativePath.toString());
- return targetPath;
+ protected void debug(Object obj) {
+ log.debug(obj);
}
}