]> git.argeo.org Git - lgpl/argeo-commons.git/blob - org.argeo.jcr/src/org/argeo/jcr/fs/JcrBasicfileAttributes.java
[maven-release-plugin] prepare release argeo-commons-2.1.56
[lgpl/argeo-commons.git] / org.argeo.jcr / src / org / argeo / jcr / fs / JcrBasicfileAttributes.java
1 package org.argeo.jcr.fs;
2
3 import java.nio.file.attribute.FileTime;
4 import java.time.Instant;
5
6 import javax.jcr.Binary;
7 import javax.jcr.Node;
8 import javax.jcr.Property;
9 import javax.jcr.RepositoryException;
10 import javax.jcr.nodetype.NodeType;
11
12 import org.argeo.jcr.JcrUtils;
13
14 public class JcrBasicfileAttributes implements NodeFileAttributes {
15 private final Node node;
16
17 private FileTime EPOCH = FileTime.fromMillis(0);
18
19 public JcrBasicfileAttributes(Node node) {
20 this.node = node;
21 }
22
23 @Override
24 public FileTime lastModifiedTime() {
25 try {
26 if (node.isNodeType(NodeType.MIX_LAST_MODIFIED)) {
27 Instant instant = node.getProperty(Property.JCR_LAST_MODIFIED).getDate().toInstant();
28 return FileTime.from(instant);
29 }
30 return EPOCH;
31 } catch (RepositoryException e) {
32 throw new JcrFsException("Cannot get last modified time", e);
33 }
34 }
35
36 @Override
37 public FileTime lastAccessTime() {
38 return lastModifiedTime();
39 }
40
41 @Override
42 public FileTime creationTime() {
43 try {
44 if (node.isNodeType(NodeType.MIX_CREATED)) {
45 Instant instant = node.getProperty(Property.JCR_CREATED).getDate().toInstant();
46 return FileTime.from(instant);
47 }
48 return EPOCH;
49 } catch (RepositoryException e) {
50 throw new JcrFsException("Cannot get creation time", e);
51 }
52 }
53
54 @Override
55 public boolean isRegularFile() {
56 try {
57 return node.isNodeType(NodeType.NT_FILE);
58 } catch (RepositoryException e) {
59 throw new JcrFsException("Cannot check if regular file", e);
60 }
61 }
62
63 @Override
64 public boolean isDirectory() {
65 try {
66 if (node.isNodeType(NodeType.NT_FOLDER))
67 return true;
68 // all other non file nodes
69 return !(node.isNodeType(NodeType.NT_FILE) || node.isNodeType(NodeType.NT_LINKED_FILE));
70 } catch (RepositoryException e) {
71 throw new JcrFsException("Cannot check if directory", e);
72 }
73 }
74
75 @Override
76 public boolean isSymbolicLink() {
77 try {
78 return node.isNodeType(NodeType.NT_LINKED_FILE);
79 } catch (RepositoryException e) {
80 throw new JcrFsException("Cannot check if linked file", e);
81 }
82 }
83
84 @Override
85 public boolean isOther() {
86 return !(isDirectory() || isRegularFile() || isSymbolicLink());
87 }
88
89 @Override
90 public long size() {
91 if (isRegularFile()) {
92 Binary binary = null;
93 try {
94 binary = node.getNode(Property.JCR_CONTENT).getProperty(Property.JCR_DATA).getBinary();
95 return binary.getSize();
96 } catch (RepositoryException e) {
97 throw new JcrFsException("Cannot check size", e);
98 } finally {
99 JcrUtils.closeQuietly(binary);
100 }
101 }
102 return -1;
103 }
104
105 @Override
106 public Object fileKey() {
107 return null;
108 }
109
110 @Override
111 public Node getNode() {
112 return node;
113 }
114
115 }