List of usage examples for org.springframework.core.env ConfigurableEnvironment getPropertySources
MutablePropertySources getPropertySources();
From source file:org.alfresco.bm.test.TestRunServicesCache.java
/** * Create an application context holding the services for the given test run *///from ww w . j a v a2s. c om private ClassPathXmlApplicationContext createContext(String test, String run) { String testRunFqn = test + "." + run; DBObject runObj; try { runObj = dao.getTestRun(test, run, true); } catch (ObjectNotFoundException e1) { logger.error("Test '" + test + "." + run + "' not found.", e1); return null; } // Dig the properties out of the test run Properties testRunProps = new Properties(); { testRunProps.put(PROP_TEST_RUN_FQN, testRunFqn); BasicDBList propObjs = (BasicDBList) runObj.get(FIELD_PROPERTIES); for (Object obj : propObjs) { DBObject propObj = (DBObject) obj; String propName = (String) propObj.get(FIELD_NAME); String propDef = (String) propObj.get(FIELD_DEFAULT); String propValue = (String) propObj.get(FIELD_VALUE); if (propValue == null) { propValue = propDef; } testRunProps.put(propName, propValue); } } // Construct the properties ClassPathXmlApplicationContext testRunCtx = new ClassPathXmlApplicationContext( new String[] { PATH_TEST_SERVICES_CONTEXT }, false); ConfigurableEnvironment ctxEnv = testRunCtx.getEnvironment(); ctxEnv.getPropertySources().addFirst(new PropertiesPropertySource("run-props", testRunProps)); // Bind to shutdown testRunCtx.registerShutdownHook(); // Attempt to start the context try { testRunCtx.refresh(); testRunCtx.start(); // Make sure that the required components are present testRunCtx.getBean(EventService.class); testRunCtx.getBean(ResultService.class); testRunCtx.getBean(SessionService.class); } catch (Exception e) { Throwable root = ExceptionUtils.getRootCause(e); if (root != null && root instanceof MongoSocketException) { // We deal with this specifically as it's a simple case of not finding the MongoDB logger.error("Failed to start test run services context '" + testRunFqn + "': " + e.getCause().getMessage()); logger.error( "Set the test run property '" + PROP_MONGO_TEST_HOST + "' (<server>:<port>) as required."); } else if (root != null && root instanceof UnknownHostException) { // We deal with this specifically as it's a simple case of not finding the MongoDB logger.error("Failed to start test run services context '" + testRunFqn + "': " + e.getCause().getCause().getMessage()); logger.error( "Set the test run property '" + PROP_MONGO_TEST_HOST + "' (<server>:<port>) as required."); } else { logger.error("Failed to start test run services context '" + testRunFqn + "': ", e); } testRunCtx = null; } // Done if (testRunCtx == null) { logger.warn("Failed to start test run services context: " + testRunFqn); } else if (logger.isDebugEnabled()) { logger.debug("Started test run services context: " + testRunFqn); } return testRunCtx; }
From source file:org.apache.servicecomb.config.ConfigurationSpringInitializer.java
/** * Traversal all {@link PropertySource} of {@link ConfigurableEnvironment}, and try to get all properties. *//*from w ww . j av a 2 s. c o m*/ private Map<String, Object> getAllProperties(Environment environment) { Map<String, Object> configFromSpringBoot = new HashMap<>(); if (!(environment instanceof ConfigurableEnvironment)) { return configFromSpringBoot; } ConfigurableEnvironment configurableEnvironment = (ConfigurableEnvironment) environment; for (PropertySource<?> propertySource : configurableEnvironment.getPropertySources()) { getProperties(configurableEnvironment, propertySource, configFromSpringBoot); } return configFromSpringBoot; }
From source file:org.apache.servicecomb.config.TestConfigurationSpringInitializer.java
@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"); /*//w ww . ja v a2 s.c o m 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()); 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())); } }
From source file:org.apache.servicecomb.config.TestConfigurationSpringInitializer.java
@Test public void testSetEnvironmentOnEnvironmentName() { // get environment name from spring.config.name ConfigurableEnvironment environment0 = Mockito.mock(ConfigurableEnvironment.class); MutablePropertySources propertySources0 = new MutablePropertySources(); Mockito.when(environment0.getPropertySources()).thenReturn(propertySources0); Map<String, Object> map0 = new HashMap<>(1); map0.put("spring.config.name", "application"); propertySources0.addFirst(new MapPropertySource("mapPropertySource0", map0)); Mockito.when(environment0.getProperty("spring.config.name")).thenReturn("application"); // get environment name from spring.application.name ConfigurableEnvironment environment1 = Mockito.mock(ConfigurableEnvironment.class); MutablePropertySources propertySources1 = new MutablePropertySources(); Mockito.when(environment1.getPropertySources()).thenReturn(propertySources1); Map<String, Object> map1 = new HashMap<>(1); map1.put("spring.application.name", "bootstrap"); propertySources1.addFirst(new MapPropertySource("mapPropertySource1", map1)); Mockito.when(environment1.getProperty("spring.application.name")).thenReturn("bootstrap"); // get environment name from className+hashcode ConfigurableEnvironment environment2 = Mockito.mock(ConfigurableEnvironment.class); MutablePropertySources propertySources2 = new MutablePropertySources(); Mockito.when(environment2.getPropertySources()).thenReturn(propertySources2); Map<String, Object> map2 = new HashMap<>(1); map2.put("key2", "value2"); propertySources2.addFirst(new MapPropertySource("mapPropertySource2", map2)); Mockito.when(environment2.getProperty("key2")).thenReturn("value2"); ConfigurationSpringInitializer configurationSpringInitializer = new ConfigurationSpringInitializer(); configurationSpringInitializer.setEnvironment(environment0); configurationSpringInitializer.setEnvironment(environment1); configurationSpringInitializer.setEnvironment(environment2); Map<String, Map<String, Object>> extraConfig = getExtraConfigMapFromConfigUtil(); assertEquals(3, extraConfig.size()); Map<String, Object> extraProperties = extraConfig .get(ConfigurationSpringInitializer.EXTRA_CONFIG_SOURCE_PREFIX + "application"); assertEquals(1, extraProperties.size()); assertEquals("application", extraProperties.get("spring.config.name")); extraProperties = extraConfig.get(ConfigurationSpringInitializer.EXTRA_CONFIG_SOURCE_PREFIX + "bootstrap"); assertEquals(1, extraProperties.size()); assertEquals("bootstrap", extraProperties.get("spring.application.name")); extraProperties = extraConfig.get(ConfigurationSpringInitializer.EXTRA_CONFIG_SOURCE_PREFIX + environment2.getClass().getName() + "@" + environment2.hashCode()); assertEquals(1, extraProperties.size()); assertEquals("value2", extraProperties.get("key2")); }
From source file:org.finra.dm.core.AbstractCoreTest.java
/** * Gets the mutable property sources object from the environment. * * @return the mutable property sources. * @throws Exception if the mutable property sources couldn't be obtained. *//* w ww .j a v a2 s . com*/ protected MutablePropertySources getMutablePropertySources() throws Exception { // Ensure we have a configurable environment so we can remove the property source. if (!(environment instanceof ConfigurableEnvironment)) { throw new Exception( "The environment is not an instance of ConfigurableEnvironment and needs to be for this test to work."); } // Return the property sources from the configurable environment. ConfigurableEnvironment configurableEnvironment = (ConfigurableEnvironment) environment; return configurableEnvironment.getPropertySources(); }
From source file:org.finra.dm.dao.config.DaoSpringModuleConfig.java
/** * The database supplied property sources placeholder configurer that allows access to externalized properties from a database. This method also adds a new * property source that contains the database properties to the environment. * * @return the property sources placeholder configurer. *///ww w . j ava 2s . c o m @Bean public static PropertySourcesPlaceholderConfigurer databasePropertySourcesPlaceholderConfigurer() { // Get the configurable environment and add a new property source to it that contains the database properties. // That way, the properties can be accessed via the environment or via an injected @Value annotation. // We are adding this property source last so other property sources (e.g. system properties, environment variables) can be used // to override the database properties. Environment environment = ApplicationContextHolder.getApplicationContext().getEnvironment(); if (environment instanceof ConfigurableEnvironment) { ConfigurableEnvironment configurableEnvironment = (ConfigurableEnvironment) environment; ReloadablePropertySource reloadablePropertySource = new ReloadablePropertySource( ReloadablePropertySource.class.getName(), ConfigurationConverter.getProperties(getPropertyDatabaseConfiguration()), getPropertyDatabaseConfiguration()); configurableEnvironment.getPropertySources().addLast(reloadablePropertySource); } return new PropertySourcesPlaceholderConfigurer(); }
From source file:org.kuali.rice.core.web.util.PropertySources.java
/** * Register {@code propertySource} as the first thing Spring will check when looking up property * values./*w ww. ja v a2s . co m*/ */ public static void addFirst(ConfigurableApplicationContext context, PropertySource<?> propertySource) { ConfigurableEnvironment env = context.getEnvironment(); MutablePropertySources propertySources = env.getPropertySources(); propertySources.addFirst(propertySource); }
From source file:org.springframework.boot.actuate.autoconfigure.EndpointWebMvcAutoConfiguration.java
/** * Add an alias for 'local.management.port' that actually resolves using * 'local.server.port'.// w w w .ja va 2 s.c o m * @param environment the environment */ private void addLocalManagementPortPropertyAlias(final ConfigurableEnvironment environment) { environment.getPropertySources().addLast(new PropertySource<Object>("Management Server") { @Override public Object getProperty(String name) { if ("local.management.port".equals(name)) { return environment.getProperty("local.server.port"); } return null; } }); }
From source file:org.springframework.boot.cloud.CloudFoundryVcapEnvironmentPostProcessor.java
@Override public void postProcessEnvironment(ConfigurableEnvironment environment, SpringApplication application) { if (CloudPlatform.CLOUD_FOUNDRY.isActive(environment)) { Properties properties = new Properties(); addWithPrefix(properties, getPropertiesFromApplication(environment), "vcap.application."); addWithPrefix(properties, getPropertiesFromServices(environment), "vcap.services."); MutablePropertySources propertySources = environment.getPropertySources(); if (propertySources.contains(CommandLinePropertySource.COMMAND_LINE_PROPERTY_SOURCE_NAME)) { propertySources.addAfter(CommandLinePropertySource.COMMAND_LINE_PROPERTY_SOURCE_NAME, new PropertiesPropertySource("vcap", properties)); } else {//from w w w . ja va2 s. c o m propertySources.addFirst(new PropertiesPropertySource("vcap", properties)); } } }
From source file:org.springframework.boot.cloudfoundry.VcapApplicationListener.java
@Override public void onApplicationEvent(ApplicationEnvironmentPreparedEvent event) { ConfigurableEnvironment environment = event.getEnvironment(); if (!environment.containsProperty(VCAP_APPLICATION) && !environment.containsProperty(VCAP_SERVICES)) { return;/*from ww w . jav a2 s . c om*/ } Properties properties = new Properties(); addWithPrefix(properties, getPropertiesFromApplication(environment), "vcap.application."); addWithPrefix(properties, getPropertiesFromServices(environment), "vcap.services."); MutablePropertySources propertySources = environment.getPropertySources(); if (propertySources.contains(CommandLinePropertySource.COMMAND_LINE_PROPERTY_SOURCE_NAME)) { propertySources.addAfter(CommandLinePropertySource.COMMAND_LINE_PROPERTY_SOURCE_NAME, new PropertiesPropertySource("vcap", properties)); } else { propertySources.addFirst(new PropertiesPropertySource("vcap", properties)); } }