From 41bbd85cbf36d5eeb78292941ab7faefd1bab30c Mon Sep 17 00:00:00 2001 From: jensp Date: Mon, 27 Aug 2018 15:40:06 +0000 Subject: [PATCH] CcmNG: - More functions for the CcmFiles service - FileSystemThemeProvider for serving themes from the file system (using CcmFiles) git-svn-id: https://svn.libreccm.org/ccm/ccm_ng@5681 8810af33-2d31-482b-a856-94f89814c4df --- .../java/org/libreccm/files/CcmFiles.java | 6 +++++ .../org/libreccm/files/FileSystemAdapter.java | 2 ++ .../libreccm/files/NIOFileSystemAdapter.java | 20 ++++++++++++++++- .../theming/FileSystemThemeProvider.java | 18 +++++++++++---- .../libreccm/files/XAFileSystemAdapter.java | 22 +++++++++++++++++++ 5 files changed, 63 insertions(+), 5 deletions(-) diff --git a/ccm-core/src/main/java/org/libreccm/files/CcmFiles.java b/ccm-core/src/main/java/org/libreccm/files/CcmFiles.java index 8795e34b0..41b7ab28a 100644 --- a/ccm-core/src/main/java/org/libreccm/files/CcmFiles.java +++ b/ccm-core/src/main/java/org/libreccm/files/CcmFiles.java @@ -403,6 +403,12 @@ public class CcmFiles { getFileSystemAdapter().copy(sourcePath, targetPath, recursive); } + public void moveFile(final String sourcePath, + final String targetPath) throws FileAccessException { + + getFileSystemAdapter().move(sourcePath, targetPath); + } + /** * Delete a file or directory. If the file is a directory the directory must * be empty. diff --git a/ccm-core/src/main/java/org/libreccm/files/FileSystemAdapter.java b/ccm-core/src/main/java/org/libreccm/files/FileSystemAdapter.java index 52e40f121..5de4c978d 100644 --- a/ccm-core/src/main/java/org/libreccm/files/FileSystemAdapter.java +++ b/ccm-core/src/main/java/org/libreccm/files/FileSystemAdapter.java @@ -143,6 +143,8 @@ public interface FileSystemAdapter { String targetPath, boolean recursive) throws FileAccessException; + void move(String sourcePath, String targetPath) throws FileAccessException; + /** * checks if the provided path points to a directory. * diff --git a/ccm-core/src/main/java/org/libreccm/files/NIOFileSystemAdapter.java b/ccm-core/src/main/java/org/libreccm/files/NIOFileSystemAdapter.java index 783e91784..a54a4f2e6 100644 --- a/ccm-core/src/main/java/org/libreccm/files/NIOFileSystemAdapter.java +++ b/ccm-core/src/main/java/org/libreccm/files/NIOFileSystemAdapter.java @@ -266,7 +266,7 @@ public class NIOFileSystemAdapter implements FileSystemAdapter { StandardCopyOption.COPY_ATTRIBUTES, StandardCopyOption.REPLACE_EXISTING, LinkOption.NOFOLLOW_LINKS); - + return FileVisitResult.CONTINUE; } @@ -319,7 +319,25 @@ public class NIOFileSystemAdapter implements FileSystemAdapter { throw new FileAccessException(sourcePath, ex); } } + } + @Override + public void move(final String sourcePath, final String targetPath) + throws FileAccessException { + + final Path nioSourcePath = Paths.get(sourcePath); + final Path nioTargetPath = Paths.get(targetPath); + + try { + Files.move(nioSourcePath, + nioTargetPath, + StandardCopyOption.ATOMIC_MOVE, + StandardCopyOption.COPY_ATTRIBUTES, + StandardCopyOption.REPLACE_EXISTING, + LinkOption.NOFOLLOW_LINKS); + } catch(IOException ex) { + throw new FileAccessException(targetPath, ex); + } } @Override diff --git a/ccm-core/src/main/java/org/libreccm/theming/FileSystemThemeProvider.java b/ccm-core/src/main/java/org/libreccm/theming/FileSystemThemeProvider.java index 253f84f8e..062936fda 100644 --- a/ccm-core/src/main/java/org/libreccm/theming/FileSystemThemeProvider.java +++ b/ccm-core/src/main/java/org/libreccm/theming/FileSystemThemeProvider.java @@ -32,6 +32,8 @@ import java.io.OutputStream; import java.util.Collections; import java.util.List; import java.util.Optional; +import java.util.logging.Level; +import java.util.logging.Logger; import java.util.stream.Collectors; import javax.enterprise.context.RequestScoped; @@ -233,13 +235,21 @@ public class FileSystemThemeProvider implements ThemeProvider { ThemeVersion.DRAFT); final String liveThemePath = createThemePath(theme, ThemeVersion.LIVE); + final String liveThemePathTmp = String.format("%_tmp", liveThemePath); try { - ccmFiles.copyFile(draftThemePath, liveThemePath, true); - } catch (FileAccessException ex) { + ccmFiles.copyFile(draftThemePath, liveThemePathTmp, true); + if (ccmFiles.existsFile(liveThemePath)) { + ccmFiles.deleteFile(liveThemePath, true); + } + + ccmFiles.moveFile(liveThemePathTmp, liveThemePath); + } catch (DirectoryNotEmptyException + | FileAccessException + | FileDoesNotExistException + | InsufficientPermissionsException ex) { throw new UnexpectedErrorException(); - } - + } } private String createThemePath(final String theme, diff --git a/ccm-xafilesystemadapter/src/main/java/org/libreccm/files/XAFileSystemAdapter.java b/ccm-xafilesystemadapter/src/main/java/org/libreccm/files/XAFileSystemAdapter.java index b6cc9d4ac..3f0d010f3 100644 --- a/ccm-xafilesystemadapter/src/main/java/org/libreccm/files/XAFileSystemAdapter.java +++ b/ccm-xafilesystemadapter/src/main/java/org/libreccm/files/XAFileSystemAdapter.java @@ -264,6 +264,28 @@ public class XAFileSystemAdapter implements FileSystemAdapter { throw new FileAccessException(targetPath, ex); } } + + @Override + public void move(final String sourcePath, + final String targetPath) + throws FileAccessException { + + final XADiskConnection connection = connect(); + final File sourceFile = new File(sourcePath); + final File targetFile = new File(targetPath); + + try { + connection.moveFile(sourceFile, targetFile); + } catch(org.xadisk.filesystem.exceptions.FileAlreadyExistsException + | FileNotExistsException + | FileUnderUseException + | InsufficientPermissionOnFileException + | InterruptedException + | LockingFailedException + | NoTransactionAssociatedException ex) { + throw new FileAccessException(targetPath, ex); + } + } @Transactional(Transactional.TxType.REQUIRED) @Override