1 package org
.argeo
.cms
.cmd
;
3 import java
.io
.IOException
;
4 import java
.nio
.file
.FileVisitResult
;
5 import java
.nio
.file
.Files
;
6 import java
.nio
.file
.Path
;
7 import java
.nio
.file
.SimpleFileVisitor
;
8 import java
.nio
.file
.attribute
.BasicFileAttributes
;
10 import org
.apache
.commons
.logging
.Log
;
11 import org
.apache
.commons
.logging
.LogFactory
;
13 /** Synchronises two directory structures. */
14 public class SyncFileVisitor
extends SimpleFileVisitor
<Path
> {
15 private final static Log log
= LogFactory
.getLog(SyncFileVisitor
.class);
17 private final Path sourceBasePath
;
18 private final Path targetBasePath
;
20 public SyncFileVisitor(Path sourceBasePath
, Path targetBasePath
) {
21 this.sourceBasePath
= sourceBasePath
;
22 this.targetBasePath
= targetBasePath
;
26 public FileVisitResult
preVisitDirectory(Path dir
, BasicFileAttributes attrs
) throws IOException
{
27 Path targetPath
= toTargetPath(dir
);
28 Files
.createDirectories(targetPath
);
29 return FileVisitResult
.CONTINUE
;
33 public FileVisitResult
visitFile(Path file
, BasicFileAttributes attrs
) throws IOException
{
34 Path targetPath
= toTargetPath(file
);
36 Files
.copy(file
, targetPath
);
37 if (log
.isDebugEnabled())
38 log
.debug("Copied " + targetPath
);
39 } catch (Exception e
) {
40 log
.error("Cannot copy " + file
+ " to " + targetPath
, e
);
42 return FileVisitResult
.CONTINUE
;
46 public FileVisitResult
visitFileFailed(Path file
, IOException exc
) throws IOException
{
47 log
.error("Cannot sync " + file
, exc
);
48 return FileVisitResult
.CONTINUE
;
51 private Path
toTargetPath(Path sourcePath
) {
52 Path relativePath
= sourceBasePath
.relativize(sourcePath
);
53 Path targetPath
= targetBasePath
.resolve(relativePath
.toString());