/*
- * Copyright (C) 2010 Mathieu Baudier <mbaudier@argeo.org>
+ * Copyright (C) 2007-2012 Argeo GmbH
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
-
package org.argeo.jcr;
import java.lang.reflect.InvocationHandler;
.getLog(ThreadBoundJcrSessionFactory.class);
private Repository repository;
+ /** can be injected as list, only used if repository is null */
+ private List<Repository> repositories;
private ThreadLocal<Session> session = new ThreadLocal<Session>();
private final Session proxiedSession;
public ThreadBoundJcrSessionFactory() {
Class<?>[] interfaces = { Session.class };
- proxiedSession = (Session) Proxy.newProxyInstance(getClass()
- .getClassLoader(), interfaces,
- new JcrSessionInvocationHandler());
+ proxiedSession = (Session) Proxy.newProxyInstance(
+ ThreadBoundJcrSessionFactory.class.getClassLoader(),
+ interfaces, new JcrSessionInvocationHandler());
}
/** Logs in to the repository using various strategies. */
// Security)
if (!forceDefaultCredentials)
try {
- newSession = repository.login(workspace);
+ newSession = repository().login(workspace);
} catch (LoginException e1) {
log.warn("Cannot login without credentials: " + e1.getMessage());
// invalid credentials, go to the next step
try {
SimpleCredentials sc = new SimpleCredentials(defaultUsername,
defaultPassword.toCharArray());
- newSession = repository.login(sc, workspace);
+ newSession = repository().login(sc, workspace);
} catch (RepositoryException e) {
throw new ArgeoException("Cannot log in to repository", e);
}
if (activeSessions.size() == 0)
return;
- if (log.isDebugEnabled())
- log.debug("Cleaning up " + activeSessions.size()
+ if (log.isTraceEnabled())
+ log.trace("Cleaning up " + activeSessions.size()
+ " active JCR sessions...");
deactivate();
for (Session sess : activeSessions.values()) {
- sess.logout();
+ JcrUtils.logoutQuietly(sess);
}
activeSessions.clear();
}
Session session = activeSessions.get(thread.getId());
activeSessions.remove(thread.getId());
session.logout();
- if (log.isDebugEnabled())
- log.debug("Cleaned up JCR session (userID="
+ if (log.isTraceEnabled())
+ log.trace("Cleaned up JCR session (userID="
+ session.getUserID() + ") from dead thread "
+ thread.getId());
}
return session;
}
+ protected Repository repository() {
+ if (repository != null)
+ return repository;
+ if (repositories != null) {
+ // hardened for OSGi dynamic services
+ Iterator<Repository> it = repositories.iterator();
+ if (it.hasNext())
+ return it.next();
+ }
+ throw new ArgeoException("No repository injected");
+ }
+
+ // /** Useful for declarative registration of OSGi services (blueprint) */
+ // public void register(Repository repository, Map<?, ?> params) {
+ // this.repository = repository;
+ // }
+ //
+ // /** Useful for declarative registration of OSGi services (blueprint) */
+ // public void unregister(Repository repository, Map<?, ?> params) {
+ // this.repository = null;
+ // }
+
public void setRepository(Repository repository) {
this.repository = repository;
}
+ public void setRepositories(List<Repository> repositories) {
+ this.repositories = repositories;
+ }
+
public void setDefaultUsername(String defaultUsername) {
this.defaultUsername = defaultUsername;
}