Improve JCR
authorMathieu Baudier <mbaudier@argeo.org>
Wed, 12 Jan 2011 12:49:20 +0000 (12:49 +0000)
committerMathieu Baudier <mbaudier@argeo.org>
Wed, 12 Jan 2011 12:49:20 +0000 (12:49 +0000)
git-svn-id: https://svn.argeo.org/commons/trunk@4009 4cfe0d0a-d680-48aa-b62c-e0a02a3f76cc

basic/runtime/org.argeo.basic.nodeps/src/main/java/org/argeo/ArgeoException.java
security/runtime/org.argeo.security.core/pom.xml
server/runtime/org.argeo.server.jackrabbit/src/main/java/org/argeo/server/jackrabbit/JackrabbitContainer.java
server/runtime/org.argeo.server.jcr/src/main/java/org/argeo/jcr/JcrUtils.java
server/runtime/org.argeo.server.jcr/src/main/java/org/argeo/jcr/ThreadBoundJcrSessionFactory.java

index 563a680b17f9389f877f8b183bb54ff2dc4f30d3..a90c16a0fc6fa38200f69dcda34d8bcfe7130ec6 100644 (file)
 
 package org.argeo;
 
+/** Argeo Commons specific exception. */
 public class ArgeoException extends RuntimeException {
        private static final long serialVersionUID = 1L;
 
+       /** Creates an exception with a message. */
        public ArgeoException(String message) {
                super(message);
        }
 
-       public ArgeoException(Throwable cause) {
-               super(cause);
+       /** Creates an exception with a message and a root cause. */
+       public ArgeoException(String message, Throwable e) {
+               super(message, e);
        }
 
-       public ArgeoException(String message, Throwable cause) {
-               super(message, cause);
+       /** @deprecated use {@link #ArgeoException(String, Throwable)} instead. */
+       @Deprecated
+       public ArgeoException(Throwable cause) {
+               super(cause);
        }
 
 }
index 37c7390ec2d5bf182bb560a41694a0be02995c83..8f370aaa778cfcda2cb6735dfd4f09457215fbef 100644 (file)
@@ -1,4 +1,5 @@
-<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
+<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
+       xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
        <modelVersion>4.0.0</modelVersion>
        <parent>
                <groupId>org.argeo.commons.security</groupId>
                </dependency>
 
                <dependency>
-                       <!--
-                               Force inclusion of commons.lang to prevent v2.1.0 to be taken by
-                               Spring Security
-                       -->
+                       <!-- Force inclusion of commons.lang to prevent v2.1.0 to be taken by 
+                               Spring Security -->
                        <groupId>org.apache.commons</groupId>
                        <artifactId>com.springsource.org.apache.commons.lang</artifactId>
                </dependency>
 
                <!-- Spring -->
                <dependency>
-                               <groupId>org.argeo.dep.osgi</groupId>
-                               <artifactId>org.argeo.dep.osgi.springframework.ldap</artifactId>
+                       <groupId>org.argeo.dep.osgi</groupId>
+                       <artifactId>org.argeo.dep.osgi.springframework.ldap</artifactId>
                </dependency>
                <dependency>
                        <groupId>org.springframework</groupId>
@@ -92,7 +91,7 @@
                        <artifactId>com.springsource.slf4j.log4j</artifactId>
                </dependency>
 
-               <!-- JSON  -->
+               <!-- JSON -->
                <dependency>
                        <groupId>org.codehaus.jackson</groupId>
                        <artifactId>com.springsource.org.codehaus.jackson.mapper</artifactId>
index ef96b3595d153ba10aaa877704c5277074ceb9de..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;
@@ -31,9 +33,11 @@ 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;
@@ -53,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);
@@ -108,23 +115,40 @@ 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) {
@@ -156,4 +180,8 @@ public class JackrabbitContainer implements InitializingBean, DisposableBean,
                this.inMemory = inMemory;
        }
 
+       public void setNamespaces(Map<String, String> namespaces) {
+               this.namespaces = namespaces;
+       }
+
 }
index b89a2845c34660e3131745c2574c4d6c7b2092d5..4e197d5fbcd2a65e30775c7515668f8ec45bbcba 100644 (file)
@@ -90,7 +90,14 @@ public class JcrUtils {
                return dateAsPath(cal, false);
        }
 
-       /** The provided data as a path ('/' at the end, not the beginning) */
+       /**
+        * The provided data as a path ('/' at the end, not the beginning)
+        * 
+        * @param cal
+        *            the date
+        * @param addHour
+        *            whether to add hour as well
+        */
        public static String dateAsPath(Calendar cal, Boolean addHour) {
                StringBuffer buf = new StringBuffer(14);
                buf.append('Y').append(cal.get(Calendar.YEAR));// 5
index b94b9bf1645a98e94584e6f8897d5a42e6242b0d..ed2857ad5cf3112085dbb51e57b1b93627c49dad 100644 (file)
@@ -34,6 +34,7 @@ import org.argeo.ArgeoException;
 import org.springframework.beans.factory.DisposableBean;
 import org.springframework.beans.factory.FactoryBean;
 
+/** Proxy JCR sessions and attach them to calling threads. */
 public class ThreadBoundJcrSessionFactory implements FactoryBean,
                DisposableBean {
        private final static Log log = LogFactory
@@ -47,6 +48,9 @@ public class ThreadBoundJcrSessionFactory implements FactoryBean,
        private boolean destroying = false;
        private final Session proxiedSession;
 
+       private String defaultUsername = "demo";
+       private String defaultPassword = "demo";
+
        public ThreadBoundJcrSessionFactory() {
                Class<?>[] interfaces = { Session.class };
                proxiedSession = (Session) Proxy.newProxyInstance(getClass()
@@ -79,8 +83,8 @@ public class ThreadBoundJcrSessionFactory implements FactoryBean,
 
        protected Session login() {
                try {
-                       SimpleCredentials sc = new SimpleCredentials("demo", "demo"
-                                       .toCharArray());
+                       SimpleCredentials sc = new SimpleCredentials(defaultUsername,
+                                       defaultPassword.toCharArray());
                        Session sess = repository.login(sc);
                        if (log.isTraceEnabled())
                                log.trace("Log in to JCR session " + sess + "; userId="
@@ -121,4 +125,12 @@ public class ThreadBoundJcrSessionFactory implements FactoryBean,
                this.repository = repository;
        }
 
+       public void setDefaultUsername(String defaultUsername) {
+               this.defaultUsername = defaultUsername;
+       }
+
+       public void setDefaultPassword(String defaultPassword) {
+               this.defaultPassword = defaultPassword;
+       }
+
 }