jackrabbit-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ju...@apache.org
Subject svn commit: r923744 - in /jackrabbit/trunk/jackrabbit-core/src/main/java/org/apache/jackrabbit/core: RepositoryFactoryImpl.java TransientRepository.java config/RepositoryConfig.java
Date Tue, 16 Mar 2010 14:05:03 GMT
Author: jukka
Date: Tue Mar 16 14:05:03 2010
New Revision: 923744

URL: http://svn.apache.org/viewvc?rev=923744&view=rev
Log:
JCR-2555: Improved reusability of the JCA package

Fixed handing of repository map in RepositoryFactoryImpl. It turns out that RepositoryImpl.shutdown()
will close the ConnectionFactory instance referenced by the RepositoryConfig object, so reusing
the config for more than one repository instance will not work. A better fix would probably
be to remove the ConnectionFactory reference from RepositoryConfig.

Modified:
    jackrabbit/trunk/jackrabbit-core/src/main/java/org/apache/jackrabbit/core/RepositoryFactoryImpl.java
    jackrabbit/trunk/jackrabbit-core/src/main/java/org/apache/jackrabbit/core/TransientRepository.java
    jackrabbit/trunk/jackrabbit-core/src/main/java/org/apache/jackrabbit/core/config/RepositoryConfig.java

Modified: jackrabbit/trunk/jackrabbit-core/src/main/java/org/apache/jackrabbit/core/RepositoryFactoryImpl.java
URL: http://svn.apache.org/viewvc/jackrabbit/trunk/jackrabbit-core/src/main/java/org/apache/jackrabbit/core/RepositoryFactoryImpl.java?rev=923744&r1=923743&r2=923744&view=diff
==============================================================================
--- jackrabbit/trunk/jackrabbit-core/src/main/java/org/apache/jackrabbit/core/RepositoryFactoryImpl.java
(original)
+++ jackrabbit/trunk/jackrabbit-core/src/main/java/org/apache/jackrabbit/core/RepositoryFactoryImpl.java
Tue Mar 16 14:05:03 2010
@@ -16,6 +16,8 @@
  */
 package org.apache.jackrabbit.core;
 
+import static org.apache.jackrabbit.core.config.RepositoryConfigurationParser.REPOSITORY_HOME_VARIABLE;
+
 import java.io.File;
 import java.io.IOException;
 import java.net.URI;
@@ -34,7 +36,6 @@ import org.apache.jackrabbit.api.Jackrab
 import org.apache.jackrabbit.api.JackrabbitRepositoryFactory;
 import org.apache.jackrabbit.api.management.RepositoryManager;
 import org.apache.jackrabbit.commons.JcrUtils;
-import org.apache.jackrabbit.core.config.RepositoryConfigurationParser;
 
 /**
  * <code>RepositoryFactoryImpl</code> implements a repository factory that
@@ -55,101 +56,106 @@ public class RepositoryFactoryImpl imple
             = "org.apache.jackrabbit.repository.conf";
 
     /**
-     * Map of repository instances. Key = repository home, value = repository
-     * instance.
+     * Map of repository instances.
+     * Key = repository parameters, value = repository instance.
      */
-    private static final Map<String, JackrabbitRepository> REPOSITORY_INSTANCES = new
HashMap<String, JackrabbitRepository>();
+    private static final Map<Properties, TransientRepository> REPOSITORIES =
+        new HashMap<Properties, TransientRepository>();
 
     /**
      * The repository instances that were created by this factory.
      */
