]>
git.argeo.org Git - lgpl/argeo-commons.git/blob - org.argeo.cms.jcr/src/org/argeo/jcr/fs/WorkspaceFileStore.java
1 package org
.argeo
.jcr
.fs
;
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
;
10 import javax
.jcr
.RepositoryException
;
11 import javax
.jcr
.Session
;
12 import javax
.jcr
.Workspace
;
14 import org
.argeo
.jcr
.JcrUtils
;
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
;
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)
35 mountDepth
= mountPath
.split(JcrPath
.separator
).length
- 1;
37 this.workspace
= workspace
;
38 this.workspaceName
= workspace
.getName();
42 JcrUtils
.logoutQuietly(workspace
.getSession());
46 public String
name() {
47 return workspace
.getName();
51 public String
type() {
56 public boolean isReadOnly() {
61 public long getTotalSpace() throws IOException
{
66 public long getUsableSpace() throws IOException
{
71 public long getUnallocatedSpace() throws IOException
{
76 public boolean supportsFileAttributeView(Class
<?
extends FileAttributeView
> type
) {
81 public boolean supportsFileAttributeView(String name
) {
86 public <V
extends FileStoreAttributeView
> V
getFileStoreAttributeView(Class
<V
> type
) {
91 public Object
getAttribute(String attribute
) throws IOException
{
92 return workspace
.getSession().getRepository().getDescriptor(attribute
);
95 public Workspace
getWorkspace() {
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();
107 public boolean isBase() {
108 return mountPath
== null;
111 Node
toNode(String
[] fullPath
) throws RepositoryException
{
112 String jcrPath
= toJcrPath(fullPath
);
113 Session session
= workspace
.getSession();
114 if (!session
.itemExists(jcrPath
))
116 Node node
= session
.getNode(jcrPath
);
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
127 return toJcrPath(arr
);
130 private String
toJcrPath(String
[] path
) {
133 if (path
.length
< mountDepth
)
134 throw new IllegalArgumentException(
135 "Path " + Arrays
.asList(path
) + " is no compatible with mount " + mountPath
);
138 // check mount compatibility
139 StringBuilder mount
= new StringBuilder();
141 for (int i
= 0; i
< mountDepth
; i
++) {
144 mount
.append(Text
.escapeIllegalJcrChars(path
[i
]));
146 if (!mountPath
.equals(mount
.toString()))
147 throw new IllegalArgumentException(
148 "Path " + Arrays
.asList(path
) + " is no compatible with mount " + mountPath
);
151 StringBuilder sb
= new StringBuilder();
153 for (int i
= mountDepth
; i
< path
.length
; i
++) {
156 sb
.append(Text
.escapeIllegalJcrChars(path
[i
]));
158 return sb
.toString();
161 public String
getMountPath() {
165 public String
getWorkspaceName() {
166 return workspaceName
;
169 public int getMountDepth() {
174 public int hashCode() {
175 return workspaceName
.hashCode();
179 public boolean equals(Object obj
) {
180 if (!(obj
instanceof WorkspaceFileStore
))
182 WorkspaceFileStore other
= (WorkspaceFileStore
) obj
;
183 return workspaceName
.equals(other
.workspaceName
);
187 public String
toString() {
188 return "WorkspaceFileStore " + workspaceName
;