Search in sources :

Example 31 with CompositePropertySource

use of org.springframework.core.env.CompositePropertySource in project CzechIdMng by bcvsolutions.

the class DefaultConfigurationService method getAllProperties.

private static Map<String, Object> getAllProperties(PropertySource<?> aPropSource) {
    Map<String, Object> result = new HashMap<>();
    if (aPropSource instanceof CompositePropertySource) {
        CompositePropertySource cps = (CompositePropertySource) aPropSource;
        cps.getPropertySources().forEach(ps -> addAll(result, getAllProperties(ps)));
        return result;
    }
    if (aPropSource instanceof EnumerablePropertySource<?>) {
        EnumerablePropertySource<?> ps = (EnumerablePropertySource<?>) aPropSource;
        Arrays.asList(ps.getPropertyNames()).forEach(key -> result.put(key, ps.getProperty(key)));
        return result;
    }
    return result;
}
Also used : EnumerablePropertySource(org.springframework.core.env.EnumerablePropertySource) CompositePropertySource(org.springframework.core.env.CompositePropertySource) HashMap(java.util.HashMap) GuardedString(eu.bcvsolutions.idm.core.security.api.domain.GuardedString)

Example 32 with CompositePropertySource

use of org.springframework.core.env.CompositePropertySource in project apollo by ctripcorp.

the class ApolloApplicationContextInitializer method initialize.

/**
 * Initialize Apollo Configurations Just after environment is ready.
 *
 * @param environment
 */
protected void initialize(ConfigurableEnvironment environment) {
    if (environment.getPropertySources().contains(PropertySourcesConstants.APOLLO_BOOTSTRAP_PROPERTY_SOURCE_NAME)) {
        // already initialized, replay the logs that were printed before the logging system was initialized
        DeferredLogger.replayTo();
        return;
    }
    String namespaces = environment.getProperty(PropertySourcesConstants.APOLLO_BOOTSTRAP_NAMESPACES, ConfigConsts.NAMESPACE_APPLICATION);
    logger.debug("Apollo bootstrap namespaces: {}", namespaces);
    List<String> namespaceList = NAMESPACE_SPLITTER.splitToList(namespaces);
    CompositePropertySource composite;
    final ConfigUtil configUtil = ApolloInjector.getInstance(ConfigUtil.class);
    if (configUtil.isPropertyNamesCacheEnabled()) {
        composite = new CachedCompositePropertySource(PropertySourcesConstants.APOLLO_BOOTSTRAP_PROPERTY_SOURCE_NAME);
    } else {
        composite = new CompositePropertySource(PropertySourcesConstants.APOLLO_BOOTSTRAP_PROPERTY_SOURCE_NAME);
    }
    for (String namespace : namespaceList) {
        Config config = ConfigService.getConfig(namespace);
        composite.addPropertySource(configPropertySourceFactory.getConfigPropertySource(namespace, config));
    }
    environment.getPropertySources().addFirst(composite);
}
Also used : CompositePropertySource(org.springframework.core.env.CompositePropertySource) CachedCompositePropertySource(com.ctrip.framework.apollo.spring.config.CachedCompositePropertySource) ConfigUtil(com.ctrip.framework.apollo.util.ConfigUtil) Config(com.ctrip.framework.apollo.Config) CachedCompositePropertySource(com.ctrip.framework.apollo.spring.config.CachedCompositePropertySource)

Example 33 with CompositePropertySource

use of org.springframework.core.env.CompositePropertySource in project spring-cloud-netflix by spring-cloud.

the class EurekaClientConfigBeanTests method serviceUrlWithCompositePropertySource.

@Test
public void serviceUrlWithCompositePropertySource() {
    CompositePropertySource source = new CompositePropertySource("composite");
    this.context.getEnvironment().getPropertySources().addFirst(source);
    source.addPropertySource(new MapPropertySource("config", Collections.<String, Object>singletonMap("eureka.client.serviceUrl.defaultZone", "http://example.com,http://example2.com")));
    this.context.register(PropertyPlaceholderAutoConfiguration.class, TestConfiguration.class);
    this.context.refresh();
    assertEquals("{defaultZone=http://example.com,http://example2.com}", this.context.getBean(EurekaClientConfigBean.class).getServiceUrl().toString());
    assertEquals("[http://example.com/, http://example2.com/]", getEurekaServiceUrlsForDefaultZone());
}
Also used : CompositePropertySource(org.springframework.core.env.CompositePropertySource) MapPropertySource(org.springframework.core.env.MapPropertySource) Test(org.junit.Test)

