svn commit: r1330779 - in /ofbiz/trunk/framework/jcr/src/org/ofbiz/jcr/loader: JCRContainer.java JCRFactory.java JCRFactoryUtil.java JCRJndi.java RepositoryFactory.java RepositoryLoader.java jackrabbit/JCRFactoryImpl.java

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
1 message Options
Reply | Threaded
Open this post in threaded view
|

svn commit: r1330779 - in /ofbiz/trunk/framework/jcr/src/org/ofbiz/jcr/loader: JCRContainer.java JCRFactory.java JCRFactoryUtil.java JCRJndi.java RepositoryFactory.java RepositoryLoader.java jackrabbit/JCRFactoryImpl.java

sascharodekamp
Author: sascharodekamp
Date: Thu Apr 26 11:18:51 2012
New Revision: 1330779

URL: http://svn.apache.org/viewvc?rev=1330779&view=rev
Log:
Clean Up the repository loding code. No functional changes.

Added:
    ofbiz/trunk/framework/jcr/src/org/ofbiz/jcr/loader/JCRJndi.java   (with props)
Modified:
    ofbiz/trunk/framework/jcr/src/org/ofbiz/jcr/loader/JCRContainer.java
    ofbiz/trunk/framework/jcr/src/org/ofbiz/jcr/loader/JCRFactory.java
    ofbiz/trunk/framework/jcr/src/org/ofbiz/jcr/loader/JCRFactoryUtil.java
    ofbiz/trunk/framework/jcr/src/org/ofbiz/jcr/loader/RepositoryFactory.java
    ofbiz/trunk/framework/jcr/src/org/ofbiz/jcr/loader/RepositoryLoader.java
    ofbiz/trunk/framework/jcr/src/org/ofbiz/jcr/loader/jackrabbit/JCRFactoryImpl.java

Modified: ofbiz/trunk/framework/jcr/src/org/ofbiz/jcr/loader/JCRContainer.java
URL: http://svn.apache.org/viewvc/ofbiz/trunk/framework/jcr/src/org/ofbiz/jcr/loader/JCRContainer.java?rev=1330779&r1=1330778&r2=1330779&view=diff
==============================================================================
--- ofbiz/trunk/framework/jcr/src/org/ofbiz/jcr/loader/JCRContainer.java (original)
+++ ofbiz/trunk/framework/jcr/src/org/ofbiz/jcr/loader/JCRContainer.java Thu Apr 26 11:18:51 2012
@@ -18,20 +18,13 @@
  *******************************************************************************/
 package org.ofbiz.jcr.loader;
 
-import javax.jcr.Repository;
 import javax.jcr.RepositoryException;
-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;
 
@@ -44,18 +37,9 @@ 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 static String configFilePath = null;
     private boolean removeRepositoryOnShutdown = false;
