]> git.argeo.org Git - lgpl/argeo-commons.git/blob - org.argeo.jcr/src/org/argeo/jcr/fs/WorkspaceFileStore.java
Improve localization framework.
[lgpl/argeo-commons.git] / org.argeo.jcr / src / org / argeo / jcr / fs / WorkspaceFileStore.java
1 package org.argeo.jcr.fs;
2
3 import java.io.IOException;
4 import java.nio.file.FileStore;
5 import java.nio.file.attribute.FileAttributeView;
6 import java.nio.file.attribute.FileStoreAttributeView;
7 import java.util.Arrays;
8
9 import javax.jcr.Node;
10 import javax.jcr.RepositoryException;
11 import javax.jcr.Session;
12 import javax.jcr.Workspace;
13
14 import org.argeo.jcr.JcrUtils;
15
16 /** A {@link FileStore} implementation based on JCR {@link Workspace}. */
17 public class WorkspaceFileStore extends FileStore {
18 private final String mountPath;
19 private final Workspace workspace;
20 private final String workspaceName;
21 private final int mountDepth;
22
23 public WorkspaceFileStore(String mountPath, Workspace workspace) {
24 if ("/".equals(mountPath) || "".equals(mountPath))
25 throw new IllegalArgumentException(
26 "Mount path '" + mountPath + "' is unsupported, use null for the base file store");
27 if (mountPath != null && !mountPath.startsWith(JcrPath.separator))
28 throw new IllegalArgumentException("Mount path '" + mountPath + "' cannot end with /");
29 if (mountPath != null && mountPath.endsWith(JcrPath.separator))
30 throw new IllegalArgumentException("Mount path '" + mountPath + "' cannot end with /");
31 this.mountPath = mountPath;
32 if (mountPath == null)
33 mountDepth = 0;
34 else {
35 mountDepth = mountPath.split(JcrPath.separator).length - 1;
36 }
37 this.workspace = workspace;
38 this.workspaceName = workspace.getName();
39 }
40
41 public void close() {
42 JcrUtils.logoutQuietly(workspace.getSession());
43 }
44
45 @Override
46 public String name() {
47 return workspace.getName();
48 }
49
50 @Override
51 public String type() {
52 return "workspace";
53 }
54
55 @Override
56 public boolean isReadOnly() {
57 return false;
58 }
59
60 @Override
61 public long getTotalSpace() throws IOException {
62 return 0;
63 }
64
65 @Override
66 public long getUsableSpace() throws IOException {
67 return 0;
68 }
69
70 @Override
71 public long getUnallocatedSpace() throws IOException {
72 return 0;
73 }
74
75 @Override
76 public boolean supportsFileAttributeView(Class<? extends FileAttributeView> type) {
77 return false;
78 }
79
80 @Override
81 public boolean supportsFileAttributeView(String name) {
82 return false;
83 }
84
85 @Override
86 public <V extends FileStoreAttributeView> V getFileStoreAttributeView(Class<V> type) {
87 return null;
88 }
89
90 @Override
91 public Object getAttribute(String attribute) throws IOException {
92 return workspace.getSession().getRepository().getDescriptor(attribute);
93 }
94
95 public Workspace getWorkspace() {
96 return workspace;
97 }
98
99 public String toFsPath(Node node) throws RepositoryException {
100 String nodeWorkspaceName = node.getSession().getWorkspace().getName();
101 if (!nodeWorkspaceName.equals(workspace.getName()))
102 throw new IllegalArgumentException("Icompatible " + node + " from workspace '" + nodeWorkspaceName
103 + "' in file store '" + workspace.getName() + "'");
104 return mountPath == null ? node.getPath() : mountPath + node.getPath();
105 }
106
107 public boolean isBase() {
108 return mountPath == null;
109 }
110
111 Node toNode(String[] fullPath) throws RepositoryException {
112 String jcrPath = toJcrPath(fullPath);
113 Session session = workspace.getSession();
114 if (!session.itemExists(jcrPath))
115 return null;
116 Node node = session.getNode(jcrPath);
117 return node;
118 }
119
120 String toJcrPath(String fsPath) {
121 if (fsPath.length() == 1)
122 return toJcrPath((String[]) null);// root
123 String[] arr = fsPath.substring(1).split("/");
124 // if (arr.length == 0 || (arr.length == 1 && arr[0].equals("")))
125 // return toJcrPath((String[]) null);// root
126 // else
127 return toJcrPath(arr);
128 }
129
130 private String toJcrPath(String[] path) {
131 if (path == null)
132 return "/";
133 if (path.length < mountDepth)
134 throw new IllegalArgumentException(
135 "Path " + Arrays.asList(path) + " is no compatible with mount " + mountPath);
136
137 if (!isBase()) {
138 // check mount compatibility
139 StringBuilder mount = new StringBuilder();
140 mount.append('/');
141 for (int i = 0; i < mountDepth; i++) {
142 if (i != 0)
143 mount.append('/');
144 mount.append(Text.escapeIllegalJcrChars(path[i]));
145 }
146 if (!mountPath.equals(mount.toString()))
147 throw new IllegalArgumentException(
148 "Path " + Arrays.asList(path) + " is no compatible with mount " + mountPath);
149 }
150
151 StringBuilder sb = new StringBuilder();
152 sb.append('/');
153 for (int i = mountDepth; i < path.length; i++) {
154 if (i != mountDepth)
155 sb.append('/');
156 sb.append(Text.escapeIllegalJcrChars(path[i]));
157 }
158 return sb.toString();
159 }
160
161 public String getMountPath() {
162 return mountPath;
163 }
164
165 public String getWorkspaceName() {
166 return workspaceName;
167 }
168
169 public int getMountDepth() {
170 return mountDepth;
171 }
172
173 @Override
174 public int hashCode() {
175 return workspaceName.hashCode();
176 }
177
178 @Override
179 public boolean equals(Object obj) {
180 if (!(obj instanceof WorkspaceFileStore))
181 return false;
182 WorkspaceFileStore other = (WorkspaceFileStore) obj;
183 return workspaceName.equals(other.workspaceName);
184 }
185
186 @Override
187 public String toString() {
188 return "WorkspaceFileStore " + workspaceName;
189 }
190
191 }