@@ -50,7 +50,7 @@ public class MongoDBConfig extends PluginConfig {
50
50
@ Name (MongoDBConstants .PORT )
51
51
@ Description ("Port that MongoDB is listening to." )
52
52
@ Macro
53
- private int port ;
53
+ private Integer port ;
54
54
55
55
@ Name (MongoDBConstants .DATABASE )
56
56
@ Description ("MongoDB database name." )
@@ -76,21 +76,29 @@ public class MongoDBConfig extends PluginConfig {
76
76
@ Nullable
77
77
private String password ;
78
78
79
+ @ Name (MongoDBConstants .CONNECT_USING_SRV_STRING )
80
+ @ Description ("Toggle to determine whether to use an SRV connection string for MongoDB. It can be " +
81
+ "enabled if the MongoDB deployment supports SRV DNS records for connection resolution." )
82
+ @ Macro
83
+ @ Nullable
84
+ private boolean connectUsingSRVString ;
85
+
79
86
@ Name (MongoDBConstants .CONNECTION_ARGUMENTS )
80
87
@ Description ("A list of arbitrary string key/value pairs as connection arguments." )
81
88
@ Macro
82
89
@ Nullable
83
90
private String connectionArguments ;
84
91
85
92
public MongoDBConfig (String referenceName , String host , int port , String database , String collection , String user ,
86
- String password , String connectionArguments ) {
93
+ String password , boolean connectUsingSRVString , String connectionArguments ) {
87
94
this .referenceName = referenceName ;
88
95
this .host = host ;
89
96
this .port = port ;
90
97
this .database = database ;
91
98
this .collection = collection ;
92
99
this .user = user ;
93
100
this .password = password ;
101
+ this .connectUsingSRVString = connectUsingSRVString ;
94
102
this .connectionArguments = connectionArguments ;
95
103
}
96
104
@@ -102,7 +110,8 @@ public String getHost() {
102
110
return host ;
103
111
}
104
112
105
- public int getPort () {
113
+ @ Nullable
114
+ public Integer getPort () {
106
115
return port ;
107
116
}
108
117
@@ -124,6 +133,10 @@ public String getPassword() {
124
133
return password ;
125
134
}
126
135
136
+ public boolean connectUsingSRVString () {
137
+ return connectUsingSRVString ;
138
+ }
139
+
127
140
@ Nullable
128
141
public String getConnectionArguments () {
129
142
return connectionArguments ;
@@ -146,7 +159,8 @@ public void validate() {
146
159
if (!containsMacro (MongoDBConstants .HOST ) && Strings .isNullOrEmpty (host )) {
147
160
throw new InvalidConfigPropertyException ("Host must be specified" , MongoDBConstants .HOST );
148
161
}
149
- if (!containsMacro (MongoDBConstants .PORT )) {
162
+ if ((!containsMacro (MongoDBConstants .CONNECT_USING_SRV_STRING ) && !connectUsingSRVString ) &&
163
+ !containsMacro (MongoDBConstants .PORT )) {
150
164
if (port < 1 ) {
151
165
throw new InvalidConfigPropertyException ("Port number must be greater than 0" , MongoDBConstants .PORT );
152
166
}
@@ -161,24 +175,32 @@ public void validate() {
161
175
162
176
/**
163
177
* Constructs a connection string such as: "mongodb://admin:password@localhost:27017/admin.analytics?key=value;"
164
- * using host, port, username, password, database, collection and optional connection properties. In the case when
165
- * username or password is not provided the connection string will not contain credentials:
178
+ * using host, port, username, password, database, collection and optional connection properties.
179
+ * If SRV is enabled, the connection string will use the "mongodb+srv://" protocol instead of "mongodb://".
180
+ * In the case when username or password is not provided, the connection string will not contain credentials:
166
181
* "mongodb://localhost:27017/admin.analytics?key=value;"
182
+ * When SRV is not used, the port will be included in the connection string.
167
183
*
168
184
* @return connection string.
169
185
*/
170
186
public String getConnectionString () {
171
- StringBuilder connectionStringBuilder = new StringBuilder ("mongodb://" );
187
+ StringBuilder connectionStringBuilder = new StringBuilder ();
188
+ if (connectUsingSRVString ()) {
189
+ connectionStringBuilder .append ("mongodb+srv://" );
190
+ } else {
191
+ connectionStringBuilder .append ("mongodb://" );
192
+ }
172
193
if (!Strings .isNullOrEmpty (user ) || !Strings .isNullOrEmpty (password )) {
173
194
connectionStringBuilder .append (user ).append (":" ).append (password ).append ("@" );
174
195
}
175
- connectionStringBuilder .append (host ).append (":" ).append (port ).append ("/" )
176
- .append (database ).append ("." ).append (collection );
177
-
196
+ connectionStringBuilder .append (host );
197
+ if (!connectUsingSRVString ()) {
198
+ connectionStringBuilder .append (":" ).append (port );
199
+ }
200
+ connectionStringBuilder .append ("/" ).append (database ).append ("." ).append (collection );
178
201
if (!Strings .isNullOrEmpty (connectionArguments )) {
179
202
connectionStringBuilder .append ("?" ).append (connectionArguments );
180
203
}
181
-
182
204
return connectionStringBuilder .toString ();
183
205
}
184
206
0 commit comments