Author: adrianc Date: Thu Apr 30 13:46:46 2015 New Revision: 1676985 URL: http://svn.apache.org/r1676985 Log: Some work on ComponentContainer.java:
1. Improved thread safety 2. Optimization 3. Code cleanup 4. Use String constant instead of hard-coded value Modified: ofbiz/trunk/framework/base/src/org/ofbiz/base/container/ComponentContainer.java Modified: ofbiz/trunk/framework/base/src/org/ofbiz/base/container/ComponentContainer.java URL: http://svn.apache.org/viewvc/ofbiz/trunk/framework/base/src/org/ofbiz/base/container/ComponentContainer.java?rev=1676985&r1=1676984&r2=1676985&view=diff ============================================================================== --- ofbiz/trunk/framework/base/src/org/ofbiz/base/container/ComponentContainer.java (original) +++ ofbiz/trunk/framework/base/src/org/ofbiz/base/container/ComponentContainer.java Thu Apr 30 13:46:46 2015 @@ -24,8 +24,8 @@ import java.net.MalformedURLException; import java.net.URL; import java.util.Arrays; import java.util.List; +import java.util.concurrent.atomic.AtomicBoolean; -import org.ofbiz.base.component.AlreadyLoadedException; import org.ofbiz.base.component.ComponentConfig; import org.ofbiz.base.component.ComponentException; import org.ofbiz.base.component.ComponentLoaderConfig; @@ -49,10 +49,13 @@ public class ComponentContainer implemen protected String configFileLocation = null; private String name; - private boolean loaded = false; + private final AtomicBoolean loaded = new AtomicBoolean(false); @Override public void init(String[] args, String name, String configFile) throws ContainerException { + if (!loaded.compareAndSet(false, true)) { + throw new ContainerException("Components already loaded, cannot start"); + } this.name = name; this.configFileLocation = configFile; @@ -68,8 +71,6 @@ public class ComponentContainer implemen // load the components try { loadComponents(loaderConfig); - } catch (AlreadyLoadedException e) { - throw new ContainerException(e); } catch (ComponentException e) { throw new ContainerException(e); } @@ -79,20 +80,12 @@ public class ComponentContainer implemen * @see org.ofbiz.base.container.Container#start() */ public boolean start() throws ContainerException { - return true; + return loaded.get(); } - public synchronized void loadComponents(String loaderConfig) throws AlreadyLoadedException, ComponentException { - // set the loaded list; and fail if already loaded - if (!loaded) { - loaded = true; - } else { - throw new AlreadyLoadedException("Components already loaded, cannot start"); - } - + public void loadComponents(String loaderConfig) throws ComponentException { // get the components to load List<ComponentLoaderConfig.ComponentDef> components = ComponentLoaderConfig.getRootComponents(loaderConfig); - String parentPath; try { parentPath = FileUtil.getFile(System.getProperty("ofbiz.home")).getCanonicalFile().toString().replaceAll("\\\\", "/"); @@ -164,11 +157,11 @@ public class ComponentContainer implemen Arrays.sort(fileNames); for (String sub: fileNames) { try { - File componentPath = FileUtil.getFile(parentPath.getCanonicalPath() + "/" + sub); + File componentPath = FileUtil.getFile(parentPath.getCanonicalPath() + File.separator + sub); if (componentPath.isDirectory() && !sub.equals("CVS") && !sub.equals(".svn")) { // make sure we have a component configuration file String componentLocation = componentPath.getCanonicalPath(); - File configFile = FileUtil.getFile(componentLocation + "/ofbiz-component.xml"); + File configFile = FileUtil.getFile(componentLocation.concat(File.separator).concat(ComponentConfig.OFBIZ_COMPONENT_XML_FILENAME)); if (configFile.exists()) { ComponentConfig config = null; try {