]>
git.argeo.org Git - lgpl/argeo-commons.git/blob - WorkspaceFileStore.java
0b81d5569058882cfcae2a29db71cc5d0ff5956b
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 int mountDepth
;
22 public WorkspaceFileStore(String mountPath
, Workspace workspace
) {
23 if ("/".equals(mountPath
) || "".equals(mountPath
))
24 throw new IllegalArgumentException(
25 "Mount path '" + mountPath
+ "' is unsupported, use null for the base file store");
26 if (mountPath
!= null && !mountPath
.startsWith(JcrPath
.separator
))
27 throw new IllegalArgumentException("Mount path '" + mountPath
+ "' cannot end with /");
28 if (mountPath
!= null && mountPath
.endsWith(JcrPath
.separator
))
29 throw new IllegalArgumentException("Mount path '" + mountPath
+ "' cannot end with /");
30 this.mountPath
= mountPath
;
31 if (mountPath
== null)
34 mountDepth
= mountPath
.split(JcrPath
.separator
).length
- 1;
36 this.workspace
= workspace
;
40 JcrUtils
.logoutQuietly(workspace
.getSession());
44 public String
name() {
45 return workspace
.getName();
49 public String
type() {
54 public boolean isReadOnly() {
59 public long getTotalSpace() throws IOException
{
64 public long getUsableSpace() throws IOException
{
69 public long getUnallocatedSpace() throws IOException
{
74 public boolean supportsFileAttributeView(Class
<?
extends FileAttributeView
> type
) {
79 public boolean supportsFileAttributeView(String name
) {
84 public <V
extends FileStoreAttributeView
> V
getFileStoreAttributeView(Class
<V
> type
) {
89 public Object
getAttribute(String attribute
) throws IOException
{
90 return workspace
.getSession().getRepository().getDescriptor(attribute
);
93 public Workspace
getWorkspace() {
97 public String
toFsPath(Node node
) throws RepositoryException
{
98 String nodeWorkspaceName
= node
.getSession().getWorkspace().getName();
99 if (!nodeWorkspaceName
.equals(workspace
.getName()))
100 throw new IllegalArgumentException("Icompatible " + node
+ " from workspace '" + nodeWorkspaceName
101 + "' in file store '" + workspace
.getName() + "'");
102 return mountPath
== null ? node
.getPath() : mountPath
+ node
.getPath();
105 public boolean isBase() {
106 return mountPath
== null;
109 Node
toNode(String
[] fullPath
) throws RepositoryException
{
110 String jcrPath
= toJcrPath(fullPath
);
111 Session session
= workspace
.getSession();
112 if (!session
.itemExists(jcrPath
))
114 Node node
= session
.getNode(jcrPath
);
118 private String
toJcrPath(String
[] path
) {
121 if (path
.length
< mountDepth
)
122 throw new IllegalArgumentException(
123 "Path " + Arrays
.asList(path
) + " is no compatible with mount " + mountPath
);
126 // check mount compatibility
127 StringBuilder mount
= new StringBuilder();
129 for (int i
= 0; i
< mountDepth
; i
++) {
132 mount
.append(Text
.escapeIllegalJcrChars(path
[i
]));
134 if (!mountPath
.equals(mount
.toString()))
135 throw new IllegalArgumentException(
136 "Path " + Arrays
.asList(path
) + " is no compatible with mount " + mountPath
);
139 StringBuilder sb
= new StringBuilder();
141 for (int i
= mountDepth
; i
< path
.length
; i
++) {
144 sb
.append(Text
.escapeIllegalJcrChars(path
[i
]));
146 return sb
.toString();