X-Git-Url: https://git.argeo.org/?a=blobdiff_plain;ds=sidebyside;f=org.argeo.jcr%2Fsrc%2Forg%2Fargeo%2Fjcr%2Ffs%2FJcrFileSystemProvider.java;h=04d1342bf3f480708f707805327487fb8f805b0e;hb=9eaf85002b003a68eea81fca417465ba52f14e4e;hp=1e2864d248a8b9d998a34f874a773710f7a63ca0;hpb=36c14000ab482f558a9679f75ac7bfb0953171f4;p=lgpl%2Fargeo-commons.git
diff --git a/org.argeo.jcr/src/org/argeo/jcr/fs/JcrFileSystemProvider.java b/org.argeo.jcr/src/org/argeo/jcr/fs/JcrFileSystemProvider.java
index 1e2864d24..04d1342bf 100644
--- a/org.argeo.jcr/src/org/argeo/jcr/fs/JcrFileSystemProvider.java
+++ b/org.argeo.jcr/src/org/argeo/jcr/fs/JcrFileSystemProvider.java
@@ -30,22 +30,22 @@ import javax.jcr.Session;
import javax.jcr.nodetype.NodeType;
import javax.jcr.nodetype.PropertyDefinition;
-import org.apache.commons.io.FileExistsException;
import org.argeo.jcr.JcrUtils;
public abstract class JcrFileSystemProvider extends FileSystemProvider {
+
@Override
public SeekableByteChannel newByteChannel(Path path, Set extends OpenOption> options, FileAttribute>... attrs)
throws IOException {
+ Node node = toNode(path);
try {
- Node node = toNode(path);
if (node == null) {
Node parent = toNode(path.getParent());
if (parent == null)
- throw new JcrFsException("No parent directory for " + path);
+ throw new IOException("No parent directory for " + path);
if (parent.getPrimaryNodeType().isNodeType(NodeType.NT_FILE)
|| parent.getPrimaryNodeType().isNodeType(NodeType.NT_LINKED_FILE))
- throw new JcrFsException(path + " parent is a file");
+ throw new IOException(path + " parent is a file");
String fileName = path.getFileName().toString();
fileName = Text.escapeIllegalJcrChars(fileName);
@@ -57,7 +57,8 @@ public abstract class JcrFileSystemProvider extends FileSystemProvider {
throw new UnsupportedOperationException(node + " must be a file");
return new BinaryChannel(node);
} catch (RepositoryException e) {
- throw new JcrFsException("Cannot read file", e);
+ discardChanges(node);
+ throw new IOException("Cannot read file", e);
}
}
@@ -67,21 +68,21 @@ public abstract class JcrFileSystemProvider extends FileSystemProvider {
Node base = toNode(dir);
return new NodeDirectoryStream((JcrFileSystem) dir.getFileSystem(), base.getNodes(), filter);
} catch (RepositoryException e) {
- throw new JcrFsException("Cannot list directory", e);
+ throw new IOException("Cannot list directory", e);
}
}
@Override
public void createDirectory(Path dir, FileAttribute>... attrs) throws IOException {
+ Node node = toNode(dir);
try {
- Node node = toNode(dir);
if (node == null) {
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 JcrFsException(dir + " parent is a 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);
@@ -89,19 +90,19 @@ public abstract class JcrFileSystemProvider extends FileSystemProvider {
node.addMixin(NodeType.MIX_LAST_MODIFIED);
node.getSession().save();
} else {
- if (!node.getPrimaryNodeType().isNodeType(NodeType.NT_FOLDER))
- throw new FileExistsException(dir + " exists and is not a directory");
+ // if (!node.getPrimaryNodeType().isNodeType(NodeType.NT_FOLDER))
+ // throw new FileExistsException(dir + " exists and is not a directory");
}
} catch (RepositoryException e) {
- throw new JcrFsException("Cannot create directory " + dir, e);
+ discardChanges(node);
+ throw new IOException("Cannot create directory " + dir, e);
}
-
}
@Override
public void delete(Path path) throws IOException {
+ Node node = toNode(path);
try {
- Node node = toNode(path);
if (node == null)
throw new NoSuchFileException(path + " does not exist");
Session session = node.getSession();
@@ -114,32 +115,36 @@ public abstract class JcrFileSystemProvider extends FileSystemProvider {
}
session.save();
} catch (RepositoryException e) {
- throw new JcrFsException("Cannot delete " + path, e);
+ discardChanges(node);
+ throw new IOException("Cannot delete " + path, e);
}
}
@Override
public void copy(Path source, Path target, CopyOption... options) throws IOException {
+ Node sourceNode = toNode(source);
+ Node targetNode = toNode(target);
try {
- Node sourceNode = toNode(source);
- Node targetNode = toNode(target);
JcrUtils.copy(sourceNode, targetNode);
sourceNode.getSession().save();
} catch (RepositoryException e) {
- throw new JcrFsException("Cannot copy from " + source + " to " + target, e);
+ discardChanges(sourceNode);
+ discardChanges(targetNode);
+ throw new IOException("Cannot copy from " + source + " to " + target, e);
}
}
@Override
public void move(Path source, Path target, CopyOption... options) throws IOException {
+ Node sourceNode = toNode(source);
try {
- Node sourceNode = toNode(source);
Session session = sourceNode.getSession();
session.move(sourceNode.getPath(), target.toString());
session.save();
} catch (RepositoryException e) {
- throw new JcrFsException("Cannot move from " + source + " to " + target, e);
+ discardChanges(sourceNode);
+ throw new IOException("Cannot move from " + source + " to " + target, e);
}
}
@@ -156,7 +161,7 @@ public abstract class JcrFileSystemProvider extends FileSystemProvider {
Node node2 = toNode(path2);
return node.isSame(node2);
} catch (RepositoryException e) {
- throw new JcrFsException("Cannot check whether " + path + " and " + path2 + " are same", e);
+ throw new IOException("Cannot check whether " + path + " and " + path2 + " are same", e);
}
}
@@ -181,7 +186,7 @@ public abstract class JcrFileSystemProvider extends FileSystemProvider {
throw new NoSuchFileException(path + " does not exist");
// TODO check access via JCR api
} catch (RepositoryException e) {
- throw new JcrFsException("Cannot delete " + path, e);
+ throw new IOException("Cannot delete " + path, e);
}
}
@@ -194,16 +199,12 @@ public abstract class JcrFileSystemProvider extends FileSystemProvider {
@Override
public A readAttributes(Path path, Class type, LinkOption... options)
throws IOException {
- try {
- // TODO check if assignable
- Node node = toNode(path);
- if(node==null) {
- throw new JcrFsException("JCR node not found for "+path);
- }
- return (A) new JcrBasicfileAttributes(node);
- } catch (RepositoryException e) {
- throw new JcrFsException("Cannot read basic attributes of " + path, e);
+ // TODO check if assignable
+ Node node = toNode(path);
+ if (node == null) {
+ throw new IOException("JCR node not found for " + path);
}
+ return (A) new JcrBasicfileAttributes(node);
}
@Override
@@ -242,14 +243,14 @@ public abstract class JcrFileSystemProvider extends FileSystemProvider {
}
return res;
} catch (RepositoryException e) {
- throw new JcrFsException("Cannot read attributes of " + path, e);
+ throw new IOException("Cannot read attributes of " + path, e);
}
}
@Override
public void setAttribute(Path path, String attribute, Object value, LinkOption... options) throws IOException {
+ Node node = toNode(path);
try {
- Node node = toNode(path);
if (value instanceof byte[]) {
JcrUtils.setBinaryAsBytes(node, attribute, (byte[]) value);
} else if (value instanceof Calendar) {
@@ -259,12 +260,31 @@ public abstract class JcrFileSystemProvider extends FileSystemProvider {
}
node.getSession().save();
} catch (RepositoryException e) {
- throw new JcrFsException("Cannot set attribute " + attribute + " on " + path, e);
+ discardChanges(node);
+ throw new IOException("Cannot set attribute " + attribute + " on " + path, e);
}
}
- protected Node toNode(Path path) throws RepositoryException {
- return ((JcrPath) path).getNode();
+ protected Node toNode(Path path) {
+ try {
+ return ((JcrPath) path).getNode();
+ } catch (RepositoryException e) {
+ throw new JcrFsException("Cannot convert path " + path + " to JCR Node", e);
+ }
+ }
+
+ /** Discard changes in the underlying session */
+ protected void discardChanges(Node node) {
+ if (node == null)
+ return;
+ try {
+ // discard changes
+ node.getSession().refresh(false);
+ } catch (RepositoryException e) {
+ e.printStackTrace();
+ // TODO log out session?
+ // TODO use Commons logging?
+ }
}
/**