From: Mathieu Baudier Date: Sat, 4 Jan 2020 11:24:29 +0000 (+0100) Subject: Move file system support to JCR bundle. X-Git-Tag: argeo-commons-2.1.85~45 X-Git-Url: http://git.argeo.org/?p=lgpl%2Fargeo-commons.git;a=commitdiff_plain;h=767e4ce53ca9125beb8d2d8d31df9dd6bac7fabf Move file system support to JCR bundle. --- diff --git a/org.argeo.enterprise/ext/test/AllEnterpriseTests.java b/org.argeo.enterprise/ext/test/AllEnterpriseTests.java deleted file mode 100644 index aed01f25f..000000000 --- a/org.argeo.enterprise/ext/test/AllEnterpriseTests.java +++ /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 index 793216b1d..000000000 --- a/org.argeo.enterprise/ext/test/org/argeo/fs/FsUtilsTest.java +++ /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 index c60492d08..000000000 --- a/org.argeo.enterprise/src/org/argeo/fs/BasicSyncFileVisitor.java +++ /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 { - // 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 index 6fc7bd25d..000000000 --- a/org.argeo.enterprise/src/org/argeo/fs/FsUtils.java +++ /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() { - @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 index 000000000..793216b1d --- /dev/null +++ b/org.argeo.jcr/ext/test/org/argeo/fs/FsUtilsTest.java @@ -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 index 000000000..c60492d08 --- /dev/null +++ b/org.argeo.jcr/src/org/argeo/fs/BasicSyncFileVisitor.java @@ -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 { + // 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 index 000000000..6fc7bd25d --- /dev/null +++ b/org.argeo.jcr/src/org/argeo/fs/FsUtils.java @@ -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() { + @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() { + } + +}