]> git.argeo.org Git - lgpl/argeo-commons.git/blobdiff - server/runtime/org.argeo.server.jackrabbit/src/main/java/org/argeo/server/jackrabbit/JackrabbitContainer.java
Improve JCR
[lgpl/argeo-commons.git] / server / runtime / org.argeo.server.jackrabbit / src / main / java / org / argeo / server / jackrabbit / JackrabbitContainer.java
index 0c72e8d45343afc6da2672e79a3ada3fc2fd3c9c..83c144c8c9ada38d0ff821f5dcdb85bb4f76354a 100644 (file)
@@ -18,6 +18,8 @@ package org.argeo.server.jackrabbit;
 
 import java.io.File;
 import java.io.InputStream;
+import java.util.HashMap;
+import java.util.Map;
 
 import javax.jcr.Credentials;
 import javax.jcr.LoginException;
@@ -25,18 +27,25 @@ import javax.jcr.NoSuchWorkspaceException;
 import javax.jcr.Repository;
 import javax.jcr.RepositoryException;
 import javax.jcr.Session;
+import javax.jcr.Value;
 
 import org.apache.commons.io.FileUtils;
 import org.apache.commons.io.IOUtils;
 import org.apache.commons.logging.Log;
 import org.apache.commons.logging.LogFactory;
+import org.apache.jackrabbit.commons.NamespaceHelper;
 import org.apache.jackrabbit.core.RepositoryImpl;
 import org.apache.jackrabbit.core.TransientRepository;
 import org.apache.jackrabbit.core.config.RepositoryConfig;
+import org.argeo.ArgeoException;
 import org.springframework.beans.factory.DisposableBean;
 import org.springframework.beans.factory.InitializingBean;
 import org.springframework.core.io.Resource;
 
+/**
+ * Wrapper around a Jackrabbit repository which allows to configure it in Spring
+ * and expose it as a {@link Repository}.
+ */
 public class JackrabbitContainer implements InitializingBean, DisposableBean,
                Repository {
        private Log log = LogFactory.getLog(JackrabbitContainer.class);
@@ -48,6 +57,9 @@ public class JackrabbitContainer implements InitializingBean, DisposableBean,
 
        private Repository repository;
 
+       /** Namespaces to register: key is prefix, value namespace */
+       private Map<String, String> namespaces = new HashMap<String, String>();
+
        public void afterPropertiesSet() throws Exception {
                if (inMemory && homeDirectory.exists()) {
                        FileUtils.deleteDirectory(homeDirectory);
@@ -57,8 +69,8 @@ public class JackrabbitContainer implements InitializingBean, DisposableBean,
                RepositoryConfig config;
                InputStream in = configuration.getInputStream();
                try {
-                       config = RepositoryConfig.create(in, homeDirectory
-                                       .getCanonicalPath());
+                       config = RepositoryConfig.create(in,
+                                       homeDirectory.getCanonicalPath());
                } catch (Exception e) {
                        throw new RuntimeException("Cannot read configuration", e);
                } finally {
@@ -103,23 +115,56 @@ public class JackrabbitContainer implements InitializingBean, DisposableBean,
        }
 
        public Session login() throws LoginException, RepositoryException {
-               return repository.login();
+               Session session = repository.login();
+               processNewSession(session);
+               return session;
        }
 
        public Session login(Credentials credentials, String workspaceName)
                        throws LoginException, NoSuchWorkspaceException,
                        RepositoryException {
-               return repository.login(credentials, workspaceName);
+               Session session = repository.login(credentials, workspaceName);
+               processNewSession(session);
+               return session;
        }
 
        public Session login(Credentials credentials) throws LoginException,
                        RepositoryException {
-               return repository.login(credentials);
+               Session session = repository.login(credentials);
+               processNewSession(session);
+               return session;
        }
 
        public Session login(String workspaceName) throws LoginException,
                        NoSuchWorkspaceException, RepositoryException {
-               return repository.login(workspaceName);
+               Session session = repository.login(workspaceName);
+               processNewSession(session);
+               return session;
+       }
+
+       protected void processNewSession(Session session) {
+               try {
+                       NamespaceHelper namespaceHelper = new NamespaceHelper(session);
+                       namespaceHelper.registerNamespaces(namespaces);
+               } catch (RepositoryException e) {
+                       throw new ArgeoException("Cannot process new session", e);
+               }
+       }
+
+       public boolean isStandardDescriptor(String key) {
+               return repository.isStandardDescriptor(key);
+       }
+
+       public boolean isSingleValueDescriptor(String key) {
+               return repository.isSingleValueDescriptor(key);
+       }
+
+       public Value getDescriptorValue(String key) {
+               return repository.getDescriptorValue(key);
+       }
+
+       public Value[] getDescriptorValues(String key) {
+               return repository.getDescriptorValues(key);
        }
 
        // BEANS METHODS
@@ -135,4 +180,8 @@ public class JackrabbitContainer implements InitializingBean, DisposableBean,
                this.inMemory = inMemory;
        }
 
+       public void setNamespaces(Map<String, String> namespaces) {
+               this.namespaces = namespaces;
+       }
+
 }