]> git.argeo.org Git - lgpl/argeo-commons.git/blob - org.argeo.jcr/src/org/argeo/jcr/fs/JcrFileSystem.java
Improve logging
[lgpl/argeo-commons.git] / org.argeo.jcr / src / org / argeo / jcr / fs / JcrFileSystem.java
1 package org.argeo.jcr.fs;
2
3 import java.io.IOException;
4 import java.nio.file.FileStore;
5 import java.nio.file.FileSystem;
6 import java.nio.file.Path;
7 import java.nio.file.PathMatcher;
8 import java.nio.file.WatchService;
9 import java.nio.file.attribute.UserPrincipalLookupService;
10 import java.nio.file.spi.FileSystemProvider;
11 import java.util.HashSet;
12 import java.util.Set;
13
14 import javax.jcr.Node;
15 import javax.jcr.RepositoryException;
16 import javax.jcr.Session;
17
18 import org.argeo.jcr.JcrUtils;
19
20 public class JcrFileSystem extends FileSystem {
21 private final JcrFileSystemProvider provider;
22 private final Session session;
23 private String userHomePath = null;
24
25 public JcrFileSystem(JcrFileSystemProvider provider, Session session) throws IOException {
26 super();
27 this.provider = provider;
28 this.session = session;
29 Node userHome = provider.getUserHome(session);
30 if (userHome != null)
31 try {
32 userHomePath = userHome.getPath();
33 } catch (RepositoryException e) {
34 throw new IOException("Cannot retrieve user home path", e);
35 }
36 }
37
38 public String getUserHomePath() {
39 return userHomePath;
40 }
41
42 @Override
43 public FileSystemProvider provider() {
44 return provider;
45 }
46
47 @Override
48 public void close() throws IOException {
49 JcrUtils.logoutQuietly(session);
50 }
51
52 @Override
53 public boolean isOpen() {
54 return session.isLive();
55 }
56
57 @Override
58 public boolean isReadOnly() {
59 return false;
60 }
61
62 @Override
63 public String getSeparator() {
64 return "/";
65 }
66
67 @Override
68 public Iterable<Path> getRootDirectories() {
69 try {
70 Set<Path> single = new HashSet<>();
71 single.add(new JcrPath(this, session.getRootNode()));
72 return single;
73 } catch (RepositoryException e) {
74 throw new JcrFsException("Cannot get root path", e);
75 }
76 }
77
78 @Override
79 public Iterable<FileStore> getFileStores() {
80 throw new UnsupportedOperationException();
81 }
82
83 @Override
84 public Set<String> supportedFileAttributeViews() {
85 try {
86 String[] prefixes = session.getNamespacePrefixes();
87 Set<String> res = new HashSet<>();
88 for (String prefix : prefixes)
89 res.add(prefix);
90 res.add("basic");
91 return res;
92 } catch (RepositoryException e) {
93 throw new JcrFsException("Cannot get supported file attributes views", e);
94 }
95 }
96
97 @Override
98 public Path getPath(String first, String... more) {
99 StringBuilder sb = new StringBuilder(first);
100 // TODO Make it more robust
101 for (String part : more)
102 sb.append('/').append(part);
103 return new JcrPath(this, sb.toString());
104 }
105
106 @Override
107 public PathMatcher getPathMatcher(String syntaxAndPattern) {
108 throw new UnsupportedOperationException();
109 }
110
111 @Override
112 public UserPrincipalLookupService getUserPrincipalLookupService() {
113 throw new UnsupportedOperationException();
114 }
115
116 @Override
117 public WatchService newWatchService() throws IOException {
118 throw new UnsupportedOperationException();
119 }
120
121 public Session getSession() {
122 return session;
123 }
124
125 }