Skip to content

Commit 25cf418

Browse files
Craigacpjhalexand
authored andcommitted
Fix for importing system properties into GlobalProperties.
1 parent e027906 commit 25cf418

File tree

4 files changed

+91
-9
lines changed

4 files changed

+91
-9
lines changed

olcut-core/src/main/java/com/oracle/labs/mlrg/olcut/config/property/GlobalProperties.java

Lines changed: 29 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -44,32 +44,56 @@
4444
*/
4545
public class GlobalProperties extends ImmutableGlobalProperties {
4646

47+
/**
48+
* Creates a GlobalProperties which only contains the built in properties.
49+
*/
4750
public GlobalProperties() {
4851
super();
4952
}
5053

54+
/**
55+
* Copies the supplied GlobalProperties.
56+
* @param globalProperties The properties to copy.
57+
*/
5158
public GlobalProperties(GlobalProperties globalProperties) {
5259
super(globalProperties);
5360
}
5461

5562
/**
56-
* Imports the system properties into GlobalProperties.
63+
* Imports the system properties into this GlobalProperties.
5764
*/
5865
public final void importSystemProperties() {
5966
Properties props = AccessController.doPrivileged((PrivilegedAction<Properties>) System::getProperties);
67+
importProperties(props);
68+
}
69+
70+
/**
71+
* Imports the supplied properties.
72+
*
73+
* @param props The properties to import.
74+
*/
75+
// Exposed for unit testing
76+
void importProperties(Properties props) {
6077
for (Map.Entry<Object,Object> e : props.entrySet()) {
6178
// These two calls use .toString rather than a cast
62-
// because sometimes Fusion inserts Integers into the system properties.
79+
// because sometimes people insert Integers into the system properties.
6380
String param = e.getKey().toString();
6481
String value = e.getValue() == null ? "null" : e.getValue().toString();
65-
setValue(param, value);
82+
// Checks to see if the system property could be a valid global property,
83+
// only insert it if it is.
84+
// Bypasses setValue as that throws an exception if the key isn't well formed.
85+
String testValue = "${" + param + "}";
86+
Matcher m = GlobalProperty.globalSymbolPattern.matcher(testValue);
87+
if (m.matches()) {
88+
map.put(param, new GlobalProperty(value));
89+
}
6690
}
6791
}
6892

6993
/**
7094
* Adds a value to this GlobalProperties. Throws PropertyException if the
7195
* name does not conform to the {@link GlobalProperty#globalSymbolPattern}.
72-
*
96+
* <p>
7397
* It overwrites values if they already exist.
7498
* @param propertyName The name of the new global property.
7599
* @param value The value for the new global property.
@@ -82,7 +106,7 @@ public void setValue(String propertyName, String value) throws PropertyException
82106
/**
83107
* Adds a value to this GlobalProperties. Throws PropertyException if the
84108
* name does not conform to the {@link GlobalProperty#globalSymbolPattern}.
85-
*
109+
* <p>
86110
* It overwrites values if they already exist.
87111
* @param propertyName The name of the new global property.
88112
* @param value The value for the new global property.

olcut-core/src/main/java/com/oracle/labs/mlrg/olcut/config/property/ImmutableGlobalProperties.java

Lines changed: 37 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
import com.oracle.labs.mlrg.olcut.config.PropertyException;
3232
import com.oracle.labs.mlrg.olcut.util.Util;
3333

34+
import java.util.Collections;
3435
import java.util.HashMap;
3536
import java.util.Iterator;
3637
import java.util.Map;
@@ -40,35 +41,58 @@
4041

4142
/**
4243
* A collection of GlobalProperties which can't be mutated.
44+
* <p>
45+
* In addition to the set properties, it also includes
46+
* preset properties. This set currently includes {@link #HOSTNAME}
47+
* which lazily resolves to the system hostname.
4348
*/
4449
public class ImmutableGlobalProperties implements Iterable<Map.Entry<String,GlobalProperty>> {
4550

51+
public static final String HOSTNAME = "gp.hostName";
52+
4653
/**
4754
* A set of distinguished properties that we would like to have.
4855
*/
49-
private static Map<String, GlobalProperty> distinguished = new HashMap<>();
56+
private static final Map<String, GlobalProperty> distinguished = new HashMap<>();
5057

5158
protected final HashMap<String, GlobalProperty> map;
5259

5360
static {
54-
distinguished.put("gp.hostName", new LazyGlobalProperty(Util::getHostName));
61+
distinguished.put(HOSTNAME, new LazyGlobalProperty(Util::getHostName));
5562
}
5663

64+
/**
65+
* Creates an ImmutableGlobalProperties.
66+
*/
5767
public ImmutableGlobalProperties() {
5868
this.map = new HashMap<>();
5969
}
6070

71+
/**
72+
* Creates an ImmutableGlobalProperties copy of the other properties.
73+
* @param globalProperties The properties to copy.
74+
*/
6175
public ImmutableGlobalProperties(GlobalProperties globalProperties) {
6276
this.map = new HashMap<>();
6377
for(String key : globalProperties.map.keySet()) {
6478
map.put(key, new GlobalProperty(globalProperties.get(key)));
6579
}
6680
}
6781

82+
/**
83+
* Creates an ImmutableGlobalProperties view of this map.
84+
* @param map The map to wrap.
85+
*/
6886
private ImmutableGlobalProperties(HashMap<String, GlobalProperty> map) {
6987
this.map = map;
7088
}
7189

90+
/**
91+
* Returns the GlobalProperty associated with the name, or
92+
* null if there is no such property.
93+
* @param propertyName The property name.
94+
* @return The GlobalProperty if found, or null.
95+
*/
7296
public GlobalProperty get(String propertyName) {
7397
GlobalProperty gp = map.get(propertyName);
7498
if(gp == null) {
@@ -127,10 +151,20 @@ public String replaceGlobalProperties(String instanceName,
127151
}
128152
}
129153

154+
/**
155+
* Returns a view over the property names.
156+
* @return The set of property names.
157+
*/
130158
public Set<String> keySet() {
131-
return map.keySet();
159+
return Collections.unmodifiableSet(map.keySet());
132160
}
133161

162+
/**
163+
* Returns an unmodifiable view of this GlobalProperties.
164+
* <p>
165+
* Changes in the underlying properties are reflected in this view.
166+
* @return An unmodifiable view of the properties.
167+
*/
134168
public ImmutableGlobalProperties getImmutableProperties() {
135169
return new ImmutableGlobalProperties(map);
136170
}

olcut-core/src/test/java/com/oracle/labs/mlrg/olcut/config/GlobalPropertyTest.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,6 @@
3030

3131
import com.oracle.labs.mlrg.olcut.config.io.ConfigLoaderException;
3232
import com.oracle.labs.mlrg.olcut.util.Util;
33-
import java.io.IOException;
3433
import org.junit.jupiter.api.AfterAll;
3534
import org.junit.jupiter.api.BeforeAll;
3635
import org.junit.jupiter.api.Test;
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package com.oracle.labs.mlrg.olcut.config.property;
2+
3+
import org.junit.jupiter.api.Test;
4+
5+
import java.util.Properties;
6+
7+
import static org.junit.jupiter.api.Assertions.assertEquals;
8+
import static org.junit.jupiter.api.Assertions.assertNull;
9+
10+
public class GlobalPropertiesTest {
11+
12+
@Test
13+
public void strangeSystemProperties() {
14+
Properties sysProps = new Properties();
15+
sysProps.setProperty("this property has spaces spaces are bad","some-value");
16+
sysProps.setProperty("this property has weird punctuation !@#$%^&&*","another-value");
17+
sysProps.setProperty("this.property.conforms.to.the.global.props.regex","also-a-value");
18+
19+
GlobalProperties gp = new GlobalProperties();
20+
gp.importProperties(sysProps);
21+
assertNull(gp.get("this property has spaces spaces are bad"));
22+
assertNull(gp.get("this property has weird punctuation !@#$%^&&*"));
23+
assertEquals(new GlobalProperty("also-a-value"),gp.get("this.property.conforms.to.the.global.props.regex"));
24+
}
25+
}

0 commit comments

Comments
 (0)