Allows to initialize Jackrabbit container in tests
[lgpl/argeo-commons.git] / server / runtime / org.argeo.server.jackrabbit / src / main / java / org / argeo / jackrabbit / JackrabbitContainer.java
index 67126362b5bd8437285eaa79510e105bd3d0ded3..51063ace7640b2323e3cf23e1cf676de2a01ae20 100644 (file)
@@ -85,6 +85,7 @@ public class JackrabbitContainer implements InitializingBean, DisposableBean,
        private Boolean autocreateWorkspaces = false;
 
        private Executor systemExecutor;
+       private Credentials adminCredentials;
 
        public void afterPropertiesSet() throws Exception {
                // remote repository
@@ -135,7 +136,8 @@ public class JackrabbitContainer implements InitializingBean, DisposableBean,
                else
                        repository = RepositoryImpl.create(config);
 
-               importNodeTypeDefinitions(repository);
+               if (cndFiles != null && cndFiles.size() > 0)
+                       importNodeTypeDefinitions(repository);
 
                log.info("Initialized Jackrabbit repository " + repository + " in "
                                + homeDirectory + " with config " + configuration);
@@ -147,17 +149,22 @@ public class JackrabbitContainer implements InitializingBean, DisposableBean,
         * will be thrown.
         */
        protected void importNodeTypeDefinitions(final Repository repository) {
+               final Credentials credentialsToUse;
                if (systemExecutor == null) {
-                       log.warn("No system executor found");
-                       return;
+                       if (adminCredentials == null)
+                               throw new ArgeoException(
+                                               "No system executor or admin credentials found");
+                       credentialsToUse = adminCredentials;
+               } else {
+                       credentialsToUse = null;
                }
 
-               systemExecutor.execute(new Runnable() {
+               Runnable action = new Runnable() {
                        public void run() {
                                Reader reader = null;
                                Session session = null;
                                try {
-                                       session = repository.login();
+                                       session = repository.login(credentialsToUse);
                                        // Load cnds as resources
                                        for (String resUrl : cndFiles) {
                                                Resource res = resourceLoader.getResource(resUrl);
@@ -177,8 +184,12 @@ public class JackrabbitContainer implements InitializingBean, DisposableBean,
                                        JcrUtils.logoutQuietly(session);
                                }
                        }
-               });
+               };
 
+               if (systemExecutor != null)
+                       systemExecutor.execute(action);
+               else
+                       action.run();
        }
 
        public void destroy() throws Exception {
@@ -335,4 +346,8 @@ public class JackrabbitContainer implements InitializingBean, DisposableBean,
                this.systemExecutor = systemExecutor;
        }
 
+       public void setAdminCredentials(Credentials adminCredentials) {
+               this.adminCredentials = adminCredentials;
+       }
+
 }