+ public void setDefaultUsername(String defaultUsername) {
+ this.defaultUsername = defaultUsername;
+ }
+
+ public void setDefaultPassword(String defaultPassword) {
+ this.defaultPassword = defaultPassword;
+ }
+
+ public void setForceDefaultCredentials(Boolean forceDefaultCredentials) {
+ this.forceDefaultCredentials = forceDefaultCredentials;
+ }
+
+ public void setWorkspace(String workspace) {
+ this.workspace = workspace;
+ }
+
+ protected class JcrSessionInvocationHandler implements InvocationHandler {
+
+ public Object invoke(Object proxy, Method method, Object[] args)
+ throws Throwable {
+ Session threadSession = session.get();
+ if (threadSession == null) {
+ if ("logout".equals(method.getName()))// no need to login
+ return Void.TYPE;
+ else if ("toString".equals(method.getName()))// maybe logging
+ return "Uninitialized Argeo thread bound JCR session";
+ threadSession = login();
+ }
+
+ 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());
+ }
+ }
+ return ret;
+ }
+ }
+
+ /** Monitors registered thread in order to clean up dead ones. */
+ private class MonitoringThread extends Thread {
+
+ @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
+ }
+ }
+ }
+ }
+
+ }