Skip to content
Open
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
improve the getConfigResources
  • Loading branch information
he1l0world committed Jun 17, 2025
commit bbb10eb76c4f74583815a7d818c4b6f0f941ddbf
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,20 @@
*/
package org.apache.cloudstack.spring.module.model.impl;

import org.apache.cloudstack.spring.module.context.ResourceApplicationContext;
import org.apache.cloudstack.spring.module.model.ModuleDefinition;
import org.apache.cloudstack.spring.module.model.ModuleDefinitionSet;
import org.apache.commons.io.IOUtils;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.Resource;
import org.springframework.core.io.UrlResource;
import org.springframework.util.StringUtils;

import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
Expand All @@ -32,21 +46,6 @@
import java.util.Set;
import java.util.Stack;

import org.apache.commons.io.IOUtils;
import org.apache.logging.log4j.Logger;
import org.apache.logging.log4j.LogManager;
import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.Resource;
import org.springframework.core.io.UrlResource;
import org.springframework.util.StringUtils;

import org.apache.cloudstack.spring.module.context.ResourceApplicationContext;
import org.apache.cloudstack.spring.module.model.ModuleDefinition;
import org.apache.cloudstack.spring.module.model.ModuleDefinitionSet;

public class DefaultModuleDefinitionSet implements ModuleDefinitionSet {

protected Logger logger = LogManager.getLogger(getClass());
Expand All @@ -61,6 +60,9 @@ public class DefaultModuleDefinitionSet implements ModuleDefinitionSet {
String root;
Map<String, ModuleDefinition> modules;
Map<String, ApplicationContext> contexts = new HashMap<String, ApplicationContext>();

Map<String, Set<Resource>> configResourcesMap = new HashMap<String, Set<Resource>>();

ApplicationContext rootContext = null;
Set<String> excludes = new HashSet<String>();
Properties configProperties = null;
Expand All @@ -72,8 +74,9 @@ public DefaultModuleDefinitionSet(Map<String, ModuleDefinition> modules, String
}

public void load() throws IOException {
if (!loadRootContext())
if (!loadRootContext()) {
return;
}

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

if (def == null)
if (def == null) {
return false;
}

ApplicationContext defaultsContext = getDefaultsContext();

Expand Down Expand Up @@ -190,8 +194,7 @@ protected ApplicationContext getDefaultsContext() {
context.setApplicationName("/defaults");
context.refresh();

@SuppressWarnings("unchecked")
final List<Resource> resources = (List<Resource>)context.getBean(DEFAULT_CONFIG_RESOURCES);
@SuppressWarnings("unchecked") final List<Resource> resources = (List<Resource>) context.getBean(DEFAULT_CONFIG_RESOURCES);

withModule(new WithModule() {
@Override
Expand All @@ -202,12 +205,12 @@ public void with(ModuleDefinition def, Stack<ModuleDefinition> parents) {
}
});

configProperties = (Properties)context.getBean(DEFAULT_CONFIG_PROPERTIES);
configProperties = (Properties) context.getBean(DEFAULT_CONFIG_PROPERTIES);
for (Resource resource : resources) {
load(resource, configProperties);
}

for (Resource resource : (Resource[])context.getBean(MODULE_PROPERITES)) {
for (Resource resource : (Resource[]) context.getBean(MODULE_PROPERITES)) {
load(resource, configProperties);
}

Expand Down Expand Up @@ -263,8 +266,9 @@ protected void withModule(WithModule with) {
}

protected void withModule(ModuleDefinition def, Stack<ModuleDefinition> parents, WithModule with) {
if (def == null)
if (def == null) {
return;
}

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

@Override
public Resource[] getConfigResources(String name) {
Set<Resource> resources = new LinkedHashSet<Resource>();

ModuleDefinition original = null;
ModuleDefinition def = original = modules.get(name);

if (def == null)
ModuleDefinition def = modules.get(name);
if (def == null) {
return new Resource[] {};
}

Set<Resource> resources = new LinkedHashSet<>();

resources.addAll(def.getContextLocations());

while (def != null) {
resources.addAll(def.getInheritableContextLocations());
def = modules.get(def.getParentName());
resources.addAll(collectInheritedResources(def));

resources.addAll(def.getOverrideContextLocations());

return resources.toArray(Resource[]::new);
}

private Set<Resource> collectInheritedResources(ModuleDefinition def) {
if (def == null) {
return new LinkedHashSet<>();
}

resources.addAll(original.getOverrideContextLocations());
if (configResourcesMap.containsKey(def.getName())) {
return configResourcesMap.get(def.getName());
Comment on lines +342 to +343
}

return resources.toArray(new Resource[resources.size()]);
Set<Resource> inheritableResources = new LinkedHashSet<>(def.getInheritableContextLocations());
inheritableResources.addAll(collectInheritedResources(modules.get(def.getParentName())));
configResourcesMap.put(def.getName(), inheritableResources);
return inheritableResources;
}

@Override
Expand Down