1 package org
.argeo
.cms
.acr
.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 /** Synchronises two paths. */
12 public class PathSync
implements Callable
<SyncResult
<Path
>> {
13 private final URI sourceUri
, targetUri
;
14 private final boolean delete
;
15 private final boolean recursive
;
17 public PathSync(URI sourceUri
, URI targetUri
) {
18 this(sourceUri
, targetUri
, false, false);
21 public PathSync(URI sourceUri
, URI targetUri
, boolean delete
, boolean recursive
) {
22 this.sourceUri
= sourceUri
;
23 this.targetUri
= targetUri
;
25 this.recursive
= recursive
;
29 public SyncResult
<Path
> call() {
31 Path sourceBasePath
= createPath(sourceUri
);
32 Path targetBasePath
= createPath(targetUri
);
33 SyncFileVisitor syncFileVisitor
= new SyncFileVisitor(sourceBasePath
, targetBasePath
, delete
, recursive
);
34 Files
.walkFileTree(sourceBasePath
, syncFileVisitor
);
35 return syncFileVisitor
.getSyncResult();
36 } catch (Exception e
) {
37 throw new IllegalStateException("Cannot sync " + sourceUri
+ " to " + targetUri
, e
);
41 private Path
createPath(URI uri
) {
43 if (uri
.getScheme() == null) {
44 path
= Paths
.get(uri
.getPath());
45 } else if (uri
.getScheme().equals("file")) {
46 FileSystemProvider fsProvider
= FileSystems
.getDefault().provider();
47 path
= fsProvider
.getPath(uri
);
48 } else if (uri
.getScheme().equals("davex")) {
49 throw new UnsupportedOperationException();
50 // FileSystemProvider fsProvider = new DavexFsProvider();
51 // path = fsProvider.getPath(uri);
52 // } else if (uri.getScheme().equals("sftp")) {
53 // Sftp sftp = new Sftp(uri);
54 // path = sftp.getBasePath();
56 throw new IllegalArgumentException("URI scheme not supported for " + uri
);