Centralise configure script
[lgpl/argeo-commons.git] / jcr / org.argeo.cms.jcr / src / org / argeo / cms / jcr / acr / JcrSessionAdapter.java
index 10b243bcbe24c8c4b90e2bec8de81172234d26c7..ae8ae80f29867636005d0c8428da88f3c769986d 100644 (file)
@@ -3,15 +3,16 @@ package org.argeo.cms.jcr.acr;
 import java.security.PrivilegedAction;
 import java.util.Collections;
 import java.util.HashMap;
-import java.util.HashSet;
 import java.util.Map;
-import java.util.Set;
 
 import javax.jcr.Repository;
 import javax.jcr.RepositoryException;
 import javax.jcr.Session;
 import javax.security.auth.Subject;
 
+import org.apache.jackrabbit.core.SessionImpl;
+import org.argeo.api.acr.spi.ProvidedSession;
+import org.argeo.jcr.JcrException;
 import org.argeo.jcr.JcrUtils;
 
 /** Manages JCR {@link Session} in an ACR context. */
@@ -19,12 +20,17 @@ class JcrSessionAdapter {
        private Repository repository;
        private Subject subject;
 
+       private ProvidedSession contentSession;
+
        private Map<Thread, Map<String, Session>> threadSessions = Collections.synchronizedMap(new HashMap<>());
 
        private boolean closed = false;
 
-       public JcrSessionAdapter(Repository repository, Subject subject) {
+       private Thread lastRetrievingThread = null;
+
+       public JcrSessionAdapter(Repository repository, ProvidedSession contentSession, Subject subject) {
                this.repository = repository;
+               this.contentSession = contentSession;
                this.subject = subject;
        }
 
@@ -44,6 +50,8 @@ class JcrSessionAdapter {
                        throw new IllegalStateException("JCR session adapter is closed.");
 
                Thread currentThread = Thread.currentThread();
+               if (lastRetrievingThread == null)
+                       lastRetrievingThread = currentThread;
 
                Map<String, Session> threadSession = threadSessions.get(currentThread);
                if (threadSession == null) {
@@ -55,7 +63,12 @@ class JcrSessionAdapter {
                if (session == null) {
                        session = Subject.doAs(subject, (PrivilegedAction<Session>) () -> {
                                try {
+//                                     String username = CurrentUser.getUsername(subject);
+//                                     SimpleCredentials credentials = new SimpleCredentials(username, new char[0]);
+//                                     credentials.setAttribute(ProvidedSession.class.getName(), contentSession);
                                        Session sess = repository.login(workspace);
+                                       // Jackrabbit specific:
+                                       ((SessionImpl)sess).setAttribute(ProvidedSession.class.getName(), contentSession);
                                        return sess;
                                } catch (RepositoryException e) {
                                        throw new IllegalStateException("Cannot log in to " + workspace, e);
@@ -63,6 +76,15 @@ class JcrSessionAdapter {
                        });
                        threadSession.put(workspace, session);
                }
+
+               if (lastRetrievingThread != currentThread) {
+                       try {
+                               session.refresh(true);
+                       } catch (RepositoryException e) {
+                               throw new JcrException("Cannot refresh JCR session " + session, e);
+                       }
+               }
+               lastRetrievingThread = currentThread;
                return session;
        }