]> git.argeo.org Git - lgpl/argeo-commons.git/blob - org.argeo.cms/src/org/argeo/cms/file/PathSync.java
FS utils throws IOException
[lgpl/argeo-commons.git] / org.argeo.cms / src / org / argeo / cms / file / PathSync.java
1 package org.argeo.cms.file;
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 /** 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;
16
17 public PathSync(URI sourceUri, URI targetUri) {
18 this(sourceUri, targetUri, false, false);
19 }
20
21 public PathSync(URI sourceUri, URI targetUri, boolean delete, boolean recursive) {
22 this.sourceUri = sourceUri;
23 this.targetUri = targetUri;
24 this.delete = delete;
25 this.recursive = recursive;
26 }
27
28 @Override
29 public SyncResult<Path> call() {
30 try {
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);
38 }
39 }
40
41 private Path createPath(URI uri) {
42 Path path;
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();
55 } else
56 throw new IllegalArgumentException("URI scheme not supported for " + uri);
57 return path;
58 }
59 }