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 class ThreadBoundJcrSessionFactory {
private final static Log log = LogFactory
.getLog(ThreadBoundJcrSessionFactory.class);
/** Logs in to the repository using various strategies. */
protected Session login() {
- // discard sesison previoussly attached to this thread
+ if (!isActive())
+ throw new ArgeoException("Thread bound session factory inactive");
+
+ // discard session previously attached to this thread
Thread thread = Thread.currentThread();
if (activeSessions.containsKey(thread.getId())) {
Session oldSession = activeSessions.remove(thread.getId());
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;
+
if (log.isDebugEnabled())
log.debug("Cleaning up " + activeSessions.size()
+ " active JCR sessions...");
sess.logout();
}
activeSessions.clear();
- monitoringThread.join(1000);
}
protected Boolean isActive() {
notifyAll();
}
+ protected synchronized void removeSession(Thread thread) {
+ if (!isActive())
+ return;
+ activeSessions.remove(thread.getId());
+ threads.remove(thread);
+ }
+
+ protected synchronized void cleanDeadThreads() {
+ if (!isActive())
+ return;
+ Iterator<Thread> it = threads.iterator();
+ while (it.hasNext()) {
+ Thread thread = it.next();
+ if (!thread.isAlive() && isActive()) {
+ if (activeSessions.containsKey(thread.getId())) {
+ Session session = activeSessions.get(thread.getId());
+ activeSessions.remove(thread.getId());
+ session.logout();
+ if (log.isDebugEnabled())
+ log.debug("Cleaned up JCR session (userID="
+ + session.getUserID() + ") from dead thread "
+ + thread.getId());
+ }
+ it.remove();
+ }
+ }
+ try {
+ wait(1000);
+ } catch (InterruptedException e) {
+ // silent
+ }
+ }
+
public Class<? extends Session> getObjectType() {
return Session.class;
}
threadSession = login();
}
+ preCall(threadSession);
Object ret = method.invoke(threadSession, args);
if ("logout".equals(method.getName())) {
- synchronized (ThreadBoundJcrSessionFactory.this) {
- session.remove();
- Thread thread = Thread.currentThread();
- if (isActive()) {
- activeSessions.remove(thread.getId());
- threads.remove(thread);
- }
- if (log.isTraceEnabled())
- log.trace("Logged out JCR session (userId="
- + threadSession.getUserID() + ") on thread "
- + thread.getId());
- }
+ session.remove();
+ Thread thread = Thread.currentThread();
+ removeSession(thread);
+ if (log.isTraceEnabled())
+ log.trace("Logged out JCR session (userId="
+ + threadSession.getUserID() + ") on thread "
+ + thread.getId());
}
return ret;
}
@Override
public void run() {
while (isActive()) {
- Iterator<Thread> it = threads.iterator();
- while (it.hasNext()) {
- Thread thread = it.next();
- if (!thread.isAlive() && isActive()) {
- if (activeSessions.containsKey(thread.getId())) {
- Session session = activeSessions
- .get(thread.getId());
- activeSessions.remove(thread.getId());
- session.logout();
- if (log.isDebugEnabled())
- log.debug("Cleaned up JCR session (userID="
- + session.getUserID()
- + ") from dead thread "
- + thread.getId());
- }
- it.remove();
- }
- }
-
- synchronized (ThreadBoundJcrSessionFactory.this) {
- try {
- ThreadBoundJcrSessionFactory.this.wait(1000);
- } catch (InterruptedException e) {
- // silent
- }
- }
+ cleanDeadThreads();
}
}