jackrabbit-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From mreut...@apache.org
Subject svn commit: r721388 - in /jackrabbit/trunk/jackrabbit-core/src: main/java/org/apache/jackrabbit/core/ main/java/org/apache/jackrabbit/core/jndi/ main/resources/META-INF/services/ test/java/org/apache/jackrabbit/core/integration/
Date Fri, 28 Nov 2008 09:00:04 GMT
Author: mreutegg
Date: Fri Nov 28 01:00:04 2008
New Revision: 721388

URL: http://svn.apache.org/viewvc?rev=721388&view=rev
Log:
JCR-1834: Create RepositoryFactory implementation

Added:
    jackrabbit/trunk/jackrabbit-core/src/main/java/org/apache/jackrabbit/core/RepositoryFactoryImpl.java
  (with props)
    jackrabbit/trunk/jackrabbit-core/src/main/java/org/apache/jackrabbit/core/jndi/RepositoryFactoryImpl.java
  (with props)
    jackrabbit/trunk/jackrabbit-core/src/main/resources/META-INF/services/
    jackrabbit/trunk/jackrabbit-core/src/main/resources/META-INF/services/org.apache.jackrabbit.api.jsr283.RepositoryFactory
    jackrabbit/trunk/jackrabbit-core/src/test/java/org/apache/jackrabbit/core/integration/RepositoryFactoryImplTest.java
  (with props)

