1
1
package com .xxdb ;
2
2
3
+ import org .slf4j .Logger ;
4
+ import org .slf4j .LoggerFactory ;
5
+ import java .util .Objects ;
6
+
3
7
public class SimpleDBConnectionPoolConfig {
4
8
private String hostName ;
5
9
private int port ;
@@ -13,6 +17,7 @@ public class SimpleDBConnectionPoolConfig {
13
17
private boolean loadBalance = false ;
14
18
private boolean enableHighAvailability = false ;
15
19
private String [] highAvailabilitySites = null ;
20
+ private static final Logger log = LoggerFactory .getLogger (DBConnection .class );
16
21
17
22
public SimpleDBConnectionPoolConfig () {
18
23
}
@@ -30,6 +35,8 @@ public int getPort() {
30
35
}
31
36
32
37
public void setPort (int port ) {
38
+ if (port <= 0 )
39
+ throw new RuntimeException ("The port should be positive." );
33
40
this .port = port ;
34
41
}
35
42
@@ -54,6 +61,8 @@ public int getInitialPoolSize() {
54
61
}
55
62
56
63
public void setInitialPoolSize (int initialPoolSize ) {
64
+ if (initialPoolSize <= 0 )
65
+ throw new RuntimeException ("The number of connection pools should be positive." );
57
66
this .initialPoolSize = initialPoolSize ;
58
67
}
59
68
@@ -112,6 +121,28 @@ public String[] getHighAvailabilitySites() {
112
121
public void setHighAvailabilitySites (String [] highAvailabilitySites ) {
113
122
this .highAvailabilitySites = highAvailabilitySites ;
114
123
}
124
+
125
+ public void validate () {
126
+ hostName = getNullIfEmpty (hostName );
127
+ if (Objects .isNull (hostName )) {
128
+ hostName = "localhost" ;
129
+ log .warn ("HostName not set, use the default value 'localhost'" );
130
+ }
131
+ if (port <= 0 )
132
+ throw new RuntimeException ("The port should be positive." );
133
+ userId = getNullIfEmpty (userId );
134
+ if (Objects .isNull (userId ))
135
+ log .warn ("Logging in needs userId." );
136
+ password = getNullIfEmpty (password );
137
+ if (Objects .isNull (password ))
138
+ log .warn ("Logging in needs password." );
139
+ if (initialPoolSize <= 0 )
140
+ throw new RuntimeException ("The number of connection pools should be positive." );
141
+ }
142
+
143
+ private static String getNullIfEmpty (String text ) {
144
+ return text == null ? null : (text .trim ().isEmpty () ? null : text .trim ());
145
+ }
115
146
}
116
147
117
148
0 commit comments