测试用户
2023-04-13 43393f2bb11cbf9e6af40077bbc5284660e8a754
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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
package com.common.component;
 
import com.alibaba.druid.filter.Filter;
import com.alibaba.druid.pool.DruidDataSource;
import com.jfinal.kit.StrKit;
import com.jfinal.plugin.IPlugin;
import com.jfinal.plugin.activerecord.IDataSourceProvider;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import javax.sql.DataSource;
 
public class DruidPlugin implements IPlugin, IDataSourceProvider {
    protected String name = null;
    protected String url;
    protected String username;
    protected String password;
    protected String publicKey;
    protected String driverClass = null;
    protected int initialSize = 1;
    protected int minIdle = 10;
    protected int maxActive = 32;
    protected long maxWait = -1L;
    protected long timeBetweenEvictionRunsMillis = 60000L;
    protected long minEvictableIdleTimeMillis = 1800000L;
    protected long timeBetweenConnectErrorMillis = 30000L;
    protected String validationQuery = "select 1";
    protected String connectionInitSql = null;
    protected String connectionProperties = null;
    protected boolean testWhileIdle = true;
    protected boolean testOnBorrow = false;
    protected boolean testOnReturn = false;
    protected boolean removeAbandoned = false;
    protected long removeAbandonedTimeoutMillis = 300000L;
    protected boolean logAbandoned = false;
    protected int maxPoolPreparedStatementPerConnectionSize = -1;
    protected Integer defaultTransactionIsolation = null;
    protected Integer validationQueryTimeout = null;
    protected Integer timeBetweenLogStatsMillis = null;
    protected Boolean keepAlive = null;
    protected String filters;
    protected List<Filter> filterList;
    protected DruidDataSource ds;
    protected volatile boolean isStarted = false;
 
    public DruidPlugin(String url, String username, String password) {
        this.url = url;
        this.username = username;
        this.password = password;
        this.validationQuery = autoCheckValidationQuery(url);
    }
 
    public DruidPlugin(String url, String username, String password, String driverClass) {
        this.url = url;
        this.username = username;
        this.password = password;
        this.driverClass = driverClass;
        this.validationQuery = autoCheckValidationQuery(url);
    }
 
    public DruidPlugin(String url, String username, String password, String driverClass, String filters) {
        this.url = url;
        this.username = username;
        this.password = password;
        this.driverClass = driverClass;
        this.filters = filters;
        this.validationQuery = autoCheckValidationQuery(url);
    }
 
    private static String autoCheckValidationQuery(String url) {
        if (url.startsWith("jdbc:oracle")) {
            return "select 1 from dual";
        } else if (url.startsWith("jdbc:db2")) {
            return "select 1 from sysibm.sysdummy1";
        } else if (url.startsWith("jdbc:hsqldb")) {
            return "select 1 from INFORMATION_SCHEMA.SYSTEM_USERS";
        } else {
            return url.startsWith("jdbc:derby") ? "select 1 from INFORMATION_SCHEMA.SYSTEM_USERS" : "select 1";
        }
    }
 
    public void setConnectionInitSql(String sql) {
        this.connectionInitSql = sql;
    }
 
    public final String getName() {
        return this.name;
    }
 
    public final void setName(String name) {
        this.name = name;
    }
 
    public DruidPlugin setFilters(String filters) {
        this.filters = filters;
        return this;
    }
 
    public synchronized DruidPlugin addFilter(Filter filter) {
        if (this.filterList == null) {
            this.filterList = new ArrayList();
        }
 
        this.filterList.add(filter);
        return this;
    }
 
