List of usage examples for org.springframework.core.env PropertySource getProperty
@Nullable public abstract Object getProperty(String name);
From source file:org.carewebframework.api.spring.SpringUtil.java
/** * Returns a property value from the application context. * /*from w ww. j a va 2s. co m*/ * @param name Property name. * @return Property value, or null if not found. */ public static String getProperty(String name) { ApplicationContext appContext = getRootAppContext(); if (appContext == null) { return null; } String value = appContext.getEnvironment().getProperty(name); if (value == null) { PropertySourcesPlaceholderConfigurer cfg = appContext .getBean(PropertySourcesPlaceholderConfigurer.class); PropertySource<?> ps = cfg.getAppliedPropertySources() .get(PropertySourcesPlaceholderConfigurer.LOCAL_PROPERTIES_PROPERTY_SOURCE_NAME); value = ps == null ? null : (String) ps.getProperty(name); } return value; }
@Test public void testPropertyResolved() { System.setProperty("ENVIRONMENT", "dev"); System.setProperty("ENV", "dev"); ctx = new ClassPathXmlApplicationContext(new String[] { "classpath:SpringPropertySourcesPlaceholderConfigurer-composite-config-withstringlocations.xml" }); String value = (String) ctx.getBean("string"); String valueSys = (String) ctx.getBean("string-sys"); String valueEnv = (String) ctx.getBean("string-env"); String valuePath = (String) ctx.getBean("string-path"); String valueOps = (String) ctx.getBean("string-ops"); assertEquals("devproperties", value); assertEquals("dev", valueSys); assertEquals(System.getenv("PATH"), valueEnv); assertEquals(System.getenv("PATH"), valuePath); assertEquals("ops have overridden me", valueOps); PropertySource p = ((PropertySource) ctx.getBean("environmentalProperties")); assertEquals("${ENVIRONMENT}", p.getProperty("sys")); assertEquals("${PATH}", p.getProperty("env")); }
From source file:com.santander.serenity.devstack.springfox.config.itest.EnvironmentListenerIntegrationTests.java
/** * Checks that the property "security.ignored" is in the environment after SpringFoxEnvironmentPostProcessor is executed. *//*from w w w . ja v a 2s .co m*/ @Test public void securityIgnoredPropertyTest() { ConfigurableEnvironment environment = context.getEnvironment(); MutablePropertySources propertySources = environment.getPropertySources(); PropertySource<?> modifiedSecurityIgnoredPropertySource = propertySources .get("ModifiedSecurityIgnoredPropertySource"); Object securityIgnoredProperty = modifiedSecurityIgnoredPropertySource.getProperty("security.ignored"); Assert.assertNotNull("security.gnored property must not be null", securityIgnoredProperty); }
From source file:com.santander.serenity.devstack.springfox.config.itest.EnvironmentListenerIntegrationTests.java
/** * Checks that the property "security.ignored" has the correct values *//*from w ww . java 2 s . c o m*/ @Test public void securityIgnoredPropertyValuesTest() { ConfigurableEnvironment environment = context.getEnvironment(); MutablePropertySources propertySources = environment.getPropertySources(); PropertySource<?> modifiedSecurityIgnoredPropertySource = propertySources .get("ModifiedSecurityIgnoredPropertySource"); Object securityIgnoredPropertyValues = modifiedSecurityIgnoredPropertySource .getProperty("security.ignored"); LinkedHashSet set = (LinkedHashSet) securityIgnoredPropertyValues; Assert.assertTrue("/webjars/** property is not in the security.ignored set", set.contains("/webjars/**")); Assert.assertTrue("/swagger-resources property is not in the security.ignored set", set.contains("/swagger-resources")); Assert.assertTrue("/v2/api-docs property is not in the security.ignored set", set.contains("/v2/api-docs")); Assert.assertTrue("/configuration/ui property is not in the security.ignored set", set.contains("/configuration/ui")); Assert.assertTrue("/configuration/security property is not in the security.ignored set", set.contains("/configuration/security")); Assert.assertTrue("/swagger-ui.html property is not in the security.ignored set", set.contains("/swagger-ui.html")); }
From source file:io.bitsquare.app.BitsquareEnvironment.java
BitsquareEnvironment(PropertySource commandLineProperties) { String userDataDir = commandLineProperties.containsProperty(USER_DATA_DIR_KEY) ? (String) commandLineProperties.getProperty(USER_DATA_DIR_KEY) : DEFAULT_USER_DATA_DIR;/* ww w .ja v a 2s. c o m*/ this.appName = commandLineProperties.containsProperty(APP_NAME_KEY) ? (String) commandLineProperties.getProperty(APP_NAME_KEY) : DEFAULT_APP_NAME; this.appDataDir = commandLineProperties.containsProperty(APP_DATA_DIR_KEY) ? (String) commandLineProperties.getProperty(APP_DATA_DIR_KEY) : appDataDir(userDataDir, appName); MutablePropertySources propertySources = this.getPropertySources(); propertySources.addFirst(commandLineProperties); try { propertySources.addLast(filesystemProperties()); propertySources.addLast(classpathProperties()); propertySources.addLast(defaultProperties()); } catch (Exception ex) { throw new BitsquareException(ex); } }
From source file:io.pivotal.springcloud.ssl.CloudFoundryCertificateTruster.java
@Override public void initialize(ConfigurableApplicationContext applicationContext) { if (!inited) { inited = true;//from w w w.j a v a 2 s . c om String trustCertUrls = null; String trustStore = null; String trustStorePassword = null; ConfigurableEnvironment environment = applicationContext.getEnvironment(); for (PropertySource<?> propertySource : environment.getPropertySources()) { if (propertySource.containsProperty("app.ssl.trustStore")) trustStore = (String) propertySource.getProperty("app.ssl.trustStore"); if (propertySource.containsProperty("app.ssl.trustStorePassword")) trustStorePassword = (String) propertySource.getProperty("app.ssl.trustStorePassword"); if (propertySource.containsProperty("app.ssl.trustCertUrls")) trustCertUrls = (String) propertySource.getProperty("app.ssl.trustCertUrls"); } if (trustCertUrls != null) trustCertificatesFromURLInternal(trustCertUrls); if (trustStore != null) trustCertificatesFromStoreInternal(applicationContext, trustStore, trustStorePassword); } }
From source file:org.cloudfoundry.identity.uaa.config.EnvironmentMapFactoryBean.java
@Override public Map<String, ?> getObject() { Map<String, Object> result = new LinkedHashMap<String, Object>(); // The result is the default application properties overridden with // Spring environment values - reversing the // order of the placeholder configurers in the application context. for (Object key : defaultProperties.keySet()) { String name = (String) key; if (environment != null && environment.containsProperty(name)) { Object value = environment.getProperty(name, Object.class); logger.debug("From Environment: " + name); result.put(name, value);/*ww w . j a va2s. c o m*/ } else { logger.debug("From Defaults: " + name); result.put(name, defaultProperties.get(key)); } } // Any properties added only in the environment can be picked up here... if (environment instanceof ConfigurableEnvironment) { for (PropertySource<?> source : ((ConfigurableEnvironment) environment).getPropertySources()) { if (source instanceof EnumerablePropertySource && !STATIC_PROPERTY_SOURCES.contains(source.getName())) { @SuppressWarnings("rawtypes") EnumerablePropertySource enumerable = (EnumerablePropertySource) source; for (String name : enumerable.getPropertyNames()) { Object value = source.getProperty(name); if (value instanceof String) { // Unresolved placeholders are legal. value = environment.resolvePlaceholders((String) value); } result.put(name, value); } } } } return result; }
From source file:org.springframework.cloud.bootstrap.encrypt.EnvironmentDecryptApplicationInitializer.java
private void decrypt(PropertySource<?> source, Map<String, Object> overrides) { if (source instanceof EnumerablePropertySource) { EnumerablePropertySource<?> enumerable = (EnumerablePropertySource<?>) source; for (String key : enumerable.getPropertyNames()) { String value = source.getProperty(key).toString(); if (value.startsWith("{cipher}")) { value = value.substring("{cipher}".length()); try { value = encryptor.decrypt(value); if (logger.isDebugEnabled()) { logger.debug("Decrypted: key=" + key); }//from w w w . j a va 2 s . co m } catch (Exception e) { String message = "Cannot decrypt: key=" + key; if (failOnError) { throw new IllegalStateException(message, e); } if (logger.isDebugEnabled()) { logger.warn(message, e); } else { logger.warn(message); } // Set value to empty to avoid making a password out of the // cipher text value = ""; } overrides.put(key, value); } } } else if (source instanceof CompositePropertySource) { for (PropertySource<?> nested : ((CompositePropertySource) source).getPropertySources()) { decrypt(nested, overrides); } } }
From source file:org.springframework.cloud.bootstrap.encrypt.EnvironmentDecryptApplicationListener.java
private void decrypt(PropertySource<?> source, Map<String, Object> overrides) { if (source instanceof EnumerablePropertySource) { EnumerablePropertySource<?> enumerable = (EnumerablePropertySource<?>) source; for (String key : enumerable.getPropertyNames()) { String value = source.getProperty(key).toString(); if (value.startsWith("{cipher}")) { value = value.substring("{cipher}".length()); try { value = encryptor.decrypt(value); if (logger.isDebugEnabled()) { logger.debug("Decrypted: key=" + key); }//from ww w . ja va2 s. co m } catch (Exception e) { String message = "Cannot decrypt: key=" + key; if (failOnError) { throw new IllegalStateException(message, e); } if (logger.isDebugEnabled()) { logger.warn(message, e); } else { logger.warn(message); } // Set value to empty to avoid making a password out of the // cipher text value = ""; } overrides.put(key, value); } } } else if (source instanceof CompositePropertySource) { try { @SuppressWarnings("unchecked") Set<PropertySource<?>> sources = (Set<PropertySource<?>>) propertySourcesField.get(source); for (PropertySource<?> nested : sources) { decrypt(nested, overrides); } } catch (IllegalAccessException e) { return; } } }