-    private final Set<TransientRepository> ownRepositories = new HashSet<TransientRepository>();
+    private final Set<TransientRepository> ownRepositories =
+        new HashSet<TransientRepository>();
 
     @SuppressWarnings("unchecked")
     public Repository getRepository(Map parameters) throws RepositoryException {
-        synchronized (REPOSITORY_INSTANCES) {
-            if (parameters == null) {
-                return getOrCreateRepository(null, Collections.emptyMap());
-            } else if (parameters.containsKey(REPOSITORY_HOME)) {
-                String home = parameters.get(REPOSITORY_HOME).toString();
-                return getOrCreateRepository(home, parameters);
-            } else if (parameters.containsKey(JcrUtils.REPOSITORY_URI)) {
-                Object parameter = parameters.get(JcrUtils.REPOSITORY_URI);
-                try {
-                    URI uri = new URI(parameter.toString().trim());
-                    String scheme = uri.getScheme();
-                    if (("file".equalsIgnoreCase(scheme)
-                            || "jcr-jackrabbit".equalsIgnoreCase(scheme))
-                            && uri.getAuthority() == null) {
-                        File file = new File(uri.getPath());
-                        if (file.isFile()) {
-                            return null; // Not a (possibly missing) directory
-                        } else {
-                            return getOrCreateRepository(
-                                    file.getPath(), parameters);
-                        }
+        if (parameters == null) {
+            return getRepository(null, Collections.emptyMap());
+        } else if (parameters.containsKey(REPOSITORY_HOME)) {
+            String home = parameters.get(REPOSITORY_HOME).toString();
+            return getRepository(home, parameters);
+        } else if (parameters.containsKey(JcrUtils.REPOSITORY_URI)) {
+            Object parameter = parameters.get(JcrUtils.REPOSITORY_URI);
+            try {
+                URI uri = new URI(parameter.toString().trim());
+                String scheme = uri.getScheme();
+                if (("file".equalsIgnoreCase(scheme)
+                        || "jcr-jackrabbit".equalsIgnoreCase(scheme))
+                        && uri.getAuthority() == null) {
+                    File file = new File(uri.getPath());
+                    if (file.isFile()) {
+                        return null; // Not a (possibly missing) directory
                     } else {
-                        return null; // not a file: or jcr-jackrabbit: URI
+                        return getRepository(file.getPath(), parameters);
                     }
-                } catch (URISyntaxException e) {
-                    return null; // not a valid URI
+                } else {
+                    return null; // not a file: or jcr-jackrabbit: URI
                 }
-            } else {
-                return null; // unknown or insufficient parameters
+            } catch (URISyntaxException e) {
+                return null; // not a valid URI
             }
+        } else {
+            return null; // unknown or insufficient parameters
         }
     }
 
+    private Repository getRepository(String home, Map<?, ?> parameters)
+            throws RepositoryException {
+        TransientRepository repository =
+            getOrCreateRepository(home, parameters);
+        ownRepositories.add(repository);
+        return repository;
+    }
+
     /**
      * Either returns a cached repository or creates a repository instance and
-     * puts it into the {@link #REPOSITORY_INSTANCES} cache.
+     * puts it into the {@link #REPOSITORIES} cache.
      *
      * @param home path to the repository home.
      * @return the repository instance.
      * @throws RepositoryException if an error occurs while creating the
      *          repository instance.
      */
-    private JackrabbitRepository getOrCreateRepository(
+    private static synchronized TransientRepository getOrCreateRepository(
             String home, Map<?, ?> parameters) throws RepositoryException {
-        JackrabbitRepository repo = REPOSITORY_INSTANCES.get(home);
-        if (repo == null) {
-            // Prepare the repository properties
-            Properties properties = new Properties(System.getProperties());
-            for (Map.Entry<?, ?> entry : parameters.entrySet()) {
-                Object key = entry.getKey();
-                if (key != null) {
-                    Object value = entry.getValue();
-                    if (value != null) {
-                        properties.setProperty(
-                                key.toString(), value.toString());
-                    } else {
-                        properties.remove(key.toString());
-                    }
+        // Prepare the repository properties
+        Properties properties = new Properties(System.getProperties());
+        for (Map.Entry<?, ?> entry : parameters.entrySet()) {
+            Object key = entry.getKey();
+            if (key != null) {
+                Object value = entry.getValue();
+                if (value != null) {
+                    properties.setProperty(
+                            key.toString(), value.toString());
+                } else {
+                    properties.remove(key.toString());
                 }
             }
+        }
+        if (home != null) {
+            properties.put(REPOSITORY_HOME_VARIABLE, home);
+        }
 
-            properties.put(
-                    RepositoryConfigurationParser.REPOSITORY_HOME_VARIABLE,
-                    home);
-
+        TransientRepository repository = REPOSITORIES.get(properties);
+        if (repository == null) {
             try {
                 TransientRepository tr;
                 if (home == null) {
                     tr = new TransientRepository(properties);
                     // also remember this instance as the default repository
-                    REPOSITORY_INSTANCES.put(null, tr);
+                    REPOSITORIES.put(null, tr);
                 } else {
                     tr = new TransientRepository(properties);
                 }
-                REPOSITORY_INSTANCES.put(tr.getHomeDir(), tr);
-                ownRepositories.add(tr);
-                repo = tr;
+                REPOSITORIES.put(properties, tr);
+                repository = tr;
             } catch (IOException e) {
                 throw new RepositoryException(
                         "Failed to install repository configuration", e);
             }
         }
-        return repo;
+        return repository;
     }
 
     public RepositoryManager getRepositoryManager(JackrabbitRepository repo) throws RepositoryException
{
@@ -161,4 +167,5 @@ public class RepositoryFactoryImpl imple
         }
         return new RepositoryManagerImpl((TransientRepository) repo);
     }
+
 }

Modified: jackrabbit/trunk/jackrabbit-core/src/main/java/org/apache/jackrabbit/core/TransientRepository.java
URL: http://svn.apache.org/viewvc/jackrabbit/trunk/jackrabbit-core/src/main/java/org/apache/jackrabbit/core/TransientRepository.java?rev=923744&r1=923743&r2=923744&view=diff
==============================================================================
--- jackrabbit/trunk/jackrabbit-core/src/main/java/org/apache/jackrabbit/core/TransientRepository.java
(original)
+++ jackrabbit/trunk/jackrabbit-core/src/main/java/org/apache/jackrabbit/core/TransientRepository.java
Tue Mar 16 14:05:03 2010
@@ -241,9 +241,24 @@ public class TransientRepository extends
         }, dir.getAbsolutePath());
     }
 
-    public TransientRepository(Properties properties)
+    public TransientRepository(final Properties properties)
             throws ConfigurationException, IOException {
-        this(RepositoryConfig.install(properties));
+        this(new RepositoryFactory() {
+            public RepositoryImpl getRepository() throws RepositoryException {
+                try {
+                    return RepositoryImpl.create(
+                            RepositoryConfig.install(properties));
+                } catch (IOException e) {
+                    throw new RepositoryException(
+                            "Automatic repository configuration failed: "
+                            + RepositoryConfig.getRepositoryHome(properties), e);
+                } catch (ConfigurationException e) {
+                    throw new RepositoryException(
+                            "Invalid repository configuration: "
+                            + properties, e);
+                }
+            }
+        }, RepositoryConfig.getRepositoryHome(properties).getAbsolutePath());
     }
 
     /**

Modified: jackrabbit/trunk/jackrabbit-core/src/main/java/org/apache/jackrabbit/core/config/RepositoryConfig.java
URL: http://svn.apache.org/viewvc/jackrabbit/trunk/jackrabbit-core/src/main/java/org/apache/jackrabbit/core/config/RepositoryConfig.java?rev=923744&r1=923743&r2=923744&view=diff
==============================================================================
--- jackrabbit/trunk/jackrabbit-core/src/main/java/org/apache/jackrabbit/core/config/RepositoryConfig.java
(original)
+++ jackrabbit/trunk/jackrabbit-core/src/main/java/org/apache/jackrabbit/core/config/RepositoryConfig.java
Tue Mar 16 14:05:03 2010
@@ -157,6 +157,15 @@ public class RepositoryConfig
         return create(new InputSource(xml.toURI().toString()), copy);
     }
 
+    public static File getRepositoryHome(Properties variables) {
+        String home = variables.getProperty(REPOSITORY_HOME_VARIABLE);
+        if (home == null) {
+            home = variables.getProperty(
+                    RepositoryFactoryImpl.REPOSITORY_HOME, "jackrabbit");
+        }
+        return new File(home);
+    }
+
     /**
      * Returns the configuration of a repository with the given configuration
      * file and repository home directory.



Mime
View raw message