    public boolean start() {
        if (this.isStarted) {
            return true;
        } else {
            this.ds = new DruidDataSource();
            if (this.name != null) {
                this.ds.setName(this.name);
            }
 
            this.ds.setUrl(this.url);
            this.ds.setUsername(this.username);
            this.ds.setPassword(this.password);
            if (this.driverClass != null) {
                this.ds.setDriverClassName(this.driverClass);
            }
 
            this.ds.setInitialSize(this.initialSize);
            this.ds.setMinIdle(this.minIdle);
            this.ds.setMaxActive(this.maxActive);
            this.ds.setMaxWait(this.maxWait);
            this.ds.setTimeBetweenConnectErrorMillis(this.timeBetweenConnectErrorMillis);
            this.ds.setTimeBetweenEvictionRunsMillis(this.timeBetweenEvictionRunsMillis);
            this.ds.setMinEvictableIdleTimeMillis(this.minEvictableIdleTimeMillis);
            this.ds.setValidationQuery(this.validationQuery);
            if (StrKit.notBlank(this.connectionInitSql)) {
                List<String> connectionInitSqls = new ArrayList();
                connectionInitSqls.add(this.connectionInitSql);
                this.ds.setConnectionInitSqls(connectionInitSqls);
            }
            this.ds.setConnectionErrorRetryAttempts(5);
            this.ds.setTestWhileIdle(this.testWhileIdle);
            this.ds.setTestOnBorrow(this.testOnBorrow);
            this.ds.setTestOnReturn(this.testOnReturn);
            this.ds.setRemoveAbandoned(this.removeAbandoned);
            this.ds.setRemoveAbandonedTimeoutMillis(this.removeAbandonedTimeoutMillis);
            this.ds.setLogAbandoned(this.logAbandoned);
            this.ds.setMaxPoolPreparedStatementPerConnectionSize(this.maxPoolPreparedStatementPerConnectionSize);
            if (this.defaultTransactionIsolation != null) {
                this.ds.setDefaultTransactionIsolation(this.defaultTransactionIsolation);
            }
 
            if (this.validationQueryTimeout != null) {
                this.ds.setValidationQueryTimeout(this.validationQueryTimeout);
            }
 
            if (this.timeBetweenLogStatsMillis != null) {
                this.ds.setTimeBetweenLogStatsMillis((long)this.timeBetweenLogStatsMillis);
            }
 
            if (this.keepAlive != null) {
                this.ds.setKeepAlive(this.keepAlive);
            }
 
            boolean hasSetConnectionProperties = false;
            if (StrKit.notBlank(this.filters)) {
                try {
                    this.ds.setFilters(this.filters);
                    if (this.filters.contains("config")) {
                        if (StrKit.isBlank(this.publicKey)) {
                            throw new RuntimeException("Druid连接池的filter设定了config时,必须设定publicKey");
                        }
 
                        String decryptStr = "config.decrypt=true;config.decrypt.key=" + this.publicKey;
                        String cp = this.connectionProperties;
                        if (StrKit.isBlank(cp)) {
                            cp = decryptStr;
                        } else {
                            cp = cp + ";" + decryptStr;
                        }
 
                        this.ds.setConnectionProperties(cp);
                        hasSetConnectionProperties = true;
                    }
                } catch (SQLException var4) {
                    throw new RuntimeException(var4);
                }
            }
 
            if (!hasSetConnectionProperties && StrKit.notBlank(this.connectionProperties)) {
                this.ds.setConnectionProperties(this.connectionProperties);
            }
 
            this.addFilterList(this.ds);
            this.isStarted = true;
            return true;
        }
    }
 
    private void addFilterList(DruidDataSource ds) {
        if (this.filterList != null) {
            List<Filter> targetList = ds.getProxyFilters();
            Iterator var3 = this.filterList.iterator();
 
            while(var3.hasNext()) {
                Filter add = (Filter)var3.next();
                boolean found = false;
                Iterator var6 = targetList.iterator();
 
                while(var6.hasNext()) {
                    Filter target = (Filter)var6.next();
                    if (add.getClass().equals(target.getClass())) {
                        found = true;
                        break;
                    }
                }
 
                if (!found) {
                    targetList.add(add);
                }
            }
        }
 
    }
 
    public boolean stop() {
        if (this.ds != null) {
            this.ds.close();
        }
 
        this.ds = null;
        this.isStarted = false;
        return true;
    }
 
