Skip to content

Commit 3a1049f

Browse files
Merge pull request #69 from bugsnag/remove-guava-dep
Remove dependency on Guava
2 parents 5404a2f + ca5f77e commit 3a1049f

9 files changed

Lines changed: 165 additions & 87 deletions

File tree

build.gradle

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@ repositories {
2626

2727
dependencies {
2828
compile 'com.fasterxml.jackson.core:jackson-databind:2.9.1'
29-
compile 'com.google.guava:guava:23.1-android'
3029
compile 'org.slf4j:slf4j-api:1.7.25'
3130
compileOnly 'javax.servlet:javax.servlet-api:3.1.0'
3231

gradle/wrapper/gradle-wrapper.jar

1.38 KB
Binary file not shown.
Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
1-
#Fri Sep 30 22:16:53 BST 2016
21
distributionBase=GRADLE_USER_HOME
32
distributionPath=wrapper/dists
43
zipStoreBase=GRADLE_USER_HOME
54
zipStorePath=wrapper/dists
6-
distributionUrl=https\://services.gradle.org/distributions/gradle-4.1-bin.zip
5+
distributionUrl=https\://services.gradle.org/distributions/gradle-4.4.1-bin.zip

gradlew

Lines changed: 17 additions & 9 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

gradlew.bat

Lines changed: 0 additions & 6 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/main/java/com/bugsnag/Report.java

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
package com.bugsnag;
22

33
import com.bugsnag.serialization.Expose;
4-
import com.bugsnag.util.FilterTransformer;
54

6-
import com.google.common.collect.Maps;
5+
import com.bugsnag.util.FilteredMap;
76

87
import java.util.ArrayList;
8+
import java.util.Arrays;
99
import java.util.List;
1010
import java.util.Map;
1111

@@ -110,8 +110,7 @@ public Map getUser() {
110110

111111
@Expose
112112
public Map getMetaData() {
113-
Map<String, Object> metaDataMap = diagnostics.metaData;
114-
return Maps.transformEntries(metaDataMap, new FilterTransformer(config.filters));
113+
return new FilteredMap(diagnostics.metaData, Arrays.asList(config.filters));
115114
}
116115

117116
/**

src/main/java/com/bugsnag/callbacks/DeviceCallback.java

Lines changed: 38 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -2,47 +2,60 @@
22

33
import com.bugsnag.Report;
44

5-
import com.google.common.base.Supplier;
6-
import com.google.common.base.Suppliers;
7-
85
import java.net.InetAddress;
96
import java.net.UnknownHostException;
107
import java.util.Locale;
118

129
public class DeviceCallback implements Callback {
13-
private static final Supplier<String> hostnameCache =
14-
Suppliers.memoize(new Supplier<String>() {
1510

16-
public String get() {
17-
// Windows always sets COMPUTERNAME
18-
if (System.getProperty("os.name").startsWith("Windows")) {
19-
return System.getenv("COMPUTERNAME");
20-
}
11+
private static volatile String hostname;
12+
private static transient volatile boolean hostnameInitialised;
13+
private static final Object lock = new Object();
2114

22-
// Try the HOSTNAME env variable (most unix systems)
23-
String hostname = System.getenv("HOSTNAME");
24-
if (hostname != null) {
25-
return hostname;
15+
/**
16+
* Memoises the hostname, as lookup can be expensive
17+
*/
18+
private static String getHostnameValue() {
19+
if (!hostnameInitialised) {
20+
synchronized (lock) {
21+
if (!hostnameInitialised) {
22+
hostname = lookupHostname();
23+
hostnameInitialised = true;
2624
}
27-
28-
// Resort to dns hostname lookup
29-
try {
30-
return InetAddress.getLocalHost().getHostName();
31-
} catch (UnknownHostException ex) {
32-
// Give up
33-
}
34-
return null;
3525
}
36-
});
26+
}
27+
return hostname;
28+
}
29+
30+
private static String lookupHostname() {
31+
// Windows always sets COMPUTERNAME
32+
if (System.getProperty("os.name").startsWith("Windows")) {
33+
return System.getenv("COMPUTERNAME");
34+
}
35+
36+
// Try the HOSTNAME env variable (most unix systems)
37+
String hostname = System.getenv("HOSTNAME");
38+
if (hostname != null) {
39+
return hostname;
40+
}
41+
42+
// Resort to dns hostname lookup
43+
try {
44+
return InetAddress.getLocalHost().getHostName();
45+
} catch (UnknownHostException ex) {
46+
// Give up
47+
}
48+
return null;
49+
}
3750

3851
public static void initializeCache() {
39-
hostnameCache.get();
52+
getHostnameValue();
4053
}
4154

4255
@Override
4356
public void beforeNotify(Report report) {
4457
report
45-
.setDeviceInfo("hostname", hostnameCache.get())
58+
.setDeviceInfo("hostname", getHostnameValue())
4659
.setDeviceInfo("osName", System.getProperty("os.name"))
4760
.setDeviceInfo("osVersion", System.getProperty("os.version"))
4861
.setDeviceInfo("osArch", System.getProperty("os.arch"))

src/main/java/com/bugsnag/util/FilterTransformer.java

Lines changed: 0 additions & 40 deletions
This file was deleted.
Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
package com.bugsnag.util;
2+
3+
4+
import java.util.ArrayList;
5+
import java.util.Collection;
6+
import java.util.Map;
7+
import java.util.Set;
8+
9+
/**
10+
* Decorates a map, and alters the return value of {@link #get(Object)} if the key matches a filter.
11+
*/
12+
public class FilteredMap implements Map<String, Object> {
13+
14+
private static final String FILTERED_PLACEHOLDER = "[FILTERED]";
15+
16+
private final Map<String, Object> map;
17+
private final Collection<String> keyFilters = new ArrayList<String>();
18+
19+
public FilteredMap(Map<String, Object> map, Collection<String> keyFilters) {
20+
this.map = map;
21+
this.keyFilters.addAll(keyFilters);
22+
}
23+
24+
@Override
25+
public int size() {
26+
return map.size();
27+
}
28+
29+
@Override
30+
public boolean isEmpty() {
31+
return map.isEmpty();
32+
}
33+
34+
@Override
35+
public boolean containsKey(Object key) {
36+
return map.containsKey(key);
37+
}
38+
39+
@Override
40+
public boolean containsValue(Object value) {
41+
return values().contains(value);
42+
}
43+
44+
@Override
45+
public Object get(Object key) {
46+
Object obj = map.get(key);
47+
return obj != null ? transformEntry(key, obj) : null;
48+
}
49+
50+
@Override
51+
public Object put(String key, Object value) {
52+
return map.put(key, value);
53+
}
54+
55+
@Override
56+
public Object remove(Object key) {
57+
return map.remove(key);
58+
}
59+
60+
@Override
61+
public void putAll(Map<? extends String, ?> mapValues) {
62+
map.putAll(mapValues);
63+
}
64+
65+
@Override
66+
public void clear() {
67+
map.clear();
68+
}
69+
70+
@Override
71+
public Set<String> keySet() {
72+
return map.keySet();
73+
}
74+
75+
@Override
76+
public Collection<Object> values() {
77+
return map.values();
78+
}
79+
80+
@Override
81+
public Set<Entry<String, Object>> entrySet() {
82+
return map.entrySet();
83+
}
84+
85+
private Object transformEntry(Object key, Object value) {
86+
if (value instanceof Map) {
87+
//noinspection unchecked
88+
return new FilteredMap((Map<String, Object>) value, keyFilters);
89+
}
90+
return shouldFilterKey((String) key) ? FILTERED_PLACEHOLDER : value;
91+
}
92+
93+
private boolean shouldFilterKey(String key) {
94+
if (keyFilters == null || key == null) {
95+
return false;
96+
}
97+
98+
for (String filter : keyFilters) {
99+
if (key.contains(filter)) {
100+
return true;
101+
}
102+
}
103+
return false;
104+
}
105+
106+
}

0 commit comments

Comments
 (0)