diff --git a/nuxeo-core/src/main/java/org/nuxeo/ecm/core/api/AbstractSession.java b/nuxeo-core/src/main/java/org/nuxeo/ecm/core/api/AbstractSession.java --- a/nuxeo-core/src/main/java/org/nuxeo/ecm/core/api/AbstractSession.java +++ b/nuxeo-core/src/main/java/org/nuxeo/ecm/core/api/AbstractSession.java @@ -97,7 +97,6 @@ import org.nuxeo.ecm.core.query.QueryParseException; import org.nuxeo.ecm.core.query.QueryResult; import org.nuxeo.ecm.core.query.sql.model.SQLQuery.Transformer; -import org.nuxeo.ecm.core.repository.RepositoryInitializationHandler; import org.nuxeo.ecm.core.schema.DocumentType; import org.nuxeo.ecm.core.schema.FacetNames; import org.nuxeo.ecm.core.schema.NXSchema; @@ -204,54 +203,7 @@ // retrieve their session on the server side CoreInstance.getInstance().registerSession(sessionId, this); - // <------------ begin repository initialization - // we need to initialize the repository if this is the first time it is - // accessed in this JVM session. - // For this we get the session and test if the - // "REPOSITORY_FIRST_ACCESS" is set after the session is created. We - // need to synchronize the call to be sure we initialize it only once. - synchronized (AbstractSession.class) { - Session session = getSession(); // force the creation of the - // underlying session - if (sessionContext.remove("REPOSITORY_FIRST_ACCESS") != null) { - // this is the first time we access the repository in this JVM - // notify the InitializationHandler if any. - RepositoryInitializationHandler handler = RepositoryInitializationHandler.getInstance(); - if (handler != null) { - // change principal to give all rights - Principal ctxPrincipal = (Principal) sessionContext.get("principal"); - try { - // change current principal to give all right to the - // handler - // FIXME: this should be fixed by using - // SystemPrincipal => we must synchronize this with - // SecurityService check - sessionContext.put("principal", new SimplePrincipal( - SYSTEM_USERNAME)); - try { - handler.initializeRepository(this); - session.save(); - } catch (ClientException e) { - // shouldn't remove the root? ... to restart with - // an empty repository - log.error( - "Failed to initialize repository content", - e); - } catch (DocumentException e) { - log.error("Unable to save session after repository init : " - + e.getMessage()); - } - } finally { - sessionContext.remove("principal"); - if (ctxPrincipal != null) { // restore principal - sessionContext.put("principal", - (Serializable) ctxPrincipal); - } - } - } - } - } - // <------------- end repository initialization + getSession(); return sessionId; } diff --git a/nuxeo-core/src/main/java/org/nuxeo/ecm/core/repository/RepositoryManager.java b/nuxeo-core/src/main/java/org/nuxeo/ecm/core/repository/RepositoryManager.java --- a/nuxeo-core/src/main/java/org/nuxeo/ecm/core/repository/RepositoryManager.java +++ b/nuxeo-core/src/main/java/org/nuxeo/ecm/core/repository/RepositoryManager.java @@ -24,6 +24,8 @@ import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; +import org.nuxeo.ecm.core.api.ClientException; +import org.nuxeo.ecm.core.api.UnrestrictedSessionRunner; import org.nuxeo.ecm.core.model.Repository; /** @@ -179,6 +181,27 @@ } } + public static class RepositoryInitializer extends UnrestrictedSessionRunner { + public RepositoryInitializer(String name) { + super(name); + } + + @Override + public void run() throws ClientException { + RepositoryInitializationHandler handler = RepositoryInitializationHandler.getInstance(); + if (handler == null) { + return; + } + try { + handler.initializeRepository(session); + } catch (ClientException e) { + // shouldn't remove the root? ... to restart with + // an empty repository + log.error("Failed to initialize repository content", e); + } + } + } + public static final class Ref { private int refcnt; @@ -194,6 +217,7 @@ if (repository == null) { refcnt = 0; repository = descriptor.create(); + new RepositoryInitializer(descriptor.getName()).runUnrestricted(); } refcnt++; return repository;