X-Git-Url: http://git.argeo.org/?a=blobdiff_plain;f=server%2Fruntime%2Forg.argeo.server.jcr%2Fsrc%2Fmain%2Fjava%2Forg%2Fargeo%2Fjcr%2FThreadBoundJcrSessionFactory.java;h=aaafdc26a704f2781c11164c07aec708a3a00769;hb=2f1a4e0b8313d3872f7cbc73cea9f0d56cfe4fe6;hp=01a056aa29432d661d157e885ff1de485e9f228f;hpb=b3992d072c6f47e7617556137edef5cd492d076e;p=lgpl%2Fargeo-commons.git diff --git a/server/runtime/org.argeo.server.jcr/src/main/java/org/argeo/jcr/ThreadBoundJcrSessionFactory.java b/server/runtime/org.argeo.server.jcr/src/main/java/org/argeo/jcr/ThreadBoundJcrSessionFactory.java index 01a056aa2..aaafdc26a 100644 --- a/server/runtime/org.argeo.server.jcr/src/main/java/org/argeo/jcr/ThreadBoundJcrSessionFactory.java +++ b/server/runtime/org.argeo.server.jcr/src/main/java/org/argeo/jcr/ThreadBoundJcrSessionFactory.java @@ -42,12 +42,14 @@ public class ThreadBoundJcrSessionFactory implements FactoryBean, .getLog(ThreadBoundJcrSessionFactory.class); private Repository repository; - private List activeSessions = Collections + private final List activeSessions = Collections .synchronizedList(new ArrayList()); private ThreadLocal session = new ThreadLocal(); private boolean destroying = false; private final Session proxiedSession; + /** If workspace is null, default will be used. */ + private String workspace = null; private String defaultUsername = "demo"; private String defaultPassword = "demo"; @@ -56,33 +58,11 @@ public class ThreadBoundJcrSessionFactory implements FactoryBean, public ThreadBoundJcrSessionFactory() { Class[] interfaces = { Session.class }; proxiedSession = (Session) Proxy.newProxyInstance(getClass() - .getClassLoader(), interfaces, new 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; - threadSession = login(); - session.set(threadSession); - } - - Object ret = method.invoke(threadSession, args); - if ("logout".equals(method.getName())) { - session.remove(); - if (!destroying) - activeSessions.remove(threadSession); - if (log.isTraceEnabled()) - log.trace("Logged out from JCR session " - + threadSession + "; userId=" - + threadSession.getUserID()); - } - return ret; - } - }); + .getClassLoader(), interfaces, + new JcrSessionInvocationHandler()); } + /** Logs in to the repository using various strategies. */ protected Session login() { Session newSession = null; // first try to login without credentials, assuming the underlying login @@ -90,7 +70,7 @@ public class ThreadBoundJcrSessionFactory implements FactoryBean, // Security) if (!forceDefaultCredentials) try { - newSession = repository.login(); + newSession = repository.login(workspace); } catch (LoginException e1) { log.warn("Cannot login without credentials: " + e1.getMessage()); // invalid credentials, go to the next step @@ -104,7 +84,7 @@ public class ThreadBoundJcrSessionFactory implements FactoryBean, try { SimpleCredentials sc = new SimpleCredentials(defaultUsername, defaultPassword.toCharArray()); - newSession = repository.login(sc); + newSession = repository.login(sc, workspace); } catch (RepositoryException e) { throw new ArgeoException("Cannot log in to repository", e); } @@ -157,4 +137,34 @@ public class ThreadBoundJcrSessionFactory implements FactoryBean, 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(); + session.set(threadSession); + } + + Object ret = method.invoke(threadSession, args); + if ("logout".equals(method.getName())) { + session.remove(); + if (!destroying) + activeSessions.remove(threadSession); + if (log.isTraceEnabled()) + log.trace("Logged out from JCR session " + threadSession + + "; userId=" + threadSession.getUserID()); + } + return ret; + } + } }