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
;
34 private String jcrWorkspace
;
36 private Map
<ProvidedSession
, JcrSessionAdapter
> sessionAdapters
= Collections
.synchronizedMap(new HashMap
<>());
38 public void start(Map
<String
, String
> properties
) {
39 mountPath
= properties
.get(CmsConstants
.ACR_MOUNT_PATH
);
40 if ("/".equals(mountPath
))
41 throw new IllegalArgumentException("JCR content provider cannot be root /");
42 Objects
.requireNonNull(mountPath
);
43 jcrWorkspace
= ContentUtils
.getParentPath(mountPath
)[1];
44 adminSession
= CmsJcrUtils
.openDataAdminSession(jcrRepository
, jcrWorkspace
);
48 if (adminSession
.isLive())
49 JcrUtils
.logoutQuietly(adminSession
);
52 public void setJcrRepository(Repository jcrRepository
) {
53 this.jcrRepository
= jcrRepository
;
57 public ProvidedContent
get(ProvidedSession contentSession
, String relativePath
) {
58 String jcrPath
= "/" + relativePath
;
59 return new JcrContent(contentSession
, this, jcrWorkspace
, jcrPath
);
63 public boolean exists(ProvidedSession contentSession
, String relativePath
) {
64 String jcrWorkspace
= ContentUtils
.getParentPath(mountPath
)[1];
65 String jcrPath
= "/" + relativePath
;
66 return new JcrContent(contentSession
, this, jcrWorkspace
, jcrPath
).exists();
69 public Session
getJcrSession(ProvidedSession contentSession
, String jcrWorkspace
) {
70 JcrSessionAdapter sessionAdapter
= sessionAdapters
.get(contentSession
);
71 if (sessionAdapter
== null) {
72 final JcrSessionAdapter newSessionAdapter
= new JcrSessionAdapter(jcrRepository
, contentSession
,
73 contentSession
.getSubject());
74 sessionAdapters
.put(contentSession
, newSessionAdapter
);
75 contentSession
.onClose().thenAccept((s
) -> newSessionAdapter
.close());
76 sessionAdapter
= newSessionAdapter
;
79 Session jcrSession
= sessionAdapter
.getSession(jcrWorkspace
);
83 public Session
getJcrSession(Content content
, String jcrWorkspace
) {
84 return getJcrSession(((ProvidedContent
) content
).getSession(), jcrWorkspace
);
88 public String
getMountPath() {
96 public String
getNamespaceURI(String prefix
) {
98 return adminSession
.getNamespaceURI(prefix
);
99 } catch (RepositoryException e
) {
100 throw new JcrException(e
);
105 public String
getPrefix(String namespaceURI
) {
107 return adminSession
.getNamespacePrefix(namespaceURI
);
108 } catch (RepositoryException e
) {
109 throw new JcrException(e
);
114 public Iterator
<String
> getPrefixes(String namespaceURI
) {
116 return Arrays
.asList(adminSession
.getNamespacePrefix(namespaceURI
)).iterator();
117 } catch (RepositoryException e
) {
118 throw new JcrException(e
);