Skip to content
Permalink
Browse files

Improved: Rewrite ‘ComponentContainer#loadComponentsInDirectory’

(OFBIZ-11264)

Use a more declarative style with the stream API.


git-svn-id: https://svn.apache.org/repos/asf/ofbiz/ofbiz-framework/trunk@1869183 13f79535-47bb-0310-9956-ffa450edef68
  • Loading branch information
mthl committed Oct 30, 2019
1 parent aeebf80 commit 5a8e2dcf3c5ba1c70cb3dd420ee457e902e2ad01
Showing with 13 additions and 19 deletions.
  1. +13 −19 framework/base/src/main/java/org/apache/ofbiz/base/container/ComponentContainer.java
@@ -26,15 +26,16 @@
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashSet;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.stream.Collectors;
import java.util.stream.Stream;

import org.apache.ofbiz.base.component.ComponentConfig;
import org.apache.ofbiz.base.component.ComponentConfig.DependsOnInfo;
@@ -193,29 +194,22 @@ private void loadComponentsInDirectoryUsingLoadFile(Path directoryPath, Path com
* a load-components.xml file. The components are sorted alphabetically
* for loading purposes
*
* @param directoryPath the absolute path of the directory
* @param directoryPath a valid absolute path of a component directory
* @throws IOException
* @throws ComponentException
*/
private void loadComponentsInDirectory(Path directoryPath) throws IOException, ComponentException {
List<Path> sortedComponentNames = Files.list(directoryPath).collect(Collectors.toList());
List<ComponentConfig> componentConfigs = new ArrayList<>();
if (sortedComponentNames == null) {
throw new IllegalArgumentException("sortedComponentNames is null, directory path is invalid " + directoryPath);
}
Collections.sort(sortedComponentNames);
try (Stream<Path> paths = Files.list(directoryPath)) {
List<ComponentConfig> componentConfigs = paths.sorted()
.map(cmpnt -> directoryPath.resolve(cmpnt).toAbsolutePath().normalize())
.filter(Files::isDirectory)
.filter(dir -> Files.exists(dir.resolve(ComponentConfig.OFBIZ_COMPONENT_XML_FILENAME)))
.map(componentDir -> retrieveComponentConfig(null, componentDir))
.filter(Objects::nonNull)
.collect(Collectors.toList());

for (Path componentName: sortedComponentNames) {
Path componentDir = directoryPath.resolve(componentName).toAbsolutePath().normalize();
Path configFile = componentDir.resolve(ComponentConfig.OFBIZ_COMPONENT_XML_FILENAME);
if (Files.isDirectory(componentDir) && Files.exists(configFile)) {
ComponentConfig config = retrieveComponentConfig(null, componentDir);
componentConfigs.add(config);
}
}
for (ComponentConfig componentConfig : componentConfigs) {
if (componentConfig != null) {
loadSingleComponent(componentConfig);
for (ComponentConfig cmpnt : componentConfigs) {
loadSingleComponent(cmpnt);
}
}
loadComponentWithDependency();

0 comments on commit 5a8e2dc

Please sign in to comment.
You can’t perform that action at this time.