Skip to content

Commit bbb10eb

Browse files
committed
improve the getConfigResources
1 parent f8c4121 commit bbb10eb

File tree

1 file changed

+48
-33
lines changed

1 file changed

+48
-33
lines changed

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

Lines changed: 48 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,20 @@
1818
*/
1919
package org.apache.cloudstack.spring.module.model.impl;
2020

21+
import org.apache.cloudstack.spring.module.context.ResourceApplicationContext;
22+
import org.apache.cloudstack.spring.module.model.ModuleDefinition;
23+
import org.apache.cloudstack.spring.module.model.ModuleDefinitionSet;
24+
import org.apache.commons.io.IOUtils;
25+
import org.apache.logging.log4j.LogManager;
26+
import org.apache.logging.log4j.Logger;
27+
import org.springframework.beans.BeansException;
28+
import org.springframework.context.ApplicationContext;
29+
import org.springframework.context.annotation.Bean;
30+
import org.springframework.context.annotation.Configuration;
31+
import org.springframework.core.io.Resource;
32+
import org.springframework.core.io.UrlResource;
33+
import org.springframework.util.StringUtils;
34+
2135
import java.io.IOException;
2236
import java.io.InputStream;
2337
import java.net.URL;
@@ -32,21 +46,6 @@
3246
import java.util.Set;
3347
import java.util.Stack;
3448

35-
import org.apache.commons.io.IOUtils;
36-
import org.apache.logging.log4j.Logger;
37-
import org.apache.logging.log4j.LogManager;
38-
import org.springframework.beans.BeansException;
39-
import org.springframework.context.ApplicationContext;
40-
import org.springframework.context.annotation.Bean;
41-
import org.springframework.context.annotation.Configuration;
42-
import org.springframework.core.io.Resource;
43-
import org.springframework.core.io.UrlResource;
44-
import org.springframework.util.StringUtils;
45-
46-
import org.apache.cloudstack.spring.module.context.ResourceApplicationContext;
47-
import org.apache.cloudstack.spring.module.model.ModuleDefinition;
48-
import org.apache.cloudstack.spring.module.model.ModuleDefinitionSet;
49-
5049
public class DefaultModuleDefinitionSet implements ModuleDefinitionSet {
5150

5251
protected Logger logger = LogManager.getLogger(getClass());
@@ -61,6 +60,9 @@ public class DefaultModuleDefinitionSet implements ModuleDefinitionSet {
6160
String root;
6261
Map<String, ModuleDefinition> modules;
6362
Map<String, ApplicationContext> contexts = new HashMap<String, ApplicationContext>();
63+
64+
Map<String, Set<Resource>> configResourcesMap = new HashMap<String, Set<Resource>>();
65+
6466
ApplicationContext rootContext = null;
6567
Set<String> excludes = new HashSet<String>();
6668
Properties configProperties = null;
@@ -72,8 +74,9 @@ public DefaultModuleDefinitionSet(Map<String, ModuleDefinition> modules, String
7274
}
7375

7476
public void load() throws IOException {
75-
if (!loadRootContext())
77+
if (!loadRootContext()) {
7678
return;
79+
}
7780

7881
printHierarchy();
7982
loadContexts();
@@ -83,8 +86,9 @@ public void load() throws IOException {
8386
protected boolean loadRootContext() {
8487
ModuleDefinition def = modules.get(root);
8588

86-
if (def == null)
89+
if (def == null) {
8790
return false;
91+
}
8892

8993
ApplicationContext defaultsContext = getDefaultsContext();
9094

@@ -190,8 +194,7 @@ protected ApplicationContext getDefaultsContext() {
190194
context.setApplicationName("/defaults");
191195
context.refresh();
192196

193-
@SuppressWarnings("unchecked")
194-
final List<Resource> resources = (List<Resource>)context.getBean(DEFAULT_CONFIG_RESOURCES);
197+
@SuppressWarnings("unchecked") final List<Resource> resources = (List<Resource>) context.getBean(DEFAULT_CONFIG_RESOURCES);
195198

196199
withModule(new WithModule() {
197200
@Override
@@ -202,12 +205,12 @@ public void with(ModuleDefinition def, Stack<ModuleDefinition> parents) {
202205
}
203206
});
204207

205-
configProperties = (Properties)context.getBean(DEFAULT_CONFIG_PROPERTIES);
208+
configProperties = (Properties) context.getBean(DEFAULT_CONFIG_PROPERTIES);
206209
for (Resource resource : resources) {
207210
load(resource, configProperties);
208211
}
209212

210-
for (Resource resource : (Resource[])context.getBean(MODULE_PROPERITES)) {
213+
for (Resource resource : (Resource[]) context.getBean(MODULE_PROPERITES)) {
211214
load(resource, configProperties);
212215
}
213216

@@ -263,8 +266,9 @@ protected void withModule(WithModule with) {
263266
}
264267

265268
protected void withModule(ModuleDefinition def, Stack<ModuleDefinition> parents, WithModule with) {
266-
if (def == null)
269+
if (def == null) {
267270
return;
271+
}
268272

269273
if (!shouldLoad(def)) {
270274
logger.info("Excluding context [" + def.getName() + "] based on configuration");
@@ -314,24 +318,35 @@ public Map<String, ApplicationContext> getContextMap() {
314318

315319
@Override
316320
public Resource[] getConfigResources(String name) {
317-
Set<Resource> resources = new LinkedHashSet<Resource>();
318-
319-
ModuleDefinition original = null;
320-
ModuleDefinition def = original = modules.get(name);
321-
322-
if (def == null)
321+
ModuleDefinition def = modules.get(name);
322+
if (def == null) {
323323
return new Resource[] {};
324+
}
325+
326+
Set<Resource> resources = new LinkedHashSet<>();
324327

325328
resources.addAll(def.getContextLocations());
326329

327-
while (def != null) {
328-
resources.addAll(def.getInheritableContextLocations());
329-
def = modules.get(def.getParentName());
330+
resources.addAll(collectInheritedResources(def));
331+
332+
resources.addAll(def.getOverrideContextLocations());
333+
334+
return resources.toArray(Resource[]::new);
335+
}
336+
337+
private Set<Resource> collectInheritedResources(ModuleDefinition def) {
338+
if (def == null) {
339+
return new LinkedHashSet<>();
330340
}
331341

332-
resources.addAll(original.getOverrideContextLocations());
342+
if (configResourcesMap.containsKey(def.getName())) {
343+
return configResourcesMap.get(def.getName());
344+
}
333345

334-
return resources.toArray(new Resource[resources.size()]);
346+
Set<Resource> inheritableResources = new LinkedHashSet<>(def.getInheritableContextLocations());
347+
inheritableResources.addAll(collectInheritedResources(modules.get(def.getParentName())));
348+
configResourcesMap.put(def.getName(), inheritableResources);
349+
return inheritableResources;
335350
}
336351

337352
@Override

0 commit comments

Comments
 (0)