Skip to content

Commit 4dae995

Browse files
authored
Improve Inherited Context Lookup in getConfigResources (#11048)
* improve the getConfigResources * revert format changes * revert format changes * revert format changes * return immutable set when def is null * add final for clarity * address comment
1 parent b535ae8 commit 4dae995

1 file changed

Lines changed: 27 additions & 11 deletions

File tree

‎framework/spring/module/src/main/java/org/apache/cloudstack/spring/module/model/impl/DefaultModuleDefinitionSet.java‎

Lines changed: 27 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
import java.io.InputStream;
2323
import java.net.URL;
2424
import java.util.ArrayList;
25+
import java.util.Collections;
2526
import java.util.EmptyStackException;
2627
import java.util.HashMap;
2728
import java.util.HashSet;
@@ -61,6 +62,9 @@ public class DefaultModuleDefinitionSet implements ModuleDefinitionSet {
6162
String root;
6263
Map<String, ModuleDefinition> modules;
6364
Map<String, ApplicationContext> contexts = new HashMap<String, ApplicationContext>();
65+
66+
Map<String, Set<Resource>> inheritedConfigResourcesMap = new HashMap<String, Set<Resource>>();
67+
6468
ApplicationContext rootContext = null;
6569
Set<String> excludes = new HashSet<String>();
6670
Properties configProperties = null;
@@ -310,24 +314,36 @@ public Map<String, ApplicationContext> getContextMap() {
310314

311315
@Override
312316
public Resource[] getConfigResources(String name) {
313-
Set<Resource> resources = new LinkedHashSet<Resource>();
314-
315-
ModuleDefinition original = null;
316-
ModuleDefinition def = original = modules.get(name);
317-
318-
if (def == null)
317+
ModuleDefinition def = modules.get(name);
318+
if (def == null) {
319319
return new Resource[] {};
320+
}
321+
322+
Set<Resource> resources = new LinkedHashSet<>();
320323

321324
resources.addAll(def.getContextLocations());
322325

323-
while (def != null) {
324-
resources.addAll(def.getInheritableContextLocations());
325-
def = modules.get(def.getParentName());
326+
resources.addAll(collectInheritedResources(def));
327+
328+
resources.addAll(def.getOverrideContextLocations());
329+
330+
return resources.toArray(Resource[]::new);
331+
}
332+
333+
private Set<Resource> collectInheritedResources(final ModuleDefinition def) {
334+
if (def == null) {
335+
return Collections.emptySet();
326336
}
327337

328-
resources.addAll(original.getOverrideContextLocations());
338+
final Set<Resource> cachedResources = inheritedConfigResourcesMap.get(def.getName());
339+
if (cachedResources != null) {
340+
return cachedResources;
341+
}
329342

330-
return resources.toArray(new Resource[resources.size()]);
343+
final Set<Resource> inheritableResources = new LinkedHashSet<>(def.getInheritableContextLocations());
344+
inheritableResources.addAll(collectInheritedResources(modules.get(def.getParentName())));
345+
inheritedConfigResourcesMap.put(def.getName(), inheritableResources);
346+
return inheritableResources;
331347
}
332348

333349
@Override

0 commit comments

Comments
 (0)