Author: sascharodekamp
Date: Mon Jun 27 12:02:12 2011 New Revision: 1140127 URL: http://svn.apache.org/viewvc?rev=1140127&view=rev Log: Update -- JNDI implementation update for JCR Repositories. Added: ofbiz/branches/jackrabbit20100709/framework/jcr/src/org/ofbiz/jcr/RepositoryFactory.java (with props) ofbiz/branches/jackrabbit20100709/framework/jcr/src/org/ofbiz/jcr/RepositoryLoader.java (with props) Removed: ofbiz/branches/jackrabbit20100709/framework/jcr/config/jcr-repositories.xml Modified: ofbiz/branches/jackrabbit20100709/framework/jcr/config/jcr-config.xml ofbiz/branches/jackrabbit20100709/framework/jcr/src/org/ofbiz/jcr/JCRContainer.java ofbiz/branches/jackrabbit20100709/framework/jcr/src/org/ofbiz/jcr/JCRFactory.java ofbiz/branches/jackrabbit20100709/framework/jcr/src/org/ofbiz/jcr/JCRFactoryUtil.java ofbiz/branches/jackrabbit20100709/framework/jcr/src/org/ofbiz/jcr/jackrabbit/JCRFactoryImpl.java Modified: ofbiz/branches/jackrabbit20100709/framework/jcr/config/jcr-config.xml URL: http://svn.apache.org/viewvc/ofbiz/branches/jackrabbit20100709/framework/jcr/config/jcr-config.xml?rev=1140127&r1=1140126&r2=1140127&view=diff ============================================================================== --- ofbiz/branches/jackrabbit20100709/framework/jcr/config/jcr-config.xml (original) +++ ofbiz/branches/jackrabbit20100709/framework/jcr/config/jcr-config.xml Mon Jun 27 12:02:12 2011 @@ -1,22 +1,7 @@ <?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. ---> +<!-- 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-config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> @@ -24,12 +9,10 @@ under the License. <jcr-context name="default" /> <!-- This is the default implementation and uses the JackRabbit implementation as its default --> - <jcr name="default" class="org.ofbiz.jcr.jackrabbit.JCRFactoryImpl" jndi-name="jcr/local"/> + <jcr name="default" class="org.ofbiz.jcr.jackrabbit.JCRFactoryImpl" jndi-name="jcr/local" /> <!-- This is an example custom implementation and uses the class name specified as jcr implementation --> - <!-- - <jcr name="myCustom" class="com.mycompany.jcr.MyCustomJCR"/> - --> + <!-- <jcr name="myCustom" class="com.mycompany.jcr.MyCustomJCR"/> --> <!-- jcr home directory --> <home-dir path="runtime/data/jcr/" /> Modified: ofbiz/branches/jackrabbit20100709/framework/jcr/src/org/ofbiz/jcr/JCRContainer.java URL: http://svn.apache.org/viewvc/ofbiz/branches/jackrabbit20100709/framework/jcr/src/org/ofbiz/jcr/JCRContainer.java?rev=1140127&r1=1140126&r2=1140127&view=diff ============================================================================== --- ofbiz/branches/jackrabbit20100709/framework/jcr/src/org/ofbiz/jcr/JCRContainer.java (original) +++ ofbiz/branches/jackrabbit20100709/framework/jcr/src/org/ofbiz/jcr/JCRContainer.java Mon Jun 27 12:02:12 2011 @@ -19,15 +19,24 @@ package org.ofbiz.jcr; import javax.jcr.RepositoryException; +import javax.jcr.RepositoryFactory; +import javax.naming.Context; +import javax.naming.NamingException; +import javax.naming.Reference; +import javax.naming.StringRefAddr; import org.ofbiz.base.config.GenericConfigException; import org.ofbiz.base.config.ResourceLoader; import org.ofbiz.base.container.Container; import org.ofbiz.base.container.ContainerConfig; import org.ofbiz.base.container.ContainerException; +import org.ofbiz.base.util.Debug; +import org.ofbiz.base.util.JNDIContextFactory; import org.ofbiz.base.util.UtilXml; import org.w3c.dom.Element; +import com.sun.corba.se.spi.activation.Repository; + /** * A container for a local JCR-compliant content repository. The default * implementation uses Apache Jackrabbit. @@ -37,13 +46,18 @@ public class JCRContainer implements Con public static final String module = JCRContainer.class.getName(); public static final String DEFAULT_JCR_CONFIG_PATH = "framework/jcr/config/jcr-config.xml"; + public static final String REP_HOME_DIR = "0"; + public static final String CONFIG_FILE_PATH = "1"; private static String jndiName = null; private static String factoryClassName = null; private static String jcrContextName = null; - private String configFilePath = null; + private static String configFilePath = null; private boolean removeRepositoryOnShutdown = false; + private String homeDir = null; + + Context jndiContext = null; /* * (non-Javadoc) @@ -53,14 +67,13 @@ public class JCRContainer implements Con */ @Override public void init(String[] args, String configFile) throws ContainerException { - // get the container config + // get the container configuration ContainerConfig.Container cc = ContainerConfig.getContainer("jcr-container", configFile); if (cc == null) { throw new ContainerException("No jcr-container configuration found in container config!"); } // embedded properties - jndiName = ContainerConfig.getPropertyValue(cc, "jndiName", "jcr/local"); removeRepositoryOnShutdown = ContainerConfig.getPropertyValue(cc, "removeRepositoryOnShutdown", false); configFilePath = ContainerConfig.getPropertyValue(cc, "configFilePath", DEFAULT_JCR_CONFIG_PATH); @@ -82,6 +95,8 @@ public class JCRContainer implements Con for (Element curElement : UtilXml.childElementList(configRootElement, "jcr")) { if (jcrContextName.equals(curElement.getAttribute("name"))) { factoryClassName = curElement.getAttribute("class"); + homeDir = curElement.getAttribute("home-dir"); + jndiName = curElement.getAttribute("jndi-name"); break; } } @@ -118,6 +133,17 @@ public class JCRContainer implements Con throw new ContainerException("Cannot start JCRFactory context", e); } + // get JNDI context + try { + jndiContext = JNDIContextFactory.getInitialContext("localjndi"); + } catch (GenericConfigException e) { + Debug.logError(e, module); + } + + bindRepository(); + // Test JNDI bind + RepositoryLoader.getRepository(); + return true; } @@ -148,4 +174,32 @@ public class JCRContainer implements Con public static String getFactoryClassName() { return factoryClassName; } + + public static String getConfigFilePath() { + return configFilePath; + } + + protected void bindRepository() { + if (this.jndiContext != null) { + try { + Reference ref = new Reference(Repository.class.getName(), RepositoryFactory.class.getName(), null); + ref.add(new StringRefAddr(REP_HOME_DIR, homeDir)); + ref.add(new StringRefAddr(CONFIG_FILE_PATH, configFilePath)); + this.jndiContext.bind(jndiName, ref); + Debug.logInfo("Repository bound to JNDI as " + jndiName, module); + } catch (NamingException ne) { + Debug.logError(ne, module); + } + } + } + + protected void unbindRepository(String name) { + if (this.jndiContext != null) { + try { + this.jndiContext.unbind(jndiName); + } catch (NamingException e) { + Debug.logError(e, module); + } + } + } } Modified: ofbiz/branches/jackrabbit20100709/framework/jcr/src/org/ofbiz/jcr/JCRFactory.java URL: http://svn.apache.org/viewvc/ofbiz/branches/jackrabbit20100709/framework/jcr/src/org/ofbiz/jcr/JCRFactory.java?rev=1140127&r1=1140126&r2=1140127&view=diff ============================================================================== --- ofbiz/branches/jackrabbit20100709/framework/jcr/src/org/ofbiz/jcr/JCRFactory.java (original) +++ ofbiz/branches/jackrabbit20100709/framework/jcr/src/org/ofbiz/jcr/JCRFactory.java Mon Jun 27 12:02:12 2011 @@ -18,6 +18,7 @@ *******************************************************************************/ package org.ofbiz.jcr; +import javax.jcr.Repository; import javax.jcr.RepositoryException; import javax.jcr.Session; @@ -59,4 +60,7 @@ public interface JCRFactory { // public static final String NODE_DOCUMENTLIBRARY = // PropsUtil.get(PropsKeys.JCR_NODE_DOCUMENTLIBRARY); + public Repository getInstance(); + + public Repository getRepository(); } Modified: ofbiz/branches/jackrabbit20100709/framework/jcr/src/org/ofbiz/jcr/JCRFactoryUtil.java URL: http://svn.apache.org/viewvc/ofbiz/branches/jackrabbit20100709/framework/jcr/src/org/ofbiz/jcr/JCRFactoryUtil.java?rev=1140127&r1=1140126&r2=1140127&view=diff ============================================================================== --- ofbiz/branches/jackrabbit20100709/framework/jcr/src/org/ofbiz/jcr/JCRFactoryUtil.java (original) +++ ofbiz/branches/jackrabbit20100709/framework/jcr/src/org/ofbiz/jcr/JCRFactoryUtil.java Mon Jun 27 12:02:12 2011 @@ -18,6 +18,7 @@ *******************************************************************************/ package org.ofbiz.jcr; +import javax.jcr.Repository; import javax.jcr.RepositoryException; import javax.jcr.Session; @@ -70,4 +71,8 @@ public class JCRFactoryUtil { return session; } + + public static Repository getRepository() { + return getJCRFactory().getRepository(); + } } Added: ofbiz/branches/jackrabbit20100709/framework/jcr/src/org/ofbiz/jcr/RepositoryFactory.java URL: http://svn.apache.org/viewvc/ofbiz/branches/jackrabbit20100709/framework/jcr/src/org/ofbiz/jcr/RepositoryFactory.java?rev=1140127&view=auto ============================================================================== --- ofbiz/branches/jackrabbit20100709/framework/jcr/src/org/ofbiz/jcr/RepositoryFactory.java (added) +++ ofbiz/branches/jackrabbit20100709/framework/jcr/src/org/ofbiz/jcr/RepositoryFactory.java Mon Jun 27 12:02:12 2011 @@ -0,0 +1,43 @@ +package org.ofbiz.jcr; + +import java.io.File; +import java.util.Hashtable; +import java.util.Map; + +import javax.jcr.RepositoryException; +import javax.naming.Context; +import javax.naming.Name; +import javax.naming.Reference; +import javax.naming.spi.ObjectFactory; + +import org.apache.commons.collections.map.ReferenceMap; +import org.apache.jackrabbit.commons.JcrUtils; +import org.apache.jackrabbit.core.TransientRepository; + +public class RepositoryFactory implements ObjectFactory { + + private static final Map<Object, Object> cache = new ReferenceMap(); + + public Object getObjectInstance(Object obj, Name name, Context nameCtx, Hashtable environment) throws RepositoryException { + synchronized (cache) { + Object instance = cache.get(obj); + if (instance == null && obj instanceof Reference) { + Reference ref = (Reference) obj; + String repHomeDir = ref.get(JCRContainer.REP_HOME_DIR).getContent().toString(); + // check if the repository is already started, than use it + // otherwise create it + File lock = new File(repHomeDir); + if (lock.exists()) { + instance = JcrUtils.getRepository(lock.toURI().toString()); + } else { + instance = new TransientRepository(ref.get(JCRContainer.DEFAULT_JCR_CONFIG_PATH).getContent().toString(), repHomeDir); + } + + cache.put(obj, instance); + } + + return instance; + } + } + +} Propchange: ofbiz/branches/jackrabbit20100709/framework/jcr/src/org/ofbiz/jcr/RepositoryFactory.java ------------------------------------------------------------------------------ svn:mime-type = text/plain Added: ofbiz/branches/jackrabbit20100709/framework/jcr/src/org/ofbiz/jcr/RepositoryLoader.java URL: http://svn.apache.org/viewvc/ofbiz/branches/jackrabbit20100709/framework/jcr/src/org/ofbiz/jcr/RepositoryLoader.java?rev=1140127&view=auto ============================================================================== --- ofbiz/branches/jackrabbit20100709/framework/jcr/src/org/ofbiz/jcr/RepositoryLoader.java (added) +++ ofbiz/branches/jackrabbit20100709/framework/jcr/src/org/ofbiz/jcr/RepositoryLoader.java Mon Jun 27 12:02:12 2011 @@ -0,0 +1,97 @@ +package org.ofbiz.jcr; + +import java.util.Map; + +import javax.jcr.Repository; +import javax.naming.NamingException; + +import javolution.util.FastMap; + +import org.ofbiz.base.config.GenericConfigException; +import org.ofbiz.base.config.ResourceLoader; +import org.ofbiz.base.util.Debug; +import org.ofbiz.base.util.JNDIContextFactory; +import org.ofbiz.base.util.UtilValidate; +import org.ofbiz.base.util.UtilXml; +import org.w3c.dom.Element; + +public class RepositoryLoader { + public static final String module = RepositoryLoader.class.getName(); + private static Map<String, Repository> repositories = loadRepositories(); + + public static Repository getRepository(String name) { + return repositories.get(name); + } + + public static Repository getRepository() { + return repositories.get("default"); + } + + private static Repository createFromFactory(String repositoryName, ClassLoader loader, String className) { + try { + JCRFactory factory = (JCRFactory) loader.loadClass(className).newInstance(); + return factory.getInstance(); + } catch (InstantiationException e) { + Debug.logError(e, module); + } catch (IllegalAccessException e) { + Debug.logError(e, module); + } catch (ClassNotFoundException e) { + Debug.logError(e, module); + } + + return null; + } + + private static Map<String, Repository> loadRepositories() { + Map<String, Repository> repos = FastMap.newInstance(); + + String configFilePath = JCRContainer.getConfigFilePath(); + Element configRootElement = null; + try { + configRootElement = ResourceLoader.getXmlRootElement(configFilePath); + } catch (GenericConfigException e) { + Debug.logError(e, "Could not load the jcr configuration in file " + configFilePath, module); + } + + if (configRootElement == null) { + Debug.logError("No jcr configuration found in file " + configFilePath, module); + } + + ClassLoader loader = Thread.currentThread().getContextClassLoader(); + // find the default JCR implementations + for (Element curElement : UtilXml.childElementList(configRootElement, "jcr")) { + String name = curElement.getAttribute("name"); + + // first check if the found repository is already in the repository + // map + if (repos.get(name) != null && (repos.get(name) instanceof Repository)) { + continue; + } + + String jndiName = curElement.getAttribute("jndi-name"); + if (UtilValidate.isNotEmpty(jndiName)) { + try { + repos.put(name, (Repository) JNDIContextFactory.getInitialContext("localjndi").lookup(jndiName)); + } catch (NamingException e) { + Debug.logError(e, module); + } catch (GenericConfigException e) { + Debug.logError(e, module); + } + continue; + } + + String factoryClass = curElement.getAttribute("class"); + if (UtilValidate.isNotEmpty(factoryClass)) { + try { + repos.put(name, createFromFactory(name, loader, factoryClass)); + } catch (Exception e) { + Debug.logError(e, module); + } + } + + } + + return repos; + } + +} Propchange: ofbiz/branches/jackrabbit20100709/framework/jcr/src/org/ofbiz/jcr/RepositoryLoader.java ------------------------------------------------------------------------------ svn:mime-type = text/plain Modified: ofbiz/branches/jackrabbit20100709/framework/jcr/src/org/ofbiz/jcr/jackrabbit/JCRFactoryImpl.java URL: http://svn.apache.org/viewvc/ofbiz/branches/jackrabbit20100709/framework/jcr/src/org/ofbiz/jcr/jackrabbit/JCRFactoryImpl.java?rev=1140127&r1=1140126&r2=1140127&view=diff ============================================================================== --- ofbiz/branches/jackrabbit20100709/framework/jcr/src/org/ofbiz/jcr/jackrabbit/JCRFactoryImpl.java (original) +++ ofbiz/branches/jackrabbit20100709/framework/jcr/src/org/ofbiz/jcr/jackrabbit/JCRFactoryImpl.java Mon Jun 27 12:02:12 2011 @@ -110,4 +110,24 @@ public class JCRFactoryImpl implements J } return session; } + + /* + * (non-Javadoc) + * + * @see org.ofbiz.jcr.JCRFactory#getInstance() + */ + @Override + public Repository getInstance() { + return null; + } + + /* + * (non-Javadoc) + * + * @see org.ofbiz.jcr.JCRFactory#getRepository() + */ + @Override + public Repository getRepository() { + return repository; + } } |
Free forum by Nabble | Edit this page |