Search in sources :

Example 16 with EnumerablePropertySource

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);
}
Also used : LivyConfigurationException(com.thinkbiganalytics.kylo.spark.exceptions.LivyConfigurationException) LivyConfigurationException(com.thinkbiganalytics.kylo.spark.exceptions.LivyConfigurationException) Arrays(java.util.Arrays) Logger(org.slf4j.Logger) SessionKind(com.thinkbiganalytics.kylo.spark.model.enums.SessionKind) LoggerFactory(org.slf4j.LoggerFactory) Autowired(org.springframework.beans.factory.annotation.Autowired) EnumerablePropertySource(org.springframework.core.env.EnumerablePropertySource) HashMap(java.util.HashMap) StringUtils(org.apache.commons.lang3.StringUtils) Lists(com.google.common.collect.Lists) Environment(org.springframework.core.env.Environment) Map(java.util.Map) PostConstruct(javax.annotation.PostConstruct) StreamSupport(java.util.stream.StreamSupport) MutablePropertySources(org.springframework.core.env.MutablePropertySources) AbstractEnvironment(org.springframework.core.env.AbstractEnvironment) EnumerablePropertySource(org.springframework.core.env.EnumerablePropertySource) AbstractEnvironment(org.springframework.core.env.AbstractEnvironment) MutablePropertySources(org.springframework.core.env.MutablePropertySources) Arrays(java.util.Arrays) PostConstruct(javax.annotation.PostConstruct)

Example 17 with EnumerablePropertySource

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;
        }
    });
}
Also used : Ordered(org.springframework.core.Ordered) DynamicConfigurationChangedEvent(org.apache.servicecomb.config.event.DynamicConfigurationChangedEvent) PropertySource(org.springframework.core.env.PropertySource) RefreshGovernanceConfigurationEvent(org.apache.servicecomb.config.event.RefreshGovernanceConfigurationEvent) BootStrapService(org.apache.servicecomb.foundation.bootstrap.BootStrapService) LoggerFactory(org.slf4j.LoggerFactory) SPIServiceUtils(org.apache.servicecomb.foundation.common.utils.SPIServiceUtils) EnumerablePropertySource(org.springframework.core.env.EnumerablePropertySource) HashMap(java.util.HashMap) StringUtils(org.apache.commons.lang3.StringUtils) ConfigCenterConfigurationSource(org.apache.servicecomb.config.spi.ConfigCenterConfigurationSource) YAMLUtil(org.apache.servicecomb.config.YAMLUtil) EventManager(org.apache.servicecomb.foundation.common.event.EventManager) AbstractConfiguration(org.apache.commons.configuration.AbstractConfiguration) ConfigurableEnvironment(org.springframework.core.env.ConfigurableEnvironment) Map(java.util.Map) PropertySourcesPlaceholderConfigurer(org.springframework.context.support.PropertySourcesPlaceholderConfigurer) Subscribe(com.google.common.eventbus.Subscribe) Properties(java.util.Properties) Logger(org.slf4j.Logger) Iterator(java.util.Iterator) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) ConfigurationManager(com.netflix.config.ConfigurationManager) ConfigUtil(org.apache.servicecomb.config.ConfigUtil) IOException(java.io.IOException) Collectors(java.util.stream.Collectors) WatchedUpdateResult(com.netflix.config.WatchedUpdateResult) DynamicPropertyFactory(com.netflix.config.DynamicPropertyFactory) CompositePropertySource(org.springframework.core.env.CompositePropertySource) List(java.util.List) EnvironmentAware(org.springframework.context.EnvironmentAware) Environment(org.springframework.core.env.Environment) MapPropertySource(org.springframework.core.env.MapPropertySource) MicroserviceConfigLoader(org.apache.servicecomb.config.archaius.sources.MicroserviceConfigLoader) MutablePropertySources(org.springframework.core.env.MutablePropertySources) Conditional(org.springframework.context.annotation.Conditional) ConfigMapping(org.apache.servicecomb.config.ConfigMapping) HashMap(java.util.HashMap) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) ConfigurableEnvironment(org.springframework.core.env.ConfigurableEnvironment) MicroserviceConfigLoader(org.apache.servicecomb.config.archaius.sources.MicroserviceConfigLoader) List(java.util.List) MutablePropertySources(org.springframework.core.env.MutablePropertySources)

Example 18 with EnumerablePropertySource

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());
}
Also used : EnumerablePropertySource(org.springframework.core.env.EnumerablePropertySource) CompositePropertySource(org.springframework.core.env.CompositePropertySource) IOException(java.io.IOException)

Example 19 with EnumerablePropertySource

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);
}
Also used : EnumerablePropertySource(org.springframework.core.env.EnumerablePropertySource) Test(org.junit.jupiter.api.Test)

Example 20 with EnumerablePropertySource

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));
                    }
                }
            }
        }
    }
}
Also used : EnumerablePropertySource(org.springframework.core.env.EnumerablePropertySource) Environment(grails.util.Environment) PropertySource(org.springframework.core.env.PropertySource) EnumerablePropertySource(org.springframework.core.env.EnumerablePropertySource)

Aggregations

EnumerablePropertySource (org.springframework.core.env.EnumerablePropertySource)22 CompositePropertySource (org.springframework.core.env.CompositePropertySource)8 MutablePropertySources (org.springframework.core.env.MutablePropertySources)7 PropertySource (org.springframework.core.env.PropertySource)7 HashMap (java.util.HashMap)5 LinkedHashMap (java.util.LinkedHashMap)4 Environment (org.springframework.core.env.Environment)4 Map (java.util.Map)3 Properties (java.util.Properties)3 Test (org.junit.jupiter.api.Test)3 IOException (java.io.IOException)2 ArrayList (java.util.ArrayList)2 Arrays (java.util.Arrays)2 HashSet (java.util.HashSet)2 Collectors (java.util.stream.Collectors)2 StringUtils (org.apache.commons.lang3.StringUtils)2 Logger (org.slf4j.Logger)2 LoggerFactory (org.slf4j.LoggerFactory)2 ConfigurableEnvironment (org.springframework.core.env.ConfigurableEnvironment)2 MapPropertySource (org.springframework.core.env.MapPropertySource)2