Skip to content

Commit

Permalink
Improved: Delay the construction of component classpaths
Browse files Browse the repository at this point in the history
(OFBIZ-11264)

The list of component classpath elements was constructed iteratively
while loading components. This list was additionally used to check if
a component was actually loaded.  This implementation was leading to
hard to understand code that has been replaced by a more appropriate
‘LinkedHashSet’ object.


git-svn-id: https://svn.apache.org/repos/asf/ofbiz/ofbiz-framework/trunk@1869182 13f79535-47bb-0310-9956-ffa450edef68
  • Loading branch information
mthl committed Oct 30, 2019
1 parent 7093974 commit aeebf80
Showing 1 changed file with 13 additions and 12 deletions.
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -26,9 +26,9 @@
import java.nio.file.Files; import java.nio.file.Files;
import java.nio.file.Path; import java.nio.file.Path;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections; import java.util.Collections;
import java.util.HashSet; import java.util.HashSet;
import java.util.LinkedHashSet;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.Set; import java.util.Set;
Expand Down Expand Up @@ -62,7 +62,8 @@ public class ComponentContainer implements Container {


private String name; private String name;
private final AtomicBoolean loaded = new AtomicBoolean(false); private final AtomicBoolean loaded = new AtomicBoolean(false);
private final List<Classpath> componentsClassPath = new ArrayList<>(); /** The set of ready components in their inverse dependency order. */
private final LinkedHashSet<ComponentConfig> readyComponents = new LinkedHashSet<>();
private static Map<String, List<DependsOnInfo>> toBeLoadedComponents = new ConcurrentHashMap<>(); private static Map<String, List<DependsOnInfo>> toBeLoadedComponents = new ConcurrentHashMap<>();


@Override @Override
Expand All @@ -80,6 +81,11 @@ public void init(List<StartupCommand> ofbizCommands, String name, String configF
} catch (IOException | ComponentException e) { } catch (IOException | ComponentException e) {
throw new ContainerException(e); throw new ContainerException(e);
} }
String fmt = "Added class path for component : [%s]";
List<Classpath> componentsClassPath = readyComponents.stream()
.peek(cmpnt -> Debug.logInfo(String.format(fmt, cmpnt.getComponentName()), module))
.map(cmpnt -> buildClasspathFromComponentConfig(cmpnt))
.collect(Collectors.toList());
loadClassPathForAllComponents(componentsClassPath); loadClassPathForAllComponents(componentsClassPath);
Debug.logInfo("All components loaded", module); Debug.logInfo("All components loaded", module);
} }
Expand Down Expand Up @@ -237,9 +243,8 @@ private void loadComponentWithDependency() throws IOException, ComponentExceptio
Debug.logInfo(msg, module); Debug.logInfo(msg, module);
} }
if (UtilValidate.isEmpty(dependencyList)) { if (UtilValidate.isEmpty(dependencyList)) {
componentsClassPath.add(buildClasspathFromComponentConfig(config)); readyComponents.add(config);
toBeLoadedComponents.replace(config.getComponentName(), dependencyList); toBeLoadedComponents.replace(config.getComponentName(), dependencyList);
Debug.logInfo("Added class path for component : [" + config.getComponentName() + "]", module);
} }
} else { } else {
Debug.logInfo("Not loading component [" + config.getComponentName() + "] because it's disabled", module); Debug.logInfo("Not loading component [" + config.getComponentName() + "] because it's disabled", module);
Expand Down Expand Up @@ -280,8 +285,7 @@ private void loadSingleComponent(ComponentConfig config) throws ComponentExcepti
if (config.enabled()) { if (config.enabled()) {
List<DependsOnInfo> dependencyList = checkDependencyForComponent(config); List<DependsOnInfo> dependencyList = checkDependencyForComponent(config);
if (UtilValidate.isEmpty(dependencyList)) { if (UtilValidate.isEmpty(dependencyList)) {
componentsClassPath.add(buildClasspathFromComponentConfig(config)); readyComponents.add(config);
Debug.logInfo("Added class path for component : [" + config.getComponentName() + "]", module);
} }
} else { } else {
Debug.logInfo("Not loading component [" + config.getComponentName() + "] because it's disabled", module); Debug.logInfo("Not loading component [" + config.getComponentName() + "] because it's disabled", module);
Expand All @@ -302,12 +306,9 @@ private List<DependsOnInfo> checkDependencyForComponent(ComponentConfig config)
for (DependsOnInfo dependency : dependencyList) { for (DependsOnInfo dependency : dependencyList) {
Debug.logInfo("Component : " + config.getComponentName() + " is Dependent on " + dependency.componentName, module); Debug.logInfo("Component : " + config.getComponentName() + " is Dependent on " + dependency.componentName, module);
ComponentConfig componentConfig = ComponentConfig.getComponentConfig(String.valueOf(dependency.componentName)); ComponentConfig componentConfig = ComponentConfig.getComponentConfig(String.valueOf(dependency.componentName));
Classpath dependentComponentClasspath = buildClasspathFromComponentConfig(componentConfig); if (readyComponents.contains(componentConfig)) {
componentsClassPath.forEach(componentClassPath -> { resolvedDependencyList.add(dependency);
if (Arrays.equals(componentClassPath.toString().split(":"), dependentComponentClasspath.toString().split(":"))) { }
resolvedDependencyList.add(dependency);
}
});
} }
resolvedDependencyList.forEach(resolvedDependency -> Debug.logInfo("Resolved : " + resolvedDependency.componentName + " Dependency for Component " + config.getComponentName(), module)); resolvedDependencyList.forEach(resolvedDependency -> Debug.logInfo("Resolved : " + resolvedDependency.componentName + " Dependency for Component " + config.getComponentName(), module));
dependencyList.removeAll(resolvedDependencyList); dependencyList.removeAll(resolvedDependencyList);
Expand Down

0 comments on commit aeebf80

Please sign in to comment.