1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.apache.commons.dbcp2;
18
19 import java.sql.Connection;
20 import java.sql.ResultSet;
21 import java.sql.Statement;
22 import java.text.MessageFormat;
23 import java.time.Duration;
24 import java.time.Instant;
25 import java.util.Collection;
26 import java.util.HashSet;
27 import java.util.Properties;
28 import java.util.ResourceBundle;
29 import java.util.Set;
30 import java.util.function.Consumer;
31
32 import org.apache.commons.pool2.PooledObject;
33
34
35
36
37
38
39 public final class Utils {
40
41 private static final ResourceBundle messages = ResourceBundle
42 .getBundle(Utils.class.getPackage().getName() + ".LocalStrings");
43
44
45
46
47
48
49 @Deprecated
50 public static final boolean IS_SECURITY_ENABLED = isSecurityEnabled();
51
52
53
54
55 public static final String DISCONNECTION_SQL_CODE_PREFIX = "08";
56
57
58
59
60
61
62
63
64
65
66
67
68
69 @Deprecated
70 public static final Set<String> DISCONNECTION_SQL_CODES;
71
72 static final ResultSet[] EMPTY_RESULT_SET_ARRAY = {};
73
74 static final String[] EMPTY_STRING_ARRAY = {};
75 static {
76 DISCONNECTION_SQL_CODES = new HashSet<>();
77 DISCONNECTION_SQL_CODES.add("57P01");
78 DISCONNECTION_SQL_CODES.add("57P02");
79 DISCONNECTION_SQL_CODES.add("57P03");
80 DISCONNECTION_SQL_CODES.add("01002");
81 DISCONNECTION_SQL_CODES.add("JZ0C0");
82 DISCONNECTION_SQL_CODES.add("JZ0C1");
83 }
84
85
86
87
88
89
90
91
92
93
94
95
96 static void checkSqlCodes(final Collection<String> codes1, final Collection<String> codes2) {
97 if (codes1 != null && codes2 != null) {
98 final Set<String> test = new HashSet<>(codes1);
99 test.retainAll(codes2);
100 if (!test.isEmpty()) {
101 throw new IllegalArgumentException(test + " cannot be in both disconnectionSqlCodes and disconnectionIgnoreSqlCodes.");
102 }
103 }
104 }
105
106
107
108
109
110
111
112 public static char[] clone(final char[] value) {
113 return value == null ? null : value.clone();
114 }
115
116
117
118
119
120
121
122
123 public static Properties cloneWithoutCredentials(final Properties properties) {
124 if (properties != null) {
125 final Properties temp = (Properties) properties.clone();
126 temp.remove(Constants.KEY_USER);
127 temp.remove(Constants.KEY_PASSWORD);
128 return temp;
129 }
130 return properties;
131 }
132
133
134
135
136
137
138
139
140 public static void close(final AutoCloseable autoCloseable, final Consumer<Exception> exceptionHandler) {
141 if (autoCloseable != null) {
142 try {
143 autoCloseable.close();
144 } catch (final Exception e) {
145 if (exceptionHandler != null) {
146 exceptionHandler.accept(e);
147 }
148 }
149 }
150 }
151
152
153
154
155
156
157
158 public static void closeQuietly(final AutoCloseable autoCloseable) {
159 close(autoCloseable, null);
160 }
161
162
163
164
165
166
167
168 @Deprecated
169 public static void closeQuietly(final Connection connection) {
170 closeQuietly((AutoCloseable) connection);
171 }
172
173
174
175
176
177
178
179 @Deprecated
180 public static void closeQuietly(final ResultSet resultSet) {
181 closeQuietly((AutoCloseable) resultSet);
182 }
183
184
185
186
187
188
189
190 @Deprecated
191 public static void closeQuietly(final Statement statement) {
192 closeQuietly((AutoCloseable) statement);
193 }
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208 public static Set<String> getDisconnectionSqlCodes() {
209 return new HashSet<>(DISCONNECTION_SQL_CODES);
210 }
211
212
213
214
215
216
217
218 public static String getMessage(final String key) {
219 return getMessage(key, (Object[]) null);
220 }
221
222
223
224
225
226
227
228
229 public static String getMessage(final String key, final Object... args) {
230 final String msg = messages.getString(key);
231 if (args == null || args.length == 0) {
232 return msg;
233 }
234 final MessageFormat mf = new MessageFormat(msg);
235 return mf.format(args, new StringBuffer(), null).toString();
236 }
237
238
239
240
241
242
243
244
245 static boolean isDisconnectionSqlCode(final String sqlState) {
246 return DISCONNECTION_SQL_CODES.contains(sqlState);
247 }
248
249 static boolean isEmpty(final Collection<?> collection) {
250 return collection == null || collection.isEmpty();
251 }
252
253 static boolean isSecurityEnabled() {
254 return System.getSecurityManager() != null;
255 }
256
257
258
259
260
261
262
263 public static char[] toCharArray(final String value) {
264 return value != null ? value.toCharArray() : null;
265 }
266
267
268
269
270
271
272
273 public static String toString(final char[] value) {
274 return value == null ? null : String.valueOf(value);
275 }
276
277
278
279
280
281
282
283
284 public static void validateLifetime(final PooledObject<?> p, final Duration maxDuration) throws LifetimeExceededException {
285 if (maxDuration.compareTo(Duration.ZERO) > 0) {
286 final Duration lifetimeDuration = Duration.between(p.getCreateInstant(), Instant.now());
287 if (lifetimeDuration.compareTo(maxDuration) > 0) {
288 throw new LifetimeExceededException(getMessage("connectionFactory.lifetimeExceeded", lifetimeDuration, maxDuration));
289 }
290 }
291 }
292
293 private Utils() {
294
295 }
296
297 }