Skip to content

Commit 2a684bf

Browse files
committed
Add ClientMetadata entity.
JAVA-5854
1 parent 4b3065c commit 2a684bf

File tree

3 files changed

+55
-5
lines changed

3 files changed

+55
-5
lines changed
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
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.internal.connection;
18+
19+
import com.mongodb.MongoDriverInformation;
20+
import com.mongodb.lang.Nullable;
21+
import org.bson.BsonDocument;
22+
23+
import static com.mongodb.internal.connection.ClientMetadataHelper.createClientMetadataDocument;
24+
import static com.mongodb.internal.connection.ClientMetadataHelper.updateClientMedataDocument;
25+
26+
/**
27+
* Represents metadata of the current MongoClient.
28+
*
29+
* <p>This class is not part of the public API and may be removed or changed at any time</p>
30+
*/
31+
public class ClientMetadata {
32+
private volatile BsonDocument clientMetadataBsonDocument;
33+
34+
public ClientMetadata(@Nullable final String applicationName, final MongoDriverInformation mongoDriverInformation) {
35+
this.clientMetadataBsonDocument = createClientMetadataDocument(applicationName, mongoDriverInformation);
36+
}
37+
38+
/**
39+
* Returns mutable BsonDocument that represents the client metadata.
40+
*/
41+
public BsonDocument getBsonDocument() {
42+
return clientMetadataBsonDocument;
43+
}
44+
45+
public void append(final MongoDriverInformation mongoDriverInformation) {
46+
this.clientMetadataBsonDocument = updateClientMedataDocument(clientMetadataBsonDocument, mongoDriverInformation);
47+
}
48+
}
49+

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

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -42,8 +42,8 @@ class InternalStreamConnectionFactory implements InternalConnectionFactory {
4242
private final MongoCredentialWithCache credential;
4343

4444
InternalStreamConnectionFactory(final ClusterConnectionMode clusterConnectionMode,
45-
final StreamFactory streamFactory,
46-
@Nullable final MongoCredentialWithCache credential,
45+
final StreamFactory streamFactory,
46+
@Nullable final MongoCredentialWithCache credential,
4747
final ClientMetadata clientMetadata,
4848
final List<MongoCompressor> compressorList,
4949
final LoggerSettings loggerSettings, @Nullable final CommandListener commandListener, @Nullable final ServerApi serverApi) {
@@ -52,8 +52,8 @@ class InternalStreamConnectionFactory implements InternalConnectionFactory {
5252
}
5353

5454
InternalStreamConnectionFactory(final ClusterConnectionMode clusterConnectionMode, final boolean isMonitoringConnection,
55-
final StreamFactory streamFactory,
56-
@Nullable final MongoCredentialWithCache credential,
55+
final StreamFactory streamFactory,
56+
@Nullable final MongoCredentialWithCache credential,
5757
final ClientMetadata clientMetadata,
5858
final List<MongoCompressor> compressorList,
5959
final LoggerSettings loggerSettings, @Nullable final CommandListener commandListener, @Nullable final ServerApi serverApi) {
@@ -72,7 +72,7 @@ class InternalStreamConnectionFactory implements InternalConnectionFactory {
7272
public InternalConnection create(final ServerId serverId, final ConnectionGenerationSupplier connectionGenerationSupplier) {
7373
Authenticator authenticator = credential == null ? null : createAuthenticator(credential);
7474
InternalStreamConnectionInitializer connectionInitializer = new InternalStreamConnectionInitializer(
75-
clusterConnectionMode, authenticator, clientMetadata.getClientMetadataBsonDocument(), compressorList, serverApi);
75+
clusterConnectionMode, authenticator, clientMetadata.getBsonDocument(), compressorList, serverApi);
7676
return new InternalStreamConnection(
7777
clusterConnectionMode, authenticator,
7878
isMonitoringConnection, serverId, connectionGenerationSupplier,

driver-core/src/test/functional/com/mongodb/internal/connection/ClientMetadataHelperProseTest.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -341,6 +341,7 @@ void testUpdateClientMetadataDocument() {
341341
.driverPlatform("JDK 99")
342342
.build();
343343

344+
//We pass metadataToAppend to a builder and prepend with initial driver information.
344345
MongoDriverInformation expectedUpdatedMetadata = MongoDriverInformation.builder(metadataToAppend)
345346
.driverName("mongo-spark")
346347
.driverVersion("2.0.0")

0 commit comments

Comments
 (0)