Skip to content
Original file line number Diff line number Diff line change
Expand Up @@ -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
*/
Expand All @@ -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);
Expand Down Expand Up @@ -151,14 +148,24 @@ 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
* @throws IOException for problems with the source file
*/
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;
Expand Down Expand Up @@ -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;
}

}
30 changes: 6 additions & 24 deletions src/main/java/ome/formats/importer/transfers/CopyFileTransfer.java
Original file line number Diff line number Diff line change
Expand Up @@ -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<String> args = new ArrayList<String>();
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()));
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<String> args = new ArrayList<String>();
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()));
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<String> args = new ArrayList<String>();
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()));
}

/**
Expand Down
Loading