@Before public void setUp() { envMap = new HashMap<>(); ps = new SystemEnvironmentPropertySource("sysEnv", envMap); }
/** * Return {@code true} if a property with the given name or any underscore/uppercase variant * thereof exists in this property source. */ @Override public boolean containsProperty(String name) { return (getProperty(name) != null); }
/** * This implementation returns {@code true} if a property with the given name or * any underscore/uppercase variant thereof exists in this property source. */ @Override @Nullable public Object getProperty(String name) { String actualName = resolvePropertyName(name); if (logger.isDebugEnabled() && !name.equals(actualName)) { logger.debug("PropertySource '" + getName() + "' does not contain property '" + name + "', but found equivalent '" + actualName + "'"); } return super.getProperty(actualName); }
private boolean containsKey(String name) { return (isSecurityManagerPresent() ? this.source.keySet().contains(name) : this.source.containsKey(name)); }
/** * Check to see if this property source contains a property with the given name, or * any underscore / uppercase variation thereof. Return the resolved name if one is * found or otherwise the original name. Never returns {@code null}. */ protected final String resolvePropertyName(String name) { Assert.notNull(name, "Property name must not be null"); String resolvedName = checkPropertyName(name); if (resolvedName != null) { return resolvedName; } String uppercasedName = name.toUpperCase(); if (!name.equals(uppercasedName)) { resolvedName = checkPropertyName(uppercasedName); if (resolvedName != null) { return resolvedName; } } return name; }
@Nullable private String checkPropertyName(String name) { // Check name as-is if (containsKey(name)) { return name; } // Check name with just dots replaced String noDotName = name.replace('.', '_'); if (!name.equals(noDotName) && containsKey(noDotName)) { return noDotName; } // Check name with just hyphens replaced String noHyphenName = name.replace('-', '_'); if (!name.equals(noHyphenName) && containsKey(noHyphenName)) { return noHyphenName; } // Check name with dots and hyphens replaced String noDotNoHyphenName = noDotName.replace('-', '_'); if (!noDotName.equals(noDotNoHyphenName) && containsKey(noDotNoHyphenName)) { return noDotNoHyphenName; } // Give up return null; }
/** * Return true if a property with the given name or any underscore/uppercase variant * thereof exists in this property source. */ @Override public boolean containsProperty(String name) { return resolvePropertyName(name) != null; }
private boolean containsKey(String name) { return (isSecurityManagerPresent() ? this.source.keySet().contains(name) : this.source.containsKey(name)); }
/** * Check to see if this property source contains a property with the given name, or * any underscore / uppercase variation thereof. Return the resolved name if one is * found or otherwise the original name. Never returns {@code null}. */ protected final String resolvePropertyName(String name) { Assert.notNull(name, "Property name must not be null"); String resolvedName = checkPropertyName(name); if (resolvedName != null) { return resolvedName; } String uppercasedName = name.toUpperCase(); if (!name.equals(uppercasedName)) { resolvedName = checkPropertyName(uppercasedName); if (resolvedName != null) { return resolvedName; } } return name; }
@Nullable private String checkPropertyName(String name) { // Check name as-is if (containsKey(name)) { return name; } // Check name with just dots replaced String noDotName = name.replace('.', '_'); if (!name.equals(noDotName) && containsKey(noDotName)) { return noDotName; } // Check name with just hyphens replaced String noHyphenName = name.replace('-', '_'); if (!name.equals(noHyphenName) && containsKey(noHyphenName)) { return noHyphenName; } // Check name with dots and hyphens replaced String noDotNoHyphenName = noDotName.replace('-', '_'); if (!noDotName.equals(noDotNoHyphenName) && containsKey(noDotNoHyphenName)) { return noDotNoHyphenName; } // Give up return null; }
/** * Customize the set of property sources with those appropriate for any standard * Java environment: * <ul> * <li>{@value #SYSTEM_PROPERTIES_PROPERTY_SOURCE_NAME} * <li>{@value #SYSTEM_ENVIRONMENT_PROPERTY_SOURCE_NAME} * </ul> * <p>Properties present in {@value #SYSTEM_PROPERTIES_PROPERTY_SOURCE_NAME} will * take precedence over those in {@value #SYSTEM_ENVIRONMENT_PROPERTY_SOURCE_NAME}. * @see AbstractEnvironment#customizePropertySources(MutablePropertySources) * @see #getSystemProperties() * @see #getSystemEnvironment() */ @Override protected void customizePropertySources(MutablePropertySources propertySources) { propertySources.addLast(new MapPropertySource(SYSTEM_PROPERTIES_PROPERTY_SOURCE_NAME, getSystemProperties())); propertySources.addLast(new SystemEnvironmentPropertySource(SYSTEM_ENVIRONMENT_PROPERTY_SOURCE_NAME, getSystemEnvironment())); }
/** * This implementation returns {@code true} if a property with the given name or * any underscore/uppercase variant thereof exists in this property source. */ @Override @Nullable public Object getProperty(String name) { String actualName = resolvePropertyName(name); if (logger.isDebugEnabled() && !name.equals(actualName)) { logger.debug("PropertySource '" + getName() + "' does not contain property '" + name + "', but found equivalent '" + actualName + "'"); } return super.getProperty(actualName); }
/** * Return {@code true} if a property with the given name or any underscore/uppercase variant * thereof exists in this property source. */ @Override public boolean containsProperty(String name) { return (getProperty(name) != null); }
private boolean containsKey(String name) { return (isSecurityManagerPresent() ? this.source.keySet().contains(name) : this.source.containsKey(name)); }
/** * Check to see if this property source contains a property with the given name, or * any underscore / uppercase variation thereof. Return the resolved name if one is * found or otherwise the original name. Never returns {@code null}. */ protected final String resolvePropertyName(String name) { Assert.notNull(name, "Property name must not be null"); String resolvedName = checkPropertyName(name); if (resolvedName != null) { return resolvedName; } String uppercasedName = name.toUpperCase(); if (!name.equals(uppercasedName)) { resolvedName = checkPropertyName(uppercasedName); if (resolvedName != null) { return resolvedName; } } return name; }
@Nullable private String checkPropertyName(String name) { // Check name as-is if (containsKey(name)) { return name; } // Check name with just dots replaced String noDotName = name.replace('.', '_'); if (!name.equals(noDotName) && containsKey(noDotName)) { return noDotName; } // Check name with just hyphens replaced String noHyphenName = name.replace('-', '_'); if (!name.equals(noHyphenName) && containsKey(noHyphenName)) { return noHyphenName; } // Check name with dots and hyphens replaced String noDotNoHyphenName = noDotName.replace('-', '_'); if (!noDotName.equals(noDotNoHyphenName) && containsKey(noDotNoHyphenName)) { return noDotNoHyphenName; } // Give up return null; }
@Test @SuppressWarnings("serial") public void withSecurityConstraints() throws Exception { envMap = new HashMap<String, Object>() { @Override public boolean containsKey(Object key) { throw new UnsupportedOperationException(); } @Override public Set<String> keySet() { return new HashSet<>(super.keySet()); } }; envMap.put("A_KEY", "a_value"); ps = new SystemEnvironmentPropertySource("sysEnv", envMap) { @Override protected boolean isSecurityManagerPresent() { return true; } }; assertThat(ps.containsProperty("A_KEY"), equalTo(true)); assertThat(ps.getProperty("A_KEY"), equalTo((Object)"a_value")); }
/** * {@inheritDoc} * <p>This implementation returns {@code true} if a property with the given name or * any underscore/uppercase variant thereof exists in this property source. */ @Override public Object getProperty(String name) { Assert.notNull(name, "property name must not be null"); String actualName = resolvePropertyName(name); if (actualName == null) { // at this point we know the property does not exist return null; } if (logger.isDebugEnabled() && !name.equals(actualName)) { logger.debug(String.format( "PropertySource [%s] does not contain '%s', but found equivalent '%s'", this.getName(), name, actualName)); } return super.getProperty(actualName); }
@Override public Object getProperty(String name) { if (this.sources.containsProperty(name)) { return this.sources.getProperty(name); } return super.getProperty(name); }
private boolean containsKey(String name) { return (isSecurityManagerPresent() ? this.source.keySet().contains(name) : this.source.containsKey(name)); }