diff --git a/src/main/java/ome/formats/importer/transfers/AbstractExecFileTransfer.java b/src/main/java/ome/formats/importer/transfers/AbstractExecFileTransfer.java index a25888a5b..eb295bf81 100644 --- a/src/main/java/ome/formats/importer/transfers/AbstractExecFileTransfer.java +++ b/src/main/java/ome/formats/importer/transfers/AbstractExecFileTransfer.java @@ -34,8 +34,7 @@ import omero.model.OriginalFile; /** - * Local-only file transfer mechanism which makes use of soft-linking. - * This is only useful where the command "ln -s source target" will work. + * Abstract base class for file transfer implementations that link or copy files. * * @since 5.0 */ @@ -46,9 +45,7 @@ public abstract class AbstractExecFileTransfer extends AbstractFileTransfer { private static final String SEPARATOR = System.getProperty("line.separator"); /** - * "Transfer" files by soft-linking them into place. This method is likely - * re-usable for other general "linking" strategies by overriding - * {@link #createProcessBuilder(File, File)} and the other protected methods here. + * Transfer files as specified by the {@link #exec(File, File)} implementation. */ public String transfer(TransferState state) throws IOException, ServerError { RawFileStorePrx rawFileStore = start(state); @@ -151,7 +148,14 @@ protected RuntimeException failLocationCheck(File location, String msg) { } /** - * Executes a local command and fails on non-0 return codes. + * If {@link createProcessBuilder(File, File)} returns non-null, + * executes a local command corresponding to the ProcessBuilder + * and fails on non-0 return codes. + * + * If the ProcessBuilder is null, this method does nothing by default. + * + * This method should be overridden by subclasses, see for example + * {@link CopyFileTransfer}. * * @param file the source file * @param location the target on the server @@ -159,6 +163,9 @@ protected RuntimeException failLocationCheck(File location, String msg) { */ protected void exec(File file, File location) throws IOException { ProcessBuilder pb = createProcessBuilder(file, location); + if (pb == null) { + return; + } pb.redirectErrorStream(true); Process process = pb.start(); Integer rcode = null; @@ -240,11 +247,11 @@ protected void checkTarget(File location, TransferState state) throws ServerErro * @param file File to be copied. * @param location Location to copy to. * @return an instance ready for performing the transfer + * @deprecated override {@link #exec(File, File)} instead */ - protected abstract ProcessBuilder createProcessBuilder(File file, File location); - - protected void printLine() { - log.error("*******************************************"); + @Deprecated + protected ProcessBuilder createProcessBuilder(File file, File location) { + return null; } } diff --git a/src/main/java/ome/formats/importer/transfers/CopyFileTransfer.java b/src/main/java/ome/formats/importer/transfers/CopyFileTransfer.java index 7a871e14f..dd091881c 100644 --- a/src/main/java/ome/formats/importer/transfers/CopyFileTransfer.java +++ b/src/main/java/ome/formats/importer/transfers/CopyFileTransfer.java @@ -21,44 +21,26 @@ import java.io.File; import java.io.IOException; -import java.util.ArrayList; +import java.nio.file.Files; +import java.nio.file.Paths; import java.util.List; /** - * Local-only file transfer mechanism which makes use of the plaform - * copy command. - * - * This is only useful where the commands "cp source target" (Unix) or - * "copy source target" (Windows) will work. + * Local-only file transfer mechanism. * * @since 5.0.7 */ public class CopyFileTransfer extends AbstractExecFileTransfer { /** - * Executes "cp file location" (Unix) or "cp file location" (Windows) - * and fails on non-0 return codes. + * Copies a file from one location to another. * * @param file File to be copied * @param location Location to copy to. * @throws IOException */ - protected ProcessBuilder createProcessBuilder(File file, File location) { - ProcessBuilder pb = new ProcessBuilder(); - List args = new ArrayList(); - if (isWindows()) { - args.add("cmd"); - args.add("/c"); - args.add("cp"); - args.add(file.getAbsolutePath()); - args.add(location.getAbsolutePath()); - } else { - args.add("cp"); - args.add(file.getAbsolutePath()); - args.add(location.getAbsolutePath()); - } - pb.command(args); - return pb; + protected void exec(File file, File location) throws IOException { + Files.copy(Paths.get(file.getAbsolutePath()), Paths.get(location.getAbsolutePath())); } /** diff --git a/src/main/java/ome/formats/importer/transfers/HardlinkFileTransfer.java b/src/main/java/ome/formats/importer/transfers/HardlinkFileTransfer.java index 2a2f3b93d..17d497bbe 100644 --- a/src/main/java/ome/formats/importer/transfers/HardlinkFileTransfer.java +++ b/src/main/java/ome/formats/importer/transfers/HardlinkFileTransfer.java @@ -21,46 +21,26 @@ import java.io.File; import java.io.IOException; -import java.util.ArrayList; +import java.nio.file.Files; +import java.nio.file.Paths; import java.util.List; /** * Local-only file transfer mechanism which makes use of hard-linking. * - * This is only useful where the commands "ln source target" (Unix) or - * "mklink /H target source" (Windows) will work. - * * @since 5.0 */ public class HardlinkFileTransfer extends AbstractExecFileTransfer { /** - * Executes "ln file location" (Unix) or "mklink /H location file" (Windows) - * and fails on non-0 return codes. + * Creates a hard link. * * @param file File to be copied. * @param location Location to copy to. * @throws IOException - * */ - // TODO Java7: replace ln once Java6 is dropped - protected ProcessBuilder createProcessBuilder(File file, File location) { - ProcessBuilder pb = new ProcessBuilder(); - List args = new ArrayList(); - if (isWindows()) { - args.add("cmd"); - args.add("/c"); - args.add("mklink"); - args.add("/H"); - args.add(location.getAbsolutePath()); - args.add(file.getAbsolutePath()); - } else { - args.add("ln"); - args.add(file.getAbsolutePath()); - args.add(location.getAbsolutePath()); - } - pb.command(args); - return pb; + protected void exec(File file, File location) throws IOException { + Files.createLink(Paths.get(location.getAbsolutePath()), Paths.get(file.getAbsolutePath())); } /** diff --git a/src/main/java/ome/formats/importer/transfers/SymlinkFileTransfer.java b/src/main/java/ome/formats/importer/transfers/SymlinkFileTransfer.java index baac6e6e1..a09f0893f 100644 --- a/src/main/java/ome/formats/importer/transfers/SymlinkFileTransfer.java +++ b/src/main/java/ome/formats/importer/transfers/SymlinkFileTransfer.java @@ -21,45 +21,26 @@ import java.io.File; import java.io.IOException; -import java.util.ArrayList; +import java.nio.file.Files; +import java.nio.file.Paths; import java.util.List; /** * Local-only file transfer mechanism which makes use of soft-linking. * - * This is only useful where the commands "ln -s source target" (Unix) or - * "mklink target source" (Windows) will work. - * * @since 5.0 */ public class SymlinkFileTransfer extends AbstractExecFileTransfer { /** - * Executes "ln -s file location" (Unix) or "mklink location file" (Windows) - * and fails on non-0 return codes. + * Creates a symlink. * * @param file File to be copied. * @param location Location to copy to. * @throws IOException */ - // TODO Java7: replace ln once Java6 is dropped - protected ProcessBuilder createProcessBuilder(File file, File location) { - ProcessBuilder pb = new ProcessBuilder(); - List args = new ArrayList(); - if (isWindows()) { - args.add("cmd"); - args.add("/c"); - args.add("mklink"); - args.add(location.getAbsolutePath()); - args.add(file.getAbsolutePath()); - } else { - args.add("ln"); - args.add("-s"); - args.add(file.getAbsolutePath()); - args.add(location.getAbsolutePath()); - } - pb.command(args); - return pb; + protected void exec(File file, File location) throws IOException { + Files.createSymbolicLink(Paths.get(location.getAbsolutePath()), Paths.get(file.getAbsolutePath())); } /**