Skip to content
Snippets Groups Projects
Commit 44fe57d6 authored by ribeaudc's avatar ribeaudc
Browse files

add:

- new parameter to addFile(File)
change:
- copyFile -> extractFile (back to previous version)
remove:
- removeFile method

SVN: 2220
parent 30f6511b
No related branches found
No related tags found
No related merge requests found
......@@ -48,10 +48,11 @@ public interface IDirectory extends INode, Iterable<INode>
* Adds the specified real file to this directory. The content of <code>file</code> will be copied. If it is a
* folder also its complete content including all subfolders will be copied.
*
* @param move whether given <var>file</var> should be copied or moved.
* @return the new node. It will be a {@link ILink} if <code>file</code> is a symbolic link, a {@link IDirectory}
* if <code>file</code> is a folder, or {@link IFile} if <code>file</code> is a plain file.
*/
public INode addFile(File file) throws UserFailureException, EnvironmentFailureException;
public INode addFile(final File file, final boolean move) throws UserFailureException, EnvironmentFailureException;
/**
* Adds a plain file named <code>key</code> with content <code>value</code> to this directory.
......
......@@ -39,20 +39,14 @@ public interface INode
public IDirectory tryToGetParent();
/**
* Copies this node to the specified directory of the file system. All descendants are also copied.
* Extracts this node to the specified directory of the file system.
* <p>
* All descendants are also extracted. This is a copy operation.
* </p>
*
* @throws UserFailureException if this or a descended node is a link referring to a node which is not this node or
* a descended node.
* @throws EnvironmentFailureException if extraction causes an IOException.
*/
public void copyTo(File directory) throws UserFailureException, EnvironmentFailureException;
/**
* Moves this node to the specified directory of the file system. All descendants are also moved.
*
* @throws UserFailureException if this or a descended node is a link referring to a node which is not this node or
* a descended node.
* @throws EnvironmentFailureException if extraction causes an IOException.
*/
public void moveTo(File directory) throws UserFailureException, EnvironmentFailureException;
public void extractTo(final File directory) throws UserFailureException, EnvironmentFailureException;
}
......@@ -27,7 +27,7 @@ import ch.systemsx.cisd.common.exceptions.UserFailureException;
*/
abstract class AbstractNode implements INode
{
protected File nodeFile;
protected final File nodeFile;
AbstractNode(File file)
{
......
......@@ -20,6 +20,7 @@ import java.io.IOException;
import java.util.Iterator;
import org.apache.commons.io.FileUtils;
import org.apache.log4j.Logger;
import ch.systemsx.cisd.bds.storage.IDirectory;
import ch.systemsx.cisd.bds.storage.IFile;
......@@ -27,6 +28,8 @@ import ch.systemsx.cisd.bds.storage.ILink;
import ch.systemsx.cisd.bds.storage.INode;
import ch.systemsx.cisd.common.exceptions.EnvironmentFailureException;
import ch.systemsx.cisd.common.exceptions.UserFailureException;
import ch.systemsx.cisd.common.logging.LogCategory;
import ch.systemsx.cisd.common.logging.LogFactory;
import ch.systemsx.cisd.common.utilities.FileUtilities;
/**
......@@ -34,6 +37,9 @@ import ch.systemsx.cisd.common.utilities.FileUtilities;
*/
class Directory extends AbstractNode implements IDirectory
{
private static final Logger operationLog = LogFactory.getLogger(LogCategory.OPERATION, AbstractNode.class);
Directory(java.io.File directory)
{
super(directory);
......@@ -83,11 +89,31 @@ class Directory extends AbstractNode implements IDirectory
return new File(file);
}
public INode addFile(java.io.File file) throws UserFailureException, EnvironmentFailureException
public INode addFile(final java.io.File file, final boolean move) throws UserFailureException,
EnvironmentFailureException
{
INode node = NodeFactory.createNode(file);
node.moveTo(nodeFile);
return node;
final java.io.File newFile = new java.io.File(nodeFile, file.getName());
if (move)
{
moveFileToDirectory(file, nodeFile);
} else
{
try
{
if (file.isDirectory())
{
FileUtils.copyDirectory(file, newFile);
} else
{
FileUtils.copyFile(file, newFile);
}
} catch (IOException ex)
{
throw EnvironmentFailureException.fromTemplate(ex, "Couldn't not copy file '%s' to directory '%s'.",
file, nodeFile.getAbsolutePath());
}
}
return NodeFactory.createNode(newFile);
}
public ILink addLink(String name, INode node)
......@@ -125,7 +151,7 @@ class Directory extends AbstractNode implements IDirectory
};
}
public final void copyTo(final java.io.File directory) throws EnvironmentFailureException
public final void extractTo(final java.io.File directory) throws EnvironmentFailureException
{
assert directory != null;
try
......@@ -138,30 +164,27 @@ class Directory extends AbstractNode implements IDirectory
}
}
public final void moveTo(java.io.File directory) throws EnvironmentFailureException
private final static void moveFileToDirectory(final java.io.File source, final java.io.File directory)
throws EnvironmentFailureException
{
assert directory != null;
directory.mkdirs();
final java.io.File destination = new java.io.File(directory, getName());
assert source != null;
assert directory != null && directory.isDirectory();
final java.io.File destination = new java.io.File(directory, source.getName());
if (destination.exists() == false)
{
// Note that 'renameTo' does not change 'nodeFile' path
final boolean successful = nodeFile.renameTo(destination);
final boolean successful = source.renameTo(destination);
if (successful == false)
{
throw EnvironmentFailureException.fromTemplate("Couldn't not move directory '%s' to directory '%s'.",
nodeFile.getAbsolutePath(), directory.getAbsolutePath());
throw EnvironmentFailureException.fromTemplate("Couldn't not move file '%s' to directory '%s'.", source
.getAbsolutePath(), directory.getAbsolutePath());
}
assert nodeFile.exists() == false;
}
if (nodeFile.equals(destination) == false)
} else
{
nodeFile = destination;
}
// Update children
for (INode node : this)
{
node.moveTo(destination);
if (operationLog.isInfoEnabled())
{
operationLog.info(String.format("Destination file '%s' already exists. Will not overwrite", destination
.getAbsolutePath()));
}
}
}
}
......@@ -58,8 +58,9 @@ class File extends AbstractNode implements IFile
return FileUtilities.loadToString(nodeFile);
}
public final void copyTo(final java.io.File directory) throws EnvironmentFailureException
public final void extractTo(final java.io.File directory) throws EnvironmentFailureException
{
assert directory != null && directory.isDirectory();
try
{
FileUtils.copyFileToDirectory(nodeFile, directory);
......@@ -69,24 +70,4 @@ class File extends AbstractNode implements IFile
nodeFile.getAbsolutePath(), directory.getAbsolutePath());
}
}
public final void moveTo(java.io.File directory) throws EnvironmentFailureException
{
assert directory != null;
final java.io.File destination = new java.io.File(directory, getName());
if (destination.exists() == false)
{
// Note that 'renameTo' does not change 'nodeFile' path
final boolean successful = nodeFile.renameTo(destination);
if (successful == false)
{
throw EnvironmentFailureException.fromTemplate("Couldn't not move file '%s' to directory '%s'.",
nodeFile.getAbsolutePath(), directory.getAbsolutePath());
}
}
if (nodeFile.equals(destination) == false)
{
nodeFile = destination;
}
}
}
......@@ -62,15 +62,8 @@ class Link implements ILink
return reference;
}
public void copyTo(File directory) throws UserFailureException, EnvironmentFailureException
public void extractTo(final File directory) throws UserFailureException, EnvironmentFailureException
{
// TODO Auto-generated method stub
}
public void moveTo(File directory) throws UserFailureException, EnvironmentFailureException
{
// TODO Auto-generated method stub
}
}
......@@ -93,7 +93,7 @@ public class DirectoryTest extends StorageTestCase
}
@Test
public void testCopyTo()
public void testExtractTo()
{
File dir = new File(TEST_DIR, "dir");
dir.mkdirs();
......@@ -103,7 +103,7 @@ public class DirectoryTest extends StorageTestCase
subdir.addKeyValuePair("p2", "property 2");
File destination = new File(TEST_DIR, "destination");
assertFalse(destination.exists());
directory.copyTo(destination);
directory.extractTo(destination);
assertTrue(destination.exists());
File copiedDir = new File(destination, "dir");
assertTrue(copiedDir.exists());
......@@ -115,29 +115,6 @@ public class DirectoryTest extends StorageTestCase
assertEquals(true, new File(TEST_DIR, "dir").exists());
}
@Test
public void testMoveTo()
{
File dir = new File(TEST_DIR, "dir");
dir.mkdirs();
Directory directory = new Directory(dir);
directory.addKeyValuePair("p1", "property 1");
IDirectory subdir = directory.makeDirectory("subdir");
subdir.addKeyValuePair("p2", "property 2");
File destination = new File(TEST_DIR, "destination");
assertFalse(destination.exists());
directory.moveTo(destination);
assertTrue(destination.exists());
File copiedDir = new File(destination, "dir");
assertTrue(copiedDir.exists());
assertEquals("property 1\n", FileUtilities.loadToString(new File(copiedDir, "p1")));
File copiedSubDir = new File(copiedDir, "subdir");
assertTrue(copiedSubDir.isDirectory());
assertEquals("property 2\n", FileUtilities.loadToString(new File(copiedSubDir, "p2")));
// Source directory does no longer exist
assertEquals(false, new File(TEST_DIR, "dir").exists());
}
@Test
public void testAddFile()
{
......@@ -151,7 +128,7 @@ public class DirectoryTest extends StorageTestCase
dest.mkdir();
Directory directory = new Directory(dest);
INode copiedDir = directory.addFile(dir);
INode copiedDir = directory.addFile(dir, false);
assertEquals("dir", copiedDir.getName());
assertTrue(copiedDir instanceof IDirectory);
......
......@@ -33,7 +33,7 @@ public class FileTest extends StorageTestCase
java.io.File file = new java.io.File(TEST_DIR, "test.txt");
FileUtilities.writeToFile(file, "Hello\nworld!\n");
File stringFile = new File(file);
assertEquals("test.txt", stringFile.getName());
assertEquals("Hello\nworld!\n", stringFile.getStringContent());
assertEquals("Hello\nworld!\n", new String(stringFile.getBinaryContent()));
......@@ -47,7 +47,8 @@ public class FileTest extends StorageTestCase
File stringFile = new File(file);
java.io.File subdir = new java.io.File(TEST_DIR, "subdir");
stringFile.copyTo(subdir);
subdir.mkdir();
stringFile.extractTo(subdir);
assertEquals("Hello\nworld!\n", FileUtilities.loadToString(new java.io.File(subdir, stringFile.getName())));
}
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment