Skip to content

Commit d731aba

Browse files
author
Darin Howard
committed
Updating databind dependency. Updating properties reader to work w/ file paths and resource paths.
1 parent 4ecdabf commit d731aba

5 files changed

Lines changed: 153 additions & 81 deletions

File tree

pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@
7373
<dependency>
7474
<groupId>com.fasterxml.jackson.core</groupId>
7575
<artifactId>jackson-databind</artifactId>
76-
<version>2.8.11.1</version>
76+
<version>2.8.11.3</version>
7777
</dependency>
7878

7979
<!-- Provided dependencies -->

src/main/java/com/stackify/api/common/ApiConfigurations.java

Lines changed: 38 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -15,14 +15,12 @@
1515
*/
1616
package com.stackify.api.common;
1717

18-
import java.io.File;
19-
import java.io.FileReader;
20-
import java.net.URL;
21-
import java.util.Properties;
22-
18+
import com.stackify.api.common.util.PropertyUtil;
2319
import org.slf4j.Logger;
2420
import org.slf4j.LoggerFactory;
2521

22+
import java.util.Map;
23+
2624
/**
2725
* ApiConfigurations
2826
* @author Eric Martin
@@ -45,7 +43,7 @@ public class ApiConfigurations {
4543
public static ApiConfiguration fromPropertiesWithOverrides(final String apiUrl, final String apiKey, final String application, final String environment) {
4644
return fromPropertiesWithOverrides(apiUrl, apiKey, application, environment, null);
4745
}
48-
46+
4947
/**
5048
* Explicitly configure the API
5149
* @param apiUrl API URL
@@ -57,7 +55,7 @@ public static ApiConfiguration fromPropertiesWithOverrides(final String apiUrl,
5755
*/
5856
public static ApiConfiguration fromPropertiesWithOverrides(final String apiUrl, final String apiKey, final String application, final String environment, final String allowComDotStackify) {
5957
ApiConfiguration props = ApiConfigurations.fromProperties();
60-
58+
6159
String mergedApiUrl = ((apiUrl != null) && (0 < apiUrl.length())) ? apiUrl : props.getApiUrl();
6260
String mergedApiKey = ((apiKey != null) && (0 < apiKey.length())) ? apiKey : props.getApiKey();
6361
String mergedApplication = ((application != null) && (0 < application.length())) ? application : props.getApplication();
@@ -70,70 +68,54 @@ public static ApiConfiguration fromPropertiesWithOverrides(final String apiUrl,
7068
builder.environment(mergedEnvironment);
7169
builder.envDetail(EnvironmentDetails.getEnvironmentDetail(mergedApplication, mergedEnvironment));
7270
builder.allowComDotStackify(Boolean.valueOf(allowComDotStackify));
73-
71+
7472
return builder.build();
7573
}
76-
74+
7775
/**
7876
* @return ApiConfiguration read from the stackify-api.properties file
7977
*/
8078
public static ApiConfiguration fromProperties() {
81-
79+
8280
ApiConfiguration.Builder builder = ApiConfiguration.newBuilder();
8381

84-
FileReader confFileReader = null;
85-
8682
try {
87-
URL confFileUrl = ApiConfigurations.class.getResource("/stackify-api.properties");
88-
89-
if (confFileUrl != null) {
90-
File confFile = new File(confFileUrl.toURI());
91-
92-
if (confFile.exists()) {
93-
94-
confFileReader = new FileReader(confFile);
95-
96-
Properties confProps = new Properties();
97-
confProps.load(confFileReader);
98-
99-
String apiUrl = null;
100-
101-
if (confProps.containsKey("stackify.apiUrl")) {
102-
apiUrl = confProps.getProperty("stackify.apiUrl");
103-
}
104-
105-
String httpProxyHost = confProps.getProperty("stackify.httpProxyHost");
106-
String httpProxyPort = confProps.getProperty("stackify.httpProxyPort");
107-
String apiKey = confProps.getProperty("stackify.apiKey");
108-
String application = confProps.getProperty("stackify.application");
109-
String environment = confProps.getProperty("stackify.environment");
110-
Boolean skipJson = Boolean.parseBoolean(confProps.getProperty("stackify.skipJson", "false"));
111-
112-
builder.httpProxyHost(httpProxyHost);
113-
builder.httpProxyPort(httpProxyPort);
114-
builder.apiUrl(apiUrl);
115-
builder.apiKey(apiKey);
116-
builder.application(application);
117-
builder.environment(environment);
118-
builder.envDetail(EnvironmentDetails.getEnvironmentDetail(application, environment));
119-
builder.skipJson(skipJson);
120-
}
83+
84+
Map<String, String> properties = PropertyUtil.read("/stackify-api.properties");
85+
86+
String apiUrl = null;
87+
88+
if (properties.containsKey("stackify.apiUrl")) {
89+
apiUrl = properties.get("stackify.apiUrl");
12190
}
91+
92+
String httpProxyHost = properties.get("stackify.httpProxyHost");
93+
String httpProxyPort = properties.get("stackify.httpProxyPort");
94+
String apiKey = properties.get("stackify.apiKey");
95+
String application = properties.get("stackify.application");
96+
String environment = properties.get("stackify.environment");
97+
98+
boolean skipJson = false;
99+
if (properties.containsKey("stackify.skipJson")) {
100+
skipJson = Boolean.parseBoolean(properties.get("stackify.skipJson"));
101+
}
102+
103+
builder.httpProxyHost(httpProxyHost);
104+
builder.httpProxyPort(httpProxyPort);
105+
builder.apiUrl(apiUrl);
106+
builder.apiKey(apiKey);
107+
builder.application(application);
108+
builder.environment(environment);
109+
builder.envDetail(EnvironmentDetails.getEnvironmentDetail(application, environment));
110+
builder.skipJson(skipJson);
111+
122112
} catch (Throwable t) {
123113
LOGGER.error("Exception reading stackify-api.properties configuration file", t);
124-
} finally {
125-
if (confFileReader != null) {
126-
try {
127-
confFileReader.close();
128-
} catch (Throwable t) {
129-
LOGGER.info("Exception closing stackify-api.properties configuration file", t);
130-
}
131-
}
132114
}
133-
115+
134116
return builder.build();
135117
}
136-
118+
137119
/**
138120
* Hidden to prevent construction
139121
*/

src/main/java/com/stackify/api/common/mask/MaskerConfiguration.java

Lines changed: 5 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -21,22 +21,10 @@
2121
public class MaskerConfiguration {
2222

2323
public static Masker fromProperties() {
24-
25-
String propertiesFilePath = null;
26-
27-
URL confFileUrl = MaskerConfiguration.class.getResource("/stackify-api.properties");
28-
if (confFileUrl != null) {
29-
try {
30-
propertiesFilePath = confFileUrl.toURI().getPath();
31-
} catch (URISyntaxException e) {
32-
log.warn(e.getMessage(), e);
33-
}
34-
}
35-
36-
return fromProperties(propertiesFilePath);
24+
return fromProperties("/stackify-api.properties");
3725
}
3826

39-
public static Masker fromProperties(String propertiesFilePath) {
27+
public static Masker fromProperties(String path) {
4028

4129
Masker masker = new Masker();
4230

@@ -46,9 +34,9 @@ public static Masker fromProperties(String propertiesFilePath) {
4634

4735
try {
4836

49-
if (propertiesFilePath != null) {
37+
if (path != null) {
5038

51-
Map<String, String> map = PropertyUtil.read(propertiesFilePath);
39+
Map<String, String> map = PropertyUtil.read(path);
5240

5341
// masker defaults to disabled
5442
if (!map.containsKey("stackify.log.mask.enabled") ||
@@ -79,7 +67,7 @@ public static Masker fromProperties(String propertiesFilePath) {
7967
}
8068

8169
} catch (Throwable t) {
82-
log.error("Exception reading " + propertiesFilePath + " configuration file", t);
70+
log.error("Exception reading " + path + " configuration file", t);
8371
}
8472

8573
return masker;

src/main/java/com/stackify/api/common/util/PropertyUtil.java

Lines changed: 63 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55

66
import java.io.File;
77
import java.io.FileInputStream;
8+
import java.io.InputStream;
89
import java.util.HashMap;
910
import java.util.Map;
1011
import java.util.Properties;
@@ -15,12 +16,12 @@
1516
@Slf4j
1617
public class PropertyUtil {
1718

18-
public static Map<String, String> readAndMerge(@NonNull String... files) {
19+
public static Map<String, String> readAndMerge(@NonNull final String... paths) {
1920

2021
Map<String, String> mergedMap = new HashMap<String, String>();
2122

22-
for (String file : files) {
23-
Map<String, String> map = read(file);
23+
for (String path : paths) {
24+
Map<String, String> map = read(path);
2425
for (Map.Entry<String, String> entry : map.entrySet()) {
2526
if (!mergedMap.containsKey(entry.getKey())) {
2627
mergedMap.put(entry.getKey(), entry.getValue());
@@ -31,14 +32,69 @@ public static Map<String, String> readAndMerge(@NonNull String... files) {
3132
return mergedMap;
3233
}
3334

34-
public static Map<String, String> read(@NonNull String file) {
35+
/**
36+
* Loads properties with given path - will load as file is able or classpath resource
37+
*/
38+
public static Properties loadProperties(final String path) {
39+
40+
if (path != null) {
41+
// try as file
42+
try {
43+
File file = new File(path);
44+
if (file.exists()) {
45+
try {
46+
Properties p = new Properties();
47+
p.load(new FileInputStream(file));
48+
return p;
49+
} catch (Exception e) {
50+
log.error("Error loading properties from file: " + path);
51+
}
52+
}
53+
} catch (Throwable e) {
54+
log.debug(e.getMessage(), e);
55+
}
56+
57+
// try as resource
58+
InputStream inputStream = null;
59+
try {
60+
inputStream = PropertyUtil.class.getResourceAsStream(path);
61+
if (inputStream != null) {
62+
try {
63+
Properties p = new Properties();
64+
p.load(inputStream);
65+
return p;
66+
} catch (Exception e) {
67+
log.error("Error loading properties from resource: " + path);
68+
}
69+
}
70+
} catch (Exception e) {
71+
log.error("Error loading properties from resource: " + path);
72+
} finally {
73+
if (inputStream != null) {
74+
try {
75+
inputStream.close();
76+
} catch (Throwable t) {
77+
log.debug("Error closing: " + path, t);
78+
}
79+
}
80+
}
81+
}
82+
83+
84+
// return empty Properties by default
85+
return new Properties();
86+
}
87+
88+
/**
89+
* Reads properties from file path or classpath
90+
*/
91+
public static Map<String, String> read(final String path) {
3592

3693
Map<String, String> map = new HashMap<String, String>();
3794

38-
if (new File(file).exists()) {
95+
if (path != null) {
3996
try {
40-
Properties p = new Properties();
41-
p.load(new FileInputStream(new File(file)));
97+
Properties p = loadProperties(path);
4298

4399
for (Object key : p.keySet()) {
44100

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
package com.stackify.api.common.util;
2+
3+
import org.junit.Assert;
4+
import org.junit.Test;
5+
6+
import java.io.File;
7+
import java.util.Map;
8+
9+
public class PropertyUtilTest {
10+
11+
@Test
12+
public void readResource() {
13+
14+
// test via resource success
15+
Map<String, String> mapSuccess = PropertyUtil.read("/stackify-api.properties");
16+
Assert.assertEquals("url", mapSuccess.get("stackify.apiUrl"));
17+
Assert.assertEquals("key", mapSuccess.get("stackify.apiKey"));
18+
Assert.assertEquals("app", mapSuccess.get("stackify.application"));
19+
Assert.assertEquals("env", mapSuccess.get("stackify.environment"));
20+
21+
// test via resource failure
22+
Map<String, String> mapFailure = PropertyUtil.read("/stackify-api-failure.properties");
23+
Assert.assertEquals(0, mapFailure.size());
24+
}
25+
26+
@Test
27+
public void readFile() throws Exception {
28+
29+
// test via file success
30+
Map<String, String> mapSuccess = PropertyUtil.read(new File(PropertyUtilTest.class.getResource("/stackify-api.properties").toURI().getPath()).getPath());
31+
Assert.assertEquals("url", mapSuccess.get("stackify.apiUrl"));
32+
Assert.assertEquals("key", mapSuccess.get("stackify.apiKey"));
33+
Assert.assertEquals("app", mapSuccess.get("stackify.application"));
34+
Assert.assertEquals("env", mapSuccess.get("stackify.environment"));
35+
36+
// test via file failure
37+
Map<String, String> mapFailure = PropertyUtil.read("/root/file/does/not/exist");
38+
Assert.assertEquals(0, mapFailure.size());
39+
}
40+
41+
@Test
42+
public void readNull() throws Exception {
43+
Map<String, String> mapFailure = PropertyUtil.read(null);
44+
Assert.assertEquals(0, mapFailure.size());
45+
}
46+
}

0 commit comments

Comments
 (0)