]> git.argeo.org Git - gpl/argeo-slc.git/blob - org.argeo.slc.runtime/src/org/argeo/cli/fs/PathSync.java
Move CLI utilities from Argeo Commons
[gpl/argeo-slc.git] / org.argeo.slc.runtime / src / org / argeo / cli / fs / PathSync.java
1 package org.argeo.cli.fs;
2
3 import java.net.URI;
4 import java.nio.file.FileSystems;
5 import java.nio.file.Files;
6 import java.nio.file.Path;
7 import java.nio.file.Paths;
8 import java.nio.file.spi.FileSystemProvider;
9 import java.util.concurrent.Callable;
10
11 import org.argeo.sync.SyncResult;
12
13 /** Synchronises two paths. */
14 public class PathSync implements Callable<SyncResult<Path>> {
15 private final URI sourceUri, targetUri;
16 private final boolean delete;
17 private final boolean recursive;
18
19 public PathSync(URI sourceUri, URI targetUri) {
20 this(sourceUri, targetUri, false, false);
21 }
22
23 public PathSync(URI sourceUri, URI targetUri, boolean delete, boolean recursive) {
24 this.sourceUri = sourceUri;
25 this.targetUri = targetUri;
26 this.delete = delete;
27 this.recursive = recursive;
28 }
29
30 @Override
31 public SyncResult<Path> call() {
32 try {
33 Path sourceBasePath = createPath(sourceUri);
34 Path targetBasePath = createPath(targetUri);
35 SyncFileVisitor syncFileVisitor = new SyncFileVisitor(sourceBasePath, targetBasePath, delete, recursive);
36 Files.walkFileTree(sourceBasePath, syncFileVisitor);
37 return syncFileVisitor.getSyncResult();
38 } catch (Exception e) {
39 throw new IllegalStateException("Cannot sync " + sourceUri + " to " + targetUri, e);
40 }
41 }
42
43 private Path createPath(URI uri) {
44 Path path;
45 if (uri.getScheme() == null) {
46 path = Paths.get(uri.getPath());
47 } else if (uri.getScheme().equals("file")) {
48 FileSystemProvider fsProvider = FileSystems.getDefault().provider();
49 path = fsProvider.getPath(uri);
50 } else if (uri.getScheme().equals("davex")) {
51 throw new UnsupportedOperationException();
52 // FileSystemProvider fsProvider = new DavexFsProvider();
53 // path = fsProvider.getPath(uri);
54 // } else if (uri.getScheme().equals("sftp")) {
55 // Sftp sftp = new Sftp(uri);
56 // path = sftp.getBasePath();
57 } else
58 throw new IllegalArgumentException("URI scheme not supported for " + uri);
59 return path;
60 }
61 }