Rename home repository into ego repository.
[lgpl/argeo-commons.git] / org.argeo.jcr / src / org / argeo / jcr / fs / JcrFileSystemProvider.java
index 7dbd4e43f88116e96a127b3b305fd1fae8dfe515..7a38ba3701d45f071e1f654f1c4145dafc7c0c46 100644 (file)
@@ -25,6 +25,7 @@ import javax.jcr.Node;
 import javax.jcr.Property;
 import javax.jcr.PropertyIterator;
 import javax.jcr.PropertyType;
+import javax.jcr.Repository;
 import javax.jcr.RepositoryException;
 import javax.jcr.Session;
 import javax.jcr.nodetype.NodeType;
@@ -32,6 +33,7 @@ import javax.jcr.nodetype.PropertyDefinition;
 
 import org.argeo.jcr.JcrUtils;
 
+/** Operations on a {@link JcrFileSystem}. */
 public abstract class JcrFileSystemProvider extends FileSystemProvider {
 
        @Override
@@ -51,7 +53,7 @@ public abstract class JcrFileSystemProvider extends FileSystemProvider {
                                fileName = Text.escapeIllegalJcrChars(fileName);
                                node = parent.addNode(fileName, NodeType.NT_FILE);
                                node.addMixin(NodeType.MIX_CREATED);
-                               node.addMixin(NodeType.MIX_LAST_MODIFIED);
+//                             node.addMixin(NodeType.MIX_LAST_MODIFIED);
                        }
                        if (!node.isNodeType(NodeType.NT_FILE))
                                throw new UnsupportedOperationException(node + " must be a file");
@@ -68,7 +70,8 @@ public abstract class JcrFileSystemProvider extends FileSystemProvider {
                        Node base = toNode(dir);
                        if (base == null)
                                throw new IOException(dir + " is not a JCR node");
-                       return new NodeDirectoryStream((JcrFileSystem) dir.getFileSystem(), base.getNodes(), filter);
+                       JcrFileSystem fileSystem = (JcrFileSystem) dir.getFileSystem();
+                       return new NodeDirectoryStream(fileSystem, base.getNodes(), fileSystem.listDirectMounts(dir), filter);
                } catch (RepositoryException e) {
                        throw new IOException("Cannot list directory", e);
                }
@@ -82,15 +85,18 @@ public abstract class JcrFileSystemProvider extends FileSystemProvider {
                                Node parent = toNode(dir.getParent());
                                if (parent == null)
                                        throw new IOException("Parent of " + dir + " does not exist");
-                               if (parent.getPrimaryNodeType().isNodeType(NodeType.NT_FILE)
-                                               || parent.getPrimaryNodeType().isNodeType(NodeType.NT_LINKED_FILE))
-                                       throw new IOException(dir + " parent is a file");
-                               String fileName = dir.getFileName().toString();
-                               fileName = Text.escapeIllegalJcrChars(fileName);
-                               node = parent.addNode(fileName, NodeType.NT_FOLDER);
-                               node.addMixin(NodeType.MIX_CREATED);
-                               node.addMixin(NodeType.MIX_LAST_MODIFIED);
-                               node.getSession().save();
+                               Session session = parent.getSession();
+                               synchronized (session) {
+                                       if (parent.getPrimaryNodeType().isNodeType(NodeType.NT_FILE)
+                                                       || parent.getPrimaryNodeType().isNodeType(NodeType.NT_LINKED_FILE))
+                                               throw new IOException(dir + " parent is a file");
+                                       String fileName = dir.getFileName().toString();
+                                       fileName = Text.escapeIllegalJcrChars(fileName);
+                                       node = parent.addNode(fileName, NodeType.NT_FOLDER);
+                                       node.addMixin(NodeType.MIX_CREATED);
+                                       node.addMixin(NodeType.MIX_LAST_MODIFIED);
+                                       save(session);
+                               }
                        } else {
                                // if (!node.getPrimaryNodeType().isNodeType(NodeType.NT_FOLDER))
                                // throw new FileExistsException(dir + " exists and is not a directory");
@@ -108,14 +114,17 @@ public abstract class JcrFileSystemProvider extends FileSystemProvider {
                        if (node == null)
                                throw new NoSuchFileException(path + " does not exist");
                        Session session = node.getSession();
-                       if (node.getPrimaryNodeType().isNodeType(NodeType.NT_FILE))
-                               node.remove();
-                       else if (node.getPrimaryNodeType().isNodeType(NodeType.NT_FOLDER)) {
-                               if (node.hasNodes())// TODO check only files
-                                       throw new DirectoryNotEmptyException(path.toString());
-                               node.remove();
+                       synchronized (session) {
+                               session.refresh(false);
+                               if (node.getPrimaryNodeType().isNodeType(NodeType.NT_FILE))
+                                       node.remove();
+                               else if (node.getPrimaryNodeType().isNodeType(NodeType.NT_FOLDER)) {
+                                       if (node.hasNodes())// TODO check only files
+                                               throw new DirectoryNotEmptyException(path.toString());
+                                       node.remove();
+                               }
+                               save(session);
                        }
-                       session.save();
                } catch (RepositoryException e) {
                        discardChanges(node);
                        throw new IOException("Cannot delete " + path, e);
@@ -128,8 +137,11 @@ public abstract class JcrFileSystemProvider extends FileSystemProvider {
                Node sourceNode = toNode(source);
                Node targetNode = toNode(target);
                try {
-                       JcrUtils.copy(sourceNode, targetNode);
-                       sourceNode.getSession().save();
+                       Session targetSession = targetNode.getSession();
+                       synchronized (targetSession) {
+                               JcrUtils.copy(sourceNode, targetNode);
+                               save(targetSession);
+                       }
                } catch (RepositoryException e) {
                        discardChanges(sourceNode);
                        discardChanges(targetNode);
@@ -142,8 +154,10 @@ public abstract class JcrFileSystemProvider extends FileSystemProvider {
                Node sourceNode = toNode(source);
                try {
                        Session session = sourceNode.getSession();
-                       session.move(sourceNode.getPath(), target.toString());
-                       session.save();
+                       synchronized (session) {
+                               session.move(sourceNode.getPath(), target.toString());
+                               save(session);
+                       }
                } catch (RepositoryException e) {
                        discardChanges(sourceNode);
                        throw new IOException("Cannot move from " + source + " to " + target, e);
@@ -176,20 +190,16 @@ public abstract class JcrFileSystemProvider extends FileSystemProvider {
 
        @Override
        public FileStore getFileStore(Path path) throws IOException {
-               Session session = ((JcrFileSystem) path.getFileSystem()).getSession();
-               return new WorkSpaceFileStore(session.getWorkspace());
+               JcrFileSystem fileSystem = (JcrFileSystem) path.getFileSystem();
+               return fileSystem.getFileStore(path.toString());
        }
 
        @Override
        public void checkAccess(Path path, AccessMode... modes) throws IOException {
-               try {
-                       Session session = ((JcrFileSystem) path.getFileSystem()).getSession();
-                       if (!session.itemExists(path.toString()))
-                               throw new NoSuchFileException(path + " does not exist");
-                       // TODO check access via JCR api
-               } catch (RepositoryException e) {
-                       throw new IOException("Cannot delete " + path, e);
-               }
+               Node node = toNode(path);
+               if (node == null)
+                       throw new NoSuchFileException(path + " does not exist");
+               // TODO check access via JCR api
        }
 
        @Override
@@ -253,14 +263,17 @@ public abstract class JcrFileSystemProvider extends FileSystemProvider {
        public void setAttribute(Path path, String attribute, Object value, LinkOption... options) throws IOException {
                Node node = toNode(path);
                try {
-                       if (value instanceof byte[]) {
-                               JcrUtils.setBinaryAsBytes(node, attribute, (byte[]) value);
-                       } else if (value instanceof Calendar) {
-                               node.setProperty(attribute, (Calendar) value);
-                       } else {
-                               node.setProperty(attribute, value.toString());
+                       Session session = node.getSession();
+                       synchronized (session) {
+                               if (value instanceof byte[]) {
+                                       JcrUtils.setBinaryAsBytes(node, attribute, (byte[]) value);
+                               } else if (value instanceof Calendar) {
+                                       node.setProperty(attribute, (Calendar) value);
+                               } else {
+                                       node.setProperty(attribute, value.toString());
+                               }
+                               save(session);
                        }
-                       node.getSession().save();
                } catch (RepositoryException e) {
                        discardChanges(node);
                        throw new IOException("Cannot set attribute " + attribute + " on " + path, e);
@@ -289,13 +302,21 @@ public abstract class JcrFileSystemProvider extends FileSystemProvider {
                }
        }
 
+       /** Make sure save is robust. */
+       protected void save(Session session) throws RepositoryException {
+               session.refresh(true);
+               session.save();
+               session.notifyAll();
+       }
+
        /**
         * To be overriden in order to support the ~ path, with an implementation
         * specific concept of user home.
         * 
         * @return null by default
         */
-       public Node getUserHome(Session session) {
+       public Node getUserHome(Repository session) {
                return null;
        }
+
 }