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