1 package org
.argeo
.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
.jackrabbit
.fs
.DavexFsProvider
;
12 import org
.argeo
.sync
.SyncResult
;
14 /** Synchronises two paths. */
15 public class PathSync
implements Callable
<SyncResult
<Path
>> {
16 private final URI sourceUri
, targetUri
;
17 private final boolean delete
;
18 private final boolean recursive
;
20 public PathSync(URI sourceUri
, URI targetUri
) {
21 this(sourceUri
, targetUri
, false, false);
24 public PathSync(URI sourceUri
, URI targetUri
, boolean delete
, boolean recursive
) {
25 this.sourceUri
= sourceUri
;
26 this.targetUri
= targetUri
;
28 this.recursive
= recursive
;
32 public SyncResult
<Path
> call() {
34 Path sourceBasePath
= createPath(sourceUri
);
35 Path targetBasePath
= createPath(targetUri
);
36 SyncFileVisitor syncFileVisitor
= new SyncFileVisitor(sourceBasePath
, targetBasePath
, delete
, recursive
);
37 Files
.walkFileTree(sourceBasePath
, syncFileVisitor
);
38 return syncFileVisitor
.getSyncResult();
39 } catch (Exception e
) {
40 throw new IllegalStateException("Cannot sync " + sourceUri
+ " to " + targetUri
, e
);
44 private Path
createPath(URI uri
) {
46 if (uri
.getScheme() == null) {
47 path
= Paths
.get(uri
.getPath());
48 } else if (uri
.getScheme().equals("file")) {
49 FileSystemProvider fsProvider
= FileSystems
.getDefault().provider();
50 path
= fsProvider
.getPath(uri
);
51 } else if (uri
.getScheme().equals("davex")) {
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
);