Skip to content

Commit

Permalink
Update OriginTrackedConfigurationPropertyInitializer.java
Browse files Browse the repository at this point in the history
  • Loading branch information
mercyblitz committed Nov 25, 2024
1 parent de9dfa3 commit 8d91976
Showing 1 changed file with 27 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,11 @@
*/
package io.microsphere.spring.boot.env.config;

import io.microsphere.logging.Logger;
import io.microsphere.logging.LoggerFactory;
import io.microsphere.spring.boot.env.PropertySourceLoaders;
import io.microsphere.spring.context.event.BeanFactoryListenerAdapter;
import io.microsphere.spring.context.event.BeanListenerAdapter;
import io.microsphere.spring.util.BeanRegistrar;
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
import org.springframework.beans.factory.support.BeanDefinitionRegistry;
import org.springframework.beans.factory.support.RootBeanDefinition;
Expand All @@ -31,7 +34,9 @@
import org.springframework.core.env.EnumerablePropertySource;
import org.springframework.core.env.MutablePropertySources;
import org.springframework.core.env.PropertySource;
import org.springframework.core.io.support.ResourcePropertySource;

import java.io.IOException;
import java.util.LinkedHashMap;
import java.util.Map;

Expand All @@ -44,40 +49,45 @@
* @see ConfigurableEnvironment
* @since ApplicationContextInitializer
*/
public class OriginTrackedConfigurationPropertyInitializer extends BeanListenerAdapter
implements ApplicationContextInitializer<ConfigurableApplicationContext> {
public class OriginTrackedConfigurationPropertyInitializer implements BeanFactoryListenerAdapter, ApplicationContextInitializer<ConfigurableApplicationContext> {

public static final String BEAN_NAME = "originTrackedConfigurationPropertyInitializer";

private static final Logger logger = LoggerFactory.getLogger(OriginTrackedConfigurationPropertyInitializer.class);

private boolean initialized = false;

private ConfigurableApplicationContext applicationContext;

private PropertySourceLoaders propertySourceLoaders;

@Override
public void initialize(ConfigurableApplicationContext applicationContext) {
this.applicationContext = applicationContext;
this.propertySourceLoaders = new PropertySourceLoaders(applicationContext.getClassLoader());
ConfigurableListableBeanFactory beanFactory = applicationContext.getBeanFactory();
BeanDefinitionRegistry registry = (BeanDefinitionRegistry) beanFactory;
registerBean(registry, BEAN_NAME, this);
}

@Override
public void onBeforeBeanInstantiate(String beanName, RootBeanDefinition mergedBeanDefinition) {
if (initialized) {
return;
}
public void onBeanFactoryConfigurationFrozen(ConfigurableListableBeanFactory beanFactory) {
ConfigurableEnvironment environment = applicationContext.getEnvironment();
MutablePropertySources propertySources = environment.getPropertySources();
initializePropertySources(propertySources);
initialized = true;
}

private void initializePropertySources(MutablePropertySources propertySources) {
for (PropertySource propertySource : propertySources) {
if (isPropertySourceCandidate(propertySource)) {
String name = propertySource.getName();
PropertySource originTrackedPropertySource = createOriginTrackedPropertySource(propertySource);
propertySources.replace(name, originTrackedPropertySource);
try {
PropertySource originTrackedPropertySource = createOriginTrackedPropertySource(propertySource);
propertySources.replace(name, originTrackedPropertySource);
} catch (IOException e) {
logger.error("Failed to create the origin tracked PropertySource[name : '{}', class : '{}']",
name, propertySource.getClass().getName());
}
}
}
}
Expand All @@ -87,14 +97,19 @@ private boolean isPropertySourceCandidate(PropertySource propertySource) {
!(propertySource instanceof OriginLookup);
}

private PropertySource createOriginTrackedPropertySource(PropertySource propertySource) {
private PropertySource createOriginTrackedPropertySource(PropertySource propertySource) throws IOException {
if (propertySource instanceof ResourcePropertySource) {
return propertySourceLoaders.reloadAsOriginTracked(propertySource);
}

EnumerablePropertySource enumerablePropertySource = (EnumerablePropertySource) propertySource;
String[] propertyNames = enumerablePropertySource.getPropertyNames();
int size = propertyNames.length;
Map<String, Object> source = new LinkedHashMap<>(size);
for (int i = 0; i < size; i++) {
String propertyName = propertyNames[i];
Object propertyValue = enumerablePropertySource.getProperty(propertyName);
// Skip if propertyValue is OriginTrackedValue
if (propertyValue instanceof OriginTrackedValue) {
continue;
}
Expand All @@ -107,7 +122,7 @@ private PropertySource createOriginTrackedPropertySource(PropertySource property
}

private Origin resolveOrigin(PropertySource propertySource) {
// TODO
// TODO more Origin implementations
return new NamedOrigin(propertySource.getName());
}

Expand Down

0 comments on commit 8d91976

Please sign in to comment.