You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@jackrabbit.apache.org by ju...@apache.org on 2010/03/16 11:49:09 UTC

svn commit: r923678 - in /jackrabbit/trunk: jackrabbit-core/src/main/java/org/apache/jackrabbit/core/config/ jackrabbit-jca/src/main/java/org/apache/jackrabbit/jca/

Author: jukka
Date: Tue Mar 16 10:49:09 2010
New Revision: 923678

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

Merge the classpath: repository config code from jca to core. Streamline the remaining jca code.

Modified:
    jackrabbit/trunk/jackrabbit-core/src/main/java/org/apache/jackrabbit/core/config/RepositoryConfig.java
    jackrabbit/trunk/jackrabbit-jca/src/main/java/org/apache/jackrabbit/jca/JCAManagedConnectionFactory.java
    jackrabbit/trunk/jackrabbit-jca/src/main/java/org/apache/jackrabbit/jca/JCARepositoryManager.java

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=923678&r1=923677&r2=923678&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 10:49:09 2010
@@ -138,12 +138,19 @@ public class RepositoryConfig
         if (conf == null) {
             conf = copy.getProperty(RepositoryFactoryImpl.REPOSITORY_CONF);
         }
+
+        ClassLoader loader = RepositoryImpl.class.getClassLoader();
+        String resource = REPOSITORY_XML;
         if (conf == null) {
             conf = new File(dir, REPOSITORY_XML).getPath();
+        } else if (conf.startsWith("classpath:")) {
+            loader = Thread.currentThread().getContextClassLoader();
+            resource = conf.substring("classpath:".length());
+            conf = new File(dir, REPOSITORY_XML).getPath();
         }
-        File xml = new File(conf);
 
-        installRepositorySkeleton(xml, dir);
+        File xml = new File(conf);
+        installRepositorySkeleton(dir, xml, loader, resource);
         return create(new InputSource(xml.toURI().toString()), copy);
     }
 
@@ -162,11 +169,14 @@ public class RepositoryConfig
      */
     public static RepositoryConfig install(File xml, File dir)
             throws IOException, ConfigurationException {
-        installRepositorySkeleton(xml, dir);
+        installRepositorySkeleton(
+                dir, xml,
+                RepositoryImpl.class.getClassLoader(), REPOSITORY_XML);
         return create(xml, dir);
     }
 