Added: 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=721388&view=auto
==============================================================================
--- jackrabbit/trunk/jackrabbit-core/src/main/java/org/apache/jackrabbit/core/RepositoryFactoryImpl.java
(added)
+++ jackrabbit/trunk/jackrabbit-core/src/main/java/org/apache/jackrabbit/core/RepositoryFactoryImpl.java
Fri Nov 28 01:00:04 2008
@@ -0,0 +1,99 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.jackrabbit.core;
+
+import java.util.Map;
+import java.util.HashMap;
+import java.io.IOException;
+
+import javax.jcr.Repository;
+import javax.jcr.RepositoryException;
+
+import org.apache.jackrabbit.api.jsr283.RepositoryFactory;
+import org.apache.jackrabbit.api.JackrabbitRepository;
+
+/**
+ * <code>RepositoryFactoryImpl</code> implements a repository factory that
+ * creates a {@link TransientRepository} on {@link #getRepository(Map)}.
+ */
+public class RepositoryFactoryImpl implements RepositoryFactory {
+
+    /**
+     * Name of the repository home parameter.
+     */
+    public static final String REPOSITORY_HOME
+            = "org.apache.jackrabbit.repository.home";
+
+    /**
+     * Name of the repository configuration parameter.
+     */
+    public static final String REPOSITORY_CONF
+            = "org.apache.jackrabbit.repository.conf";
+
+    /**
+     * Map of repository instances. Key = repository home, value = repository
+     * instance.
+     */
+    private static final Map REPOSITORY_INSTANCES = new HashMap();
+
+    public Repository getRepository(Map parameters) throws RepositoryException {
+        JackrabbitRepository repo;
+        synchronized (REPOSITORY_INSTANCES) {
+            if (parameters == null) {
+                repo = getOrCreateRepository(null, null);
+            } else if (parameters.containsKey(REPOSITORY_CONF)
+                    && parameters.containsKey(REPOSITORY_HOME)) {
+                String conf = (String) parameters.get(REPOSITORY_CONF);
+                String home = (String) parameters.get(REPOSITORY_HOME);
+                repo = getOrCreateRepository(conf, home);
+            } else {
+                // unknown or insufficient parameters
+                repo = null;
+            }
+        }
+        return repo;
+    }
+
+    /**
+     * Either returns a cached repository or creates a repository instance and
+     * puts it into the {@link #REPOSITORY_INSTANCES} cache.
+     *
+     * @param conf path to the repository configuration file.
+     * @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(String conf,
+                                                       String home)
+            throws RepositoryException {
+        JackrabbitRepository repo = (JackrabbitRepository) REPOSITORY_INSTANCES.get(home);
+        try {
+            if (repo == null) {
+                if (home == null) {
+                    repo = new TransientRepository();
+                } else {
+                    repo = new TransientRepository(conf, home);
+                }
+                REPOSITORY_INSTANCES.put(home, repo);
+            }
+        } catch (IOException e) {
+            throw new RepositoryException(e);
+        }
+        return repo;
+    }
+}

Propchange: jackrabbit/trunk/jackrabbit-core/src/main/java/org/apache/jackrabbit/core/RepositoryFactoryImpl.java
------------------------------------------------------------------------------
    svn:eol-style = native

Added: jackrabbit/trunk/jackrabbit-core/src/main/java/org/apache/jackrabbit/core/jndi/RepositoryFactoryImpl.java
URL: http://svn.apache.org/viewvc/jackrabbit/trunk/jackrabbit-core/src/main/java/org/apache/jackrabbit/core/jndi/RepositoryFactoryImpl.java?rev=721388&view=auto
==============================================================================
--- jackrabbit/trunk/jackrabbit-core/src/main/java/org/apache/jackrabbit/core/jndi/RepositoryFactoryImpl.java
(added)
+++ jackrabbit/trunk/jackrabbit-core/src/main/java/org/apache/jackrabbit/core/jndi/RepositoryFactoryImpl.java
Fri Nov 28 01:00:04 2008
@@ -0,0 +1,62 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.jackrabbit.core.jndi;
+
+import java.util.Map;
+import java.util.Hashtable;
+
+import javax.jcr.Repository;
+import javax.jcr.RepositoryException;
+import javax.naming.InitialContext;
+
+import org.apache.jackrabbit.api.jsr283.RepositoryFactory;
+
+/**
+ * <code>RepositoryFactoryImpl</code> implements a repository factory that
+ * obtains a repository instance through JNDI.
+ * <p/>
+ * This implementation does not support the notion of a default repository.
+ */
+public class RepositoryFactoryImpl implements RepositoryFactory {
+
+    /**
+     * The name of the JNDI name property.
+     */
+    public static final String REPOSITORY_JNDI_NAME
+            = "org.apache.jackrabbit.repository.jndi.name";
+
+    public Repository getRepository(Map parameters) throws RepositoryException {
+        if (parameters == null) {
+            // this implementation does not support a default repository
+            return null;
+        }
+
+        String name = (String) parameters.get(REPOSITORY_JNDI_NAME);
+        if (name == null) {
+            // don't know how to handle
+            return null;
+        }
+
+        try {
+            Hashtable environment = new Hashtable(parameters);
+            InitialContext context = new InitialContext(environment);
+            return (Repository) context.lookup(name);
+        } catch (Exception e) {
+            throw new RepositoryException(e);
+        }
+    }
+}

Propchange: jackrabbit/trunk/jackrabbit-core/src/main/java/org/apache/jackrabbit/core/jndi/RepositoryFactoryImpl.java
------------------------------------------------------------------------------
    svn:eol-style = native

Added: jackrabbit/trunk/jackrabbit-core/src/main/resources/META-INF/services/org.apache.jackrabbit.api.jsr283.RepositoryFactory
URL: http://svn.apache.org/viewvc/jackrabbit/trunk/jackrabbit-core/src/main/resources/META-INF/services/org.apache.jackrabbit.api.jsr283.RepositoryFactory?rev=721388&view=auto
==============================================================================
--- jackrabbit/trunk/jackrabbit-core/src/main/resources/META-INF/services/org.apache.jackrabbit.api.jsr283.RepositoryFactory
(added)
+++ jackrabbit/trunk/jackrabbit-core/src/main/resources/META-INF/services/org.apache.jackrabbit.api.jsr283.RepositoryFactory
Fri Nov 28 01:00:04 2008
@@ -0,0 +1,21 @@
+#  Licensed to the Apache Software Foundation (ASF) under one or more
+#  contributor license agreements.  See the NOTICE file distributed with
+#  this work for additional information regarding copyright ownership.
+#  The ASF licenses this file to You under the Apache License, Version 2.0
+#  (the "License"); you may not use this file except in compliance with
+#  the License.  You may obtain a copy of the License at
+#
+#       http://www.apache.org/licenses/LICENSE-2.0
+#
+#  Unless required by applicable law or agreed to in writing, software
+#  distributed under the License is distributed on an "AS IS" BASIS,
+#  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+#  See the License for the specific language governing permissions and
+#  limitations under the License.
+
+#
+# This file lists the repository factory implementation for jackrabbit
+#
+
+org.apache.jackrabbit.core.RepositoryFactoryImpl
+org.apache.jackrabbit.core.jndi.RepositoryFactoryImpl

Added: jackrabbit/trunk/jackrabbit-core/src/test/java/org/apache/jackrabbit/core/integration/RepositoryFactoryImplTest.java
URL: http://svn.apache.org/viewvc/jackrabbit/trunk/jackrabbit-core/src/test/java/org/apache/jackrabbit/core/integration/RepositoryFactoryImplTest.java?rev=721388&view=auto
==============================================================================
--- jackrabbit/trunk/jackrabbit-core/src/test/java/org/apache/jackrabbit/core/integration/RepositoryFactoryImplTest.java
(added)
+++ jackrabbit/trunk/jackrabbit-core/src/test/java/org/apache/jackrabbit/core/integration/RepositoryFactoryImplTest.java
Fri Nov 28 01:00:04 2008
@@ -0,0 +1,194 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.jackrabbit.core.integration;
+
+import java.io.File;
+import java.util.Map;
+import java.util.HashMap;
+import java.util.Hashtable;
+import java.util.Iterator;
+
+import javax.jcr.RepositoryException;
+import javax.jcr.Repository;
+import javax.jcr.SimpleCredentials;
+import javax.jcr.Credentials;
+import javax.naming.InitialContext;
+import javax.naming.Context;
+import javax.imageio.spi.ServiceRegistry;
+
+import org.apache.jackrabbit.api.jsr283.RepositoryFactory;
+import org.apache.jackrabbit.api.JackrabbitRepository;
+import org.apache.jackrabbit.core.RepositoryFactoryImpl;
+import org.apache.jackrabbit.core.jndi.provider.DummyInitialContextFactory;
+import org.apache.jackrabbit.core.jndi.RegistryHelper;
+
+import junit.framework.TestCase;
+
+/**
+ * <code>RepositoryFactoryImplTest</code> performs tests on
+ * {@link RepositoryFactoryImpl}.
+ */
+public class RepositoryFactoryImplTest extends TestCase {
+
+    private static final Credentials CREDENTIALS
+            = new SimpleCredentials("user", "pass".toCharArray());
+
+    private static final File TARGET = new File("target");
+
+    private static final File REPO_HOME = new File(TARGET, "repository-factory-test");
+
+    private static final File REPO_CONF = new File(new File(TARGET, "repository"), "repository.xml");
+
+    private Repository repo;
+
+    /**
+     * Makes sure the repository is shutdown.
+     */
+    protected void tearDown() throws Exception {
+        if (repo instanceof JackrabbitRepository) {
+            ((JackrabbitRepository) repo).shutdown();
+        }
+        super.tearDown();
+    }
+
+    /**
+     * Checks if a default repository is returned.
+     *
+     * @throws RepositoryException if an error occurs.
+     */
+    public void testDefaultRepository() throws RepositoryException {
+        setProperties();
+        repo = RepositoryManager.getRepository(null);
+        checkRepository(repo);
+    }
+
+    /**
+     * Checks if a repository is returned for the given parameters.
+     *
+     * @throws RepositoryException if an error occurs.
+     */
+    public void testWithParameters() throws RepositoryException {
+        resetProperties();
+        repo = RepositoryManager.getRepository(getParamters());
+        checkRepository(repo);
+    }
+
+    /**
+     * Checks if multiple {@link RepositoryFactory#getRepository(Map)} calls
+     * return the same repository instance.
+     *
+     * @throws RepositoryException if an error occurs.
+     */
+    public void testMultipleConnect() throws RepositoryException {
+        setProperties();
+        repo = RepositoryManager.getRepository(null);
+        checkRepository(repo);
+        assertSame(repo, RepositoryManager.getRepository(null));
+    }
+
+    /**
+     * Checks if multiple {@link RepositoryFactory#getRepository(Map)} calls
+     * return the same repository instance.
+     *
+     * @throws RepositoryException if an error occurs.
+     */
+    public void testMultipleConnectWithParameters() throws RepositoryException {
+        resetProperties();
+        repo = RepositoryManager.getRepository(getParamters());
+        checkRepository(repo);
+        assertSame(repo, RepositoryManager.getRepository(getParamters()));
+    }
+
+    /**
+     * Checks if {@link RepositoryFactory#getRepository(Map)} returns a repository
+     * that can be used even after a previously retrieved repository had been
+     * {@link JackrabbitRepository#shutdown() shutdown}.
+     *
+     * @throws RepositoryException if an error occurs.
+     */
+    public void testShutdown() throws RepositoryException {
+        setProperties();
+        for (int i = 0; i < 2; i++) {
+            repo = RepositoryManager.getRepository(null);
+            checkRepository(repo);
+            ((JackrabbitRepository) repo).shutdown();
+        }
+    }
+
+    /**
+     * Checks if a repository can be obtained by specifying JNDI parameters
+     * on {@link RepositoryFactory#getRepository(Map)}.
+     *
+     * @throws Exception if an error occurs.
+     */
+    public void testJNDI() throws Exception {
+        String name = "jackrabbit-repository";
+        Map parameters = new HashMap();
+        parameters.put(Context.INITIAL_CONTEXT_FACTORY, DummyInitialContextFactory.class.getName());
+        parameters.put(Context.PROVIDER_URL, "localhost");
+        InitialContext context = new InitialContext(new Hashtable(parameters));
+        RegistryHelper.registerRepository(context, name,
+                REPO_CONF.getAbsolutePath(), REPO_HOME.getAbsolutePath(), false);
+        try {
+            parameters.put(org.apache.jackrabbit.core.jndi.RepositoryFactoryImpl.REPOSITORY_JNDI_NAME,
name);
+            repo = RepositoryManager.getRepository(parameters);
+            checkRepository(repo);
+        } finally {
+            RegistryHelper.unregisterRepository(context, name);
+        }
+    }
+
+    //-------------------------< internal helper >------------------------------
+
+    private static void checkRepository(Repository r) throws RepositoryException {
+        r.login(CREDENTIALS).logout();
+    }
+
+    private static void setProperties() {
+        System.setProperty(RepositoryFactoryImpl.REPOSITORY_HOME, REPO_HOME.getAbsolutePath());
+        System.setProperty(RepositoryFactoryImpl.REPOSITORY_CONF, REPO_CONF.getAbsolutePath());
+    }
+
+    private static void resetProperties() {
+        System.setProperty(RepositoryFactoryImpl.REPOSITORY_HOME, "");
+        System.setProperty(RepositoryFactoryImpl.REPOSITORY_CONF, "");
+    }
+
+    private static Map getParamters() {
+        Map params = new HashMap();
+        params.put(RepositoryFactoryImpl.REPOSITORY_HOME, REPO_HOME.getAbsolutePath());
+        params.put(RepositoryFactoryImpl.REPOSITORY_CONF, REPO_CONF.getAbsolutePath());
+        return params;
+    }
+
+    private static final class RepositoryManager {
+
+        private static Repository getRepository(Map parameters)
+                throws RepositoryException {
+            Repository repo = null;
+            Iterator factories = ServiceRegistry.lookupProviders(RepositoryFactory.class);
+            while (factories.hasNext()) {
+                RepositoryFactory factory = (RepositoryFactory) factories.next();
+                repo = factory.getRepository(parameters);
+                if (repo != null) {
+                    break;
+                }
+            }
+            return repo;
+        }
+    }
+}

Propchange: jackrabbit/trunk/jackrabbit-core/src/test/java/org/apache/jackrabbit/core/integration/RepositoryFactoryImplTest.java
------------------------------------------------------------------------------
    svn:eol-style = native



Mime
View raw message