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 public static final String DISCONNECTION_SQL_CODE_PREFIX = "08";
54
55
56
57
58
59
60
61
62
63
64
65
66
67 @Deprecated
68 public static final Set<String> DISCONNECTION_SQL_CODES;
69
70 static final ResultSet[] EMPTY_RESULT_SET_ARRAY = {};
71
72 static final String[] EMPTY_STRING_ARRAY = {};
73 static {
74 DISCONNECTION_SQL_CODES = new HashSet<>();
75 DISCONNECTION_SQL_CODES.add("57P01");
76 DISCONNECTION_SQL_CODES.add("57P02");
77 DISCONNECTION_SQL_CODES.add("57P03");
78 DISCONNECTION_SQL_CODES.add("01002");
79 DISCONNECTION_SQL_CODES.add("JZ0C0");
80 DISCONNECTION_SQL_CODES.add("JZ0C1");
81 }
82
83
84
85
86
87
88
89 public static char[] clone(final char[] value) {
90 return value == null ? null : value.clone();
91 }
92
93
94
95
96
97
98
99
100 public static Properties cloneWithoutCredentials(final Properties properties) {
101 if (properties != null) {
102 final Properties temp = (Properties) properties.clone();
103 temp.remove(Constants.KEY_USER);
104 temp.remove(Constants.KEY_PASSWORD);
105 return temp;
106 }
107 return properties;
108 }
109
110
111
112
113
114
115
116
117 public static void close(final AutoCloseable autoCloseable, final Consumer<Exception> exceptionHandler) {
118 if (autoCloseable != null) {
119 try {
120 autoCloseable.close();
121 } catch (final Exception e) {
122 if (exceptionHandler != null) {
123 exceptionHandler.accept(e);
124 }
125 }
126 }
127 }
128
129
130
131
132
133
134
135 public static void closeQuietly(final AutoCloseable autoCloseable) {
136 close(autoCloseable, null);
137 }
138
139
140
141
142
143
144
145 @Deprecated
146 public static void closeQuietly(final Connection connection) {
147 closeQuietly((AutoCloseable) connection);
148 }
149
150
151
152
153
154
155
156 @Deprecated
157 public static void closeQuietly(final ResultSet resultSet) {
158 closeQuietly((AutoCloseable) resultSet);
159 }
160
161
162
163
164
165
166
167 @Deprecated
168 public static void closeQuietly(final Statement statement) {
169 closeQuietly((AutoCloseable) statement);
170 }
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185 public static Set<String> getDisconnectionSqlCodes() {
186 return new HashSet<>(DISCONNECTION_SQL_CODES);
187 }
188
189
190
191
192
193
194
195 public static String getMessage(final String key) {
196 return getMessage(key, (Object[]) null);
197 }
198
199
200
201
202
203
204
205
206 public static String getMessage(final String key, final Object... args) {
207 final String msg = messages.getString(key);
208 if (args == null || args.length == 0) {
209 return msg;
210 }
211 final MessageFormat mf = new MessageFormat(msg);
212 return mf.format(args, new StringBuffer(), null).toString();
213 }
214
215 static boolean isEmpty(final Collection<?> collection) {
216 return collection == null || collection.isEmpty();
217 }
218
219 static boolean isSecurityEnabled() {
220 return System.getSecurityManager() != null;
221 }
222
223
224
225
226
227
228
229 public static char[] toCharArray(final String value) {
230 return value != null ? value.toCharArray() : null;
231 }
232
233
234
235
236
237
238
239 public static String toString(final char[] value) {
240 return value == null ? null : String.valueOf(value);
241 }
242
243 public static void validateLifetime(final PooledObject<?> p, final Duration maxDuration) throws LifetimeExceededException {
244 if (maxDuration.compareTo(Duration.ZERO) > 0) {
245 final Duration lifetimeDuration = Duration.between(p.getCreateInstant(), Instant.now());
246 if (lifetimeDuration.compareTo(maxDuration) > 0) {
247 throw new LifetimeExceededException(Utils.getMessage("connectionFactory.lifetimeExceeded", lifetimeDuration, maxDuration));
248 }
249 }
250 }
251
252 private Utils() {
253
254 }
255
256 }