|
19 | 19 | import java.util.Map; |
20 | 20 |
|
21 | 21 | import org.springframework.util.Assert; |
22 | | -import org.springframework.util.ObjectUtils; |
23 | 22 |
|
24 | 23 | /** |
25 | 24 | * Specialization of {@link MapPropertySource} designed for use with |
|
56 | 55 | * and all its subclasses. |
57 | 56 | * |
58 | 57 | * @author Chris Beams |
| 58 | + * @author Juergen Hoeller |
59 | 59 | * @since 3.1 |
60 | 60 | * @see StandardEnvironment |
61 | 61 | * @see AbstractEnvironment#getSystemEnvironment() |
62 | 62 | * @see AbstractEnvironment#ACTIVE_PROFILES_PROPERTY_NAME |
63 | 63 | */ |
64 | 64 | public class SystemEnvironmentPropertySource extends MapPropertySource { |
65 | 65 |
|
66 | | - /** if SecurityManager scenarios mean that property access should be via getPropertyNames() */ |
67 | | - private boolean usePropertyNames; |
68 | | - |
69 | 66 | /** |
70 | 67 | * Create a new {@code SystemEnvironmentPropertySource} with the given name and |
71 | 68 | * delegating to the given {@code MapPropertySource}. |
@@ -105,48 +102,37 @@ public Object getProperty(String name) { |
105 | 102 | */ |
106 | 103 | private String resolvePropertyName(String name) { |
107 | 104 | Assert.notNull(name, "Property name must not be null"); |
108 | | - try { |
109 | | - String[] propertyNames = (this.usePropertyNames ? getPropertyNames() : null); |
110 | | - if (containsProperty(propertyNames, name)) { |
111 | | - return name; |
112 | | - } |
113 | | - |
114 | | - String usName = name.replace('.', '_'); |
115 | | - if (!name.equals(usName) && containsProperty(propertyNames, usName)) { |
116 | | - return usName; |
117 | | - } |
118 | | - |
119 | | - String ucName = name.toUpperCase(); |
120 | | - if (!name.equals(ucName)) { |
121 | | - if (containsProperty(propertyNames, ucName)) { |
122 | | - return ucName; |
123 | | - } |
124 | | - else { |
125 | | - String usUcName = ucName.replace('.', '_'); |
126 | | - if (!ucName.equals(usUcName) && containsProperty(propertyNames, usUcName)) { |
127 | | - return usUcName; |
128 | | - } |
129 | | - } |
130 | | - } |
131 | | - |
| 105 | + if (containsKey(name)) { |
132 | 106 | return name; |
133 | 107 | } |
134 | | - catch (RuntimeException ex) { |
135 | | - if (this.usePropertyNames) { |
136 | | - throw ex; |
| 108 | + |
| 109 | + String usName = name.replace('.', '_'); |
| 110 | + if (!name.equals(usName) && containsKey(usName)) { |
| 111 | + return usName; |
| 112 | + } |
| 113 | + |
| 114 | + String ucName = name.toUpperCase(); |
| 115 | + if (!name.equals(ucName)) { |
| 116 | + if (containsKey(ucName)) { |
| 117 | + return ucName; |
137 | 118 | } |
138 | 119 | else { |
139 | | - this.usePropertyNames = true; |
140 | | - return resolvePropertyName(name); |
| 120 | + String usUcName = ucName.replace('.', '_'); |
| 121 | + if (!ucName.equals(usUcName) && containsKey(usUcName)) { |
| 122 | + return usUcName; |
| 123 | + } |
141 | 124 | } |
142 | 125 | } |
| 126 | + |
| 127 | + return name; |
143 | 128 | } |
144 | 129 |
|
145 | | - private boolean containsProperty(String[] propertyNames, String name) { |
146 | | - if (propertyNames == null) { |
147 | | - return super.containsProperty(name); |
148 | | - } |
149 | | - return ObjectUtils.containsElement(propertyNames, name); |
| 130 | + private boolean containsKey(String name) { |
| 131 | + return (isSecurityManagerPresent() ? this.source.keySet().contains(name) : this.source.containsKey(name)); |
| 132 | + } |
| 133 | + |
| 134 | + protected boolean isSecurityManagerPresent() { |
| 135 | + return (System.getSecurityManager() != null); |
150 | 136 | } |
151 | 137 |
|
152 | 138 | } |
0 commit comments