1 package org
.argeo
.slc
.cli
.fs
;
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
;
11 import org
.argeo
.sync
.SyncResult
;
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
;
19 public PathSync(URI sourceUri
, URI targetUri
) {
20 this(sourceUri
, targetUri
, false, false);
23 public PathSync(URI sourceUri
, URI targetUri
, boolean delete
, boolean recursive
) {
24 this.sourceUri
= sourceUri
;
25 this.targetUri
= targetUri
;
27 this.recursive
= recursive
;
31 public SyncResult
<Path
> call() {
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
);
43 private Path
createPath(URI uri
) {
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();
58 throw new IllegalArgumentException("URI scheme not supported for " + uri
);