Skip to content

Commit d65def4

Browse files
authored
Collect FaaS info during handshake (#1156)
JAVA-4761
1 parent b22d339 commit d65def4

File tree

10 files changed

+603
-298
lines changed

10 files changed

+603
-298
lines changed

driver-core/src/main/com/mongodb/MongoDriverInformation.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -167,7 +167,7 @@ private List<String> prependToList(final List<String> stringList, final String v
167167
}
168168

169169
private Builder() {
170-
List<String> immutableEmptyList = Collections.unmodifiableList(Collections.emptyList());
170+
List<String> immutableEmptyList = Collections.emptyList();
171171
driverInformation = new MongoDriverInformation(immutableEmptyList, immutableEmptyList, immutableEmptyList);
172172
}
173173

driver-core/src/main/com/mongodb/internal/connection/ClientMetadataHelper.java

Lines changed: 184 additions & 95 deletions
Large diffs are not rendered by default.
Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
/*
2+
* Copyright 2008-present MongoDB, Inc.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package com.mongodb.client;
18+
19+
import com.mongodb.lang.Nullable;
20+
21+
import java.lang.reflect.Field;
22+
import java.util.Map;
23+
24+
import static java.lang.System.getenv;
25+
26+
@FunctionalInterface
27+
public interface WithWrapper {
28+
29+
void run(Runnable r);
30+
31+
static WithWrapper withWrapper() {
32+
return r -> r.run();
33+
}
34+
35+
default WithWrapper withEnvironmentVariable(final String name, @Nullable final String value) {
36+
return runnable -> {
37+
Map<String, String> innerMap = getEnvInnerMap();
38+
String original = innerMap.get(name);
39+
if (value == null) {
40+
innerMap.remove(name);
41+
} else {
42+
innerMap.put(name, value);
43+
}
44+
try {
45+
this.run(runnable);
46+
} finally {
47+
if (original == null) {
48+
innerMap.remove(name);
49+
} else {
50+
innerMap.put(name, original);
51+
}
52+
}
53+
};
54+
}
55+
56+
default WithWrapper withSystemProperty(final String name, final String value) {
57+
return runnable -> {
58+
String original = System.getProperty(name);
59+
System.setProperty(name, value);
60+
try {
61+
this.run(runnable);
62+
} finally {
63+
System.setProperty(name, original);
64+
}
65+
};
66+
}
67+
68+
static Map<String, String> getEnvInnerMap() {
69+
try {
70+
Map<String, String> env = getenv();
71+
Field field = env.getClass().getDeclaredField("m");
72+
field.setAccessible(true);
73+
@SuppressWarnings("unchecked")
74+
Map<String, String> result = (Map<String, String>) field.get(env);
75+
return result;
76+
} catch (IllegalAccessException | NoSuchFieldException e) {
77+
throw new RuntimeException(e);
78+
}
79+
}
80+
}

0 commit comments

Comments
 (0)