@@ -50,7 +50,7 @@ public class MongoDBConfig extends PluginConfig {
5050 @ Name (MongoDBConstants .PORT )
5151 @ Description ("Port that MongoDB is listening to." )
5252 @ Macro
53- private int port ;
53+ private Integer port ;
5454
5555 @ Name (MongoDBConstants .DATABASE )
5656 @ Description ("MongoDB database name." )
@@ -76,21 +76,29 @@ public class MongoDBConfig extends PluginConfig {
7676 @ Nullable
7777 private String password ;
7878
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+
7986 @ Name (MongoDBConstants .CONNECTION_ARGUMENTS )
8087 @ Description ("A list of arbitrary string key/value pairs as connection arguments." )
8188 @ Macro
8289 @ Nullable
8390 private String connectionArguments ;
8491
8592 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 ) {
8794 this .referenceName = referenceName ;
8895 this .host = host ;
8996 this .port = port ;
9097 this .database = database ;
9198 this .collection = collection ;
9299 this .user = user ;
93100 this .password = password ;
101+ this .connectUsingSRVString = connectUsingSRVString ;
94102 this .connectionArguments = connectionArguments ;
95103 }
96104
@@ -102,7 +110,8 @@ public String getHost() {
102110 return host ;
103111 }
104112
105- public int getPort () {
113+ @ Nullable
114+ public Integer getPort () {
106115 return port ;
107116 }
108117
@@ -124,6 +133,10 @@ public String getPassword() {
124133 return password ;
125134 }
126135
136+ public boolean connectUsingSRVString () {
137+ return connectUsingSRVString ;
138+ }
139+
127140 @ Nullable
128141 public String getConnectionArguments () {
129142 return connectionArguments ;
@@ -146,7 +159,8 @@ public void validate() {
146159 if (!containsMacro (MongoDBConstants .HOST ) && Strings .isNullOrEmpty (host )) {
147160 throw new InvalidConfigPropertyException ("Host must be specified" , MongoDBConstants .HOST );
148161 }
149- if (!containsMacro (MongoDBConstants .PORT )) {
162+ if ((!containsMacro (MongoDBConstants .CONNECT_USING_SRV_STRING ) && !connectUsingSRVString ) &&
163+ !containsMacro (MongoDBConstants .PORT )) {
150164 if (port < 1 ) {
151165 throw new InvalidConfigPropertyException ("Port number must be greater than 0" , MongoDBConstants .PORT );
152166 }
@@ -161,24 +175,32 @@ public void validate() {
161175
162176 /**
163177 * 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:
166181 * "mongodb://localhost:27017/admin.analytics?key=value;"
182+ * When SRV is not used, the port will be included in the connection string.
167183 *
168184 * @return connection string.
169185 */
170186 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+ }
172193 if (!Strings .isNullOrEmpty (user ) || !Strings .isNullOrEmpty (password )) {
173194 connectionStringBuilder .append (user ).append (":" ).append (password ).append ("@" );
174195 }
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 );
178201 if (!Strings .isNullOrEmpty (connectionArguments )) {
179202 connectionStringBuilder .append ("?" ).append (connectionArguments );
180203 }
181-
182204 return connectionStringBuilder .toString ();
183205 }
184206
0 commit comments