org.zodiac.autoconfigure.application.ApplicationAutoConfiguration Maven / Gradle / Ivy
package org.zodiac.autoconfigure.application;
import java.util.List;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.stream.Collectors;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.ObjectProvider;
import org.springframework.boot.SpringBootConfiguration;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.web.ServerProperties;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.core.env.ConfigurableEnvironment;
import org.zodiac.autoconfigure.application.main.ApplicationMainProperties;
import org.zodiac.commons.constants.Constants;
import org.zodiac.commons.constants.SystemPropertiesConstants;
import org.zodiac.core.application.AppContext;
import org.zodiac.core.application.AppContextLoader;
import org.zodiac.core.application.AppContextLoaderFactory;
import org.zodiac.core.application.AppContextPrinterListener;
import org.zodiac.core.application.NotFoundAppContextLoaderException;
import org.zodiac.core.application.availability.AppAvailability;
import org.zodiac.core.application.availability.AppAvailabilityListener;
import org.zodiac.core.bootstrap.discovery.GenericAppDiscoveryClientBeanPostProcessor;
import org.zodiac.core.constants.AppConstants;
import org.zodiac.core.context.ApplicationEventConsumer;
import org.zodiac.core.support.SpringContextHolder;
@SpringBootConfiguration
public class ApplicationAutoConfiguration {
protected Logger log = LoggerFactory.getLogger(getClass());
private AtomicBoolean appContextInitialized = new AtomicBoolean(false);
private ConfigurableApplicationContext applicationContext;
private ServerProperties serverProperties;
public ApplicationAutoConfiguration(ObjectProvider serverProperties,
ConfigurableApplicationContext applicationContext) {
this.serverProperties = serverProperties.getIfAvailable();
this.applicationContext = applicationContext;
SpringContextHolder.setApplicationContext(this.applicationContext);
}
@Bean
@ConditionalOnMissingBean
@ConfigurationProperties(prefix = SystemPropertiesConstants.Zodiac.SPRING_APP_PREFIX)
protected ApplicationInfoProperties applicationInfoProperties(ConfigurableEnvironment environment) {
ApplicationInfoProperties applicationInfoProperties = ApplicationInfoProperties.ME;
if (null == applicationInfoProperties.getPort()) {
applicationInfoProperties.setPort(null != serverProperties && null != serverProperties.getPort()
? serverProperties.getPort() : Constants.Spring.DEFAULT_SERVER_PORT_NUMBER);
}
if (null == applicationInfoProperties.getEnvType()) {
applicationInfoProperties.setEnvType(
environment.getProperty(SystemPropertiesConstants.Spring.SPRING_PROFILES_ACTIVE, AppConstants.DEFAULT));
}
return applicationInfoProperties;
}
@Bean
@ConditionalOnMissingBean
@ConfigurationProperties(prefix = SystemPropertiesConstants.Zodiac.SPRING_MAIN_PREFIX)
protected ApplicationMainProperties applicationMainProperties() {
return ApplicationMainProperties.ME;
}
@Bean
@ConditionalOnMissingBean
protected AppContext appContext(ApplicationInfoProperties applicationInfoProperties,
ObjectProvider> appContextLoaders) {
AppContext appContext = null;
if (!appContextInitialized.get()) {
synchronized (this) {
try {
appContext = AppContextLoaderFactory.load(applicationContext, appContextLoaders.getIfAvailable())
.getAppContext();
appContextInitialized.set(true);
applicationContext.getBeanFactory()
.addBeanPostProcessor(new GenericAppDiscoveryClientBeanPostProcessor(appContext));
} catch (NotFoundAppContextLoaderException e) {
log.error("{}", e);
}
}
}
return appContext;
}
@Bean
@ConditionalOnMissingBean
protected AppContextPrinterListener appContextInstancePrinter(AppContext appContext) {
return new AppContextPrinterListener(appContext);
}
@Bean
@ConditionalOnMissingBean
protected AppAvailabilityListener availabilityBootstrap(ObjectProvider consumerProvider,
AppAvailability appAvailability) {
return new AppAvailabilityListener(consumerProvider.stream().collect(Collectors.toList()), appAvailability);
}
}