|
1 | 1 | package dev.mongocamp.driver.mongodb.jdbc |
2 | 2 |
|
3 | | -import com.mongodb.MongoCredential.createCredential |
4 | 3 | import com.vdurmont.semver4j.Semver |
5 | 4 | import dev.mongocamp.driver.mongodb.BuildInfo |
6 | | -import org.mongodb.scala.MongoClient.DEFAULT_CODEC_REGISTRY |
7 | | -import org.mongodb.scala.{ConnectionString, MongoClient, MongoClientSettings, MongoCredential} |
| 5 | +import dev.mongocamp.driver.mongodb.database.{ DatabaseProvider, MongoConfig } |
| 6 | +import org.mongodb.scala.{ ConnectionString, ServerAddress } |
8 | 7 |
|
9 | | -import java.sql.{Connection, DriverPropertyInfo} |
| 8 | +import java.sql.{ Connection, DriverPropertyInfo } |
10 | 9 | import java.util.Properties |
11 | 10 | import java.util.logging.Logger |
| 11 | +import scala.jdk.CollectionConverters.CollectionHasAsScala |
12 | 12 |
|
13 | 13 | class MongoJdbcDriver extends java.sql.Driver { |
14 | 14 |
|
15 | 15 | private lazy val semVer = new Semver(BuildInfo.version) |
16 | 16 |
|
17 | | - |
18 | | - /** |
19 | | - * Connect to the database using a URL like : |
20 | | - * jdbc:mongodb://[username:password@]host1[:port1][,host2[:port2],...[,hostN[:portN]]][/[database][?options]] |
21 | | - * The URL excepting the jdbc: prefix is passed as it is to the MongoDb native Java driver. |
22 | | - */ |
| 17 | + /** Connect to the database using a URL like : jdbc:mongodb://[username:password@]host1[:port1][,host2[:port2],...[,hostN[:portN]]][/[database][?options]] The |
| 18 | + * URL excepting the jdbc: prefix is passed as it is to the MongoDb native Java driver. |
| 19 | + */ |
23 | 20 | override def connect(url: String, info: Properties): Connection = { |
24 | 21 | if (url == null || !acceptsURL(url)) { |
25 | 22 | return null |
26 | 23 | } |
27 | 24 |
|
28 | 25 | val connectionUrl = url.replaceFirst("^jdbc:", "") |
29 | | - val username = info.getProperty("user") |
30 | | - val password = info.getProperty("password") |
31 | | - |
32 | | - val builder = MongoClientSettings |
33 | | - .builder() |
34 | | - .applyConnectionString(new ConnectionString(connectionUrl)) |
35 | | - .codecRegistry(DEFAULT_CODEC_REGISTRY) |
36 | | - |
37 | | - if (!username.equalsIgnoreCase("") && !password.equalsIgnoreCase("")) { |
38 | | - val credential: MongoCredential = createCredential(username, "admin", password.toCharArray) |
39 | | - builder.credential(credential).build() |
40 | | - } |
41 | | - |
42 | | - val client: MongoClient = MongoClient(builder.build()) |
43 | | - new MongoJdbcConnection(client) |
| 26 | + val username = Option(info.getProperty("user")).filter(_.trim.nonEmpty) |
| 27 | + val password = Option(info.getProperty("password")).filter(_.trim.nonEmpty) |
| 28 | + |
| 29 | + val string = new ConnectionString(connectionUrl) |
| 30 | + val provider = DatabaseProvider( |
| 31 | + MongoConfig( |
| 32 | + string.getDatabase, |
| 33 | + MongoConfig.DefaultHost, |
| 34 | + MongoConfig.DefaultPort, |
| 35 | + string.getApplicationName, |
| 36 | + username, |
| 37 | + password, |
| 38 | + string.getDatabase, |
| 39 | + serverAddressList = string.getHosts.asScala.toList.map(h => new ServerAddress(h)) |
| 40 | + ) |
| 41 | + ) |
| 42 | + new MongoJdbcConnection(provider) |
44 | 43 | } |
45 | 44 |
|
46 | 45 | override def acceptsURL(url: String): Boolean = { |
|
0 commit comments