Index: src/main/java/org/apache/jackrabbit/core/jndi/RepositoryFactoryImpl.java =================================================================== --- src/main/java/org/apache/jackrabbit/core/jndi/RepositoryFactoryImpl.java (revision 0) +++ src/main/java/org/apache/jackrabbit/core/jndi/RepositoryFactoryImpl.java (revision 0) @@ -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; + +/** + * RepositoryFactoryImpl implements a repository factory that + * obtains a repository instance through JNDI. + *

+ * This implementation does not support the notion of a default repository. + */ +public class RepositoryFactoryImpl extends RepositoryFactory { + + /** + * The name of the JNDI name property. + */ + public static final String REPOSITORY_JNDI_NAME + = "org.apache.jackrabbit.repository.jndi.name"; + + public Repository connect(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); + } + } +} Property changes on: src\main\java\org\apache\jackrabbit\core\jndi\RepositoryFactoryImpl.java ___________________________________________________________________ Added: svn:eol-style + native Index: src/main/java/org/apache/jackrabbit/core/RepositoryFactoryImpl.java =================================================================== --- src/main/java/org/apache/jackrabbit/core/RepositoryFactoryImpl.java (revision 0) +++ src/main/java/org/apache/jackrabbit/core/RepositoryFactoryImpl.java (revision 0) @@ -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; + +/** + * RepositoryFactoryImpl implements a repository factory that + * creates a {@link TransientRepository} on {@link #connect(Map)}. + */ +public class RepositoryFactoryImpl extends 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 connect(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; + } +} Property changes on: src\main\java\org\apache\jackrabbit\core\RepositoryFactoryImpl.java ___________________________________________________________________ Added: svn:eol-style + native Index: src/main/resources/META-INF/services/org.apache.jackrabbit.api.jsr283.RepositoryFactory =================================================================== --- src/main/resources/META-INF/services/org.apache.jackrabbit.api.jsr283.RepositoryFactory (revision 0) +++ src/main/resources/META-INF/services/org.apache.jackrabbit.api.jsr283.RepositoryFactory (revision 0) @@ -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 Property changes on: src\main\resources\META-INF\services\org.apache.jackrabbit.api.jsr283.RepositoryFactory ___________________________________________________________________ Added: svn:eol-style + native Index: src/test/java/org/apache/jackrabbit/core/integration/RepositoryFactoryImplTest.java =================================================================== --- src/test/java/org/apache/jackrabbit/core/integration/RepositoryFactoryImplTest.java (revision 0) +++ src/test/java/org/apache/jackrabbit/core/integration/RepositoryFactoryImplTest.java (revision 0) @@ -0,0 +1,175 @@ +/* + * 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 javax.jcr.RepositoryException; +import javax.jcr.Repository; +import javax.jcr.SimpleCredentials; +import javax.jcr.Credentials; +import javax.naming.InitialContext; +import javax.naming.Context; + +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; + +/** + * RepositoryFactoryImplTest 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 = RepositoryFactory.getRepository(); + 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 = RepositoryFactory.getRepository(getParamters()); + checkRepository(repo); + } + + /** + * Checks if multiple {@link RepositoryFactory#getRepository()} calls return + * the same repository instance. + * + * @throws RepositoryException if an error occurs. + */ + public void testMultipleConnect() throws RepositoryException { + setProperties(); + repo = RepositoryFactory.getRepository(); + checkRepository(repo); + assertSame(repo, RepositoryFactory.getRepository()); + } + + /** + * 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 = RepositoryFactory.getRepository(getParamters()); + checkRepository(repo); + assertSame(repo, RepositoryFactory.getRepository(getParamters())); + } + + /** + * Checks if {@link RepositoryFactory#getRepository()} 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 = RepositoryFactory.getRepository(); + 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 = RepositoryFactory.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; + } +} Property changes on: src\test\java\org\apache\jackrabbit\core\integration\RepositoryFactoryImplTest.java ___________________________________________________________________ Added: svn:eol-style + native