use of org.springframework.core.env.EnumerablePropertySource in project kylo by Teradata.
the class LivyProperties method postConstruct.
@PostConstruct
private void postConstruct() {
logger.debug("PostConstruct called for LivyProperties");
if (!Lists.newArrayList(env.getActiveProfiles()).contains("kylo-livy")) {
throw new IllegalStateException("Attempting to instantiate LivyProperties bean when 'kylo-livy' is not an active profile");
}
if (!StringUtils.isNotEmpty(hostname)) {
throw new LivyConfigurationException("Attempt to start when 'kylo-livy' is an active profile and property 'spark.livy.hostname' not defined, or invalid.");
}
if (port == null || port <= 0) {
throw new LivyConfigurationException("Attempt to start when 'kylo-livy' is an active profile and property 'spark.livy.port' not defined, or invalid.");
}
logger.debug("determine the set of spark properties to pass to Livy");
MutablePropertySources propSrcs = ((AbstractEnvironment) env).getPropertySources();
StreamSupport.stream(propSrcs.spliterator(), false).filter(ps -> ps instanceof EnumerablePropertySource).map(ps -> ((EnumerablePropertySource) ps).getPropertyNames()).flatMap(Arrays::<String>stream).filter(propName -> propName.startsWith("spark.") && !(propName.startsWith("spark.livy.") || propName.startsWith("spark.shell."))).forEach(propName -> sparkProperties.put(propName, env.getProperty(propName)));
logger.debug("Validate session kinds are supportable");
if (!(livySessionKind.equals(SessionKind.shared) || livySessionKind.equals(SessionKind.spark))) {
throw new LivyConfigurationException(String.format("Session kind='%s' is not yet supported"));
}
logger.info("The following spark properties were found in kylo config files: '{}'", sparkProperties);
}
use of org.springframework.core.env.EnumerablePropertySource in project java-chassis by ServiceComb.
the class ConfigurationSpringInitializer method addMicroserviceYAMLToSpring.
/**
* make springboot have a change to add microservice.yaml source earlier<br>
* to affect {@link Conditional}
* @param environment environment
*/
private static void addMicroserviceYAMLToSpring(Environment environment) {
if (!(environment instanceof ConfigurableEnvironment)) {
return;
}
MutablePropertySources propertySources = ((ConfigurableEnvironment) environment).getPropertySources();
if (propertySources.contains(MICROSERVICE_PROPERTY_SOURCE_NAME)) {
return;
}
propertySources.addLast(new EnumerablePropertySource<MicroserviceConfigLoader>(MICROSERVICE_PROPERTY_SOURCE_NAME) {
private final Map<String, Object> values = new HashMap<>();
private final String[] propertyNames;
{
MicroserviceConfigLoader loader = new MicroserviceConfigLoader();
loader.loadAndSort();
loader.getConfigModels().forEach(configModel -> values.putAll(YAMLUtil.retrieveItems("", configModel.getConfig())));
propertyNames = values.keySet().toArray(new String[values.size()]);
}
@Override
public String[] getPropertyNames() {
return propertyNames;
}
@SuppressWarnings("unchecked")
@Override
public Object getProperty(String name) {
Object value = this.values.get(name);
// spring will not resolve nested placeholder of list, so try to fix the problem
if (value instanceof List) {
value = ((List<Object>) value).stream().filter(item -> item instanceof String).map(item -> environment.resolvePlaceholders((String) item)).collect(Collectors.toList());
}
return value;
}
});
}
use of org.springframework.core.env.EnumerablePropertySource in project java-chassis by ServiceComb.
the class ConfigurationSpringInitializer method getProperties.
/**
* Get property names from {@link EnumerablePropertySource}, and get property value from {@link ConfigurableEnvironment#getProperty(String)}
*/
private void getProperties(ConfigurableEnvironment environment, PropertySource<?> propertySource, Map<String, Object> configFromSpringBoot) {
if (propertySource instanceof CompositePropertySource) {
// recursively get EnumerablePropertySource
CompositePropertySource compositePropertySource = (CompositePropertySource) propertySource;
compositePropertySource.getPropertySources().forEach(ps -> getProperties(environment, ps, configFromSpringBoot));
return;
}
if (propertySource instanceof EnumerablePropertySource) {
EnumerablePropertySource<?> enumerablePropertySource = (EnumerablePropertySource<?>) propertySource;
for (String propertyName : enumerablePropertySource.getPropertyNames()) {
try {
configFromSpringBoot.put(propertyName, environment.getProperty(propertyName, Object.class));
} catch (Exception e) {
throw new RuntimeException("set up spring property source failed.If you still want to start up the application and ignore errors, you can set servicecomb.config.ignoreResolveFailure to true.", e);
}
}
return;
}
LOGGER.debug("a none EnumerablePropertySource is ignored, propertySourceName = [{}]", propertySource.getName());
}
use of org.springframework.core.env.EnumerablePropertySource in project spring-framework by spring-projects.
the class InlinedPropertiesTestPropertySourceTests method propertyNameOrderingIsPreservedInEnvironment.
@Test
@SuppressWarnings("rawtypes")
void propertyNameOrderingIsPreservedInEnvironment() {
final String[] expectedPropertyNames = new String[] { "foo", "baz", "enigma", "x.y.z", "server.url", "key.value.1", "key.value.2", "key.value.3" };
EnumerablePropertySource eps = (EnumerablePropertySource) env.getPropertySources().get(INLINED_PROPERTIES_PROPERTY_SOURCE_NAME);
assertThat(eps.getPropertyNames()).isEqualTo(expectedPropertyNames);
}
use of org.springframework.core.env.EnumerablePropertySource in project grails-core by grails.
the class EnvironmentAwarePropertySource method initialize.
private void initialize() {
if (propertyNames == null) {
propertyNames = new ArrayList<>();
Environment env = Environment.getCurrent();
String key = "environments." + env.getName();
for (PropertySource propertySource : source) {
if ((propertySource != this) && // plugin default configuration is not allowed to be environment aware (GRAILS-12123)
!propertySource.getName().contains("plugin") && propertySource instanceof EnumerablePropertySource) {
EnumerablePropertySource enumerablePropertySource = (EnumerablePropertySource) propertySource;
for (String propertyName : enumerablePropertySource.getPropertyNames()) {
if (propertyName.startsWith(key) && propertyName.length() > key.length()) {
propertyNames.add(propertyName.substring(key.length() + 1));
}
}
}
}
}
}
Aggregations