1 package org
.argeo
.cms
.jcr
.acr
;
3 import java
.util
.Arrays
;
4 import java
.util
.Collections
;
5 import java
.util
.HashMap
;
6 import java
.util
.Iterator
;
8 import java
.util
.Objects
;
10 import javax
.jcr
.Repository
;
11 import javax
.jcr
.RepositoryException
;
12 import javax
.jcr
.Session
;
13 import javax
.xml
.namespace
.NamespaceContext
;
15 import org
.argeo
.api
.acr
.Content
;
16 import org
.argeo
.api
.acr
.spi
.ContentProvider
;
17 import org
.argeo
.api
.acr
.spi
.ProvidedContent
;
18 import org
.argeo
.api
.acr
.spi
.ProvidedSession
;
19 import org
.argeo
.api
.cms
.CmsConstants
;
20 import org
.argeo
.cms
.acr
.ContentUtils
;
21 import org
.argeo
.cms
.jcr
.CmsJcrUtils
;
22 import org
.argeo
.jcr
.JcrException
;
23 import org
.argeo
.jcr
.JcrUtils
;
25 /** A JCR workspace accessed as an {@link ContentProvider}. */
26 public class JcrContentProvider
implements ContentProvider
, NamespaceContext
{
28 private Repository jcrRepository
;
29 private Session adminSession
;
31 private String mountPath
;
33 private Map
<ProvidedSession
, JcrSessionAdapter
> sessionAdapters
= Collections
.synchronizedMap(new HashMap
<>());
35 public void start(Map
<String
, String
> properties
) {
36 mountPath
= properties
.get(CmsConstants
.ACR_MOUNT_PATH
);
37 if ("/".equals(mountPath
))
38 throw new IllegalArgumentException("JCR content provider cannot be root /");
39 Objects
.requireNonNull(mountPath
);
40 adminSession
= CmsJcrUtils
.openDataAdminSession(jcrRepository
, null);
44 if (adminSession
.isLive())
45 JcrUtils
.logoutQuietly(adminSession
);
48 public void setJcrRepository(Repository jcrRepository
) {
49 this.jcrRepository
= jcrRepository
;
53 public ProvidedContent
get(ProvidedSession contentSession
, String relativePath
) {
54 String jcrWorkspace
= ContentUtils
.getParentPath(mountPath
)[1];
55 String jcrPath
= "/" + relativePath
;
56 return new JcrContent(contentSession
, this, jcrWorkspace
, jcrPath
);
60 public boolean exists(ProvidedSession contentSession
, String relativePath
) {
61 String jcrWorkspace
= ContentUtils
.getParentPath(mountPath
)[1];
62 String jcrPath
= "/" + relativePath
;
63 return new JcrContent(contentSession
, this, jcrWorkspace
, jcrPath
).exists();
66 public Session
getJcrSession(ProvidedSession contentSession
, String jcrWorkspace
) {
67 JcrSessionAdapter sessionAdapter
= sessionAdapters
.get(contentSession
);
68 if (sessionAdapter
== null) {
69 final JcrSessionAdapter newSessionAdapter
= new JcrSessionAdapter(jcrRepository
, contentSession
,
70 contentSession
.getSubject());
71 sessionAdapters
.put(contentSession
, newSessionAdapter
);
72 contentSession
.onClose().thenAccept((s
) -> newSessionAdapter
.close());
73 sessionAdapter
= newSessionAdapter
;
76 Session jcrSession
= sessionAdapter
.getSession(jcrWorkspace
);
80 public Session
getJcrSession(Content content
, String jcrWorkspace
) {
81 return getJcrSession(((ProvidedContent
) content
).getSession(), jcrWorkspace
);
85 public String
getMountPath() {
93 public String
getNamespaceURI(String prefix
) {
95 return adminSession
.getNamespaceURI(prefix
);
96 } catch (RepositoryException e
) {
97 throw new JcrException(e
);
102 public String
getPrefix(String namespaceURI
) {
104 return adminSession
.getNamespacePrefix(namespaceURI
);
105 } catch (RepositoryException e
) {
106 throw new JcrException(e
);
111 public Iterator
<String
> getPrefixes(String namespaceURI
) {
113 return Arrays
.asList(adminSession
.getNamespacePrefix(namespaceURI
)).iterator();
114 } catch (RepositoryException e
) {
115 throw new JcrException(e
);