]> git.argeo.org Git - lgpl/argeo-commons.git/blobdiff - server/runtime/org.argeo.server.jackrabbit/src/main/java/org/argeo/jackrabbit/JackrabbitRepositoryFactory.java
Fix NPE when TRACE logging is activated
[lgpl/argeo-commons.git] / server / runtime / org.argeo.server.jackrabbit / src / main / java / org / argeo / jackrabbit / JackrabbitRepositoryFactory.java
index 1f2ec2ae2c16716ad020af4cb576be50bc043b1d..d64bb5e688d137d66edef8ef509bf3641c4cf037 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Copyright (C) 2007-2012 Mathieu Baudier
+ * Copyright (C) 2007-2012 Argeo GmbH
  *
  * Licensed under the Apache License, Version 2.0 (the "License");
  * you may not use this file except in compliance with the License.
@@ -15,8 +15,8 @@
  */
 package org.argeo.jackrabbit;
 
-import java.net.URI;
-import java.net.URISyntaxException;
+import java.io.File;
+import java.io.InputStream;
 import java.util.HashMap;
 import java.util.Map;
 import java.util.Properties;
@@ -24,14 +24,22 @@ import java.util.Properties;
 import javax.jcr.Repository;
 import javax.jcr.RepositoryException;
 import javax.jcr.RepositoryFactory;
+import javax.jcr.Session;
 
+import org.apache.commons.io.IOUtils;
 import org.apache.commons.logging.Log;
 import org.apache.commons.logging.LogFactory;
 import org.apache.jackrabbit.commons.JcrUtils;
+import org.apache.jackrabbit.core.RepositoryImpl;
+import org.apache.jackrabbit.core.config.RepositoryConfig;
+import org.apache.jackrabbit.core.config.RepositoryConfigurationParser;
 import org.apache.jackrabbit.jcr2dav.Jcr2davRepositoryFactory;
 import org.argeo.ArgeoException;
 import org.argeo.jcr.ArgeoJcrConstants;
 import org.argeo.jcr.DefaultRepositoryFactory;
+import org.springframework.core.io.ClassPathResource;
+import org.springframework.core.io.Resource;
+import org.xml.sax.InputSource;
 
 /**
  * Repository factory which can create new repositories and access remote
@@ -39,9 +47,13 @@ import org.argeo.jcr.DefaultRepositoryFactory;
  */
 public class JackrabbitRepositoryFactory extends DefaultRepositoryFactory
                implements RepositoryFactory, ArgeoJcrConstants {
+
        private final static Log log = LogFactory
                        .getLog(JackrabbitRepositoryFactory.class);
 
+       private Resource fileRepositoryConfiguration = new ClassPathResource(
+                       "/org/argeo/jackrabbit/repository-h2.xml");
+
        @SuppressWarnings({ "rawtypes", "unchecked" })
        public Repository getRepository(Map parameters) throws RepositoryException {
                // check if can be found by alias
@@ -59,18 +71,13 @@ public class JackrabbitRepositoryFactory extends DefaultRepositoryFactory
                if (uri != null) {
                        if (uri.startsWith("http"))// http, https
                                repository = createRemoteRepository(uri);
+                       else if (uri.startsWith("file"))// http, https
+                               repository = createFileRepository(uri, parameters);
                        else if (uri.startsWith("vm")) {
-                               try {
-                                       URI uriObj = new URI(uri);
-                                       String alias = uriObj.getPath();
-                                       if (alias.charAt(0) == '/')
-                                               alias = alias.substring(1);
-                                       if (alias.charAt(alias.length() - 1) == '/')
-                                               alias = alias.substring(0, alias.length() - 1);
-                                       repository = getRepositoryByAlias(alias);
-                               } catch (URISyntaxException e) {
-                                       throw new ArgeoException("Cannot interpret URI " + uri, e);
-                               }
+                               log.warn("URI "
+                                               + uri
+                                               + " should have been managed by generic JCR repository factory");
+                               repository = getRepositoryByAlias(getAliasFromURI(uri));
                        }
                }
 
@@ -100,6 +107,51 @@ public class JackrabbitRepositoryFactory extends DefaultRepositoryFactory
                return repository;
        }
 
+       @SuppressWarnings({ "rawtypes", "unchecked" })
+       protected Repository createFileRepository(final String uri, Map parameters)
+                       throws RepositoryException {
+               InputStream configurationIn = null;
+               try {
+                       Properties vars = new Properties();
+                       vars.putAll(parameters);
+                       String dirPath = uri.substring("file:".length());
+                       File homeDir = new File(dirPath);
+                       if (homeDir.exists() && !homeDir.isDirectory())
+                               throw new ArgeoException("Repository home " + dirPath
+                                               + " is not a directory");
+                       if (!homeDir.exists())
+                               homeDir.mkdirs();
+                       configurationIn = fileRepositoryConfiguration.getInputStream();
+                       vars.put(RepositoryConfigurationParser.REPOSITORY_HOME_VARIABLE,
+                                       homeDir.getCanonicalPath());
+                       RepositoryConfig repositoryConfig = RepositoryConfig.create(
+                                       new InputSource(configurationIn), vars);
+
+                       // TransientRepository repository = new
+                       // TransientRepository(repositoryConfig);
+                       final RepositoryImpl repository = RepositoryImpl
+                                       .create(repositoryConfig);
+                       Session session = repository.login();
+                       // FIXME make it generic
+                       org.argeo.jcr.JcrUtils.addPrivilege(session, "/", "ROLE_ADMIN",
+                                       "jcr:all");
+                       org.argeo.jcr.JcrUtils.logoutQuietly(session);
+                       Runtime.getRuntime().addShutdownHook(
+                                       new Thread("Clean JCR repository " + uri) {
+                                               public void run() {
+                                                       repository.shutdown();
+                                                       log.info("Destroyed repository " + uri);
+                                               }
+                                       });
+                       log.info("Initialized file Jackrabbit repository from uri " + uri);
+                       return repository;
+               } catch (Exception e) {
+                       throw new ArgeoException("Cannot create repository " + uri, e);
+               } finally {
+                       IOUtils.closeQuietly(configurationIn);
+               }
+       }
+
        /**
         * Called after the repository has been initialised. Does nothing by
         * default.
@@ -109,4 +161,9 @@ public class JackrabbitRepositoryFactory extends DefaultRepositoryFactory
 
        }
 
+       public void setFileRepositoryConfiguration(
+                       Resource fileRepositoryConfiguration) {
+               this.fileRepositoryConfiguration = fileRepositoryConfiguration;
+       }
+
 }