Skip to content

Commit 1317703

Browse files
Samuel Trégouëtmthl
authored andcommitted
Implemented: Show dependency resolution algorithm problem
(OFBIZ-11275) This adds a test demonstrating a bug in the dependency resolution algorithm done in ‘ComponentContainer’. When relying on <depends-on> declaration in “ofbiz-component.xml” files we should expect to retrieve components (and their associated containers) in a topological ordering meaning a linear ordering where each component configuration element is placed after its dependencies configuration elements. Currently this is not the case and the dependency declarations only impact the construction of the dynamic classpath.
1 parent 8aea160 commit 1317703

File tree

4 files changed

+163
-1
lines changed

4 files changed

+163
-1
lines changed

framework/base/src/main/java/org/apache/ofbiz/base/container/ComponentContainer.java

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,17 @@ public class ComponentContainer implements Container {
6868

6969
@Override
7070
public void init(List<StartupCommand> ofbizCommands, String name, String configFile) throws ContainerException {
71+
init(name, Start.getInstance().getConfig().ofbizHome);
72+
}
73+
74+
/**
75+
* Loads components found in a directory.
76+
*
77+
* @param name the name of this container
78+
* @param ofbizHome the directory where to search for components
79+
* @throws ContainerException when components are already loaded or when failing to load them.
80+
*/
81+
void init(String name, Path ofbizHome) throws ContainerException {
7182
if (!loaded.compareAndSet(false, true)) {
7283
throw new ContainerException("Components already loaded, cannot start");
7384
}
@@ -76,7 +87,7 @@ public void init(List<StartupCommand> ofbizCommands, String name, String configF
7687
// load the components from framework/base/config/component-load.xml (root components)
7788
try {
7889
for (ComponentDef def: ComponentLoaderConfig.getRootComponents()) {
79-
loadComponent(Start.getInstance().getConfig().ofbizHome, def);
90+
loadComponent(ofbizHome, def);
8091
}
8192
} catch (IOException | ComponentException e) {
8293
throw new ContainerException(e);
Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one
3+
* or more contributor license agreements. See the NOTICE file
4+
* distributed with this work for additional information
5+
* regarding copyright ownership. The ASF licenses this file
6+
* to you under the Apache License, Version 2.0 (the
7+
* "License"); you may not use this file except in compliance
8+
* with the License. You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing,
13+
* software distributed under the License is distributed on an
14+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15+
* KIND, either express or implied. See the License for the
16+
* specific language governing permissions and limitations
17+
* under the License.
18+
*/
19+
package org.apache.ofbiz.base.container;
20+
21+
import static org.junit.Assert.assertEquals;
22+
23+
import java.io.IOException;
24+
import java.net.MalformedURLException;
25+
import java.net.URL;
26+
import java.net.URLClassLoader;
27+
import java.nio.file.Files;
28+
import java.nio.file.Path;
29+
import java.nio.file.Paths;
30+
import java.util.Arrays;
31+
import java.util.List;
32+
import java.util.stream.Collectors;
33+
34+
import org.apache.ofbiz.base.component.ComponentConfig;
35+
import org.apache.ofbiz.base.start.StartupException;
36+
import org.junit.After;
37+
import org.junit.Before;
38+
import org.junit.Ignore;
39+
import org.junit.Test;
40+
41+
42+
public class ComponentContainerTest {
43+
private static final Path ORDER_CONFIG = Paths.get("applications", "order", "config");
44+
private static final Path ACCOUNTING_CONFIG = Paths.get("applications", "accounting", "config");
45+
private static final Path[] CONFIGS = {ORDER_CONFIG, ACCOUNTING_CONFIG};
46+
47+
private Path ofbizHome = Paths.get("testsdata", "ComponentContainerTest").toAbsolutePath().normalize();
48+
49+
@Before
50+
public void setUp() throws IOException {
51+
cleanUp();
52+
for (Path cfg : CONFIGS) {
53+
Files.createDirectory(ofbizHome.resolve(cfg));
54+
}
55+
}
56+
57+
@After
58+
public void cleanUp() throws IOException {
59+
for (Path cfg : CONFIGS) {
60+
Files.deleteIfExists(ofbizHome.resolve(cfg));
61+
}
62+
}
63+
64+
@Ignore("Dependency declarations do not impact the components order")
65+
@Test
66+
public void testCheckDependencyForComponent() throws ContainerException, StartupException, MalformedURLException {
67+
ComponentContainer containerObj = new ComponentContainer();
68+
containerObj.init("component-container", ofbizHome);
69+
70+
List<String> loadedComponents = ComponentConfig.components()
71+
.map(c -> c.getGlobalName())
72+
.collect(Collectors.toList());
73+
// we can cast ContextClassLoader since ComponentContainer.loadClassPathForAllComponents has called
74+
// setContextClassLoader with an URLClassLoader instance
75+
URL[] classpath = ((URLClassLoader) Thread.currentThread().getContextClassLoader()).getURLs();
76+
List<URL> actualClasspath = Arrays.asList(classpath);
77+
List<URL> expectedClasspath = Arrays.asList(
78+
ofbizHome.resolve(ORDER_CONFIG).toUri().toURL(),
79+
ofbizHome.resolve(ACCOUNTING_CONFIG).toUri().toURL());
80+
81+
assertEquals(expectedClasspath, actualClasspath);
82+
assertEquals(Arrays.asList("order", "accounting"), loadedComponents);
83+
}
84+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<!--
3+
Licensed to the Apache Software Foundation (ASF) under one
4+
or more contributor license agreements. See the NOTICE file
5+
distributed with this work for additional information
6+
regarding copyright ownership. The ASF licenses this file
7+
to you under the Apache License, Version 2.0 (the
8+
"License"); you may not use this file except in compliance
9+
with the License. You may obtain a copy of the License at
10+
11+
http://www.apache.org/licenses/LICENSE-2.0
12+
13+
Unless required by applicable law or agreed to in writing,
14+
software distributed under the License is distributed on an
15+
"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16+
KIND, either express or implied. See the License for the
17+
specific language governing permissions and limitations
18+
under the License.
19+
-->
20+
21+
<ofbiz-component name="accounting"
22+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
23+
xsi:noNamespaceSchemaLocation="http://ofbiz.apache.org/dtds/ofbiz-component.xsd">
24+
<depends-on component-name="order" />
25+
<resource-loader name="main" type="component"/>
26+
<classpath type="dir" location="config"/>
27+
<webapp name="accounting"
28+
title="Accounting"
29+
server="default-server"
30+
location="webapp/accounting"
31+
base-permission="OFBTOOLS,ACCOUNTING"
32+
mount-point="/accounting"/>
33+
</ofbiz-component>
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<!--
3+
Licensed to the Apache Software Foundation (ASF) under one
4+
or more contributor license agreements. See the NOTICE file
5+
distributed with this work for additional information
6+
regarding copyright ownership. The ASF licenses this file
7+
to you under the Apache License, Version 2.0 (the
8+
"License"); you may not use this file except in compliance
9+
with the License. You may obtain a copy of the License at
10+
11+
http://www.apache.org/licenses/LICENSE-2.0
12+
13+
Unless required by applicable law or agreed to in writing,
14+
software distributed under the License is distributed on an
15+
"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16+
KIND, either express or implied. See the License for the
17+
specific language governing permissions and limitations
18+
under the License.
19+
-->
20+
21+
<ofbiz-component name="order"
22+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
23+
xsi:noNamespaceSchemaLocation="http://ofbiz.apache.org/dtds/ofbiz-component.xsd">
24+
<resource-loader name="main" type="component"/>
25+
<classpath type="dir" location="config"/>
26+
<webapp name="order"
27+
title="Order"
28+
description="OrderComponentDescription"
29+
server="default-server"
30+
location="webapp/ordermgr"
31+
base-permission="OFBTOOLS,ORDERMGR"
32+
mount-point="/ordermgr"/>
33+
</ofbiz-component>
34+

0 commit comments

Comments
 (0)