-
Notifications
You must be signed in to change notification settings - Fork 172
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #26 from cfieber/master
Updates kork-core with more complete configuration for Eureka
- Loading branch information
Showing
24 changed files
with
557 additions
and
403 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,13 @@ | ||
dependencies { | ||
compile spinnaker.dependency('bootAutoConfigure') | ||
compile spinnaker.dependency('bootActuator') | ||
compile spinnaker.dependency('archaiusCore') | ||
compile spinnaker.dependency('eurekaClient') | ||
compile spinnaker.dependency('awsCore') | ||
compile spinnaker.dependency('spectatorApi') | ||
compile spinnaker.dependency('spectatorAws') | ||
compile spinnaker.dependency('spectatorGc') | ||
compile spinnaker.dependency('spectatorJvm') | ||
testCompile spinnaker.dependency('junit') | ||
testRuntime spinnaker.dependency('slf4jSimple') | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
158 changes: 158 additions & 0 deletions
158
kork-core/src/main/java/com/netflix/spinnaker/kork/archaius/ArchaiusConfiguration.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,158 @@ | ||
/* | ||
* Copyright 2015 Netflix, Inc. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License") | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package com.netflix.spinnaker.kork.archaius; | ||
|
||
import com.netflix.config.AbstractPollingScheduler; | ||
import com.netflix.config.ConfigurationManager; | ||
import com.netflix.config.DynamicConfiguration; | ||
import com.netflix.config.FixedDelayPollingScheduler; | ||
import com.netflix.spinnaker.kork.internal.Precondition; | ||
import org.apache.commons.configuration.AbstractConfiguration; | ||
import org.apache.commons.configuration.CompositeConfiguration; | ||
import org.springframework.beans.BeansException; | ||
import org.springframework.beans.factory.FactoryBean; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.beans.factory.config.AbstractFactoryBean; | ||
import org.springframework.beans.factory.config.BeanPostProcessor; | ||
import org.springframework.context.ApplicationContext; | ||
import org.springframework.context.ConfigurableApplicationContext; | ||
import org.springframework.context.annotation.Bean; | ||
import org.springframework.context.annotation.Configuration; | ||
import org.springframework.context.annotation.DependsOn; | ||
import org.springframework.core.Ordered; | ||
import org.springframework.core.env.ConfigurableEnvironment; | ||
import org.springframework.core.env.MutablePropertySources; | ||
import org.springframework.core.io.ClassPathResource; | ||
import org.springframework.core.io.Resource; | ||
import org.springframework.core.io.ResourceLoader; | ||
import org.springframework.core.io.support.ResourcePropertySource; | ||
|
||
import javax.annotation.PostConstruct; | ||
import javax.annotation.PreDestroy; | ||
import java.io.IOException; | ||
import java.util.*; | ||
import java.util.concurrent.TimeUnit; | ||
|
||
@Configuration | ||
public class ArchaiusConfiguration { | ||
|
||
@Autowired | ||
ConfigurableApplicationContext applicationContext; | ||
|
||
@Autowired(required = false) | ||
List<ClasspathPropertySource> propertyBindings; | ||
|
||
/** | ||
* This is a BeanPostProcessor to ensure early initialization only. | ||
*/ | ||
static class ArchaiusInitializingBeanPostProcessor implements BeanPostProcessor, Ordered { | ||
private final ConfigurableApplicationContext applicationContext; | ||
private final AbstractPollingScheduler pollingScheduler; | ||
private final SpringEnvironmentPolledConfigurationSource polledConfigurationSource; | ||
private final List<ClasspathPropertySource> propertyBindings; | ||
private final DynamicConfiguration configurationInstance; | ||
|
||
public ArchaiusInitializingBeanPostProcessor(ConfigurableApplicationContext applicationContext, AbstractPollingScheduler pollingScheduler, SpringEnvironmentPolledConfigurationSource polledConfigurationSource, List<ClasspathPropertySource> propertyBindings) { | ||
this.applicationContext = Precondition.notNull(applicationContext, "applicationContext"); | ||
this.pollingScheduler = Precondition.notNull(pollingScheduler, "pollingScheduler"); | ||
this.polledConfigurationSource = Precondition.notNull(polledConfigurationSource, "polledConfigurationSource"); | ||
this.propertyBindings = propertyBindings != null ? propertyBindings : Collections.emptyList(); | ||
initPropertyBindings(); | ||
|
||
configurationInstance = new DynamicConfiguration(polledConfigurationSource, pollingScheduler); | ||
if (!ConfigurationManager.isConfigurationInstalled()) { | ||
ConfigurationManager.install(new CompositeConfiguration()); | ||
} | ||
CompositeConfiguration config = (CompositeConfiguration) ConfigurationManager.getConfigInstance(); | ||
config.addConfiguration(configurationInstance); | ||
|
||
applicationContext.getBeanFactory().registerSingleton("environmentBackedConfig", ConfigurationManager.getConfigInstance()); | ||
applicationContext.getBeanFactory().registerAlias("environmentBackedConfig", "abstractConfiguration"); | ||
} | ||
|
||
@PreDestroy | ||
public void shutdown() { | ||
pollingScheduler.stop(); | ||
((CompositeConfiguration) ConfigurationManager.getConfigInstance()).removeConfiguration(configurationInstance); | ||
} | ||
|
||
private void initPropertyBindings() { | ||
MutablePropertySources sources = applicationContext.getEnvironment().getPropertySources(); | ||
Set<String> activeProfiles = new HashSet<>(Arrays.asList(applicationContext.getEnvironment().getActiveProfiles())); | ||
for (ClasspathPropertySource binding : propertyBindings) { | ||
for (String profile : activeProfiles) { | ||
if (binding.supportsProfile(profile)) { | ||
res(binding.getBaseName(), profile).ifPresent(sources::addLast); | ||
} | ||
} | ||
res(binding.getBaseName(), null).ifPresent(sources::addLast); | ||
} | ||
} | ||
|
||
private Optional<ResourcePropertySource> res(String base, String profile) { | ||
String name = base; | ||
String res = "/" + base; | ||
if (profile != null && !profile.isEmpty()) { | ||
name += ": " + profile; | ||
res += "-" + profile; | ||
} | ||
res += ".properties"; | ||
Resource r = applicationContext.getResource(res); | ||
if (r.exists()) { | ||
try { | ||
return Optional.of(new ResourcePropertySource(name, r)); | ||
} catch (IOException ioe) { | ||
throw new RuntimeException("Error loading property source [" + name + "]: " + res, ioe); | ||
} | ||
} | ||
return Optional.empty(); | ||
} | ||
|
||
|
||
@Override | ||
public int getOrder() { | ||
return Ordered.HIGHEST_PRECEDENCE + 10; | ||
} | ||
|
||
@Override | ||
public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException { | ||
return bean; | ||
} | ||
|
||
@Override | ||
public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException { | ||
return bean; | ||
} | ||
} | ||
|
||
@Bean | ||
ArchaiusInitializingBeanPostProcessor archaiusInitializingBeanPostProcessor() { | ||
return new ArchaiusInitializingBeanPostProcessor(applicationContext, pollingScheduler(), polledConfigurationSource(), propertyBindings); | ||
} | ||
|
||
@Bean | ||
AbstractPollingScheduler pollingScheduler() { | ||
int initialDelayMillis = applicationContext.getEnvironment().getProperty(FixedDelayPollingScheduler.INITIAL_DELAY_PROPERTY, Integer.class, 0); | ||
int delayMillis = applicationContext.getEnvironment().getProperty(FixedDelayPollingScheduler.DELAY_PROPERTY, Integer.class, (int) TimeUnit.SECONDS.toMillis(15)); | ||
return new FixedDelayPollingScheduler(initialDelayMillis, delayMillis, false); | ||
} | ||
|
||
@Bean | ||
SpringEnvironmentPolledConfigurationSource polledConfigurationSource() { | ||
return new SpringEnvironmentPolledConfigurationSource(applicationContext.getEnvironment()); | ||
} | ||
} |
25 changes: 25 additions & 0 deletions
25
kork-core/src/main/java/com/netflix/spinnaker/kork/archaius/ClasspathPropertySource.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
/* | ||
* Copyright 2015 Netflix, Inc. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License") | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package com.netflix.spinnaker.kork.archaius; | ||
|
||
public interface ClasspathPropertySource { | ||
String getBaseName(); | ||
|
||
default boolean supportsProfile(String profile) { | ||
return true; | ||
} | ||
} |
32 changes: 32 additions & 0 deletions
32
...ore/src/main/java/com/netflix/spinnaker/kork/archaius/DefaultClasspathPropertySource.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
/* | ||
* Copyright 2015 Netflix, Inc. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License") | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package com.netflix.spinnaker.kork.archaius; | ||
|
||
import com.netflix.spinnaker.kork.internal.Precondition; | ||
|
||
public class DefaultClasspathPropertySource implements ClasspathPropertySource { | ||
private final String baseName; | ||
|
||
public DefaultClasspathPropertySource(String baseName) { | ||
this.baseName = Precondition.notNull(baseName, "baseName"); | ||
} | ||
|
||
@Override | ||
public String getBaseName() { | ||
return baseName; | ||
} | ||
} |
51 changes: 51 additions & 0 deletions
51
.../java/com/netflix/spinnaker/kork/archaius/SpringEnvironmentPolledConfigurationSource.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
/* | ||
* Copyright 2015 Netflix, Inc. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License") | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package com.netflix.spinnaker.kork.archaius; | ||
|
||
import com.netflix.config.PollResult; | ||
import com.netflix.config.PolledConfigurationSource; | ||
import com.netflix.spinnaker.kork.internal.Precondition; | ||
import org.springframework.core.env.ConfigurableEnvironment; | ||
import org.springframework.core.env.EnumerablePropertySource; | ||
|
||
import java.util.HashMap; | ||
import java.util.Map; | ||
import java.util.Optional; | ||
|
||
public class SpringEnvironmentPolledConfigurationSource implements PolledConfigurationSource { | ||
|
||
private final ConfigurableEnvironment environment; | ||
|
||
public SpringEnvironmentPolledConfigurationSource(ConfigurableEnvironment environment) { | ||
this.environment = Precondition.notNull(environment, "environment"); | ||
} | ||
|
||
@Override | ||
public PollResult poll(boolean initial, Object checkPoint) throws Exception { | ||
Map<String, Object> result = new HashMap<>(); | ||
environment.getPropertySources().iterator().forEachRemaining(source -> { | ||
if (source instanceof EnumerablePropertySource) { | ||
EnumerablePropertySource<?> enumerable = (EnumerablePropertySource<?>) source; | ||
for (String key : enumerable.getPropertyNames()) { | ||
result.putIfAbsent(key, enumerable.getProperty(key)); | ||
} | ||
} | ||
}); | ||
return PollResult.createFull(result); | ||
} | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.