1 package org
.argeo
.cms
.internal
.kernel
;
3 import java
.io
.IOException
;
5 import java
.nio
.file
.FileSystem
;
6 import java
.nio
.file
.FileSystemAlreadyExistsException
;
7 import java
.nio
.file
.Path
;
8 import java
.util
.HashMap
;
11 import javax
.jcr
.Repository
;
12 import javax
.jcr
.Session
;
14 import org
.argeo
.cms
.CmsException
;
15 import org
.argeo
.cms
.auth
.CurrentUser
;
16 import org
.argeo
.jackrabbit
.fs
.AbstractJackrabbitFsProvider
;
17 import org
.argeo
.jcr
.fs
.JcrFileSystem
;
18 import org
.argeo
.jcr
.fs
.JcrFsException
;
19 import org
.osgi
.framework
.BundleContext
;
20 import org
.osgi
.framework
.FrameworkUtil
;
22 public class CmsFsProvider
extends AbstractJackrabbitFsProvider
{
23 private Map
<String
, JcrFileSystem
> fileSystems
= new HashMap
<>();
24 private BundleContext bc
= FrameworkUtil
.getBundle(CmsFsProvider
.class).getBundleContext();
27 public String
getScheme() {
32 public FileSystem
newFileSystem(URI uri
, Map
<String
, ?
> env
) throws IOException
{
33 String username
= CurrentUser
.getUsername();
34 if (username
== null) {
35 // TODO deal with anonymous
38 if (fileSystems
.containsKey(username
))
39 throw new FileSystemAlreadyExistsException("CMS file system already exists for user " + username
);
42 Repository repository
= bc
43 .getService(bc
.getServiceReferences(Repository
.class, "(cn=node)").iterator().next());
44 Session session
= repository
.login();
45 JcrFileSystem fileSystem
= new JcrFileSystem(this, session
);
46 fileSystems
.put(username
, fileSystem
);
48 } catch (Exception e
) {
49 throw new CmsException("Cannot open file system " + uri
+ " for user " + username
, e
);
54 public FileSystem
getFileSystem(URI uri
) {
55 return currentUserFileSystem();
59 public Path
getPath(URI uri
) {
60 JcrFileSystem fileSystem
= currentUserFileSystem();
61 String path
= uri
.getPath();
62 if (fileSystem
== null)
64 fileSystem
= (JcrFileSystem
) newFileSystem(uri
, new HashMap
<String
, Object
>());
65 } catch (IOException e
) {
66 throw new JcrFsException("Could not autocreate file system", e
);
68 return fileSystem
.getPath(path
);
71 protected JcrFileSystem
currentUserFileSystem() {
72 String username
= CurrentUser
.getUsername();
73 return fileSystems
.get(username
);