]> git.argeo.org Git - lgpl/argeo-commons.git/blobdiff - server/runtime/org.argeo.server.jcr/src/main/java/org/argeo/jcr/ThreadBoundJcrSessionFactory.java
Introduce JCR proxys
[lgpl/argeo-commons.git] / server / runtime / org.argeo.server.jcr / src / main / java / org / argeo / jcr / ThreadBoundJcrSessionFactory.java
index d548b6eddaa227a63be3449994e08ba6ad6da326..003bcc69b552fd956e5db28b8efd27e72f81237b 100644 (file)
@@ -17,6 +17,7 @@
 package org.argeo.jcr;
 
 import java.lang.reflect.InvocationHandler;
+import java.lang.reflect.InvocationTargetException;
 import java.lang.reflect.Method;
 import java.lang.reflect.Proxy;
 import java.util.ArrayList;
@@ -37,7 +38,7 @@ import org.apache.commons.logging.LogFactory;
 import org.argeo.ArgeoException;
 
 /** Proxy JCR sessions and attach them to calling threads. */
-public class ThreadBoundJcrSessionFactory {
+public abstract class ThreadBoundJcrSessionFactory {
        private final static Log log = LogFactory
                        .getLog(ThreadBoundJcrSessionFactory.class);
 
@@ -69,7 +70,7 @@ public class ThreadBoundJcrSessionFactory {
        }
 
        /** Logs in to the repository using various strategies. */
-       protected Session login() {
+       protected synchronized Session login() {
                if (!isActive())
                        throw new ArgeoException("Thread bound session factory inactive");
 
@@ -224,7 +225,7 @@ public class ThreadBoundJcrSessionFactory {
        protected class JcrSessionInvocationHandler implements InvocationHandler {
 
                public Object invoke(Object proxy, Method method, Object[] args)
-                               throws Throwable {
+                               throws Throwable, RepositoryException {
                        Session threadSession = session.get();
                        if (threadSession == null) {
                                if ("logout".equals(method.getName()))// no need to login
@@ -235,7 +236,16 @@ public class ThreadBoundJcrSessionFactory {
                        }
 
                        preCall(threadSession);
-                       Object ret = method.invoke(threadSession, args);
+                       Object ret;
+                       try {
+                               ret = method.invoke(threadSession, args);
+                       } catch (InvocationTargetException e) {
+                               Throwable cause = e.getCause();
+                               if (cause instanceof RepositoryException)
+                                       throw (RepositoryException) cause;
+                               else
+                                       throw cause;
+                       }
                        if ("logout".equals(method.getName())) {
                                session.remove();
                                Thread thread = Thread.currentThread();
@@ -252,6 +262,10 @@ public class ThreadBoundJcrSessionFactory {
        /** Monitors registered thread in order to clean up dead ones. */
        private class MonitoringThread extends Thread {
 
+               public MonitoringThread() {
+                       super("ThreadBound JCR Session Monitor");
+               }
+
                @Override
                public void run() {
                        while (isActive()) {