-    private static void installRepositorySkeleton(File xml, File dir)
+    private static void installRepositorySkeleton(
+            File dir, File xml, ClassLoader loader, String resource)
             throws IOException {
         if (!dir.exists()) {
             log.info("Creating repository directory {}", dir);
@@ -177,8 +187,7 @@ public class RepositoryConfig
             log.info("Installing default repository configuration to {}", xml);
             OutputStream output = new FileOutputStream(xml);
             try {
-                InputStream input =
-                    RepositoryImpl.class.getResourceAsStream(REPOSITORY_XML);
+                InputStream input = loader.getResourceAsStream(resource);
                 try {
                     IOUtils.copy(input, output);
                 } finally {

Modified: jackrabbit/trunk/jackrabbit-jca/src/main/java/org/apache/jackrabbit/jca/JCAManagedConnectionFactory.java
URL: http://svn.apache.org/viewvc/jackrabbit/trunk/jackrabbit-jca/src/main/java/org/apache/jackrabbit/jca/JCAManagedConnectionFactory.java?rev=923678&r1=923677&r2=923678&view=diff
==============================================================================
--- jackrabbit/trunk/jackrabbit-jca/src/main/java/org/apache/jackrabbit/jca/JCAManagedConnectionFactory.java (original)
+++ jackrabbit/trunk/jackrabbit-jca/src/main/java/org/apache/jackrabbit/jca/JCAManagedConnectionFactory.java Tue Mar 16 10:49:09 2010
@@ -17,6 +17,8 @@
 package org.apache.jackrabbit.jca;
 
 import java.io.PrintWriter;
+import java.util.HashMap;
+import java.util.Map;
 import java.util.Set;
 
 import javax.jcr.Credentials;
@@ -31,6 +33,7 @@ import javax.resource.spi.ManagedConnect
 import javax.security.auth.Subject;
 
 import org.apache.jackrabbit.api.XASession;
+import org.apache.jackrabbit.commons.JcrUtils;
 
 /**
  * Implements the JCA ManagedConnectionFactory contract.
@@ -39,14 +42,9 @@ public final class JCAManagedConnectionF
         implements ManagedConnectionFactory {
 
     /**
-     * Home directory.
+     * Repository parameters.
      */
-    private String homeDir;
-
-    /**
-     * Config file.
-     */
-    private String configFile;
+    private final Map<String, String> parameters = new HashMap<String, String>();
 
     /**
      * Flag indicating whether the session should be bound to the
@@ -67,31 +65,45 @@ public final class JCAManagedConnectionF
     private transient PrintWriter logWriter;
 
     /**
+     * Return the repository URI.
+     */
+    public String getRepositoryURI() {
+        return parameters.get(JcrUtils.REPOSITORY_URI);
+    }
+
+    /**
+     * Set the repository URI.
+     */
+    public void setRepositoryURI(String uri) {
+        parameters.put(JcrUtils.REPOSITORY_URI, uri);
+    }
+
+    /**
      * Return the repository home directory.
      */
     public String getHomeDir() {
-        return homeDir;
+        return parameters.get("org.apache.jackrabbit.repository.home");
     }
 
     /**
      * Set the repository home directory.
      */
-    public void setHomeDir(String homeDir) {
-        this.homeDir = homeDir;
+    public void setHomeDir(String home) {
+        parameters.put("org.apache.jackrabbit.repository.home", home);
     }
 
     /**
      * Return the repository configuration file.
      */
     public String getConfigFile() {
-        return configFile;
+        return parameters.get("org.apache.jackrabbit.repository.conf");
     }
 
     /**
      * Set the repository configuration file.
      */
-    public void setConfigFile(String configFile) {
-        this.configFile = configFile;
+    public void setConfigFile(String conf) {
+        parameters.put("org.apache.jackrabbit.repository.conf", conf);
     }
 
     /**
@@ -189,7 +201,7 @@ public final class JCAManagedConnectionF
                 JCAManagedConnection mc = (JCAManagedConnection) connection;
                 if (equals(mc.getManagedConnectionFactory())) {
                     JCAConnectionRequestInfo otherCri = mc.getConnectionRequestInfo();
-                    if (equals(cri, otherCri)) {
+                    if (cri == otherCri || (cri != null && cri.equals(otherCri))) {
                         return mc;
                     }
                 }
@@ -230,9 +242,7 @@ public final class JCAManagedConnectionF
      * Return the hash code.
      */
     public int hashCode() {
-        int result = homeDir != null ? homeDir.hashCode() : 0;
-        result = 37 * result + (configFile != null ? configFile.hashCode() : 0);
-        return result;
+        return parameters.hashCode();
     }
 
     /**
@@ -252,21 +262,7 @@ public final class JCAManagedConnectionF
      * Return true if equals.
      */
     private boolean equals(JCAManagedConnectionFactory o) {
-        return equals(homeDir, o.homeDir)
-            && equals(configFile, o.configFile);
-    }
-
-    /**
-     * Return true if equals.
-     */
-    private boolean equals(Object o1, Object o2) {
-        if (o1 == o2) {
-            return true;
-        } else if ((o1 == null) || (o2 == null)) {
-            return false;
-        } else {
-            return o1.equals(o2);
-        }
+        return parameters.equals(o.parameters);
     }
 
     /**
@@ -275,19 +271,9 @@ public final class JCAManagedConnectionF
     private void createRepository()
             throws ResourceException {
         if (repository == null) {
-            // Check the home directory
-            if ((homeDir == null) || homeDir.equals("")) {
-                throw new ResourceException("Property 'homeDir' not set");
-            }
-
-            // Check the config file
-            if ((configFile == null) || configFile.equals("")) {
-                throw new ResourceException("Property 'configFile' not set");
-            }
-
             try {
                 JCARepositoryManager mgr = JCARepositoryManager.getInstance();
-                repository = mgr.createRepository(homeDir, configFile);
+                repository = mgr.createRepository(parameters);
                 log("Created repository (" + repository + ")");
             } catch (RepositoryException e) {
                 log("Failed to create repository", e);
@@ -304,7 +290,7 @@ public final class JCAManagedConnectionF
      */
     protected void finalize() {
         JCARepositoryManager mgr = JCARepositoryManager.getInstance();
-        mgr.autoShutdownRepository(homeDir, configFile);
+        mgr.autoShutdownRepository(parameters);
     }
 
     public Boolean getBindSessionToTransaction() {

Modified: jackrabbit/trunk/jackrabbit-jca/src/main/java/org/apache/jackrabbit/jca/JCARepositoryManager.java
URL: http://svn.apache.org/viewvc/jackrabbit/trunk/jackrabbit-jca/src/main/java/org/apache/jackrabbit/jca/JCARepositoryManager.java?rev=923678&r1=923677&r2=923678&view=diff
==============================================================================
--- jackrabbit/trunk/jackrabbit-jca/src/main/java/org/apache/jackrabbit/jca/JCARepositoryManager.java (original)
+++ jackrabbit/trunk/jackrabbit-jca/src/main/java/org/apache/jackrabbit/jca/JCARepositoryManager.java Tue Mar 16 10:49:09 2010
@@ -19,18 +19,10 @@ package org.apache.jackrabbit.jca;
 import javax.jcr.Repository;
 import javax.jcr.RepositoryException;
 
-import org.apache.commons.io.IOUtils;
 import org.apache.jackrabbit.api.JackrabbitRepository;
 import org.apache.jackrabbit.commons.JcrUtils;
 
-import java.io.File;
-import java.io.FileOutputStream;
-import java.io.IOException;
-import java.io.InputStream;
-import java.io.OutputStream;
-import java.util.Collection;
 import java.util.HashMap;
-import java.util.Iterator;
 import java.util.Map;
 
 /**
@@ -50,7 +42,8 @@ public final class JCARepositoryManager 
     /**
      * References.
      */
-    private final Map<Reference, Reference> references;
+    private final Map<Map<String, String>, Repository> repositories =
+        new HashMap<Map<String, String>, Repository>();
 
     /**
      * Flag indicating that the life cycle
@@ -63,53 +56,34 @@ public final class JCARepositoryManager 
      * Construct the manager.
      */
     private JCARepositoryManager() {
-        this.references = new HashMap<Reference, Reference>();
     }
 
     /**
      * Create repository.
      *
-     * @param homeDir   The location of the repository.
-     * @param configFile The path to the repository configuration file. If the file is located on
-     *                   the classpath, the path should be prepended with
-     *                   JCARepositoryManager.CLASSPATH_CONFIG_PREFIX.
+     * @param parameters repository parameters
      * @return repository instance
      */
-    public Repository createRepository(String homeDir, String configFile)
-            throws RepositoryException {
-        Reference ref = getReference(homeDir, configFile);
-        return ref.create();
-    }
-
-    /**
-     * Shutdown all the repositories.
-     */
-    public void shutdown() {
-        Collection<Reference> references = this.references.values();
-        Iterator<Reference> iter = references.iterator();
-        while (iter.hasNext()) {
-            Reference ref = iter.next();
-            ref.shutdown();
+    public synchronized Repository createRepository(
+            Map<String, String> parameters) throws RepositoryException {
+        Repository repository = repositories.get(parameters);
+        if (repository == null) {
+            repository =  JcrUtils.getRepository(parameters);
+            repositories.put(parameters, repository);
         }
-        this.references.clear();
+        return repository;
     }
 
     /**
-     * Return the reference.
-     *
-     * @param homeDir   The location of the repository.
-     * @param configFile The path to the repository configuration file.
+     * Shutdown all the repositories.
      */
-    private synchronized Reference getReference(String homeDir, String configFile) {
-        Reference ref = new Reference(homeDir, configFile);
-        Reference other = references.get(ref);
-
-        if (other == null) {
-            references.put(ref, ref);
-            return ref;
-        } else {
-            return other;
+    public synchronized void shutdown() {
+        for (Repository repository : repositories.values()) {
+            if (repository instanceof JackrabbitRepository) {
+                ((JackrabbitRepository) repository).shutdown();
+            }
         }
+        repositories.clear();
     }
 
     /**
@@ -119,151 +93,6 @@ public final class JCARepositoryManager 
         return INSTANCE;
     }
 
-    /**
-     * Repository reference implementation.
-     */
-    private final class Reference {
-        /**
-         * Home directory.
-         */
-        private final String homeDir;
-
-        /**
-         * Configuration file.
-         *
-         * Configuration files located on the classpath begin with
-         * JCARepositoryManager.CLASSPATH_CONFIG_PREFIX.
-         */
-        private String configFile;
-
-        /**
-         * Repository instance.
-         */
-        private Repository repository;
-
-        /**
-         * Construct the manager.
-         */
-        private Reference(String homeDir, String configFile) {
-            this.homeDir = homeDir;
-            this.configFile = configFile;
-            this.repository = null;
-        }
-
-        /**
-         * Return the repository.
-         */
-        public Repository create() throws RepositoryException {
-            if (repository == null) {
-                File dir = new File(homeDir);
-                dir.mkdirs();
-
-                File xml;
-                if (configFile.startsWith(CLASSPATH_CONFIG_PREFIX)) {
-                    String source =
-                        configFile.substring(CLASSPATH_CONFIG_PREFIX.length());
-                    xml = new File(homeDir, "repository.xml");
-                    copyConfigFile(source, xml);
-                } else {
-                    xml = new File(configFile);
-                }
-
-                Map<String, String> parameters = new HashMap<String, String>();
-                parameters.put(
-                        "org.apache.jackrabbit.repository.home",
-                        dir.getPath());
-                parameters.put(
-                        "org.apache.jackrabbit.repository.conf",
-                        xml.getPath());
-                repository = JcrUtils.getRepository(parameters);
-            }
-
-            return repository;
-        }
-
-        private void copyConfigFile(String source, File target)
-                throws RepositoryException {
-            ClassLoader cl = Thread.currentThread().getContextClassLoader();
-            if (cl == null) {
-                cl = this.getClass().getClassLoader();
-            }
-
-            InputStream input = cl.getResourceAsStream(source);
-            if (input != null) {
-                try {
-                    try {
-                        OutputStream output = new FileOutputStream(target);
-                        try {
-                            IOUtils.copy(input, output);
-                        } finally {
-                            output.close();
-                        }
-                    } finally {
-                        input.close();
-                    }
-                } catch (IOException e) {
-                    throw new RepositoryException(
-                            "Failed to copy configuration to " + target, e);
-                }
-            } else {
-                throw new RepositoryException(
-                        "Repository configuration not found: " + source);
-            }
-        }
-
-        /**
-         * Shutdown the repository.
-         */
-        public void shutdown() {
-            if (repository instanceof JackrabbitRepository) {
-                ((JackrabbitRepository) repository).shutdown();
-            }
-        }
-
-        /**
-         * Return the hash code.
-         */
-        public int hashCode() {
-            int result = homeDir != null ? homeDir.hashCode() : 0;
-            result = 37 * result + (configFile != null ? configFile.hashCode() : 0);
-            return result;
-        }
-
-        /**
-         * Return true if equals.
-         */
-        public boolean equals(Object o) {
-            if (o == this) {
-                return true;
-            } else if (o instanceof Reference) {
-                return equals((Reference) o);
-            } else {
-                return false;
-            }
-        }
-
-        /**
-         * Return true if equals.
-         */
-        private boolean equals(Reference o) {
-            return equals(homeDir, o.homeDir)
-                && equals(configFile, o.configFile);
-        }
-
-        /**
-         * Return true if equals.
-         */
-        private boolean equals(String s1, String s2) {
-            if (s1 == s2) {
-                return true;
-            } else if ((s1 == null) || (s2 == null)) {
-                return false;
-            } else {
-                return s1.equals(s2);
-            }
-        }
-    }
-
     public boolean isAutoShutdown() {
         return autoShutdown;
     }
@@ -279,10 +108,13 @@ public final class JCARepositoryManager 
      * @param homeDir   The location of the repository.
      * @param configFile The path to the repository configuration file.
      */
-    public void autoShutdownRepository(String homeDir, String configFile) {
+    public synchronized void autoShutdownRepository(
+            Map<String, String> parameters) {
         if (this.isAutoShutdown()) {
-            Reference ref = getReference(homeDir, configFile);
-            ref.shutdown();
+            Repository repository = repositories.get(parameters);
+            if (repository instanceof JackrabbitRepository) {
+                ((JackrabbitRepository) repository).shutdown();
+            }
         }
     }