Example 34 with CompositePropertySource

use of org.springframework.core.env.CompositePropertySource 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 35 with CompositePropertySource

use of org.springframework.core.env.CompositePropertySource in project java-chassis by ServiceComb.

the class TestConfigurationSpringInitializer method testSetEnvironment.

@Test
public void testSetEnvironment() {
    ConfigurableEnvironment environment = Mockito.mock(ConfigurableEnvironment.class);
    MutablePropertySources propertySources = new MutablePropertySources();
    Map<String, String> propertyMap = new HashMap<>();
    final String map0Key0 = "map0-Key0";
    final String map1Key0 = "map1-Key0";
    final String map2Key0 = "map2-Key0";
    final String map3Key0 = "map3-Key0";
    propertyMap.put(map0Key0, "map0-Value0");
    propertyMap.put(map1Key0, "map1-Value0");
    propertyMap.put(map2Key0, "map2-Value0");
    propertyMap.put(map3Key0, "map3-Value0");
    /*
    propertySources
    |- compositePropertySource0
    |  |- mapPropertySource0
    |  |  |- map0-Key0 = map0-Value0
    |  |- compositePropertySource1
    |     |- mapPropertySource1
    |     |  |- map1-Key0 = map1-Value0
    |     |- mapPropertySource2
    |        |- map2-Key0 = map2-Value0
    |     |- JndiPropertySource(mocked)
    |- mapPropertySource3
      |- map3-Key0 = map3-Value0
     */
    CompositePropertySource compositePropertySource0 = new CompositePropertySource("compositePropertySource0");
    propertySources.addFirst(compositePropertySource0);
    HashMap<String, Object> map0 = new HashMap<>();
    map0.put(map0Key0, propertyMap.get(map0Key0));
    MapPropertySource mapPropertySource0 = new MapPropertySource("mapPropertySource0", map0);
    compositePropertySource0.addFirstPropertySource(mapPropertySource0);
    CompositePropertySource compositePropertySource1 = new CompositePropertySource("compositePropertySource1");
    compositePropertySource0.addPropertySource(compositePropertySource1);
    HashMap<String, Object> map1 = new HashMap<>();
    map1.put(map1Key0, propertyMap.get(map1Key0));
    MapPropertySource mapPropertySource1 = new MapPropertySource("mapPropertySource1", map1);
    compositePropertySource1.addPropertySource(mapPropertySource1);
    HashMap<String, Object> map2 = new HashMap<>();
    map2.put(map2Key0, propertyMap.get(map2Key0));
    MapPropertySource mapPropertySource2 = new MapPropertySource("mapPropertySource2", map2);
    compositePropertySource1.addPropertySource(mapPropertySource2);
    compositePropertySource1.addPropertySource(Mockito.mock(JndiPropertySource.class));
    HashMap<String, Object> map3 = new HashMap<>();
    map3.put(map3Key0, propertyMap.get(map3Key0));
    MapPropertySource mapPropertySource3 = new MapPropertySource("mapPropertySource3", map3);
    compositePropertySource0.addPropertySource(mapPropertySource3);
    Mockito.when(environment.getPropertySources()).thenReturn(propertySources);
    Mockito.doAnswer((Answer<String>) invocation -> {
        Object[] args = invocation.getArguments();
        String propertyName = (String) args[0];
        if ("spring.config.name".equals(propertyName) || "spring.application.name".equals(propertyName)) {
            return null;
        }
        String value = propertyMap.get(propertyName);
        if (null == value) {
            fail("get unexpected property name: " + propertyName);
        }
        return value;
    }).when(environment).getProperty(anyString(), Matchers.eq(Object.class));
    new ConfigurationSpringInitializer().setEnvironment(environment);
    Map<String, Map<String, Object>> extraLocalConfig = getExtraConfigMapFromConfigUtil();
    assertFalse(extraLocalConfig.isEmpty());
    Map<String, Object> extraProperties = extraLocalConfig.get(ConfigurationSpringInitializer.EXTRA_CONFIG_SOURCE_PREFIX + environment.getClass().getName() + "@" + environment.hashCode());
    assertNotNull(extraLocalConfig);
    for (Entry<String, String> entry : propertyMap.entrySet()) {
        assertEquals(entry.getValue(), extraProperties.get(entry.getKey()));
    }
}
Also used : Configuration(org.apache.commons.configuration.Configuration) PropertySource(org.springframework.core.env.PropertySource) Matchers(org.mockito.Matchers) ArchaiusUtils(org.apache.servicecomb.foundation.test.scaffolding.config.ArchaiusUtils) HashMap(java.util.HashMap) Matchers.anyString(org.mockito.Matchers.anyString) Logger(org.apache.log4j.Logger) Answer(org.mockito.stubbing.Answer) ConfigurableEnvironment(org.springframework.core.env.ConfigurableEnvironment) Map(java.util.Map) Level(org.apache.log4j.Level) After(org.junit.After) Assert.fail(org.junit.Assert.fail) Before(org.junit.Before) Assert.assertNotNull(org.junit.Assert.assertNotNull) ConfigurationManager(com.netflix.config.ConfigurationManager) ConfigUtil(org.apache.servicecomb.config.ConfigUtil) Test(org.junit.Test) StandardEnvironment(org.springframework.core.env.StandardEnvironment) SystemEnvironmentPropertySource(org.springframework.core.env.SystemEnvironmentPropertySource) Deencapsulation(mockit.Deencapsulation) Mockito(org.mockito.Mockito) CompositePropertySource(org.springframework.core.env.CompositePropertySource) List(java.util.List) Assert.assertFalse(org.junit.Assert.assertFalse) MapPropertySource(org.springframework.core.env.MapPropertySource) Entry(java.util.Map.Entry) MicroserviceConfigLoader(org.apache.servicecomb.config.archaius.sources.MicroserviceConfigLoader) MutablePropertySources(org.springframework.core.env.MutablePropertySources) Assert(org.junit.Assert) JndiPropertySource(org.springframework.jndi.JndiPropertySource) Assert.assertEquals(org.junit.Assert.assertEquals) HashMap(java.util.HashMap) Matchers.anyString(org.mockito.Matchers.anyString) ConfigurableEnvironment(org.springframework.core.env.ConfigurableEnvironment) CompositePropertySource(org.springframework.core.env.CompositePropertySource) MapPropertySource(org.springframework.core.env.MapPropertySource) JndiPropertySource(org.springframework.jndi.JndiPropertySource) MutablePropertySources(org.springframework.core.env.MutablePropertySources) HashMap(java.util.HashMap) Map(java.util.Map) Test(org.junit.Test)

Aggregations

CompositePropertySource (org.springframework.core.env.CompositePropertySource)35 MapPropertySource (org.springframework.core.env.MapPropertySource)11 Test (org.junit.Test)10 MutablePropertySources (org.springframework.core.env.MutablePropertySources)10 lombok.val (lombok.val)9 HashMap (java.util.HashMap)7 Test (org.junit.jupiter.api.Test)7 ConfigurableEnvironment (org.springframework.core.env.ConfigurableEnvironment)6 StandardEnvironment (org.springframework.core.env.StandardEnvironment)5 IOException (java.io.IOException)4 Map (java.util.Map)4 SpringBootTest (org.springframework.boot.test.context.SpringBootTest)4 EnumerablePropertySource (org.springframework.core.env.EnumerablePropertySource)4 PropertySource (org.springframework.core.env.PropertySource)4 Arrays (java.util.Arrays)3 List (java.util.List)3 FileSystemResource (org.springframework.core.io.FileSystemResource)3 Resource (org.springframework.core.io.Resource)3 Config (com.ctrip.framework.apollo.Config)2 File (java.io.File)2