]> git.argeo.org Git - lgpl/argeo-commons.git/blobdiff - server/runtime/org.argeo.server.jackrabbit/src/main/java/org/argeo/jackrabbit/JackrabbitContainer.java
Introduce OSAuthentication
[lgpl/argeo-commons.git] / server / runtime / org.argeo.server.jackrabbit / src / main / java / org / argeo / jackrabbit / JackrabbitContainer.java
index 67126362b5bd8437285eaa79510e105bd3d0ded3..eb090514f425aa8f3ead1045ec072e78b38a7eda 100644 (file)
@@ -18,6 +18,7 @@ package org.argeo.jackrabbit;
 
 import java.io.ByteArrayInputStream;
 import java.io.File;
+import java.io.IOException;
 import java.io.InputStream;
 import java.io.InputStreamReader;
 import java.io.Reader;
@@ -55,6 +56,7 @@ import org.springframework.beans.factory.InitializingBean;
 import org.springframework.context.ResourceLoaderAware;
 import org.springframework.core.io.Resource;
 import org.springframework.core.io.ResourceLoader;
+import org.springframework.util.SystemPropertyUtils;
 import org.xml.sax.InputSource;
 
 /**
@@ -85,6 +87,7 @@ public class JackrabbitContainer implements InitializingBean, DisposableBean,
        private Boolean autocreateWorkspaces = false;
 
        private Executor systemExecutor;
+       private Credentials adminCredentials;
 
        public void afterPropertiesSet() throws Exception {
                // remote repository
@@ -97,7 +100,7 @@ public class JackrabbitContainer implements InitializingBean, DisposableBean,
                                throw new ArgeoException("Remote Davex repository " + uri
                                                + " not found");
                        log.info("Initialized Jackrabbit repository " + repository
-                                       + " from uri " + uri);
+                                       + " from URI " + uri);
                        // do not perform further initialization since we assume that the
                        // remote repository has been properly configured
                        return;
@@ -110,16 +113,9 @@ public class JackrabbitContainer implements InitializingBean, DisposableBean,
                }
 
                RepositoryConfig config;
+               Properties vars = getConfigurationProperties();
                InputStream in = configuration.getInputStream();
-               InputStream propsIn = null;
                try {
-                       Properties vars = new Properties();
-                       if (variables != null) {
-                               propsIn = variables.getInputStream();
-                               vars.load(propsIn);
-                       }
-                       // override with system properties
-                       vars.putAll(System.getProperties());
                        vars.put(RepositoryConfigurationParser.REPOSITORY_HOME_VARIABLE,
                                        homeDirectory.getCanonicalPath());
                        config = RepositoryConfig.create(new InputSource(in), vars);
@@ -127,7 +123,6 @@ public class JackrabbitContainer implements InitializingBean, DisposableBean,
                        throw new RuntimeException("Cannot read configuration", e);
                } finally {
                        IOUtils.closeQuietly(in);
-                       IOUtils.closeQuietly(propsIn);
                }
 
                if (inMemory)
@@ -135,29 +130,62 @@ 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);
        }
 
+       protected Properties getConfigurationProperties() {
+               InputStream propsIn = null;
+               Properties vars;
+               try {
+                       vars = new Properties();
+                       if (variables != null) {
+                               propsIn = variables.getInputStream();
+                               vars.load(propsIn);
+                       }
+                       // resolve system properties
+                       for (Object key : vars.keySet()) {
+                               // TODO: implement a smarter mechanism to resolve nested ${}
+                               String newValue = SystemPropertyUtils.resolvePlaceholders(vars
+                                               .getProperty(key.toString()));
+                               vars.put(key, newValue);
+                       }
+                       // override with system properties
+                       vars.putAll(System.getProperties());
+               } catch (IOException e) {
+                       throw new ArgeoException("Cannot read configuration properties", e);
+               } finally {
+                       IOUtils.closeQuietly(propsIn);
+               }
+               return vars;
+       }
+
        /**
         * Import declared node type definitions, trying to update them if they have
         * changed. In case of failures an error will be logged but no exception
         * 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);
+                                       processNewSession(session);
                                        // Load cnds as resources
                                        for (String resUrl : cndFiles) {
                                                Resource res = resourceLoader.getResource(resUrl);
@@ -177,8 +205,12 @@ public class JackrabbitContainer implements InitializingBean, DisposableBean,
                                        JcrUtils.logoutQuietly(session);
                                }
                        }
-               });
+               };
 
+               if (systemExecutor != null)
+                       systemExecutor.execute(action);
+               else
+                       action.run();
        }
 
        public void destroy() throws Exception {
@@ -263,6 +295,7 @@ public class JackrabbitContainer implements InitializingBean, DisposableBean,
                try {
                        NamespaceHelper namespaceHelper = new NamespaceHelper(session);
                        namespaceHelper.registerNamespaces(namespaces);
+
                } catch (Exception e) {
                        throw new ArgeoException("Cannot process new session", e);
                }
@@ -335,4 +368,8 @@ public class JackrabbitContainer implements InitializingBean, DisposableBean,
                this.systemExecutor = systemExecutor;
        }
 
+       public void setAdminCredentials(Credentials adminCredentials) {
+               this.adminCredentials = adminCredentials;
+       }
+
 }