-    private String homeDir = null;
-
-    Context jndiContext = null;
 
     /*
      * (non-Javadoc)
@@ -65,52 +49,12 @@ public class JCRContainer implements Con
      */
     @Override
     public void init(String[] args, String configFile) throws ContainerException {
-        // 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
-        removeRepositoryOnShutdown = ContainerConfig.getPropertyValue(cc, "removeRepositoryOnShutdown", false);
-        configFilePath = ContainerConfig.getPropertyValue(cc, "configFilePath", DEFAULT_JCR_CONFIG_PATH);
-
-        Element configRootElement = null;
-        try {
-            configRootElement = ResourceLoader.getXmlRootElement(configFilePath);
-        } catch (GenericConfigException e) {
-            throw new ContainerException("Could not load the jcr configuration in file " + configFilePath, e);
-        }
-
-        if (configRootElement == null) {
-            throw new ContainerException("No jcr configuration found in file " + configFilePath);
-        }
-
-        homeDir = UtilXml.childElementAttribute(configRootElement, "home-dir", "path", "runtime/data/jcr/");
-        Element childElement = UtilXml.firstChildElement(configRootElement, "jcr-context");
-        jcrContextName = UtilXml.elementAttribute(childElement, "name", "default");
+        readContainerConfig(configFile);
 
-        // find the default JCR implementation
-        for (Element curElement : UtilXml.childElementList(configRootElement, "jcr")) {
-            if (jcrContextName.equals(curElement.getAttribute("name"))) {
-                factoryClassName = curElement.getAttribute("class");
-                jndiName = curElement.getAttribute("jndi-name");
-                break;
-            }
-        }
-
-        // get the default JCR factory
-        JCRFactory jcrFactory = JCRFactoryUtil.getJCRFactory();
-
-        if (jcrFactory == null) {
-            throw new ContainerException("Cannot load JCRFactory implementation class");
-        }
+        Element configRootElement = getConfigFileRootElement();
 
-        try {
-            jcrFactory.initialize(configRootElement);
-        } catch (RepositoryException e) {
-            throw new ContainerException("Cannot initialize JCRFactory context", e);
-        }
+        Element factoryImplDefinition = getJcrFactoryImplementationClassName(configRootElement);
+        initializeJcrFactory(configRootElement, factoryImplDefinition);
     }
 
     /*
@@ -120,10 +64,7 @@ public class JCRContainer implements Con
      */
     @Override
     public boolean start() throws ContainerException {
-        JCRFactory jcrFactory = JCRFactoryUtil.getJCRFactory();
-        if (jcrFactory == null) {
-            throw new ContainerException("Cannot load JCRFactory implementation class");
-        }
+        JCRFactory jcrFactory = getJCRFactory();
 
         try {
             jcrFactory.start();
@@ -131,17 +72,6 @@ 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;
     }
 
@@ -152,10 +82,7 @@ public class JCRContainer implements Con
      */
     @Override
     public void stop() throws ContainerException {
-        JCRFactory jcrFactory = JCRFactoryUtil.getJCRFactory();
-        if (jcrFactory == null) {
-            throw new ContainerException("Cannot load JCRFactory implementation class");
-        }
+        JCRFactory jcrFactory = getJCRFactory();
 
         try {
             jcrFactory.stop(removeRepositoryOnShutdown);
@@ -164,40 +91,67 @@ public class JCRContainer implements Con
         }
     }
 
-    /**
-     * returns the class name of the JCRFactory implementation
-     *
-     * @return
-     */
-    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(), org.ofbiz.jcr.loader.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);
-            }
+    private void readContainerConfig(String configFile) throws ContainerException {
+        // 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
+        removeRepositoryOnShutdown = ContainerConfig.getPropertyValue(cc, "removeRepositoryOnShutdown", false);
+        configFilePath = ContainerConfig.getPropertyValue(cc, "configFilePath", DEFAULT_JCR_CONFIG_PATH);
     }
 
