Move file system support to JCR bundle.
authorMathieu Baudier <mbaudier@argeo.org>
Sat, 4 Jan 2020 11:24:29 +0000 (12:24 +0100)
committerMathieu Baudier <mbaudier@argeo.org>
Sat, 4 Jan 2020 11:24:29 +0000 (12:24 +0100)
org.argeo.enterprise/ext/test/AllEnterpriseTests.java [deleted file]
org.argeo.enterprise/ext/test/org/argeo/fs/FsUtilsTest.java [deleted file]
org.argeo.enterprise/src/org/argeo/fs/BasicSyncFileVisitor.java [deleted file]
org.argeo.enterprise/src/org/argeo/fs/FsUtils.java [deleted file]
org.argeo.jcr/ext/test/org/argeo/fs/FsUtilsTest.java [new file with mode: 0644]
org.argeo.jcr/src/org/argeo/fs/BasicSyncFileVisitor.java [new file with mode: 0644]
org.argeo.jcr/src/org/argeo/fs/FsUtils.java [new file with mode: 0644]

diff --git a/org.argeo.enterprise/ext/test/AllEnterpriseTests.java b/org.argeo.enterprise/ext/test/AllEnterpriseTests.java
deleted file mode 100644 (file)
index aed01f2..0000000
+++ /dev/null
@@ -1,20 +0,0 @@
-import org.argeo.fs.FsUtilsTest;
-import org.argeo.osgi.useradmin.LdifParserTest;
-import org.argeo.osgi.useradmin.UserAdminConfTest;
-import org.argeo.util.test.Tester;
-
-class AllEnterpriseTests {
-
-       public static void main(String[] args) throws Exception {
-               Tester tester = new Tester();
-
-               // FS
-               tester.execute(FsUtilsTest.class.getName());
-
-               // User admin
-               tester.execute(LdifParserTest.class.getName());
-               //tester.execute(LdifUserAdminTest.class.getName());
-               tester.execute(UserAdminConfTest.class.getName());
-       }
-
-}
diff --git a/org.argeo.enterprise/ext/test/org/argeo/fs/FsUtilsTest.java b/org.argeo.enterprise/ext/test/org/argeo/fs/FsUtilsTest.java
deleted file mode 100644 (file)
index 793216b..0000000
+++ /dev/null
@@ -1,49 +0,0 @@
-package org.argeo.fs;
-
-import java.io.File;
-import java.io.IOException;
-import java.nio.file.Files;
-import java.nio.file.Path;
-
-/** {@link FsUtils} tests. */
-public class FsUtilsTest {
-       final static String FILE00 = "file00";
-       final static String FILE01 = "file01";
-       final static String SUB_DIR = "subDir";
-
-       public void testDelete() throws IOException {
-               Path dir = createDir00();
-               assert Files.exists(dir);
-               FsUtils.delete(dir);
-               assert !Files.exists(dir);
-       }
-
-       public void testSync() throws IOException {
-               Path source = createDir00();
-               Path target = Files.createTempDirectory(getClass().getName());
-               FsUtils.sync(source, target);
-               assert Files.exists(target.resolve(FILE00));
-               assert Files.exists(target.resolve(SUB_DIR));
-               assert Files.exists(target.resolve(SUB_DIR + File.separator + FILE01));
-               FsUtils.delete(source.resolve(SUB_DIR));
-               FsUtils.sync(source, target, true);
-               assert Files.exists(target.resolve(FILE00));
-               assert !Files.exists(target.resolve(SUB_DIR));
-               assert !Files.exists(target.resolve(SUB_DIR + File.separator + FILE01));
-
-               // clean up
-               FsUtils.delete(source);
-               FsUtils.delete(target);
-
-       }
-
-       Path createDir00() throws IOException {
-               Path base = Files.createTempDirectory(getClass().getName());
-               base.toFile().deleteOnExit();
-               Files.createFile(base.resolve(FILE00)).toFile().deleteOnExit();
-               Path subDir = Files.createDirectories(base.resolve(SUB_DIR));
-               subDir.toFile().deleteOnExit();
-               Files.createFile(subDir.resolve(FILE01)).toFile().deleteOnExit();
-               return base;
-       }
-}
diff --git a/org.argeo.enterprise/src/org/argeo/fs/BasicSyncFileVisitor.java b/org.argeo.enterprise/src/org/argeo/fs/BasicSyncFileVisitor.java
deleted file mode 100644 (file)
index c60492d..0000000
+++ /dev/null
@@ -1,139 +0,0 @@
-package org.argeo.fs;
-
-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.StandardCopyOption;
-import java.nio.file.attribute.BasicFileAttributes;
-import java.nio.file.attribute.FileTime;
-
-/** Synchronises two directory structures. */
-public class BasicSyncFileVisitor extends SimpleFileVisitor<Path> {
-       // TODO make it configurable
-       private boolean debug = false;
-
-       private final Path sourceBasePath;
-       private final Path targetBasePath;
-       private final boolean delete;
-
-       public BasicSyncFileVisitor(Path sourceBasePath, Path targetBasePath, boolean delete) {
-               this.sourceBasePath = sourceBasePath;
-               this.targetBasePath = targetBasePath;
-               this.delete = delete;
-       }
-
-       @Override
-       public FileVisitResult preVisitDirectory(Path sourceDir, BasicFileAttributes attrs) throws IOException {
-               Path targetDir = toTargetPath(sourceDir);
-               Files.createDirectories(targetDir);
-               return FileVisitResult.CONTINUE;
-       }
-
-       @Override
-       public FileVisitResult postVisitDirectory(Path sourceDir, IOException exc) throws IOException {
-               if (delete) {
-                       Path targetDir = toTargetPath(sourceDir);
-                       for (Path targetPath : Files.newDirectoryStream(targetDir)) {
-                               Path sourcePath = sourceDir.resolve(targetPath.getFileName());
-                               if (!Files.exists(sourcePath)) {
-                                       try {
-                                               FsUtils.delete(targetPath);
-                                               deleted(targetPath);
-                                       } catch (Exception e) {
-                                               deleteFailed(targetPath, exc);
-                                       }
-                               }
-                       }
-               }
-               return FileVisitResult.CONTINUE;
-       }
-
-       @Override
-       public FileVisitResult visitFile(Path sourceFile, BasicFileAttributes attrs) throws IOException {
-               Path targetFile = toTargetPath(sourceFile);
-               try {
-                       if (!Files.exists(targetFile)) {
-                               Files.copy(sourceFile, targetFile);
-                               copied(sourceFile, targetFile);
-                       } else {
-                               if (shouldOverwrite(sourceFile, targetFile)) {
-                                       Files.copy(sourceFile, targetFile, StandardCopyOption.REPLACE_EXISTING);
-                               }
-                       }
-               } catch (Exception e) {
-                       copyFailed(sourceFile, targetFile, e);
-               }
-               return FileVisitResult.CONTINUE;
-       }
-
-       protected boolean shouldOverwrite(Path sourceFile, Path targetFile) throws IOException {
-               long sourceSize = Files.size(sourceFile);
-               long targetSize = Files.size(targetFile);
-               if (sourceSize != targetSize) {
-                       return true;
-               }
-               FileTime sourceLastModif = Files.getLastModifiedTime(sourceFile);
-               FileTime targetLastModif = Files.getLastModifiedTime(targetFile);
-               if (sourceLastModif.compareTo(targetLastModif) > 0)
-                       return true;
-               return shouldOverwriteLaterSameSize(sourceFile, targetFile);
-       }
-
-       protected boolean shouldOverwriteLaterSameSize(Path sourceFile, Path targetFile) {
-               return false;
-       }
-
-//     @Override
-//     public FileVisitResult visitFileFailed(Path sourceFile, IOException exc) throws IOException {
-//             error("Cannot sync " + sourceFile, exc);
-//             return FileVisitResult.CONTINUE;
-//     }
-
-       private Path toTargetPath(Path sourcePath) {
-               Path relativePath = sourceBasePath.relativize(sourcePath);
-               Path targetPath = targetBasePath.resolve(relativePath.toString());
-               return targetPath;
-       }
-
-       public Path getSourceBasePath() {
-               return sourceBasePath;
-       }
-
-       public Path getTargetBasePath() {
-               return targetBasePath;
-       }
-
-       protected void copied(Path sourcePath, Path targetPath) {
-               if (isDebugEnabled())
-                       debug("Copied " + sourcePath + " to " + targetPath);
-       }
-
-       protected void copyFailed(Path sourcePath, Path targetPath, Exception e) {
-               error("Cannot copy " + sourcePath + " to " + targetPath, e);
-       }
-
-       protected void deleted(Path targetPath) {
-               if (isDebugEnabled())
-                       debug("Deleted " + targetPath);
-       }
-
-       protected void deleteFailed(Path targetPath, Exception e) {
-               error("Cannot delete " + targetPath, e);
-       }
-
-       /** Log error. */
-       protected void error(Object obj, Throwable e) {
-               System.err.println(obj);
-               e.printStackTrace();
-       }
-
-       protected boolean isDebugEnabled() {
-               return debug;
-       }
-
-       protected void debug(Object obj) {
-               System.out.println(obj);
-       }
-}
diff --git a/org.argeo.enterprise/src/org/argeo/fs/FsUtils.java b/org.argeo.enterprise/src/org/argeo/fs/FsUtils.java
deleted file mode 100644 (file)
index 6fc7bd2..0000000
+++ /dev/null
@@ -1,58 +0,0 @@
-package org.argeo.fs;
-
-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;
-
-/** Utilities around the standard Java file abstractions. */
-public class FsUtils {
-       /** Sync a source path with a target path. */
-       public static void sync(Path sourceBasePath, Path targetBasePath) {
-               sync(sourceBasePath, targetBasePath, false);
-       }
-
-       /** Sync a source path with a target path. */
-       public static void sync(Path sourceBasePath, Path targetBasePath, boolean delete) {
-               sync(new BasicSyncFileVisitor(sourceBasePath, targetBasePath, delete));
-       }
-
-       public static void sync(BasicSyncFileVisitor syncFileVisitor) {
-               try {
-                       Files.walkFileTree(syncFileVisitor.getSourceBasePath(), syncFileVisitor);
-               } catch (Exception e) {
-                       throw new RuntimeException("Cannot sync " + syncFileVisitor.getSourceBasePath() + " with "
-                                       + syncFileVisitor.getTargetBasePath(), e);
-               }
-       }
-
-       /** Deletes this path, recursively if needed. */
-       public static void delete(Path path) {
-               try {
-                       Files.walkFileTree(path, new SimpleFileVisitor<Path>() {
-                               @Override
-                               public FileVisitResult postVisitDirectory(Path directory, IOException e) throws IOException {
-                                       if (e != null)
-                                               throw e;
-                                       Files.delete(directory);
-                                       return FileVisitResult.CONTINUE;
-                               }
-
-                               @Override
-                               public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
-                                       Files.delete(file);
-                                       return FileVisitResult.CONTINUE;
-                               }
-                       });
-               } catch (IOException e) {
-                       throw new RuntimeException("Cannot delete " + path, e);
-               }
-       }
-
-       /** Singleton. */
-       private FsUtils() {
-       }
-
-}
diff --git a/org.argeo.jcr/ext/test/org/argeo/fs/FsUtilsTest.java b/org.argeo.jcr/ext/test/org/argeo/fs/FsUtilsTest.java
new file mode 100644 (file)
index 0000000..793216b
--- /dev/null
@@ -0,0 +1,49 @@
+package org.argeo.fs;
+
+import java.io.File;
+import java.io.IOException;
+import java.nio.file.Files;
+import java.nio.file.Path;
+
+/** {@link FsUtils} tests. */
+public class FsUtilsTest {
+       final static String FILE00 = "file00";
+       final static String FILE01 = "file01";
+       final static String SUB_DIR = "subDir";
+
+       public void testDelete() throws IOException {
+               Path dir = createDir00();
+               assert Files.exists(dir);
+               FsUtils.delete(dir);
+               assert !Files.exists(dir);
+       }
+
+       public void testSync() throws IOException {
+               Path source = createDir00();
+               Path target = Files.createTempDirectory(getClass().getName());
+               FsUtils.sync(source, target);
+               assert Files.exists(target.resolve(FILE00));
+               assert Files.exists(target.resolve(SUB_DIR));
+               assert Files.exists(target.resolve(SUB_DIR + File.separator + FILE01));
+               FsUtils.delete(source.resolve(SUB_DIR));
+               FsUtils.sync(source, target, true);
+               assert Files.exists(target.resolve(FILE00));
+               assert !Files.exists(target.resolve(SUB_DIR));
+               assert !Files.exists(target.resolve(SUB_DIR + File.separator + FILE01));
+
+               // clean up
+               FsUtils.delete(source);
+               FsUtils.delete(target);
+
+       }
+
+       Path createDir00() throws IOException {
+               Path base = Files.createTempDirectory(getClass().getName());
+               base.toFile().deleteOnExit();
+               Files.createFile(base.resolve(FILE00)).toFile().deleteOnExit();
+               Path subDir = Files.createDirectories(base.resolve(SUB_DIR));
+               subDir.toFile().deleteOnExit();
+               Files.createFile(subDir.resolve(FILE01)).toFile().deleteOnExit();
+               return base;
+       }
+}
diff --git a/org.argeo.jcr/src/org/argeo/fs/BasicSyncFileVisitor.java b/org.argeo.jcr/src/org/argeo/fs/BasicSyncFileVisitor.java
new file mode 100644 (file)
index 0000000..c60492d
--- /dev/null
@@ -0,0 +1,139 @@
+package org.argeo.fs;
+
+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.StandardCopyOption;
+import java.nio.file.attribute.BasicFileAttributes;
+import java.nio.file.attribute.FileTime;
+
+/** Synchronises two directory structures. */
+public class BasicSyncFileVisitor extends SimpleFileVisitor<Path> {
+       // TODO make it configurable
+       private boolean debug = false;
+
+       private final Path sourceBasePath;
+       private final Path targetBasePath;
+       private final boolean delete;
+
+       public BasicSyncFileVisitor(Path sourceBasePath, Path targetBasePath, boolean delete) {
+               this.sourceBasePath = sourceBasePath;
+               this.targetBasePath = targetBasePath;
+               this.delete = delete;
+       }
+
+       @Override
+       public FileVisitResult preVisitDirectory(Path sourceDir, BasicFileAttributes attrs) throws IOException {
+               Path targetDir = toTargetPath(sourceDir);
+               Files.createDirectories(targetDir);
+               return FileVisitResult.CONTINUE;
+       }
+
+       @Override
+       public FileVisitResult postVisitDirectory(Path sourceDir, IOException exc) throws IOException {
+               if (delete) {
+                       Path targetDir = toTargetPath(sourceDir);
+                       for (Path targetPath : Files.newDirectoryStream(targetDir)) {
+                               Path sourcePath = sourceDir.resolve(targetPath.getFileName());
+                               if (!Files.exists(sourcePath)) {
+                                       try {
+                                               FsUtils.delete(targetPath);
+                                               deleted(targetPath);
+                                       } catch (Exception e) {
+                                               deleteFailed(targetPath, exc);
+                                       }
+                               }
+                       }
+               }
+               return FileVisitResult.CONTINUE;
+       }
+
+       @Override
+       public FileVisitResult visitFile(Path sourceFile, BasicFileAttributes attrs) throws IOException {
+               Path targetFile = toTargetPath(sourceFile);
+               try {
+                       if (!Files.exists(targetFile)) {
+                               Files.copy(sourceFile, targetFile);
+                               copied(sourceFile, targetFile);
+                       } else {
+                               if (shouldOverwrite(sourceFile, targetFile)) {
+                                       Files.copy(sourceFile, targetFile, StandardCopyOption.REPLACE_EXISTING);
+                               }
+                       }
+               } catch (Exception e) {
+                       copyFailed(sourceFile, targetFile, e);
+               }
+               return FileVisitResult.CONTINUE;
+       }
+
+       protected boolean shouldOverwrite(Path sourceFile, Path targetFile) throws IOException {
+               long sourceSize = Files.size(sourceFile);
+               long targetSize = Files.size(targetFile);
+               if (sourceSize != targetSize) {
+                       return true;
+               }
+               FileTime sourceLastModif = Files.getLastModifiedTime(sourceFile);
+               FileTime targetLastModif = Files.getLastModifiedTime(targetFile);
+               if (sourceLastModif.compareTo(targetLastModif) > 0)
+                       return true;
+               return shouldOverwriteLaterSameSize(sourceFile, targetFile);
+       }
+
+       protected boolean shouldOverwriteLaterSameSize(Path sourceFile, Path targetFile) {
+               return false;
+       }
+
+//     @Override
+//     public FileVisitResult visitFileFailed(Path sourceFile, IOException exc) throws IOException {
+//             error("Cannot sync " + sourceFile, exc);
+//             return FileVisitResult.CONTINUE;
+//     }
+
+       private Path toTargetPath(Path sourcePath) {
+               Path relativePath = sourceBasePath.relativize(sourcePath);
+               Path targetPath = targetBasePath.resolve(relativePath.toString());
+               return targetPath;
+       }
+
+       public Path getSourceBasePath() {
+               return sourceBasePath;
+       }
+
+       public Path getTargetBasePath() {
+               return targetBasePath;
+       }
+
+       protected void copied(Path sourcePath, Path targetPath) {
+               if (isDebugEnabled())
+                       debug("Copied " + sourcePath + " to " + targetPath);
+       }
+
+       protected void copyFailed(Path sourcePath, Path targetPath, Exception e) {
+               error("Cannot copy " + sourcePath + " to " + targetPath, e);
+       }
+
+       protected void deleted(Path targetPath) {
+               if (isDebugEnabled())
+                       debug("Deleted " + targetPath);
+       }
+
+       protected void deleteFailed(Path targetPath, Exception e) {
+               error("Cannot delete " + targetPath, e);
+       }
+
+       /** Log error. */
+       protected void error(Object obj, Throwable e) {
+               System.err.println(obj);
+               e.printStackTrace();
+       }
+
+       protected boolean isDebugEnabled() {
+               return debug;
+       }
+
+       protected void debug(Object obj) {
+               System.out.println(obj);
+       }
+}
diff --git a/org.argeo.jcr/src/org/argeo/fs/FsUtils.java b/org.argeo.jcr/src/org/argeo/fs/FsUtils.java
new file mode 100644 (file)
index 0000000..6fc7bd2
--- /dev/null
@@ -0,0 +1,58 @@
+package org.argeo.fs;
+
+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;
+
+/** Utilities around the standard Java file abstractions. */
+public class FsUtils {
+       /** Sync a source path with a target path. */
+       public static void sync(Path sourceBasePath, Path targetBasePath) {
+               sync(sourceBasePath, targetBasePath, false);
+       }
+
+       /** Sync a source path with a target path. */
+       public static void sync(Path sourceBasePath, Path targetBasePath, boolean delete) {
+               sync(new BasicSyncFileVisitor(sourceBasePath, targetBasePath, delete));
+       }
+
+       public static void sync(BasicSyncFileVisitor syncFileVisitor) {
+               try {
+                       Files.walkFileTree(syncFileVisitor.getSourceBasePath(), syncFileVisitor);
+               } catch (Exception e) {
+                       throw new RuntimeException("Cannot sync " + syncFileVisitor.getSourceBasePath() + " with "
+                                       + syncFileVisitor.getTargetBasePath(), e);
+               }
+       }
+
+       /** Deletes this path, recursively if needed. */
+       public static void delete(Path path) {
+               try {
+                       Files.walkFileTree(path, new SimpleFileVisitor<Path>() {
+                               @Override
+                               public FileVisitResult postVisitDirectory(Path directory, IOException e) throws IOException {
+                                       if (e != null)
+                                               throw e;
+                                       Files.delete(directory);
+                                       return FileVisitResult.CONTINUE;
+                               }
+
+                               @Override
+                               public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
+                                       Files.delete(file);
+                                       return FileVisitResult.CONTINUE;
+                               }
+                       });
+               } catch (IOException e) {
+                       throw new RuntimeException("Cannot delete " + path, e);
+               }
+       }
+
+       /** Singleton. */
+       private FsUtils() {
+       }
+
+}