    public DataSource getDataSource() {
        return this.ds;
    }
 
    public DruidPlugin set(int initialSize, int minIdle, int maxActive) {
        this.initialSize = initialSize;
        this.minIdle = minIdle;
        this.maxActive = maxActive;
        return this;
    }
 
    public DruidPlugin setDriverClass(String driverClass) {
        this.driverClass = driverClass;
        return this;
    }
 
    public DruidPlugin setInitialSize(int initialSize) {
        this.initialSize = initialSize;
        return this;
    }
 
    public DruidPlugin setMinIdle(int minIdle) {
        this.minIdle = minIdle;
        return this;
    }
 
    public DruidPlugin setMaxActive(int maxActive) {
        this.maxActive = maxActive;
        return this;
    }
 
    public DruidPlugin setMaxWait(long maxWait) {
        this.maxWait = maxWait;
        return this;
    }
 
    public DruidPlugin setDefaultTransactionIsolation(int defaultTransactionIsolation) {
        this.defaultTransactionIsolation = defaultTransactionIsolation;
        return this;
    }
 
    public DruidPlugin setValidationQueryTimeout(int validationQueryTimeout) {
        this.validationQueryTimeout = validationQueryTimeout;
        return this;
    }
 
    public DruidPlugin setTimeBetweenLogStatsMillis(int timeBetweenLogStatsMillis) {
        this.timeBetweenLogStatsMillis = timeBetweenLogStatsMillis;
        return this;
    }
 
    public DruidPlugin setKeepAlive(boolean keepAlive) {
        this.keepAlive = keepAlive;
        return this;
    }
 
    public DruidPlugin setTimeBetweenEvictionRunsMillis(long timeBetweenEvictionRunsMillis) {
        this.timeBetweenEvictionRunsMillis = timeBetweenEvictionRunsMillis;
        return this;
    }
 
    public DruidPlugin setMinEvictableIdleTimeMillis(long minEvictableIdleTimeMillis) {
        this.minEvictableIdleTimeMillis = minEvictableIdleTimeMillis;
        return this;
    }
 
    public DruidPlugin setValidationQuery(String validationQuery) {
        this.validationQuery = validationQuery;
        return this;
    }
 
    public DruidPlugin setTestWhileIdle(boolean testWhileIdle) {
        this.testWhileIdle = testWhileIdle;
        return this;
    }
 
    public DruidPlugin setTestOnBorrow(boolean testOnBorrow) {
        this.testOnBorrow = testOnBorrow;
        return this;
    }
 
    public DruidPlugin setTestOnReturn(boolean testOnReturn) {
        this.testOnReturn = testOnReturn;
        return this;
    }
 
    public DruidPlugin setMaxPoolPreparedStatementPerConnectionSize(int maxPoolPreparedStatementPerConnectionSize) {
        this.maxPoolPreparedStatementPerConnectionSize = maxPoolPreparedStatementPerConnectionSize;
        return this;
    }
 
    public final DruidPlugin setTimeBetweenConnectErrorMillis(long timeBetweenConnectErrorMillis) {
        this.timeBetweenConnectErrorMillis = timeBetweenConnectErrorMillis;
        return this;
    }
 
    public final DruidPlugin setRemoveAbandoned(boolean removeAbandoned) {
        this.removeAbandoned = removeAbandoned;
        return this;
    }
 
    public final DruidPlugin setRemoveAbandonedTimeoutMillis(long removeAbandonedTimeoutMillis) {
        this.removeAbandonedTimeoutMillis = removeAbandonedTimeoutMillis;
        return this;
    }
 
    public final DruidPlugin setLogAbandoned(boolean logAbandoned) {
        this.logAbandoned = logAbandoned;
        return this;
    }
 
    public final DruidPlugin setConnectionProperties(String connectionProperties) {
        this.connectionProperties = connectionProperties;
        return this;
    }
 
    public final DruidPlugin setPublicKey(String publicKey) {
        this.publicKey = publicKey;
        return this;
    }
}