-
Notifications
You must be signed in to change notification settings - Fork 31
/
AbstractMongoConnectionSourceSettings.groovy
118 lines (101 loc) · 2.85 KB
/
AbstractMongoConnectionSourceSettings.groovy
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
package org.grails.datastore.mapping.mongo.connections
import com.mongodb.ConnectionString
import com.mongodb.ServerAddress
import groovy.transform.AutoClone
import groovy.transform.CompileStatic
import groovy.transform.builder.Builder
import groovy.transform.builder.SimpleStrategy
import org.bson.codecs.Codec
import org.bson.codecs.configuration.CodecRegistry
import org.grails.datastore.mapping.core.connections.ConnectionSourceSettings
import org.grails.datastore.mapping.mongo.MongoConstants
import org.grails.datastore.mapping.mongo.config.MongoSettings
/**
* @author Graeme Rocher
* @since 6.0
*/
@AutoClone
@Builder(builderStrategy = SimpleStrategy, prefix = '')
@CompileStatic
abstract class AbstractMongoConnectionSourceSettings extends ConnectionSourceSettings implements MongoSettings {
/**
* The connection string
*/
protected ConnectionString connectionString
/**
* The default database name
*/
String databaseName = DEFAULT_DATABASE_NAME
/**
* The host name to use
*/
String host = ServerAddress.defaultHost();
/**
* The port to use
*/
Integer port = ServerAddress.defaultPort()
/**
* The username to use
*/
String username
/**
* The password to use
*/
String password
/**
* The engine to use by default
*/
String engine = MongoConstants.CODEC_ENGINE
/**
* Whether to use stateless mode by default
*/
boolean stateless = false
/**
* Whether to use the decimal128 type for BigDecimal values
*
* @see org.bson.types.Decimal128
*/
boolean decimalType = true
/**
* The collection name to use to resolve connections when using {@link MongoConnectionSources}
*/
String connectionsCollection = "mongo.connections"
/**
* Custom MongoDB codecs
*/
List<Class<? extends Codec>> codecs = []
/**
* An additional codec registry
*/
CodecRegistry codecRegistry
/**
* @return Obtain the final URL whether from the connection string or the host/port setting
*/
ConnectionString getUrl() {
if(connectionString != null) {
return connectionString
}
else {
String uAndP = username && password ? "$username:$password@" : ''
String portStr = port ? ":$port" : ''
return new ConnectionString("mongodb://${uAndP}${host}${portStr}/$database")
}
}
/**
* @param connectionString The connection string
*/
void url(ConnectionString connectionString) {
this.connectionString = connectionString
}
/**
* @return Obtain the database name
*/
String getDatabase() {
if(connectionString != null) {
return connectionString.database ?: databaseName
}
else {
return databaseName
}
}
}