Author: bleny Date: 2010-05-18 15:45:05 +0200 (Tue, 18 May 2010) New Revision: 41 Url: http://nuiton.org/repositories/revision/diswork/41 Log: read, write, mkdir, exists Added: trunk/diswork-fs/src/main/java/org/nuiton/disworkfs/nodes/ trunk/diswork-fs/src/main/java/org/nuiton/disworkfs/nodes/DirectoryNode.java trunk/diswork-fs/src/main/java/org/nuiton/disworkfs/nodes/FileNode.java trunk/diswork-fs/src/main/java/org/nuiton/disworkfs/nodes/Node.java trunk/diswork-fs/src/main/java/org/nuiton/disworkfs/nodes/NodeType.java trunk/diswork-fs/src/main/java/org/nuiton/disworkfs/nodes/SymLinkNode.java trunk/diswork-fs/src/main/java/org/nuiton/disworkfs/nodes/package-info.java trunk/diswork-fs/src/main/java/org/nuiton/disworkfs/operations/ trunk/diswork-fs/src/main/java/org/nuiton/disworkfs/operations/Operation.java trunk/diswork-fs/src/main/java/org/nuiton/disworkfs/operations/Read.java trunk/diswork-fs/src/main/java/org/nuiton/disworkfs/operations/Write.java trunk/diswork-fs/src/main/java/org/nuiton/disworkfs/storage/ trunk/diswork-fs/src/main/java/org/nuiton/disworkfs/storage/Storage.java trunk/diswork-fs/src/main/java/org/nuiton/disworkfs/storage/StorageStub.java Removed: trunk/diswork-fs/src/main/java/org/nuiton/disworkfs/transport/ Modified: trunk/diswork-fs/ trunk/diswork-fs/pom.xml trunk/diswork-fs/src/main/java/org/nuiton/disworkfs/DistributedFileSystem.java trunk/diswork-fs/src/main/java/org/nuiton/disworkfs/DisworkFileSystem.java trunk/diswork-fs/src/main/resources/log4j.properties trunk/diswork-fs/src/test/java/org/nuiton/disworkfs/DistributedFileSystemTest.java Property changes on: trunk/diswork-fs ___________________________________________________________________ Modified: svn:ignore - target *.iml *.ipr *.iws .classpath .project + target *.iml *.ipr *.iws .classpath .project .settings Modified: trunk/diswork-fs/pom.xml =================================================================== --- trunk/diswork-fs/pom.xml 2010-05-17 09:58:49 UTC (rev 40) +++ trunk/diswork-fs/pom.xml 2010-05-18 13:45:05 UTC (rev 41) @@ -21,10 +21,6 @@ <artifactId>log4j</artifactId> </dependency> <dependency> - <groupId>jgroups</groupId> - <artifactId>jgroups</artifactId> - </dependency> - <dependency> <groupId>commons-io</groupId> <artifactId>commons-io</artifactId> </dependency> Modified: trunk/diswork-fs/src/main/java/org/nuiton/disworkfs/DistributedFileSystem.java =================================================================== --- trunk/diswork-fs/src/main/java/org/nuiton/disworkfs/DistributedFileSystem.java 2010-05-17 09:58:49 UTC (rev 40) +++ trunk/diswork-fs/src/main/java/org/nuiton/disworkfs/DistributedFileSystem.java 2010-05-18 13:45:05 UTC (rev 41) @@ -1,10 +1,12 @@ package org.nuiton.disworkfs; +import java.io.Closeable; import java.io.FileNotFoundException; import java.io.IOException; import java.io.InputStream; +import java.net.URISyntaxException; -public interface DistributedFileSystem { +public interface DistributedFileSystem extends Closeable { /** * checks the existence of a file on the virtual FS @@ -43,5 +45,7 @@ public InputStream read(String path) throws FileNotFoundException, IOException; + public void mkdir(String path) throws IOException; + } Modified: trunk/diswork-fs/src/main/java/org/nuiton/disworkfs/DisworkFileSystem.java =================================================================== --- trunk/diswork-fs/src/main/java/org/nuiton/disworkfs/DisworkFileSystem.java 2010-05-17 09:58:49 UTC (rev 40) +++ trunk/diswork-fs/src/main/java/org/nuiton/disworkfs/DisworkFileSystem.java 2010-05-18 13:45:05 UTC (rev 41) @@ -4,40 +4,187 @@ import java.io.IOException; import java.io.InputStream; +import org.apache.commons.io.FilenameUtils; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; +import org.nuiton.disworkfs.nodes.DirectoryNode; +import org.nuiton.disworkfs.nodes.FileNode; +import org.nuiton.disworkfs.nodes.Node; +import org.nuiton.disworkfs.operations.Read; +import org.nuiton.disworkfs.operations.Write; +import org.nuiton.disworkfs.storage.Storage; +import org.nuiton.disworkfs.storage.StorageStub; -import sun.reflect.generics.reflectiveObjects.NotImplementedException; - public class DisworkFileSystem implements DistributedFileSystem { - + protected Storage storage; + private static final Log log = LogFactory.getLog(DisworkFileSystem.class); public DisworkFileSystem(DisworkConfig disworkConfig) { - + storage = new StorageStub(); + + DirectoryNode root = new DirectoryNode(); + storage.put("/", root); + } - + @Override public boolean exists(String path) { - // TODO - throw new NotImplementedException(); + boolean exists = storage.containsKey(path); + log.info("exists(" + path + ") returns " + exists); + return exists; } @Override public InputStream read(String path) throws FileNotFoundException, IOException { - // TODO - throw new NotImplementedException(); + + log.info("trying read(" + path + ")"); + + if (exists(path)) { + Node target = storage.get(path); + Read read = new Read(storage); + try { + target.operation(read); + return read.getInputStream(); + } catch (Exception eee) { + throw new IOException("diswork intern error", eee); + } + } else { + log.warn("file not found : " + path); + throw new FileNotFoundException(); + } } + + public void create(String path) throws IOException { + + // First check everything is OK with parent dir + log.info("trying create(" + path + ")"); + + if (exists(path)) { + + throw new IOException("can't create " + path + ", already exists"); + + } else { + + String parentPath = FilenameUtils.getFullPathNoEndSeparator(path); + + // needed if parent dir is root + if (parentPath.equals("")) { + parentPath = "/"; + } + + Node parentNode = storage.get(parentPath); + + if (parentNode == null) { + throw new IOException("can't create file " + path + + ", " + parentPath + " doesn't exists"); + } + + if (parentNode.isDirectory()) { + + DirectoryNode parent = (DirectoryNode) parentNode; + + String fileName = FilenameUtils.getName(path); + log.info("adding \"" + fileName + "\" to " + parentPath + " (" + parent + ")"); + + parent.add(fileName); + + storage.put(path, new FileNode(null)); + + } else { + throw new IOException("can't write : " + parentPath + "is not a directory"); + } + } + + } + @Override public void write(String path, InputStream source) throws IOException { - // TODO - throw new NotImplementedException(); + write(path, source, true); } + + public void write(String path, InputStream source, boolean forceCreate) + throws IOException { - public void close() { + if (! exists(path)) { + if (forceCreate) { + create(path); + } else { + throw new IOException("can't write not created file"); + } + } + + try { + Write write = new Write(storage, source); + + Node target = storage.get(path); + target.operation(write); + } catch (Exception eee) { + throw new IOException("diswork intern error", eee); + } } + @Override + public void close() throws IOException { + storage.close(); + } + + @Override + public void mkdir(String path) throws IOException { + + // First check everything is OK with parent dir + log.info("trying create(" + path + ")"); + + if (exists(path)) { + + throw new IOException("can't create " + path + ", already exists"); + + } else { + + String parentPath = FilenameUtils.getFullPathNoEndSeparator(path); + + // needed if parent dir is root + if (parentPath.equals("")) { + parentPath = "/"; + } + + Node parentNode = storage.get(parentPath); + + if (parentNode == null) { + throw new IOException("can't create " + path + + " directory " + parentPath + " doesn't exists"); + } + + if (parentNode.isDirectory()) { + + DirectoryNode parent = (DirectoryNode) parentNode; + + String fileName = FilenameUtils.getName(path); + + log.info("adding \"" + fileName + "\" to " + parentPath + " (" + parent + ")"); + + parent.add(fileName); + + storage.put(path, new DirectoryNode()); + + } else { + + throw new IOException("can't write : " + parentPath + " is not a directory"); + + } + } + + } + + public void dump() { + + for (String path : storage.keySet()) { + System.out.println(path + " → " + storage.get(path)); + } + + } + } Added: trunk/diswork-fs/src/main/java/org/nuiton/disworkfs/nodes/DirectoryNode.java =================================================================== --- trunk/diswork-fs/src/main/java/org/nuiton/disworkfs/nodes/DirectoryNode.java (rev 0) +++ trunk/diswork-fs/src/main/java/org/nuiton/disworkfs/nodes/DirectoryNode.java 2010-05-18 13:45:05 UTC (rev 41) @@ -0,0 +1,133 @@ +package org.nuiton.disworkfs.nodes; + +import java.util.ArrayList; +import java.util.Collection; +import java.util.Iterator; +import java.util.List; +import java.util.ListIterator; + +import org.nuiton.disworkfs.operations.Operation; + +public class DirectoryNode extends Node implements List<String> { + + protected List<String> childs; + + public DirectoryNode() { + super(NodeType.DIRECTORY); + childs = new ArrayList<String>(); + } + + @Override + public void operation(Operation operation) throws Exception { + operation.operation(this); + } + + // generated delegation code + + public void add(int index, String element) { + childs.add(index, element); + } + + public boolean add(String e) { + return childs.add(e); + } + + public boolean addAll(Collection<? extends String> c) { + return childs.addAll(c); + } + + public boolean addAll(int index, Collection<? extends String> c) { + return childs.addAll(index, c); + } + + public void clear() { + childs.clear(); + } + + public boolean contains(Object o) { + return childs.contains(o); + } + + public boolean containsAll(Collection<?> c) { + return childs.containsAll(c); + } + + public boolean equals(Object o) { + return childs.equals(o); + } + + public String get(int index) { + return childs.get(index); + } + + public int hashCode() { + return childs.hashCode(); + } + + public int indexOf(Object o) { + return childs.indexOf(o); + } + + public boolean isEmpty() { + return childs.isEmpty(); + } + + public Iterator<String> iterator() { + return childs.iterator(); + } + + public int lastIndexOf(Object o) { + return childs.lastIndexOf(o); + } + + public ListIterator<String> listIterator() { + return childs.listIterator(); + } + + public ListIterator<String> listIterator(int index) { + return childs.listIterator(index); + } + + public String remove(int index) { + return childs.remove(index); + } + + public boolean remove(Object o) { + return childs.remove(o); + } + + public boolean removeAll(Collection<?> c) { + return childs.removeAll(c); + } + + public boolean retainAll(Collection<?> c) { + return childs.retainAll(c); + } + + public String set(int index, String element) { + return childs.set(index, element); + } + + public int size() { + return childs.size(); + } + + public List<String> subList(int fromIndex, int toIndex) { + return childs.subList(fromIndex, toIndex); + } + + public Object[] toArray() { + return childs.toArray(); + } + + public <T> T[] toArray(T[] a) { + return childs.toArray(a); + } + + @Override + public String toString() { + return "DirectoryNode (size = " + childs.size() + ")"; + } + + +} Added: trunk/diswork-fs/src/main/java/org/nuiton/disworkfs/nodes/FileNode.java =================================================================== --- trunk/diswork-fs/src/main/java/org/nuiton/disworkfs/nodes/FileNode.java (rev 0) +++ trunk/diswork-fs/src/main/java/org/nuiton/disworkfs/nodes/FileNode.java 2010-05-18 13:45:05 UTC (rev 41) @@ -0,0 +1,63 @@ +package org.nuiton.disworkfs.nodes; + +import java.security.MessageDigest; +import java.security.NoSuchAlgorithmException; + +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; +import org.nuiton.disworkfs.operations.Operation; +import org.nuiton.util.StringUtil; + +public class FileNode extends Node { + + protected byte[] data; + protected byte[] checkSum; + + private Log log = LogFactory.getLog(FileNode.class); + + public FileNode(byte[] data) { + super(NodeType.FILE); + this.setData(data); + } + + public byte[] getData() { + return data; + } + + public byte[] getCheckSum() { + return checkSum; + } + + public void setData(byte[] data) { + this.data = data; + + // compute checkSum + if (data != null) { + try { + MessageDigest algorithm; + algorithm = MessageDigest.getInstance("MD5"); + algorithm.reset(); + algorithm.update(data); + this.checkSum = algorithm.digest(); + } catch (NoSuchAlgorithmException eee) { + // silent exception, "MD5" is provided in java.security + log.error("unable compute checksum", eee); + } + } + } + + @Override + public void operation(Operation operation) throws Exception { + operation.operation(this); + } + + @Override + public String toString() { + if (data == null) { + return "FileNode (empty file)"; + } else { + String checkSumString = StringUtil.asHex(checkSum); + return "FileNode (size = " + data.length + ", checksum = " + checkSumString + ")"; + } + } +} Added: trunk/diswork-fs/src/main/java/org/nuiton/disworkfs/nodes/Node.java =================================================================== --- trunk/diswork-fs/src/main/java/org/nuiton/disworkfs/nodes/Node.java (rev 0) +++ trunk/diswork-fs/src/main/java/org/nuiton/disworkfs/nodes/Node.java 2010-05-18 13:45:05 UTC (rev 41) @@ -0,0 +1,47 @@ +package org.nuiton.disworkfs.nodes; + +import java.io.Serializable; + +import org.nuiton.disworkfs.operations.Operation; + +/** + * This class represent a node in the tree structure of a file system. + * + * Since it's a data that way be distributed on a network, it has to be + * {@link Serializable}. + */ + +public abstract class Node implements Serializable { + + protected NodeType nodeType; + + public NodeType getNodeType() { + return nodeType; + } + + public void setNodeType(NodeType nodeType) { + this.nodeType = nodeType; + } + + protected Node(NodeType nodeType) { + this.nodeType = nodeType; + } + + public boolean isDirectory() { + boolean isDirectory = nodeType == NodeType.DIRECTORY; + return isDirectory; + } + + public boolean isFile() { + boolean isFile = nodeType == NodeType.FILE; + return isFile; + } + + public boolean isLink() { + boolean isLink = nodeType == NodeType.LINK; + return isLink; + } + + public abstract void operation(Operation operation) throws Exception; + +} \ No newline at end of file Added: trunk/diswork-fs/src/main/java/org/nuiton/disworkfs/nodes/NodeType.java =================================================================== --- trunk/diswork-fs/src/main/java/org/nuiton/disworkfs/nodes/NodeType.java (rev 0) +++ trunk/diswork-fs/src/main/java/org/nuiton/disworkfs/nodes/NodeType.java 2010-05-18 13:45:05 UTC (rev 41) @@ -0,0 +1,7 @@ +package org.nuiton.disworkfs.nodes; + +public enum NodeType { + DIRECTORY, + FILE, + LINK +} \ No newline at end of file Added: trunk/diswork-fs/src/main/java/org/nuiton/disworkfs/nodes/SymLinkNode.java =================================================================== --- trunk/diswork-fs/src/main/java/org/nuiton/disworkfs/nodes/SymLinkNode.java (rev 0) +++ trunk/diswork-fs/src/main/java/org/nuiton/disworkfs/nodes/SymLinkNode.java 2010-05-18 13:45:05 UTC (rev 41) @@ -0,0 +1,28 @@ +package org.nuiton.disworkfs.nodes; + +import org.nuiton.disworkfs.operations.Operation; + +public class SymLinkNode extends Node { + + protected String target; + + public SymLinkNode(String target) { + super(NodeType.LINK); + this.target = target; + } + + public String getTarget() { + return target; + } + + @Override + public String toString() { + return "symlink -> " + target.toString(); + } + + @Override + public void operation(Operation operation) throws Exception { + operation.operation(this); + } + +} Added: trunk/diswork-fs/src/main/java/org/nuiton/disworkfs/nodes/package-info.java =================================================================== --- trunk/diswork-fs/src/main/java/org/nuiton/disworkfs/nodes/package-info.java (rev 0) +++ trunk/diswork-fs/src/main/java/org/nuiton/disworkfs/nodes/package-info.java 2010-05-18 13:45:05 UTC (rev 41) @@ -0,0 +1,37 @@ +/** + * The classes in this package represent the different concept of the content + * of the tree-structure of a file-sytem. + * + * A node of the tree is always a {@link Node}. A node is one of those sub-types + * of Node : + * <dl> + * <dt>{@link DirectoryNode}</dt> + * <dd> + * It's a node that contains other nodes, it can be used as a + * {@link java.util.List} to browse its content. + * </dd> + * <dt>{@link FileNode}</dt> + * <dd> + * A file node, it contains the raw-data of the file (some bytes) + * and a checksum + * </dd> + * <dt>{@link SymLinkNode}</dt> + * <dd> + * It's a + * <a href="http://en.wikipedia.org/wiki/Symbolic_link"> + * symbolic link + * </a> + * in the UNIX way if thinking. + * It's a virtual node which consider another node as a target. Every + * operation done on this symbolic link should target the node behind + * the link. + * </dd> + * </dl> + * + * Note that the Directory and the SymLink do not contains direct references to + * other nodes. Instead, it contains String that should be interpreted as paths + * to those nodes (multiple paths for a directory, the path of the target node + * for the SymLink). + */ + +package org.nuiton.disworkfs.nodes; \ No newline at end of file Added: trunk/diswork-fs/src/main/java/org/nuiton/disworkfs/operations/Operation.java =================================================================== --- trunk/diswork-fs/src/main/java/org/nuiton/disworkfs/operations/Operation.java (rev 0) +++ trunk/diswork-fs/src/main/java/org/nuiton/disworkfs/operations/Operation.java 2010-05-18 13:45:05 UTC (rev 41) @@ -0,0 +1,22 @@ +package org.nuiton.disworkfs.operations; + +import org.nuiton.disworkfs.nodes.DirectoryNode; +import org.nuiton.disworkfs.nodes.FileNode; +import org.nuiton.disworkfs.nodes.SymLinkNode; +import org.nuiton.disworkfs.storage.Storage; + +public abstract class Operation { + + protected Storage storage; + + public Operation(Storage storage) { + this.storage = storage; + } + + public abstract void operation(FileNode fileNode) throws Exception; + + public abstract void operation(DirectoryNode directoryNode) throws Exception; + + public abstract void operation(SymLinkNode symLinkNode) throws Exception; + +} Added: trunk/diswork-fs/src/main/java/org/nuiton/disworkfs/operations/Read.java =================================================================== --- trunk/diswork-fs/src/main/java/org/nuiton/disworkfs/operations/Read.java (rev 0) +++ trunk/diswork-fs/src/main/java/org/nuiton/disworkfs/operations/Read.java 2010-05-18 13:45:05 UTC (rev 41) @@ -0,0 +1,50 @@ +package org.nuiton.disworkfs.operations; + +import java.io.ByteArrayInputStream; +import java.io.IOException; +import java.io.InputStream; + +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; +import org.nuiton.disworkfs.nodes.DirectoryNode; +import org.nuiton.disworkfs.nodes.FileNode; +import org.nuiton.disworkfs.nodes.Node; +import org.nuiton.disworkfs.nodes.SymLinkNode; +import org.nuiton.disworkfs.storage.Storage; + +public class Read extends Operation { + + protected InputStream inputStream; + + private Log log = LogFactory.getLog(Read.class); + + public Read(Storage storage) { + super(storage); + } + + @Override + public void operation(FileNode fileNode) throws Exception { + log.info("reading file " + fileNode); + byte[] data = fileNode.getData(); + inputStream = new ByteArrayInputStream(data); + } + + @Override + public void operation(DirectoryNode directoryNode) throws Exception { + log.error("trying to read a file but " + directoryNode + " is a directory"); + throw new IOException(directoryNode + " is not a file but a directory"); + } + + @Override + public void operation(SymLinkNode symLinkNode) throws Exception { + log.info(symLinkNode + " is a link, forwarding"); + String targetPath = symLinkNode.getTarget(); + Node targetNode = storage.get(targetPath); + targetNode.operation(this); + } + + public InputStream getInputStream() { + return inputStream; + } + +} Added: trunk/diswork-fs/src/main/java/org/nuiton/disworkfs/operations/Write.java =================================================================== --- trunk/diswork-fs/src/main/java/org/nuiton/disworkfs/operations/Write.java (rev 0) +++ trunk/diswork-fs/src/main/java/org/nuiton/disworkfs/operations/Write.java 2010-05-18 13:45:05 UTC (rev 41) @@ -0,0 +1,52 @@ +package org.nuiton.disworkfs.operations; + +import java.io.IOException; +import java.io.InputStream; + +import org.apache.commons.io.IOUtils; +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; +import org.nuiton.disworkfs.nodes.DirectoryNode; +import org.nuiton.disworkfs.nodes.FileNode; +import org.nuiton.disworkfs.nodes.Node; +import org.nuiton.disworkfs.nodes.SymLinkNode; +import org.nuiton.disworkfs.storage.Storage; + +public class Write extends Operation { + + protected InputStream source; + + private Log log = LogFactory.getLog(Write.class); + + public Write(Storage storage, InputStream source) { + super(storage); + this.source = source; + } + + @Override + public void operation(FileNode fileNode) throws Exception { + if (fileNode == null) { + throw new IOException(fileNode + " has not been created"); + } + + byte[] data = IOUtils.toByteArray(source); + + fileNode.setData(data); + + log.info("writing " + fileNode); + + } + + @Override + public void operation(DirectoryNode directoryNode) throws Exception { + throw new IOException(directoryNode + " is not a file but a directory"); + } + + @Override + public void operation(SymLinkNode symLinkNode) throws Exception { + String targetPath = symLinkNode.getTarget(); + Node targetNode = storage.get(targetPath); + targetNode.operation(this); + } + +} Added: trunk/diswork-fs/src/main/java/org/nuiton/disworkfs/storage/Storage.java =================================================================== --- trunk/diswork-fs/src/main/java/org/nuiton/disworkfs/storage/Storage.java (rev 0) +++ trunk/diswork-fs/src/main/java/org/nuiton/disworkfs/storage/Storage.java 2010-05-18 13:45:05 UTC (rev 41) @@ -0,0 +1,58 @@ +package org.nuiton.disworkfs.storage; + +import java.io.Closeable; +import java.util.Map; + +import org.nuiton.disworkfs.nodes.Node; + +/** + * This is a common memory where all the file system will be + * stored. Keys are paths of the tree and values are nodes + * (directory, file or symlink) you find at this precise path + * + * Exemple : + * <table> + * <tr> + * <th> + * Path (key) + * </th> + * <th> + * Node (value) + * </th> + * </tr> + * <tr> + * <td>"/"</td> + * <td> + * an instance of {@link org.nuiton.disworkfs.nodes.DirectoryNode} + * (content ["my_folder", "my_file"]) + * </td> + * </tr> + * <tr> + * <td>"/my_folder"</td> + * <td> + * an instance of {@link org.nuiton.disworkfs.nodes.DirectoryNode} + * (content ["my_second_file"]) + * </td> + * </tr> + * <tr> + * <td>"/my_file"</td> + * <td>an instance of {@link org.nuiton.disworkfs.nodes.FileNode}</td> + * </tr> + * <tr> + * <td>"/my_folder/my_second_file"</td> + * <td>an instance of {@link org.nuiton.disworkfs.nodes.FileNode}</td> + * </tr> + * </table> + * + * The Map should contains at least one entry : a DirectoryNode with the + * key "/". It will be the root folder. + * + * This interface should be implemented using a DHT or any other way to + * store a map on multiple nodes. + * + * Since this storage may use ressources (sockets or stream on files), it + * has to be closeable. + */ +public interface Storage extends Map<String, Node>, Closeable { + +} Added: trunk/diswork-fs/src/main/java/org/nuiton/disworkfs/storage/StorageStub.java =================================================================== --- trunk/diswork-fs/src/main/java/org/nuiton/disworkfs/storage/StorageStub.java (rev 0) +++ trunk/diswork-fs/src/main/java/org/nuiton/disworkfs/storage/StorageStub.java 2010-05-18 13:45:05 UTC (rev 41) @@ -0,0 +1,58 @@ +package org.nuiton.disworkfs.storage; + +import java.io.IOException; +import java.util.HashMap; + +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; +import org.nuiton.disworkfs.nodes.Node; + +/** + * This class is a simple stub of {@link Storage}, it simply uses a HashMap + * in the local memory to store all the data. + * + * All the overridden methods of Map are rewritten only for logging purpose + * + */ +public class StorageStub extends HashMap<String, Node> implements Storage { + + private Log log = LogFactory.getLog(StorageStub.class); + + @Override + public boolean containsKey(Object key) { + log.info("containsKey" + key); + + if (!(key instanceof String)) { + log.warn("trying to look for a wrong-typed key"); + } + + return super.containsKey(key); + } + + @Override + public Node put(String key, Node value) { + log.info("put(" + key + ", " + value + ")"); + + return super.put(key, value); + } + + @Override + public Node get(Object key) { + + if (!(key instanceof String)) { + log.warn("trying to get for a wrong-typed key"); + } + + Node value = super.get(key); + + log.info("get(" + key + ") returns " + value + ")"); + + return value; + } + + @Override + public void close() throws IOException { + // nothing to do + } + +} Modified: trunk/diswork-fs/src/main/resources/log4j.properties =================================================================== --- trunk/diswork-fs/src/main/resources/log4j.properties 2010-05-17 09:58:49 UTC (rev 40) +++ trunk/diswork-fs/src/main/resources/log4j.properties 2010-05-18 13:45:05 UTC (rev 41) @@ -6,6 +6,4 @@ log4j.appender.stdout.layout.ConversionPattern=%d %5p [%t] (%F:%L) %M - %m%n # package level log4j.logger.org.nuiton.disworkfs=TRACE -#log4j.logger.org.nuiton.disworkfs.transport=WARN -#log4j.logger.org.nuiton.disworkfs.services.DownloadService=WARN -#log4j.logger.org.nuiton.disworkfs.services.UploadService=WARN +log4j.logger.org.nuiton.disworkfs.storage=WARN \ No newline at end of file Modified: trunk/diswork-fs/src/test/java/org/nuiton/disworkfs/DistributedFileSystemTest.java =================================================================== --- trunk/diswork-fs/src/test/java/org/nuiton/disworkfs/DistributedFileSystemTest.java 2010-05-17 09:58:49 UTC (rev 40) +++ trunk/diswork-fs/src/test/java/org/nuiton/disworkfs/DistributedFileSystemTest.java 2010-05-18 13:45:05 UTC (rev 41) @@ -5,6 +5,9 @@ import java.io.File; import java.io.FileInputStream; +import java.io.FileNotFoundException; +import java.io.IOException; +import java.io.InputStream; import java.util.Random; import org.apache.commons.io.FileUtils; @@ -34,24 +37,13 @@ */ static protected int randomFileSize = 3000; - static protected String storagePath1; - static protected String storagePath2; - - static protected DisworkConfig disworkConfig1; - static protected DisworkConfig disworkConfig2; - + static protected DistributedFileSystem fileSystem; + @Before public void setUp() throws Exception { File tempDirectory = new File(tempDirectoryPath); tempDirectory.mkdir(); - File storage1 = new File(tempDirectory, "storage1"); - storage1.mkdir(); - storagePath1 = storage1.getAbsolutePath(); - File storage2 = new File(tempDirectory, "storage2"); - storage2.mkdir(); - storagePath2 = storage2.getAbsolutePath(); - Random random = new Random(); // creating random data for the file byte[] randomBytes = new byte[randomFileSize]; @@ -61,88 +53,84 @@ File randomFile = new File(randomFilePath); FileUtils.writeByteArrayToFile(randomFile, randomBytes); - disworkConfig1 = new DisworkConfig(); - disworkConfig1.setOption("storage", storagePath1); + DisworkConfig disworkConfig = new DisworkConfig(); - disworkConfig2 = new DisworkConfig(); - disworkConfig2.setOption("storage", storagePath2); + fileSystem = new DisworkFileSystem(disworkConfig); } @After public void tearDown() throws Exception { // cleaning + fileSystem.close(); + FileUtil.deleteRecursively(tempDirectoryPath); } - - @Test - public void fakeTest() throws Exception { - // TODO 2010-05-17 to be removed when a test is written - } + + @Test + public void testWrite() throws Exception { + fileSystem.write("/my_file", new FileInputStream(randomFilePath)); + } + + @Test + public void testExists() throws Exception { + fileSystem.write("/my_file", new FileInputStream(randomFilePath)); + assertTrue(fileSystem.exists("/my_file")); + assertFalse(fileSystem.exists("/my_other_file")); + } + + /** + * try to read a file that as never been created nor written + */ + @Test(expected = FileNotFoundException.class) + public void testFailAtRead() throws Exception { + fileSystem.read("/not_existing_file"); + } + + /** + * writing a file at the root directory and reading it. + * finally, compare original source and read result + * byte-to-byte : contents should be equals + * @throws Exception + */ + @Test + public void testWriteRead() throws Exception { + + InputStream source = new FileInputStream(randomFilePath); - /* - @Test - public void testWrite() throws Exception { + fileSystem.write("/my_file", source); + + InputStream readResult = fileSystem.read("/my_file"); + + source = new FileInputStream(randomFilePath); + boolean actualContentEquality = + IOUtils.contentEquals(source, readResult); + + assertTrue(actualContentEquality); + } + + @Test + public void testMkdir() throws Exception { + fileSystem.mkdir("/my_folder"); + assertTrue(fileSystem.exists("/my_folder")); + } + + /** + * this use case should raise an exception because my_folder + * doesn't exists + */ + @Test(expected = IOException.class) + public void testWriteFail() throws Exception { + fileSystem.write("/my_folder/my_file", new FileInputStream(randomFilePath)); + } - DisworkFileSystem disworkFileSystem1 = new DisworkFileSystem( - disworkConfig1); - disworkFileSystem1.write("monfichier", new FileInputStream(randomFilePath)); - disworkFileSystem1.close(); - + @Test + public void testWriteInFolder() throws Exception { + fileSystem.mkdir("/my_folder"); + fileSystem.write("/my_folder/my_file", new FileInputStream(randomFilePath)); + fileSystem.mkdir("/my_folder/my_sub_folder"); + fileSystem.write("/my_folder/my_sub_folder/my_file", new FileInputStream(randomFilePath)); + + ((DisworkFileSystem) fileSystem).dump(); + } - File monfichier = new File(randomFilePath); - File monfichierstorage = new File(tempDirectoryPath - + "/storage1/monfichier"); - - boolean actualContentEquality = IOUtils.contentEquals( - new FileInputStream(monfichier), new FileInputStream( - monfichierstorage)); - - assertTrue("file and copy content should be the same", - actualContentEquality); - } - */ - - /** - * this test run two DistributedFS. A file is written on the first when. We - * try to read from the other FS : since it doesn't own the file, it will - * try a lookup and download the file. - */ - /* - @Test - public void testRead() throws Exception { - - DisworkFileSystem disworkFileSystem1 = new DisworkFileSystem( - disworkConfig1); - DisworkFileSystem disworkFileSystem2 = new DisworkFileSystem( - disworkConfig2); - - disworkFileSystem1.write("mon/chemin/vers/mon/fichier", new FileInputStream( - randomFilePath)); - - boolean existsResult = disworkFileSystem2 - .exists("mon/chemin/vers/mon/fichier"); - assertTrue(existsResult); - - existsResult = disworkFileSystem2.exists("unautrefichierquinexistepas"); - assertFalse(existsResult); - - disworkFileSystem2.read("mon/chemin/vers/mon/fichier"); - - File monfichierstorage1 = new File(tempDirectoryPath - + "/storage1/mon/chemin/vers/mon/fichier"); - File monfichierstorage2 = new File(tempDirectoryPath - + "/storage2/mon/chemin/vers/mon/fichier"); - - boolean actualContentEquality = IOUtils.contentEquals( - new FileInputStream(monfichierstorage1), new FileInputStream( - monfichierstorage2)); - - assertTrue("file and copy content should be the same", - actualContentEquality); - - disworkFileSystem1.close(); - disworkFileSystem2.close(); - } - */ - }