-    protected void unbindRepository(String name) {
-        if (this.jndiContext != null) {
-            try {
-                this.jndiContext.unbind(jndiName);
-            } catch (NamingException e) {
-                Debug.logError(e, module);
+    private Element getConfigFileRootElement() throws ContainerException {
+        Element configRootElement = null;
+        try {
+            configRootElement = ResourceLoader.getXmlRootElement(configFilePath);
+        } catch (GenericConfigException e) {
+            throw new ContainerException("Could not load the jcr configuration in file " + configFilePath, e);
+        }
+
+        if (configRootElement == null) {
+            throw new ContainerException("No jcr configuration found in file " + configFilePath);
+        }
+        return configRootElement;
+    }
+
+    private Element getJcrFactoryImplementationClassName(Element configRootElement) {
+        Element childElement = UtilXml.firstChildElement(configRootElement, "jcr-context");
+        String jcrContextName = UtilXml.elementAttribute(childElement, "name", "default");
+
+        // find the default JCR implementation
+        for (Element curElement : UtilXml.childElementList(configRootElement, "jcr")) {
+            if (jcrContextName.equals(curElement.getAttribute("name"))) {
+                return curElement;
             }
         }
+
+        return null;
+    }
+
+    private void initializeJcrFactory(Element configRootElement, Element factoryImplDefinition) throws ContainerException {
+        JCRFactoryUtil.setJcrFactoryClassName(factoryImplDefinition.getAttribute("class"));
+        JCRFactory jcrFactory = getJCRFactory();
+
+        try {
+            jcrFactory.initialize(configRootElement, factoryImplDefinition);
+        } catch (RepositoryException e) {
+            throw new ContainerException("Cannot initialize JCRFactory context", e);
+        }
+    }
+
+    private JCRFactory getJCRFactory() throws ContainerException {
+        JCRFactory jcrFactory = JCRFactoryUtil.getJCRFactory();
+        if (jcrFactory == null) {
+            throw new ContainerException("Cannot load JCRFactory implementation class");
+        }
+        return jcrFactory;
     }
 }

Modified: ofbiz/trunk/framework/jcr/src/org/ofbiz/jcr/loader/JCRFactory.java
URL: http://svn.apache.org/viewvc/ofbiz/trunk/framework/jcr/src/org/ofbiz/jcr/loader/JCRFactory.java?rev=1330779&r1=1330778&r2=1330779&view=diff
==============================================================================
--- ofbiz/trunk/framework/jcr/src/org/ofbiz/jcr/loader/JCRFactory.java (original)
+++ ofbiz/trunk/framework/jcr/src/org/ofbiz/jcr/loader/JCRFactory.java Thu Apr 26 11:18:51 2012
@@ -29,9 +29,10 @@ public interface JCRFactory {
     /**
      *
      * @param configRootElement
+     * @param factoryImplDefinition
      * @throws RepositoryException
      */
-    public void initialize(Element configRootElement) throws RepositoryException;
+    public void initialize(Element configRootElement, Element factoryImplDefinition) throws RepositoryException;
 
     /**
      *

Modified: ofbiz/trunk/framework/jcr/src/org/ofbiz/jcr/loader/JCRFactoryUtil.java
URL: http://svn.apache.org/viewvc/ofbiz/trunk/framework/jcr/src/org/ofbiz/jcr/loader/JCRFactoryUtil.java?rev=1330779&r1=1330778&r2=1330779&view=diff
==============================================================================
--- ofbiz/trunk/framework/jcr/src/org/ofbiz/jcr/loader/JCRFactoryUtil.java (original)
+++ ofbiz/trunk/framework/jcr/src/org/ofbiz/jcr/loader/JCRFactoryUtil.java Thu Apr 26 11:18:51 2012
@@ -28,7 +28,8 @@ public class JCRFactoryUtil {
 
     public static final String module = JCRFactoryUtil.class.getName();
 
-    private static JCRFactory jcrFactory = null;
+    private static JCRFactory jcrFactory;
+    private static String jcrFactoryName;
 
     /**
      *
@@ -43,7 +44,7 @@ public class JCRFactoryUtil {
                     ClassLoader loader = Thread.currentThread().getContextClassLoader();
                     Class<?> c;
                     try {
-                        c = loader.loadClass(JCRContainer.getFactoryClassName());
+                        c = loader.loadClass(jcrFactoryName);
                         jcrFactory = (JCRFactory) c.newInstance();
                     } catch (ClassNotFoundException e) {
                         Debug.logError(e, "Cannot get instance of the jcr implementation", module);
@@ -70,4 +71,8 @@ public class JCRFactoryUtil {
 
         return session;
     }
+
+    public static void setJcrFactoryClassName(String jcrFactoryClassName) {
+        jcrFactoryName = jcrFactoryClassName;
+    }
 }

Added: ofbiz/trunk/framework/jcr/src/org/ofbiz/jcr/loader/JCRJndi.java
URL: http://svn.apache.org/viewvc/ofbiz/trunk/framework/jcr/src/org/ofbiz/jcr/loader/JCRJndi.java?rev=1330779&view=auto
==============================================================================
--- ofbiz/trunk/framework/jcr/src/org/ofbiz/jcr/loader/JCRJndi.java (added)
+++ ofbiz/trunk/framework/jcr/src/org/ofbiz/jcr/loader/JCRJndi.java Thu Apr 26 11:18:51 2012
@@ -0,0 +1,70 @@
+package org.ofbiz.jcr.loader;
+
+import javax.jcr.Repository;
+import javax.naming.InitialContext;
+import javax.naming.NamingException;
+import javax.naming.Reference;
+import javax.naming.StringRefAddr;
+
+import org.ofbiz.base.config.GenericConfigException;
+import org.ofbiz.base.util.Debug;
+import org.ofbiz.base.util.JNDIContextFactory;
+
+public class JCRJndi {
+
+    public static final String module = JCRJndi.class.getName();
+
+    public final static String ADDR_TYPE_FOR_REPOSITORY_HOME_DIR = "REPHOME";
+    public final String ADDR_TYPE_FOR_CONFIG_FILE_PATH = "CONFPATH";
+
+    private final String jndiName;
+    private final String configFilePath;
+    private final String repositoryHomeDir;
+
+    public JCRJndi(String configFilePath, String jndiName, String repositoryHomeDir) {
+        this.configFilePath = configFilePath;
+        this.jndiName = jndiName;
+        this.repositoryHomeDir = repositoryHomeDir;
+    }
+
+    public void registerJcrToJndi() {
+        InitialContext jndiContext = null;
+
+        try {
+            jndiContext = getInitialContext();
+        } catch (GenericConfigException e) {
+            Debug.logError(e, module);
+        }
+
+        bindRepository(jndiContext);
+        // Test JNDI bind
+        RepositoryLoader.getRepository();
+    }
+
+    public void unbindRepository() {
+        try {
+            InitialContext jndiContext = getInitialContext();
+            jndiContext.unbind(jndiName);
+        } catch (NamingException e) {
+            Debug.logError(e, module);
+        } catch (GenericConfigException e) {
+            Debug.logError(e, module);
+        }
+    }
+
+    private InitialContext getInitialContext() throws GenericConfigException {
+        return JNDIContextFactory.getInitialContext("default");
+    }
+
+    private void bindRepository(InitialContext jndiContext) {
+        try {
+            Reference ref = new Reference(Repository.class.getName(), org.ofbiz.jcr.loader.RepositoryFactory.class.getName(), null);
+            ref.add(new StringRefAddr(ADDR_TYPE_FOR_REPOSITORY_HOME_DIR, repositoryHomeDir));
+            ref.add(new StringRefAddr(ADDR_TYPE_FOR_CONFIG_FILE_PATH, configFilePath));
+            jndiContext.bind(jndiName, ref);
+            Debug.logInfo("Repository bound to JNDI as " + jndiName, module);
+        } catch (NamingException ne) {
+            Debug.logError(ne, module);
+        }
+    }
+}

Propchange: ofbiz/trunk/framework/jcr/src/org/ofbiz/jcr/loader/JCRJndi.java
------------------------------------------------------------------------------
    svn:mime-type = text/plain

Modified: ofbiz/trunk/framework/jcr/src/org/ofbiz/jcr/loader/RepositoryFactory.java
URL: http://svn.apache.org/viewvc/ofbiz/trunk/framework/jcr/src/org/ofbiz/jcr/loader/RepositoryFactory.java?rev=1330779&r1=1330778&r2=1330779&view=diff
==============================================================================
--- ofbiz/trunk/framework/jcr/src/org/ofbiz/jcr/loader/RepositoryFactory.java (original)
+++ ofbiz/trunk/framework/jcr/src/org/ofbiz/jcr/loader/RepositoryFactory.java Thu Apr 26 11:18:51 2012
@@ -22,15 +22,15 @@ public class RepositoryFactory implement
         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();
+                Reference reference = (Reference) obj;
+                String repHomeDir = reference.get(JCRJndi.ADDR_TYPE_FOR_REPOSITORY_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);
+                    instance = new TransientRepository(reference.get(JCRContainer.DEFAULT_JCR_CONFIG_PATH).getContent().toString(), repHomeDir);
                 }
 
                 cache.put(obj, instance);

Modified: ofbiz/trunk/framework/jcr/src/org/ofbiz/jcr/loader/RepositoryLoader.java
URL: http://svn.apache.org/viewvc/ofbiz/trunk/framework/jcr/src/org/ofbiz/jcr/loader/RepositoryLoader.java?rev=1330779&r1=1330778&r2=1330779&view=diff
==============================================================================
--- ofbiz/trunk/framework/jcr/src/org/ofbiz/jcr/loader/RepositoryLoader.java (original)
+++ ofbiz/trunk/framework/jcr/src/org/ofbiz/jcr/loader/RepositoryLoader.java Thu Apr 26 11:18:51 2012
@@ -71,7 +71,7 @@ public class RepositoryLoader {
             String jndiName = curElement.getAttribute("jndi-name");
             if (UtilValidate.isNotEmpty(jndiName)) {
                 try {
-                    repos.put(name, (Repository) JNDIContextFactory.getInitialContext("localjndi").lookup(jndiName));
+                    repos.put(name, (Repository) JNDIContextFactory.getInitialContext("default").lookup(jndiName));
                 } catch (NamingException e) {
                     Debug.logError(e, module);
                 } catch (GenericConfigException e) {

Modified: ofbiz/trunk/framework/jcr/src/org/ofbiz/jcr/loader/jackrabbit/JCRFactoryImpl.java
URL: http://svn.apache.org/viewvc/ofbiz/trunk/framework/jcr/src/org/ofbiz/jcr/loader/jackrabbit/JCRFactoryImpl.java?rev=1330779&r1=1330778&r2=1330779&view=diff
==============================================================================
--- ofbiz/trunk/framework/jcr/src/org/ofbiz/jcr/loader/jackrabbit/JCRFactoryImpl.java (original)
+++ ofbiz/trunk/framework/jcr/src/org/ofbiz/jcr/loader/jackrabbit/JCRFactoryImpl.java Thu Apr 26 11:18:51 2012
@@ -47,7 +47,9 @@ import org.apache.jackrabbit.spi.QNodeTy
 import org.ofbiz.base.util.Debug;
 import org.ofbiz.base.util.UtilValidate;
 import org.ofbiz.base.util.UtilXml;
+import org.ofbiz.entity.Delegator;
 import org.ofbiz.jcr.loader.JCRFactory;
+import org.ofbiz.jcr.loader.JCRJndi;
 import org.ofbiz.jcr.orm.jackrabbit.data.JackrabbitArticle;
 import org.ofbiz.jcr.orm.jackrabbit.file.JackrabbitFile;
 import org.ofbiz.jcr.orm.jackrabbit.file.JackrabbitFolder;
@@ -71,21 +73,28 @@ public class JCRFactoryImpl implements J
 
     protected static Repository repository = null;
     protected Session session = null;
+
     protected static Mapper mapper = null;
 
+    private JCRJndi jndi;
+
     /*
      * (non-Javadoc)
      *
      * @see org.ofbiz.jcr.JCRFactory#initialize(org.w3c.dom.Element)
      */
     @Override
-    public void initialize(Element configRootElement) throws RepositoryException {
+    public void initialize(Element configRootElement, Element factoryImplDefinition) throws RepositoryException {
+        homeDir = UtilXml.childElementAttribute(configRootElement, "home-dir", "path", "runtime/data/jcr/");
+        String factoryJndiName = factoryImplDefinition.getAttribute("jndi-name");
+
+        jndi = new JCRJndi(jackrabbitConfigFile, factoryJndiName, homeDir);
+
         Element childElement = UtilXml.firstChildElement(configRootElement, "jcr-credentials");
         CREDENTIALS_USERNAME = UtilXml.elementAttribute(childElement, "username", null);
         CREDENTIALS_PASSWORD = UtilXml.elementAttribute(childElement, "password", null).toCharArray();
 
         jackrabbitConfigFile = UtilXml.childElementAttribute(configRootElement, "config-file-path", "path", "framework/jcr/config/jackrabbit.xml");
-        homeDir = UtilXml.childElementAttribute(configRootElement, "home-dir", "path", "runtime/data/jcr/");
     }
 
     /*
@@ -113,6 +122,8 @@ public class JCRFactoryImpl implements J
         classes.add(JackrabbitArticle.class);
 
         mapper = new AnnotationMapperImpl(classes);
+
+        jndi.registerJcrToJndi();
     }
 
     /*
@@ -136,6 +147,8 @@ public class JCRFactoryImpl implements J
                 }
             }
         }
+
+        jndi.unbindRepository();
     }
 
     /*
@@ -186,7 +199,7 @@ public class JCRFactoryImpl implements J
     /*
      * Register some new node types
      */
-    protected void registerNodeTypes(Session session) throws InvalidNodeTypeDefException, javax.jcr.RepositoryException, IOException {
+    private void registerNodeTypes(Session session) throws InvalidNodeTypeDefException, javax.jcr.RepositoryException, IOException {
         InputStream xml = new FileInputStream(CUSTOM_NODE_TYPES);
 
         // HINT: throws InvalidNodeTypeDefException, IOException