From ef710ed02fa40593df693bf9591eddd26cef75c2 Mon Sep 17 00:00:00 2001 From: Adrian Crum Date: Thu, 30 Apr 2015 12:12:17 +0000 Subject: [PATCH] Some work on ComponentLoaderConfig.java: 1. Thread safety 2. Optimization 3. Code cleanup 4. Improved error handling git-svn-id: https://svn.apache.org/repos/asf/ofbiz/trunk@1676970 13f79535-47bb-0310-9956-ffa450edef68 --- .../base/component/ComponentLoaderConfig.java | 79 ++++++++++--------- 1 file changed, 42 insertions(+), 37 deletions(-) diff --git a/framework/base/src/org/ofbiz/base/component/ComponentLoaderConfig.java b/framework/base/src/org/ofbiz/base/component/ComponentLoaderConfig.java index 2c074bc45f..6253720100 100644 --- a/framework/base/src/org/ofbiz/base/component/ComponentLoaderConfig.java +++ b/framework/base/src/org/ofbiz/base/component/ComponentLoaderConfig.java @@ -20,15 +20,15 @@ package org.ofbiz.base.component; import java.io.IOException; import java.net.URL; -import java.util.LinkedList; +import java.util.ArrayList; import java.util.List; -import java.util.Properties; +import java.util.Map; +import java.util.concurrent.atomic.AtomicReference; import javax.xml.parsers.ParserConfigurationException; import org.ofbiz.base.util.UtilGenerics; import org.ofbiz.base.util.UtilURL; -import org.ofbiz.base.util.UtilValidate; import org.ofbiz.base.util.UtilXml; import org.ofbiz.base.util.string.FlexibleStringExpander; import org.w3c.dom.Document; @@ -43,33 +43,31 @@ public class ComponentLoaderConfig { public static final String module = ComponentLoaderConfig.class.getName(); public static final String COMPONENT_LOAD_XML_FILENAME = "component-load.xml"; - public static final int SINGLE_COMPONENT = 0; public static final int COMPONENT_DIRECTORY = 1; - - protected static List componentsToLoad = null; + private static final AtomicReference> componentDefsRef = new AtomicReference>(null); public static List getRootComponents(String configFile) throws ComponentException { - if (componentsToLoad == null) { - synchronized (ComponentLoaderConfig.class) { - if (componentsToLoad == null) { - if (configFile == null) { - configFile = COMPONENT_LOAD_XML_FILENAME; - } - URL xmlUrl = UtilURL.fromResource(configFile); - ComponentLoaderConfig.componentsToLoad = ComponentLoaderConfig.getComponentsFromConfig(xmlUrl); - } + List existingInstance = componentDefsRef.get(); + if (existingInstance == null) { + if (configFile == null) { + configFile = COMPONENT_LOAD_XML_FILENAME; + } + URL xmlUrl = UtilURL.fromResource(configFile); + List newInstance = getComponentsFromConfig(xmlUrl); + if (componentDefsRef.compareAndSet(existingInstance, newInstance)) { + existingInstance = newInstance; + } else { + existingInstance = componentDefsRef.get(); } } - return componentsToLoad; + return existingInstance; } public static List getComponentsFromConfig(URL configUrl) throws ComponentException { if (configUrl == null) { - throw new ComponentException("Component config file does not exist: " + configUrl); + throw new IllegalArgumentException("configUrl cannot be null"); } - - List componentsFromConfig = null; Document document = null; try { document = UtilXml.readXmlDocument(configUrl, true); @@ -80,13 +78,27 @@ public class ComponentLoaderConfig { } catch (IOException e) { throw new ComponentException("Error reading the component config file: " + configUrl, e); } - Element root = document.getDocumentElement(); List toLoad = UtilXml.childElementList(root); - if (UtilValidate.isNotEmpty(toLoad)) { - componentsFromConfig = new LinkedList(); - for (Element element: toLoad) { - componentsFromConfig.add(new ComponentDef(element)); + List componentsFromConfig = null; + if (!toLoad.isEmpty()) { + componentsFromConfig = new ArrayList(toLoad.size()); + Map systemProps = UtilGenerics. checkMap(System.getProperties()); + for (Element element : toLoad) { + String nodeName = element.getNodeName(); + String name = null; + String location = null; + int type = SINGLE_COMPONENT; + if ("load-component".equals(nodeName)) { + name = element.getAttribute("component-name"); + location = FlexibleStringExpander.expandString(element.getAttribute("component-location"), systemProps); + } else if ("load-components".equals(nodeName)) { + location = FlexibleStringExpander.expandString(element.getAttribute("parent-directory"), systemProps); + type = COMPONENT_DIRECTORY; + } else { + throw new ComponentException("Invalid element '" + nodeName + "' found in component config file " + configUrl); + } + componentsFromConfig.add(new ComponentDef(name, location, type)); } } return componentsFromConfig; @@ -94,20 +106,13 @@ public class ComponentLoaderConfig { public static class ComponentDef { public String name; - public String location; - public int type = -1; + public final String location; + public final int type; - public ComponentDef(Element element) { - Properties systemProps = System.getProperties(); - if ("load-component".equals(element.getNodeName())) { - name = element.getAttribute("component-name"); - location = FlexibleStringExpander.expandString(element.getAttribute("component-location"), UtilGenerics.checkMap(systemProps)); - type = SINGLE_COMPONENT; - } else if ("load-components".equals(element.getNodeName())) { - name = null; - location = FlexibleStringExpander.expandString(element.getAttribute("parent-directory"), UtilGenerics.checkMap(systemProps)); - type = COMPONENT_DIRECTORY; - } + private ComponentDef(String name, String location, int type) { + this.name = name; + this.location = location; + this.type = type; } } }