]> git.argeo.org Git - lgpl/argeo-commons.git/blobdiff - server/runtime/org.argeo.server.jcr/src/main/java/org/argeo/jcr/ThreadBoundJcrSessionFactory.java
Code cleaning / documentation
[lgpl/argeo-commons.git] / server / runtime / org.argeo.server.jcr / src / main / java / org / argeo / jcr / ThreadBoundJcrSessionFactory.java
index 1c33a43dec9a6788831fa92a2e8b83326645438c..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;
@@ -35,13 +36,9 @@ import javax.jcr.SimpleCredentials;
 import org.apache.commons.logging.Log;
 import org.apache.commons.logging.LogFactory;
 import org.argeo.ArgeoException;
-import org.springframework.beans.factory.DisposableBean;
-import org.springframework.beans.factory.FactoryBean;
-import org.springframework.beans.factory.InitializingBean;
 
 /** Proxy JCR sessions and attach them to calling threads. */
-public class ThreadBoundJcrSessionFactory implements FactoryBean,
-               InitializingBean, DisposableBean {
+public abstract class ThreadBoundJcrSessionFactory {
        private final static Log log = LogFactory
                        .getLog(ThreadBoundJcrSessionFactory.class);
 
@@ -73,7 +70,7 @@ public class ThreadBoundJcrSessionFactory implements FactoryBean,
        }
 
        /** 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");
 
@@ -126,12 +123,12 @@ public class ThreadBoundJcrSessionFactory implements FactoryBean,
                return proxiedSession;
        }
 
-       public void afterPropertiesSet() throws Exception {
+       public void init() throws Exception {
                monitoringThread = new MonitoringThread();
                monitoringThread.start();
        }
 
-       public synchronized void destroy() throws Exception {
+       public synchronized void dispose() throws Exception {
                if (activeSessions.size() == 0)
                        return;
 
@@ -228,7 +225,7 @@ public class ThreadBoundJcrSessionFactory implements FactoryBean,
        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
@@ -239,7 +236,16 @@ public class ThreadBoundJcrSessionFactory implements FactoryBean,
                        }
 
                        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();
@@ -256,6 +262,10 @@ public class ThreadBoundJcrSessionFactory implements FactoryBean,
        /** 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()) {