package org.argeo.cms.cmd;
+import java.net.URI;
+import java.nio.file.FileSystems;
+import java.nio.file.Files;
+import java.nio.file.Path;
+import java.nio.file.spi.FileSystemProvider;
+import java.time.ZonedDateTime;
import java.util.HashMap;
import java.util.Map;
-import javax.jcr.Repository;
-import javax.jcr.RepositoryException;
-import javax.jcr.Session;
-
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
-import org.apache.jackrabbit.jcr2dav.Jcr2davRepositoryFactory;
-import org.argeo.cms.internal.kernel.KernelConstants;
+import org.argeo.cms.CmsException;
import org.argeo.jackrabbit.fs.DavexFsProvider;
-import org.argeo.jcr.ArgeoJcrException;
-import org.argeo.jcr.JcrUtils;
-import org.argeo.jcr.fs.JcrFileSystem;
+import org.argeo.util.LangUtils;
public class Sync {
private final static Log log = LogFactory.getLog(Sync.class);
}
}
- log.debug("Hello world " + arguments);
- String sourceUri = arguments.get("-i");
- DavexFsProvider fsProvider = new DavexFsProvider();
-// Map<String, String> params = new HashMap<String, String>();
-// params.put(KernelConstants.JACKRABBIT_REPOSITORY_URI, sourceUri);
-// params.put(KernelConstants.JACKRABBIT_REMOTE_DEFAULT_WORKSPACE, "main");
-// Repository repository;
-// try {
-// repository = new Jcr2davRepositoryFactory().getRepository(params);
-// if (repository == null)
-// throw new ArgeoJcrException("Remote Davex repository " + sourceUri + " not found");
-// Session session = repository.login();
-// if (log.isDebugEnabled())
-// log.debug("Opened JCR session to " + sourceUri);
-// JcrUtils.logoutQuietly(session);
-// } catch (RepositoryException e) {
-// throw new ArgeoJcrException("Cannot load " + sourceUri, e);
-// }
+ try {
+ URI sourceUri = new URI(arguments.get("-i"));
+ URI targetUri = new URI(arguments.get("-o"));
+ FileSystemProvider sourceFsProvider = createFsProvider(sourceUri);
+ FileSystemProvider targetFsProvider = createFsProvider(targetUri);
+ Path sourceBasePath = sourceFsProvider.getPath(sourceUri);
+ Path targetBasePath = targetFsProvider.getPath(targetUri);
+ SyncFileVisitor syncFileVisitor = new SyncFileVisitor(sourceBasePath, targetBasePath);
+ ZonedDateTime begin = ZonedDateTime.now();
+ Files.walkFileTree(sourceBasePath, syncFileVisitor);
+ if (log.isDebugEnabled())
+ log.debug("Sync from " + sourceBasePath + " to " + targetBasePath + " took " + LangUtils.since(begin));
+ } catch (Exception e) {
+ e.printStackTrace();
+ }
+ }
+ private static FileSystemProvider createFsProvider(URI uri) {
+ FileSystemProvider fsProvider;
+ if (uri.getScheme().equals("file"))
+ fsProvider = FileSystems.getDefault().provider();
+ else if (uri.getScheme().equals("davex"))
+ fsProvider = new DavexFsProvider();
+ else
+ throw new CmsException("URI scheme not supported for " + uri);
+ return fsProvider;
}
static enum Arg {
--- /dev/null
+package org.argeo.cms.cmd;
+
+import java.io.IOException;
+import java.nio.file.FileVisitResult;
+import java.nio.file.Files;
+import java.nio.file.Path;
+import java.nio.file.SimpleFileVisitor;
+import java.nio.file.attribute.BasicFileAttributes;
+
+import org.apache.commons.logging.Log;
+import org.apache.commons.logging.LogFactory;
+
+/** Synchronises two directory structures. */
+public class SyncFileVisitor extends SimpleFileVisitor<Path> {
+ private final static Log log = LogFactory.getLog(SyncFileVisitor.class);
+
+ private final Path sourceBasePath;
+ private final Path targetBasePath;
+
+ public SyncFileVisitor(Path sourceBasePath, Path targetBasePath) {
+ this.sourceBasePath = sourceBasePath;
+ this.targetBasePath = targetBasePath;
+ }
+
+ @Override
+ public FileVisitResult preVisitDirectory(Path dir, BasicFileAttributes attrs) throws IOException {
+ Path targetPath = toTargetPath(dir);
+ Files.createDirectories(targetPath);
+ return FileVisitResult.CONTINUE;
+ }
+
+ @Override
+ public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
+ Path targetPath = toTargetPath(file);
+ try {
+ Files.copy(file, targetPath);
+ if (log.isDebugEnabled())
+ log.debug("Copied " + targetPath);
+ } catch (Exception e) {
+ log.error("Cannot copy " + file + " to " + targetPath, e);
+ }
+ return FileVisitResult.CONTINUE;
+ }
+
+ @Override
+ public FileVisitResult visitFileFailed(Path file, IOException exc) throws IOException {
+ log.error("Cannot sync " + file, exc);
+ return FileVisitResult.CONTINUE;
+ }
+
+ private Path toTargetPath(Path sourcePath) {
+ Path relativePath = sourceBasePath.relativize(sourcePath);
+ Path targetPath = targetBasePath.resolve(relativePath.toString());
+ return targetPath;
+ }
+}