diff --git a/bds/source/java/ch/systemsx/cisd/bds/AbstractDataStructure.java b/bds/source/java/ch/systemsx/cisd/bds/AbstractDataStructure.java index 0e8f1c9c0ca168b028613c2c0089c4ad7d1fd5ee..684ecd3d09df4ca68016d4c595629775f18244a6 100644 --- a/bds/source/java/ch/systemsx/cisd/bds/AbstractDataStructure.java +++ b/bds/source/java/ch/systemsx/cisd/bds/AbstractDataStructure.java @@ -39,7 +39,7 @@ public abstract class AbstractDataStructure implements IHasVersion public void load() { - storage.load(); + storage.mount(); Version loadedVersion = Version.loadFrom(root); if (getVersion().isBackwardsCompatibleWith(loadedVersion) == false) { @@ -51,6 +51,6 @@ public abstract class AbstractDataStructure implements IHasVersion public void save() { getVersion().saveTo(root); - storage.save(); + storage.unmount(); } } diff --git a/bds/source/java/ch/systemsx/cisd/bds/Container.java b/bds/source/java/ch/systemsx/cisd/bds/Container.java index d161d843fa0f06ed5ae2047171cd14bc5e498299..a3e88af305ae453d8cd3708ef5816fc59540ee33 100644 --- a/bds/source/java/ch/systemsx/cisd/bds/Container.java +++ b/bds/source/java/ch/systemsx/cisd/bds/Container.java @@ -42,7 +42,7 @@ public class Container public AbstractDataStructure load(String name) { IStorage storage = createStorage(name); - storage.load(); + storage.mount(); Version version = Version.loadFrom(storage.getRoot()); return DataStructureFactory.createDataStructure(storage, version); } diff --git a/bds/source/java/ch/systemsx/cisd/bds/storage/IFile.java b/bds/source/java/ch/systemsx/cisd/bds/storage/IFile.java index 6a3b733bbb2cd21eae961f5b6ab77ab03f66add7..ffe6e069e413a36596a5e0377e8af08f6334cfd3 100644 --- a/bds/source/java/ch/systemsx/cisd/bds/storage/IFile.java +++ b/bds/source/java/ch/systemsx/cisd/bds/storage/IFile.java @@ -17,7 +17,7 @@ package ch.systemsx.cisd.bds.storage; /** - * Role of a file with a value (content) of type <code>T</code>. + * Role of a leaf node representing a file with some content. * * @author Franz-Josef Elmer */ diff --git a/bds/source/java/ch/systemsx/cisd/bds/storage/ILink.java b/bds/source/java/ch/systemsx/cisd/bds/storage/ILink.java index c533b230cb2100540b96b9423c76b26c3c5ed7a5..ee0f5074e9a1d43035d7024709c20cca42b8818d 100644 --- a/bds/source/java/ch/systemsx/cisd/bds/storage/ILink.java +++ b/bds/source/java/ch/systemsx/cisd/bds/storage/ILink.java @@ -17,11 +17,14 @@ package ch.systemsx.cisd.bds.storage; /** - * + * Node representing a symbolic link. * * @author Franz-Josef Elmer */ public interface ILink extends INode { + /** + * Returns the node referred by this node. + */ public INode getReference(); } diff --git a/bds/source/java/ch/systemsx/cisd/bds/storage/IStorage.java b/bds/source/java/ch/systemsx/cisd/bds/storage/IStorage.java index b02fd16a11c6885d65633e11f15c17fdc5c14097..98cf5ae759362c9b360f94223aa255af7fc5e1a9 100644 --- a/bds/source/java/ch/systemsx/cisd/bds/storage/IStorage.java +++ b/bds/source/java/ch/systemsx/cisd/bds/storage/IStorage.java @@ -16,16 +16,29 @@ package ch.systemsx.cisd.bds.storage; +import ch.systemsx.cisd.common.exceptions.UserFailureException; + /** - * + * Abstraction of a hierarchical storage. * * @author Franz-Josef Elmer */ public interface IStorage { - public void load(); + /** + * Mounts this storage. May perform some initializations. Should be called before calling {@link #getRoot()}. + */ + public void mount(); - public IDirectory getRoot(); + /** + * Returns root directory of this storage. + * + * @throws UserFailureException if invoked before {@link #mount()} or after {@link #unmount()}. + */ + public IDirectory getRoot() throws UserFailureException; - public void save(); + /** + * Unmounts this storage. May perform some finalization (e.g. make cached data persistent). + */ + public void unmount(); } diff --git a/bds/source/java/ch/systemsx/cisd/bds/storage/filesystem/AbstractNode.java b/bds/source/java/ch/systemsx/cisd/bds/storage/filesystem/AbstractNode.java index a07ca45f6a43072ebe2569bde78d38f9b170e0ff..073cd777486c9dc1d5e82b675415cf5489555122 100644 --- a/bds/source/java/ch/systemsx/cisd/bds/storage/filesystem/AbstractNode.java +++ b/bds/source/java/ch/systemsx/cisd/bds/storage/filesystem/AbstractNode.java @@ -20,6 +20,7 @@ import java.io.File; import ch.systemsx.cisd.bds.storage.IDirectory; import ch.systemsx.cisd.bds.storage.INode; +import ch.systemsx.cisd.common.exceptions.UserFailureException; /** * @@ -32,8 +33,14 @@ abstract class AbstractNode implements INode AbstractNode(File file) { - assert file != null : "Unspecified file"; - assert file.exists() : "Non existing file " + file; + if (file == null) + { + throw new UserFailureException("Unspecified file"); + } + if (file.exists() == false) + { + throw new UserFailureException("Non existing file " + file); + } this.nodeFile = file; } diff --git a/bds/source/java/ch/systemsx/cisd/bds/storage/filesystem/Directory.java b/bds/source/java/ch/systemsx/cisd/bds/storage/filesystem/Directory.java index 0ddf8d91dba7cb6c9421eae8d0bf61821bc8ca39..d3b54e72311486c1001059f93c41df6442d41d5a 100644 --- a/bds/source/java/ch/systemsx/cisd/bds/storage/filesystem/Directory.java +++ b/bds/source/java/ch/systemsx/cisd/bds/storage/filesystem/Directory.java @@ -34,7 +34,10 @@ class Directory extends AbstractNode implements IDirectory Directory(java.io.File directory) { super(directory); - assert directory.isDirectory() : "Not a directory: " + directory.getAbsolutePath(); + if (directory.isDirectory() == false) + { + throw new UserFailureException("Not a directory: " + directory.getAbsolutePath()); + } } public INode getNode(String name) diff --git a/bds/source/java/ch/systemsx/cisd/bds/storage/filesystem/FileStorage.java b/bds/source/java/ch/systemsx/cisd/bds/storage/filesystem/FileStorage.java index d5e74a49073344e67d220dd2064cc9b79e72e478..4a51cd3692e8e5bb53a78c029fa23cc3a1980c55 100644 --- a/bds/source/java/ch/systemsx/cisd/bds/storage/filesystem/FileStorage.java +++ b/bds/source/java/ch/systemsx/cisd/bds/storage/filesystem/FileStorage.java @@ -20,16 +20,24 @@ import java.io.File; import ch.systemsx.cisd.bds.storage.IDirectory; import ch.systemsx.cisd.bds.storage.IStorage; +import ch.systemsx.cisd.common.exceptions.UserFailureException; /** - * + * Implementation of {@link IStorage} based on the file system. * * @author Franz-Josef Elmer */ public class FileStorage implements IStorage { - private Directory root; + private final Directory root; + + private boolean mounted; + /** + * Creates an instance with the specified folder as the root directory. + * + * @throws UserFailureException if <code>folder</code> does not exist or is not a directory in the file system. + */ public FileStorage(File folder) { root = new Directory(folder); @@ -37,15 +45,21 @@ public class FileStorage implements IStorage public IDirectory getRoot() { + if (mounted == false) + { + throw new UserFailureException("Can not get root of an unmounted storage."); + } return root; } - public void load() + public void mount() { + mounted = true; } - public void save() + public void unmount() { + mounted = false; } } diff --git a/bds/source/java/ch/systemsx/cisd/bds/storage/hdf5/HDF5Storage.java b/bds/source/java/ch/systemsx/cisd/bds/storage/hdf5/HDF5Storage.java index 55f2a8a9c07ab409f80645fe027bdc557418357c..78b523a53279e2abe39014828a5be3b5a3e38a8c 100644 --- a/bds/source/java/ch/systemsx/cisd/bds/storage/hdf5/HDF5Storage.java +++ b/bds/source/java/ch/systemsx/cisd/bds/storage/hdf5/HDF5Storage.java @@ -22,7 +22,7 @@ import ch.systemsx.cisd.bds.storage.IDirectory; import ch.systemsx.cisd.bds.storage.IStorage; /** - * + * Storage based on HDF5. * * @author Franz-Josef Elmer */ @@ -39,13 +39,13 @@ public class HDF5Storage implements IStorage return null; } - public void load() + public void mount() { // TODO Auto-generated method stub } - public void save() + public void unmount() { // TODO Auto-generated method stub diff --git a/bds/sourceTest/java/ch/systemsx/cisd/bds/storage/filesystem/FileStorageTest.java b/bds/sourceTest/java/ch/systemsx/cisd/bds/storage/filesystem/FileStorageTest.java index 386431cf05b99cdceb8e5d49fb89a1daa882f110..67d2f5b687d8d24bf1324ad8fc0a8b3969dd20dd 100644 --- a/bds/sourceTest/java/ch/systemsx/cisd/bds/storage/filesystem/FileStorageTest.java +++ b/bds/sourceTest/java/ch/systemsx/cisd/bds/storage/filesystem/FileStorageTest.java @@ -16,9 +16,13 @@ package ch.systemsx.cisd.bds.storage.filesystem; +import static org.testng.AssertJUnit.assertEquals; + import org.testng.AssertJUnit; import org.testng.annotations.Test; +import ch.systemsx.cisd.common.exceptions.UserFailureException; + /** * * @@ -30,6 +34,37 @@ public class FileStorageTest extends StorageTestCase public void testGetRoot() { FileStorage fileStorage = new FileStorage(TEST_DIR); - AssertJUnit.assertEquals(TEST_DIR.getName(), fileStorage.getRoot().getName()); + fileStorage.mount(); + assertEquals(TEST_DIR.getName(), fileStorage.getRoot().getName()); + } + + @Test + public void testGetRootOfNeverMountedStorage() + { + FileStorage fileStorage = new FileStorage(TEST_DIR); + try + { + fileStorage.getRoot(); + AssertJUnit.fail("UserFailureException because storage isn't mounted."); + } catch (UserFailureException e) + { + assertEquals("Can not get root of an unmounted storage.", e.getMessage()); + } + } + + @Test + public void testGetRootOfUnMountedStorage() + { + FileStorage fileStorage = new FileStorage(TEST_DIR); + fileStorage.mount(); + fileStorage.unmount(); + try + { + fileStorage.getRoot(); + AssertJUnit.fail("UserFailureException because storage isn't mounted."); + } catch (UserFailureException e) + { + assertEquals("Can not get root of an unmounted storage.", e.getMessage()); + } } }