|
Author: adrianc
Date: Mon Jul 12 06:18:45 2010 New Revision: 963192 URL: http://svn.apache.org/viewvc?rev=963192&view=rev Log: RepositoryFactory supports multiple JCR Repositories. Added: ofbiz/branches/jackrabbit20100709/framework/jackrabbit/config/jcr-repositories.xml (with props) ofbiz/branches/jackrabbit20100709/framework/jackrabbit/src/org/ofbiz/jackrabbit/JcrRepositoryFactory.java (with props) Removed: ofbiz/branches/jackrabbit20100709/framework/jackrabbit/config/repository.properties Modified: ofbiz/branches/jackrabbit20100709/framework/jackrabbit/src/org/ofbiz/jackrabbit/RepositoryFactory.java Added: ofbiz/branches/jackrabbit20100709/framework/jackrabbit/config/jcr-repositories.xml URL: http://svn.apache.org/viewvc/ofbiz/branches/jackrabbit20100709/framework/jackrabbit/config/jcr-repositories.xml?rev=963192&view=auto ============================================================================== --- ofbiz/branches/jackrabbit20100709/framework/jackrabbit/config/jcr-repositories.xml (added) +++ ofbiz/branches/jackrabbit20100709/framework/jackrabbit/config/jcr-repositories.xml Mon Jul 12 06:18:45 2010 @@ -0,0 +1,31 @@ +<?xml version="1.0" encoding="UTF-8" ?> +<!-- +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. +--> +<jcr-repositories xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> + + <!-- A JCR Repository located using JNDI --> + <repository name="default" jndi-name="jcr/local"/> + + <!-- A JCR Repository created from a factory. The + class-name attribute contains the name of a class that + implements JcrRepositoryFactory. --> + <!-- + <repository name="foo" class-name="com.mydomain.fooRepoFactory"/> + --> +</jcr-repositories> Propchange: ofbiz/branches/jackrabbit20100709/framework/jackrabbit/config/jcr-repositories.xml ------------------------------------------------------------------------------ svn:eol-style = native Propchange: ofbiz/branches/jackrabbit20100709/framework/jackrabbit/config/jcr-repositories.xml ------------------------------------------------------------------------------ svn:keywords = Date Rev Author URL Id Propchange: ofbiz/branches/jackrabbit20100709/framework/jackrabbit/config/jcr-repositories.xml ------------------------------------------------------------------------------ svn:mime-type = text/xml Added: ofbiz/branches/jackrabbit20100709/framework/jackrabbit/src/org/ofbiz/jackrabbit/JcrRepositoryFactory.java URL: http://svn.apache.org/viewvc/ofbiz/branches/jackrabbit20100709/framework/jackrabbit/src/org/ofbiz/jackrabbit/JcrRepositoryFactory.java?rev=963192&view=auto ============================================================================== --- ofbiz/branches/jackrabbit20100709/framework/jackrabbit/src/org/ofbiz/jackrabbit/JcrRepositoryFactory.java (added) +++ ofbiz/branches/jackrabbit20100709/framework/jackrabbit/src/org/ofbiz/jackrabbit/JcrRepositoryFactory.java Mon Jul 12 06:18:45 2010 @@ -0,0 +1,25 @@ +/******************************************************************************* + * 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.ofbiz.jackrabbit; + +import javax.jcr.Repository; + +public interface JcrRepositoryFactory { + Repository getInstance(); +} Propchange: ofbiz/branches/jackrabbit20100709/framework/jackrabbit/src/org/ofbiz/jackrabbit/JcrRepositoryFactory.java ------------------------------------------------------------------------------ svn:eol-style = native Propchange: ofbiz/branches/jackrabbit20100709/framework/jackrabbit/src/org/ofbiz/jackrabbit/JcrRepositoryFactory.java ------------------------------------------------------------------------------ svn:keywords = Date Rev Author URL Id Propchange: ofbiz/branches/jackrabbit20100709/framework/jackrabbit/src/org/ofbiz/jackrabbit/JcrRepositoryFactory.java ------------------------------------------------------------------------------ svn:mime-type = text/plain Modified: ofbiz/branches/jackrabbit20100709/framework/jackrabbit/src/org/ofbiz/jackrabbit/RepositoryFactory.java URL: http://svn.apache.org/viewvc/ofbiz/branches/jackrabbit20100709/framework/jackrabbit/src/org/ofbiz/jackrabbit/RepositoryFactory.java?rev=963192&r1=963191&r2=963192&view=diff ============================================================================== --- ofbiz/branches/jackrabbit20100709/framework/jackrabbit/src/org/ofbiz/jackrabbit/RepositoryFactory.java (original) +++ ofbiz/branches/jackrabbit20100709/framework/jackrabbit/src/org/ofbiz/jackrabbit/RepositoryFactory.java Mon Jul 12 06:18:45 2010 @@ -18,39 +18,96 @@ *******************************************************************************/ package org.ofbiz.jackrabbit; +import java.io.IOException; +import java.net.URL; +import java.util.Collections; +import java.util.Enumeration; +import java.util.HashMap; +import java.util.List; +import java.util.Map; + import javax.jcr.Repository; import javax.naming.InitialContext; import javax.naming.NamingException; import org.ofbiz.base.util.Debug; -import org.ofbiz.base.util.UtilProperties; import org.ofbiz.base.util.UtilValidate; +import org.ofbiz.base.util.UtilXml; +import org.w3c.dom.Document; +import org.w3c.dom.Element; public class RepositoryFactory { public static final String module = RepositoryFactory.class.getName(); - private static final Repository repository = createRepoInstance(); + private static final Map<String, Repository> repositoryMap = createRepositoryMap(); - private static Repository createRepoInstance() { - Repository result = null; - try { - result = createUsingJndi(); - } catch (Exception e) { - Debug.logError(e, module); - } - Debug.logInfo("JNDI lookup returned " + result, module); - return result; + private static Repository createFromFactory(ClassLoader loader, String className) throws InstantiationException, IllegalAccessException, ClassNotFoundException { + JcrRepositoryFactory factory = (JcrRepositoryFactory) loader.loadClass(className).newInstance(); + return factory.getInstance(); } - private static Repository createUsingJndi() throws NamingException { - String repoUrl = UtilProperties.getPropertyValue("repository.properties", "jndi.repository.url"); - if (UtilValidate.isEmpty(repoUrl)) { - return null; - } - return (Repository) new InitialContext().lookup(repoUrl); + private static Map<String, Repository> createRepositoryMap() { + Map<String, Repository> result = new HashMap<String, Repository>(); + loadRepositories(result); + Debug.logInfo("Repositories loaded: " + result.size(), module); + return Collections.unmodifiableMap(result); } public static Repository getRepository() { - return repository; + return repositoryMap.get("default"); + } + + public static Repository getRepository(String name) { + return repositoryMap.get(name); } + + private static void loadRepositories(Map<String, Repository> map) { + ClassLoader loader = Thread.currentThread().getContextClassLoader(); + Enumeration<URL> resources; + try { + resources = loader.getResources("jcr-repositories.xml"); + } catch (IOException e) { + Debug.logError(e, "Could not load list of jcr-repositories.xml", module); + return; + } + while (resources.hasMoreElements()) { + URL repositoriesURL = resources.nextElement(); + Debug.logInfo("Loading repositories from: " + repositoriesURL, module); + Document doc = null; + try { + doc = UtilXml.readXmlDocument(repositoriesURL, false); + } catch (Exception e) { + Debug.logError(e, module); + continue; + } + Element resourceElement = doc.getDocumentElement(); + List<? extends Element> repositoryList = UtilXml.childElementList(resourceElement, "repository"); + for (Element element : repositoryList) { + String name = element.getAttribute("name"); + if (UtilValidate.isEmpty(name)) { + continue; + } + String jndiName = element.getAttribute("jndi-name"); + if (UtilValidate.isNotEmpty(jndiName)) { + try { + map.put(name, (Repository) new InitialContext().lookup(jndiName)); + } catch (NamingException e) { + Debug.logError(e, module); + } + continue; + } + String className = element.getAttribute("class-name"); + if (UtilValidate.isNotEmpty(className)) { + try { + map.put(name, createFromFactory(loader, className)); + } catch (Exception e) { + Debug.logError(e, module); + } + } + } + } + + } + + private RepositoryFactory() {} } |
| Free forum by Nabble | Edit this page |
