]> git.argeo.org Git - lgpl/argeo-commons.git/blob - org.argeo.jcr/src/org/argeo/jcr/fs/JcrBasicfileAttributes.java
e59abbc70a8a1fc1f65556c4208b7946eb2c5b96
[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 return node.isNodeType(NodeType.NT_FOLDER);
67 } catch (RepositoryException e) {
68 throw new JcrFsException("Cannot check if directory", e);
69 }
70 }
71
72 @Override
73 public boolean isSymbolicLink() {
74 try {
75 return node.isNodeType(NodeType.NT_LINKED_FILE);
76 } catch (RepositoryException e) {
77 throw new JcrFsException("Cannot check if linked file", e);
78 }
79 }
80
81 @Override
82 public boolean isOther() {
83 return !(isDirectory() || isRegularFile() || isSymbolicLink());
84 }
85
86 @Override
87 public long size() {
88 if (isRegularFile()) {
89 Binary binary = null;
90 try {
91 binary = node.getNode(Property.JCR_DATA).getProperty(Property.JCR_CONTENT).getBinary();
92 return binary.getSize();
93 } catch (RepositoryException e) {
94 throw new JcrFsException("Cannot check size", e);
95 } finally {
96 JcrUtils.closeQuietly(binary);
97 }
98 }
99 return -1;
100 }
101
102 @Override
103 public Object fileKey() {
104 return null;
105 }
106
107 @Override
108 public Node getNode() {
109 return node;
110 }
111
112 }