Admin session to the proper content provider workspace
[lgpl/argeo-commons.git] / jcr / org.argeo.cms.jcr / src / org / argeo / cms / jcr / acr / JcrContentProvider.java
index fc4a61bf9e61a72c316f5b5f5f8bf50a291932fb..eaa27b7fc631d806fde1f1590c0eca142f786cd7 100644 (file)
@@ -5,34 +5,48 @@ import java.util.Collections;
 import java.util.HashMap;
 import java.util.Iterator;
 import java.util.Map;
+import java.util.Objects;
 
-import javax.jcr.Node;
 import javax.jcr.Repository;
 import javax.jcr.RepositoryException;
 import javax.jcr.Session;
 import javax.xml.namespace.NamespaceContext;
 
 import org.argeo.api.acr.Content;
-import org.argeo.api.acr.ContentUtils;
 import org.argeo.api.acr.spi.ContentProvider;
+import org.argeo.api.acr.spi.ProvidedContent;
 import org.argeo.api.acr.spi.ProvidedSession;
+import org.argeo.api.cms.CmsConstants;
+import org.argeo.cms.acr.ContentUtils;
 import org.argeo.cms.jcr.CmsJcrUtils;
 import org.argeo.jcr.JcrException;
 import org.argeo.jcr.JcrUtils;
 
 /** A JCR workspace accessed as an {@link ContentProvider}. */
 public class JcrContentProvider implements ContentProvider, NamespaceContext {
+
        private Repository jcrRepository;
        private Session adminSession;
 
+       private String mountPath;
+
+       // cache
+       private String jcrWorkspace;
+
        private Map<ProvidedSession, JcrSessionAdapter> sessionAdapters = Collections.synchronizedMap(new HashMap<>());
 
-       public void start() {
-               adminSession = CmsJcrUtils.openDataAdminSession(jcrRepository, null);
+       public void start(Map<String, String> properties) {
+               mountPath = properties.get(CmsConstants.ACR_MOUNT_PATH);
+               if ("/".equals(mountPath))
+                       throw new IllegalArgumentException("JCR content provider cannot be root /");
+               Objects.requireNonNull(mountPath);
+               jcrWorkspace = ContentUtils.getParentPath(mountPath)[1];
+               adminSession = CmsJcrUtils.openDataAdminSession(jcrRepository, jcrWorkspace);
        }
 
        public void stop() {
-               JcrUtils.logoutQuietly(adminSession);
+               if (adminSession.isLive())
+                       JcrUtils.logoutQuietly(adminSession);
        }
 
        public void setJcrRepository(Repository jcrRepository) {
@@ -40,26 +54,39 @@ public class JcrContentProvider implements ContentProvider, NamespaceContext {
        }
 
        @Override
-       public Content get(ProvidedSession contentSession, String mountPath, String relativePath) {
-               String workspace = ContentUtils.getParentPath(mountPath)[1];
+       public ProvidedContent get(ProvidedSession contentSession, String relativePath) {
+               String jcrPath = "/" + relativePath;
+               return new JcrContent(contentSession, this, jcrWorkspace, jcrPath);
+       }
+
+       @Override
+       public boolean exists(ProvidedSession contentSession, String relativePath) {
+               String jcrWorkspace = ContentUtils.getParentPath(mountPath)[1];
+               String jcrPath = "/" + relativePath;
+               return new JcrContent(contentSession, this, jcrWorkspace, jcrPath).exists();
+       }
+
+       public Session getJcrSession(ProvidedSession contentSession, String jcrWorkspace) {
                JcrSessionAdapter sessionAdapter = sessionAdapters.get(contentSession);
                if (sessionAdapter == null) {
-                       final JcrSessionAdapter newSessionAdapter = new JcrSessionAdapter(jcrRepository,
+                       final JcrSessionAdapter newSessionAdapter = new JcrSessionAdapter(jcrRepository, contentSession,
                                        contentSession.getSubject());
                        sessionAdapters.put(contentSession, newSessionAdapter);
                        contentSession.onClose().thenAccept((s) -> newSessionAdapter.close());
                        sessionAdapter = newSessionAdapter;
                }
 
-               Session jcrSession = sessionAdapter.getSession(workspace);
-               String jcrPath = "/" + relativePath;
-               try {
-                       Node node = jcrSession.getNode(jcrPath);
-                       return new JcrContent(contentSession, this, node);
-               } catch (RepositoryException e) {
-                       throw new JcrException("Cannot get JCR content '" + jcrPath + ", mounted from '" + mountPath
-                                       + "' with JCR session " + jcrSession, e);
-               }
+               Session jcrSession = sessionAdapter.getSession(jcrWorkspace);
+               return jcrSession;
+       }
+
+       public Session getJcrSession(Content content, String jcrWorkspace) {
+               return getJcrSession(((ProvidedContent) content).getSession(), jcrWorkspace);
+       }
+
+       @Override
+       public String getMountPath() {
+               return mountPath;
        }
 
        /*