|
| 1 | +/* |
| 2 | + * Copyright 2015 Netflix, Inc. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License") |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package com.netflix.spinnaker.kork.archaius; |
| 18 | + |
| 19 | +import com.netflix.config.AbstractPollingScheduler; |
| 20 | +import com.netflix.config.ConfigurationManager; |
| 21 | +import com.netflix.config.DynamicConfiguration; |
| 22 | +import com.netflix.config.FixedDelayPollingScheduler; |
| 23 | +import com.netflix.spinnaker.kork.internal.Precondition; |
| 24 | +import org.apache.commons.configuration.AbstractConfiguration; |
| 25 | +import org.apache.commons.configuration.CompositeConfiguration; |
| 26 | +import org.springframework.beans.BeansException; |
| 27 | +import org.springframework.beans.factory.FactoryBean; |
| 28 | +import org.springframework.beans.factory.annotation.Autowired; |
| 29 | +import org.springframework.beans.factory.config.AbstractFactoryBean; |
| 30 | +import org.springframework.beans.factory.config.BeanPostProcessor; |
| 31 | +import org.springframework.context.ApplicationContext; |
| 32 | +import org.springframework.context.ConfigurableApplicationContext; |
| 33 | +import org.springframework.context.annotation.Bean; |
| 34 | +import org.springframework.context.annotation.Configuration; |
| 35 | +import org.springframework.context.annotation.DependsOn; |
| 36 | +import org.springframework.core.Ordered; |
| 37 | +import org.springframework.core.env.ConfigurableEnvironment; |
| 38 | +import org.springframework.core.env.MutablePropertySources; |
| 39 | +import org.springframework.core.io.ClassPathResource; |
| 40 | +import org.springframework.core.io.Resource; |
| 41 | +import org.springframework.core.io.ResourceLoader; |
| 42 | +import org.springframework.core.io.support.ResourcePropertySource; |
| 43 | + |
| 44 | +import javax.annotation.PostConstruct; |
| 45 | +import javax.annotation.PreDestroy; |
| 46 | +import java.io.IOException; |
| 47 | +import java.util.*; |
| 48 | +import java.util.concurrent.TimeUnit; |
| 49 | + |
| 50 | +@Configuration |
| 51 | +public class ArchaiusConfiguration { |
| 52 | + |
| 53 | + @Autowired |
| 54 | + ConfigurableApplicationContext applicationContext; |
| 55 | + |
| 56 | + @Autowired(required = false) |
| 57 | + List<ClasspathPropertySource> propertyBindings; |
| 58 | + |
| 59 | + /** |
| 60 | + * This is a BeanPostProcessor to ensure early initialization only. |
| 61 | + */ |
| 62 | + static class ArchaiusInitializingBeanPostProcessor implements BeanPostProcessor, Ordered { |
| 63 | + private final ConfigurableApplicationContext applicationContext; |
| 64 | + private final AbstractPollingScheduler pollingScheduler; |
| 65 | + private final SpringEnvironmentPolledConfigurationSource polledConfigurationSource; |
| 66 | + private final List<ClasspathPropertySource> propertyBindings; |
| 67 | + private final DynamicConfiguration configurationInstance; |
| 68 | + |
| 69 | + public ArchaiusInitializingBeanPostProcessor(ConfigurableApplicationContext applicationContext, AbstractPollingScheduler pollingScheduler, SpringEnvironmentPolledConfigurationSource polledConfigurationSource, List<ClasspathPropertySource> propertyBindings) { |
| 70 | + this.applicationContext = Precondition.notNull(applicationContext, "applicationContext"); |
| 71 | + this.pollingScheduler = Precondition.notNull(pollingScheduler, "pollingScheduler"); |
| 72 | + this.polledConfigurationSource = Precondition.notNull(polledConfigurationSource, "polledConfigurationSource"); |
| 73 | + this.propertyBindings = propertyBindings != null ? propertyBindings : Collections.emptyList(); |
| 74 | + initPropertyBindings(); |
| 75 | + |
| 76 | + configurationInstance = new DynamicConfiguration(polledConfigurationSource, pollingScheduler); |
| 77 | + if (!ConfigurationManager.isConfigurationInstalled()) { |
| 78 | + ConfigurationManager.install(new CompositeConfiguration()); |
| 79 | + } |
| 80 | + CompositeConfiguration config = (CompositeConfiguration) ConfigurationManager.getConfigInstance(); |
| 81 | + config.addConfiguration(configurationInstance); |
| 82 | + |
| 83 | + applicationContext.getBeanFactory().registerSingleton("environmentBackedConfig", ConfigurationManager.getConfigInstance()); |
| 84 | + applicationContext.getBeanFactory().registerAlias("environmentBackedConfig", "abstractConfiguration"); |
| 85 | + } |
| 86 | + |
| 87 | + @PreDestroy |
| 88 | + public void shutdown() { |
| 89 | + pollingScheduler.stop(); |
| 90 | + ((CompositeConfiguration) ConfigurationManager.getConfigInstance()).removeConfiguration(configurationInstance); |
| 91 | + } |
| 92 | + |
| 93 | + private void initPropertyBindings() { |
| 94 | + MutablePropertySources sources = applicationContext.getEnvironment().getPropertySources(); |
| 95 | + Set<String> activeProfiles = new HashSet<>(Arrays.asList(applicationContext.getEnvironment().getActiveProfiles())); |
| 96 | + for (ClasspathPropertySource binding : propertyBindings) { |
| 97 | + for (String profile : activeProfiles) { |
| 98 | + if (binding.supportsProfile(profile)) { |
| 99 | + res(binding.getBaseName(), profile).ifPresent(sources::addLast); |
| 100 | + } |
| 101 | + } |
| 102 | + res(binding.getBaseName(), null).ifPresent(sources::addLast); |
| 103 | + } |
| 104 | + } |
| 105 | + |
| 106 | + private Optional<ResourcePropertySource> res(String base, String profile) { |
| 107 | + String name = base; |
| 108 | + String res = "/" + base; |
| 109 | + if (profile != null && !profile.isEmpty()) { |
| 110 | + name += ": " + profile; |
| 111 | + res += "-" + profile; |
| 112 | + } |
| 113 | + res += ".properties"; |
| 114 | + Resource r = applicationContext.getResource(res); |
| 115 | + if (r.exists()) { |
| 116 | + try { |
| 117 | + return Optional.of(new ResourcePropertySource(name, r)); |
| 118 | + } catch (IOException ioe) { |
| 119 | + throw new RuntimeException("Error loading property source [" + name + "]: " + res, ioe); |
| 120 | + } |
| 121 | + } |
| 122 | + return Optional.empty(); |
| 123 | + } |
| 124 | + |
| 125 | + |
| 126 | + @Override |
| 127 | + public int getOrder() { |
| 128 | + return Ordered.HIGHEST_PRECEDENCE + 10; |
| 129 | + } |
| 130 | + |
| 131 | + @Override |
| 132 | + public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException { |
| 133 | + return bean; |
| 134 | + } |
| 135 | + |
| 136 | + @Override |
| 137 | + public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException { |
| 138 | + return bean; |
| 139 | + } |
| 140 | + } |
| 141 | + |
| 142 | + @Bean |
| 143 | + ArchaiusInitializingBeanPostProcessor archaiusInitializingBeanPostProcessor() { |
| 144 | + return new ArchaiusInitializingBeanPostProcessor(applicationContext, pollingScheduler(), polledConfigurationSource(), propertyBindings); |
| 145 | + } |
| 146 | + |
| 147 | + @Bean |
| 148 | + AbstractPollingScheduler pollingScheduler() { |
| 149 | + int initialDelayMillis = applicationContext.getEnvironment().getProperty(FixedDelayPollingScheduler.INITIAL_DELAY_PROPERTY, Integer.class, 0); |
| 150 | + int delayMillis = applicationContext.getEnvironment().getProperty(FixedDelayPollingScheduler.DELAY_PROPERTY, Integer.class, (int) TimeUnit.SECONDS.toMillis(15)); |
| 151 | + return new FixedDelayPollingScheduler(initialDelayMillis, delayMillis, false); |
| 152 | + } |
| 153 | + |
| 154 | + @Bean |
| 155 | + SpringEnvironmentPolledConfigurationSource polledConfigurationSource() { |
| 156 | + return new SpringEnvironmentPolledConfigurationSource(applicationContext.getEnvironment()); |
| 157 | + } |
| 158 | +} |
0 commit comments