Skip to content

Commit

Permalink
Merge pull request #27 from microsphere-projects/dev
Browse files Browse the repository at this point in the history
Dev
  • Loading branch information
mercyblitz authored Nov 22, 2024
2 parents cf8ff58 + a903835 commit 13a0318
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,13 @@
*/
package io.microsphere.spring.core.env;

import io.microsphere.constants.PropertyConstants;
import io.microsphere.logging.Logger;
import io.microsphere.logging.LoggerFactory;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.core.convert.support.ConfigurableConversionService;
import org.springframework.core.env.ConfigurableEnvironment;
import org.springframework.core.env.Environment;
import org.springframework.core.env.MissingRequiredPropertiesException;
import org.springframework.core.env.MutablePropertySources;
import org.springframework.core.env.Profiles;
Expand All @@ -32,6 +34,7 @@
import java.util.Map;
import java.util.function.Consumer;

import static io.microsphere.spring.constants.PropertyConstants.MICROSPHERE_SPRING_PROPERTY_NAME_PREFIX;
import static io.microsphere.spring.util.SpringFactoriesLoaderUtils.loadFactories;
import static org.springframework.core.annotation.AnnotationAwareOrderComparator.sort;

Expand All @@ -47,6 +50,16 @@ public class ListenableConfigurableEnvironment implements ConfigurableEnvironmen

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

/**
* The prefix of the property name of {@link ListenableConfigurableEnvironment}
*/
public static final String PROPERTY_NAME_PREFIX = MICROSPHERE_SPRING_PROPERTY_NAME_PREFIX + "listenable-environment.";

/**
* The property name of {@link ListenableConfigurableEnvironment} to be 'enabled'
*/
public static final String ENABLED_PROPERTY_NAME = PROPERTY_NAME_PREFIX + PropertyConstants.ENABLED_PROPERTY_NAME;

private final ConfigurableEnvironment delegate;

private List<EnvironmentListener> environmentListeners;
Expand Down Expand Up @@ -311,6 +324,30 @@ public ConfigurableEnvironment getDelegate() {
return this.delegate;
}

/**
* Set the {@link ListenableConfigurableEnvironment} into {@link ConfigurableApplicationContext} if
* {@link #isEnabled(Environment) enabled}
*
* @param applicationContext {@link ConfigurableApplicationContext}
*/
public static void setEnvironmentIfEnabled(ConfigurableApplicationContext applicationContext) {
ConfigurableEnvironment environment = applicationContext.getEnvironment();
if (!isEnabled(environment) && environment instanceof ListenableConfigurableEnvironment) {
return;
}
applicationContext.setEnvironment(new ListenableConfigurableEnvironment(applicationContext));
}

/**
* Determine whether the {@link ListenableConfigurableEnvironment} is enabled
*
* @param environment {@link Environment the underlying Environment}
* @return <code>true</code> if enabled, <code>false</code> otherwise
*/
public static boolean isEnabled(Environment environment) {
return environment.getProperty(ENABLED_PROPERTY_NAME, boolean.class, false);
}

private void forEachEnvironmentListener(Consumer<EnvironmentListener> listenerConsumer) {
forEachListener(this.environmentListeners, listenerConsumer);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@

import org.springframework.context.ApplicationContextInitializer;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.core.env.ConfigurableEnvironment;

import static io.microsphere.spring.core.env.ListenableConfigurableEnvironment.setEnvironmentIfEnabled;

/**
* The Initializer of {@link ListenableConfigurableEnvironment} based on {@link ApplicationContextInitializer}
Expand All @@ -31,10 +32,6 @@ public class ListenableConfigurableEnvironmentInitializer implements Application

@Override
public void initialize(ConfigurableApplicationContext applicationContext) {
ConfigurableEnvironment environment = applicationContext.getEnvironment();
if (environment instanceof ListenableConfigurableEnvironment) {
return;
}
applicationContext.setEnvironment(new ListenableConfigurableEnvironment(applicationContext));
setEnvironmentIfEnabled(applicationContext);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@
)
@TestPropertySource(
properties = {
"microsphere.spring.listenable-environment.enabled=true",
"spring.profiles.active=test",
"user.name=Mercy",
"score=99"
Expand Down

0 comments on commit 13a0318

Please sign in to comment.