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
;
9 import javax
.jcr
.Repository
;
10 import javax
.jcr
.RepositoryException
;
11 import javax
.jcr
.Session
;
12 import javax
.xml
.namespace
.NamespaceContext
;
14 import org
.argeo
.api
.acr
.Content
;
15 import org
.argeo
.api
.acr
.ContentUtils
;
16 import org
.argeo
.api
.acr
.spi
.ContentProvider
;
17 import org
.argeo
.api
.acr
.spi
.ProvidedSession
;
18 import org
.argeo
.cms
.jcr
.CmsJcrUtils
;
19 import org
.argeo
.jcr
.JcrException
;
20 import org
.argeo
.jcr
.JcrUtils
;
22 /** A JCR workspace accessed as an {@link ContentProvider}. */
23 public class JcrContentProvider
implements ContentProvider
, NamespaceContext
{
24 private Repository jcrRepository
;
25 private Session adminSession
;
27 private Map
<ProvidedSession
, JcrSessionAdapter
> sessionAdapters
= Collections
.synchronizedMap(new HashMap
<>());
30 adminSession
= CmsJcrUtils
.openDataAdminSession(jcrRepository
, null);
34 JcrUtils
.logoutQuietly(adminSession
);
37 public void setJcrRepository(Repository jcrRepository
) {
38 this.jcrRepository
= jcrRepository
;
42 public Content
get(ProvidedSession contentSession
, String mountPath
, String relativePath
) {
43 String jcrWorkspace
= ContentUtils
.getParentPath(mountPath
)[1];
44 String jcrPath
= "/" + relativePath
;
45 return new JcrContent(contentSession
, this, jcrWorkspace
, jcrPath
);
48 public Session
getJcrSession(ProvidedSession contentSession
, String jcrWorkspace
) {
49 JcrSessionAdapter sessionAdapter
= sessionAdapters
.get(contentSession
);
50 if (sessionAdapter
== null) {
51 final JcrSessionAdapter newSessionAdapter
= new JcrSessionAdapter(jcrRepository
,
52 contentSession
.getSubject());
53 sessionAdapters
.put(contentSession
, newSessionAdapter
);
54 contentSession
.onClose().thenAccept((s
) -> newSessionAdapter
.close());
55 sessionAdapter
= newSessionAdapter
;
58 Session jcrSession
= sessionAdapter
.getSession(jcrWorkspace
);
66 public String
getNamespaceURI(String prefix
) {
68 return adminSession
.getNamespaceURI(prefix
);
69 } catch (RepositoryException e
) {
70 throw new JcrException(e
);
75 public String
getPrefix(String namespaceURI
) {
77 return adminSession
.getNamespacePrefix(namespaceURI
);
78 } catch (RepositoryException e
) {
79 throw new JcrException(e
);
84 public Iterator
<String
> getPrefixes(String namespaceURI
) {
86 return Arrays
.asList(adminSession
.getNamespacePrefix(namespaceURI
)).iterator();
87 } catch (RepositoryException e
) {
88 throw new JcrException(e
);