@@ -3,29 +3,29 @@ package dev.mongocamp.driver.mongodb.database
3
3
import java .util .concurrent .TimeUnit
4
4
import com .mongodb .MongoCompressor
5
5
import com .mongodb .MongoCredential .createCredential
6
- import com .mongodb .event .{ CommandListener , ConnectionPoolListener }
6
+ import com .mongodb .event .{CommandListener , ConnectionPoolListener }
7
7
import dev .mongocamp .driver .mongodb .database .MongoConfig ._
8
- import com .typesafe .config .{ Config , ConfigFactory }
8
+ import com .typesafe .config .{Config , ConfigFactory }
9
9
import org .mongodb .scala .connection ._
10
- import org .mongodb .scala .{ MongoClientSettings , MongoCredential , ServerAddress }
10
+ import org .mongodb .scala .{MongoClientSettings , MongoCredential , ServerAddress }
11
11
12
12
import scala .jdk .CollectionConverters ._
13
13
import scala .collection .mutable .ArrayBuffer
14
14
15
15
case class MongoConfig (
16
- database : String ,
17
- host : String = DefaultHost ,
18
- port : Int = DefaultPort ,
19
- applicationName : String = DefaultApplicationName ,
20
- userName : Option [String ] = None ,
21
- password : Option [String ] = None ,
22
- authDatabase : String = DefaultAuthenticationDatabaseName ,
23
- poolOptions : MongoPoolOptions = MongoPoolOptions (),
24
- compressors : List [String ] = List (),
25
- connectionPoolListener : List [ConnectionPoolListener ] = List (),
26
- commandListener : List [CommandListener ] = List (),
27
- customClientSettings : Option [MongoClientSettings ] = None
28
- ) {
16
+ database : String ,
17
+ host : String = DefaultHost ,
18
+ port : Int = DefaultPort ,
19
+ applicationName : String = DefaultApplicationName ,
20
+ userName : Option [String ] = None ,
21
+ password : Option [String ] = None ,
22
+ authDatabase : String = DefaultAuthenticationDatabaseName ,
23
+ poolOptions : MongoPoolOptions = MongoPoolOptions (),
24
+ compressors : List [String ] = List (),
25
+ connectionPoolListener : List [ConnectionPoolListener ] = List (),
26
+ commandListener : List [CommandListener ] = List (),
27
+ customClientSettings : Option [MongoClientSettings ] = None
28
+ ) {
29
29
30
30
val clientSettings : MongoClientSettings = {
31
31
if (customClientSettings.isDefined) {
@@ -81,71 +81,88 @@ case class MongoConfig(
81
81
trait ConfigHelper {
82
82
val conf : Config = ConfigFactory .load()
83
83
84
- def stringConfig (configPath : String , key : String , default : String = " " ): Option [String ] =
84
+ def stringConfig (configPath : String , key : String , default : String = " " ): Option [String ] = {
85
85
if (conf.hasPath(" %s.%s" .format(configPath, key))) {
86
86
val str = conf.getString(" %s.%s" .format(configPath, key))
87
- if (str.nonEmpty)
87
+ if (str.nonEmpty) {
88
88
Some (str)
89
- else
89
+ }
90
+ else {
90
91
None
92
+ }
91
93
}
92
- else if (default.nonEmpty)
94
+ else if (default.nonEmpty) {
93
95
Some (default)
94
- else
96
+ }
97
+ else {
95
98
None
99
+ }
100
+ }
96
101
97
- def intConfig (configPath : String , key : String , default : Int = 0 ): Int =
98
- if (conf.hasPath(" %s.%s" .format(configPath, key)))
102
+ def intConfig (configPath : String , key : String , default : Int = 0 ): Int = {
103
+ if (conf.hasPath(" %s.%s" .format(configPath, key))) {
99
104
conf.getInt(" %s.%s" .format(configPath, key))
100
- else
105
+ }
106
+ else {
101
107
default
108
+ }
109
+ }
102
110
103
- def booleanConfig (configPath : String , key : String , default : Boolean = false ): Boolean =
104
- if (conf.hasPath(" %s.%s" .format(configPath, key)))
111
+ def booleanConfig (configPath : String , key : String , default : Boolean = false ): Boolean = {
112
+ if (conf.hasPath(" %s.%s" .format(configPath, key))) {
105
113
conf.getBoolean(" %s.%s" .format(configPath, key))
106
- else
114
+ }
115
+ else {
107
116
default
117
+ }
118
+ }
108
119
}
109
120
110
121
object MongoConfig extends ConfigHelper {
111
- val DefaultHost = " 127.0.0.1"
112
- val DefaultPort = 27017
122
+ val DefaultHost = " 127.0.0.1"
123
+ val DefaultPort = 27017
113
124
val DefaultAuthenticationDatabaseName = " admin"
114
- val DefaultApplicationName = " mongocampdb-app"
125
+ val DefaultApplicationName = " mongocampdb-app"
115
126
116
- val DefaultPoolMaxConnectionIdleTime = 60
117
- val DefaultPoolMaxSize = 50
118
- val DefaultPoolMinSize = 0
119
- val DefaultPoolMaxWaitQueueSize = 500
127
+ val DefaultPoolMaxConnectionIdleTime = 60
128
+ val DefaultPoolMaxSize = 50
129
+ val DefaultPoolMinSize = 0
130
+ val DefaultPoolMaxWaitQueueSize = 500
120
131
val DefaultPoolMaintenanceInitialDelay = 0
121
132
122
133
val ComressionSnappy = " snappy"
123
- val ComressionZlib = " zlib"
124
- val ComressionZstd = " zstd"
134
+ val ComressionZlib = " zlib"
135
+ val ComressionZstd = " zstd"
125
136
126
137
val DefaultConfigPathPrefix = " mongodb"
127
138
128
139
def fromPath (configPath : String = DefaultConfigPathPrefix ): MongoConfig = {
129
140
130
- def poolOptionsConfig (key : String , default : Int ): Int =
131
- if (conf.hasPath(" %s.pool.%s" .format(configPath, key)))
141
+ def poolOptionsConfig (key : String , default : Int ): Int = {
142
+ if (conf.hasPath(" %s.pool.%s" .format(configPath, key))) {
132
143
conf.getInt(" %s.pool.%s" .format(configPath, key))
133
- else
144
+ }
145
+ else {
134
146
default
147
+ }
148
+ }
135
149
136
150
val port : Int = intConfig(configPath, " port" , DefaultPort )
137
151
138
- val compressors : List [String ] =
139
- if (conf.hasPath(" %s.compressors" .format(configPath)))
152
+ val compressors : List [String ] = {
153
+ if (conf.hasPath(" %s.compressors" .format(configPath))) {
140
154
conf.getStringList(" %s.compressors" .format(configPath)).asScala.toList
141
- else
155
+ }
156
+ else {
142
157
List ()
158
+ }
159
+ }
143
160
144
- val host = stringConfig(configPath, " host" , DefaultHost ).get
145
- val database = stringConfig(configPath, " database" ).get
146
- val userName = stringConfig(configPath, " userName" )
147
- val password = stringConfig(configPath, " password" )
148
- val authDatabase = stringConfig(configPath, " authDatabase" , DefaultAuthenticationDatabaseName ).get
161
+ val host = stringConfig(configPath, " host" , DefaultHost ).get
162
+ val database = stringConfig(configPath, " database" ).get
163
+ val userName = stringConfig(configPath, " userName" )
164
+ val password = stringConfig(configPath, " password" )
165
+ val authDatabase = stringConfig(configPath, " authDatabase" , DefaultAuthenticationDatabaseName ).get
149
166
val applicationName = stringConfig(configPath, " applicationName" , DefaultApplicationName ).get
150
167
151
168
val poolOptions = MongoPoolOptions (
0 commit comments