X-Git-Url: https://git.argeo.org/?a=blobdiff_plain;f=jcr%2Forg.argeo.cms.jcr%2Fsrc%2Forg%2Fargeo%2Fcms%2Fjcr%2Facr%2FJcrSessionAdapter.java;h=ae8ae80f29867636005d0c8428da88f3c769986d;hb=da9d144b6b241e1526a3bd255dff905a7969a5bc;hp=896f30a9e0248c1c8896ee7f1eac712e489af64d;hpb=284e5f62177da9640a16435107925d68f0611dcb;p=lgpl%2Fargeo-commons.git diff --git a/jcr/org.argeo.cms.jcr/src/org/argeo/cms/jcr/acr/JcrSessionAdapter.java b/jcr/org.argeo.cms.jcr/src/org/argeo/cms/jcr/acr/JcrSessionAdapter.java index 896f30a9e..ae8ae80f2 100644 --- a/jcr/org.argeo.cms.jcr/src/org/argeo/cms/jcr/acr/JcrSessionAdapter.java +++ b/jcr/org.argeo.cms.jcr/src/org/argeo/cms/jcr/acr/JcrSessionAdapter.java @@ -10,6 +10,9 @@ 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. */ @@ -17,12 +20,17 @@ class JcrSessionAdapter { private Repository repository; private Subject subject; + private ProvidedSession contentSession; + private Map> 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; } @@ -42,6 +50,8 @@ class JcrSessionAdapter { throw new IllegalStateException("JCR session adapter is closed."); Thread currentThread = Thread.currentThread(); + if (lastRetrievingThread == null) + lastRetrievingThread = currentThread; Map threadSession = threadSessions.get(currentThread); if (threadSession == null) { @@ -53,7 +63,12 @@ class JcrSessionAdapter { if (session == null) { session = Subject.doAs(subject, (PrivilegedAction) () -> { 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); @@ -61,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; }