Skip to content

Commit 609cdc8

Browse files
committed
Use try-with-resources
- Don't hide exceptions on internal close() - Use generics - Remove now unnecessary @SuppressWarnings
1 parent 9da485f commit 609cdc8

2 files changed

Lines changed: 16 additions & 25 deletions

File tree

src/main/java/org/apache/commons/dbcp2/datasources/InstanceKeyDataSourceFactory.java

Lines changed: 9 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@
2424
import java.util.Hashtable;
2525
import java.util.List;
2626
import java.util.Map;
27-
import java.util.Properties;
2827
import java.util.concurrent.ConcurrentHashMap;
2928

3029
import javax.naming.Context;
@@ -74,22 +73,16 @@ public static void closeAll() throws ListException {
7473
/**
7574
* Deserializes the provided byte array to create an object.
7675
*
77-
* @param data
78-
* Data to deserialize to create the configuration parameter.
79-
*
76+
* @param <T> The type of the object to be deserialized.
77+
* @param data Data to deserialize to create the configuration parameter.
8078
* @return The Object created by deserializing the data.
81-
* @throws ClassNotFoundException
82-
* If a class cannot be found during the deserialization of a configuration parameter.
83-
* @throws IOException
84-
* If an I/O error occurs during the deserialization of a configuration parameter.
79+
* @throws ClassNotFoundException If a class cannot be found during the deserialization of a configuration parameter.
80+
* @throws IOException If an I/O error occurs during the deserialization of a configuration parameter.
8581
*/
86-
protected static final Object deserialize(final byte[] data) throws IOException, ClassNotFoundException {
87-
ObjectInputStream in = null;
88-
try {
89-
in = new ObjectInputStream(new ByteArrayInputStream(data));
90-
return in.readObject();
91-
} finally {
92-
Utils.closeQuietly(in);
82+
@SuppressWarnings("unchecked") // call site must ensure that the type is correct
83+
protected static final <T> T deserialize(final byte[] data) throws IOException, ClassNotFoundException {
84+
try (ObjectInputStream in = new ObjectInputStream(new ByteArrayInputStream(data))) {
85+
return (T) in.readObject();
9386
}
9487
}
9588

@@ -216,7 +209,7 @@ private void setCommonProperties(final Reference ref, final InstanceKeyDataSourc
216209
refAddr = ref.get("jndiEnvironment");
217210
if (hasContent(refAddr)) {
218211
final byte[] serialized = (byte[]) refAddr.getContent();
219-
ikds.setJndiEnvironment((Properties) deserialize(serialized));
212+
ikds.setJndiEnvironment(deserialize(serialized));
220213
}
221214

222215
refAddr = ref.get("loginTimeout");

src/main/java/org/apache/commons/dbcp2/datasources/PerUserPoolDataSourceFactory.java

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@
1818

1919
import java.io.IOException;
2020
import java.time.Duration;
21-
import java.util.Map;
2221

2322
import javax.naming.RefAddr;
2423
import javax.naming.Reference;
@@ -32,13 +31,12 @@ public class PerUserPoolDataSourceFactory extends InstanceKeyDataSourceFactory {
3231
private static final String PER_USER_POOL_CLASSNAME = PerUserPoolDataSource.class.getName();
3332

3433
/**
35-
* Constructs a new instance.
34+
* Constructs a new instance.Ωø∂
3635
*/
3736
public PerUserPoolDataSourceFactory() {
3837
// empty
3938
}
4039

41-
@SuppressWarnings("unchecked") // Avoid warnings on deserialization
4240
@Override
4341
protected InstanceKeyDataSource getNewInstance(final Reference ref) throws IOException, ClassNotFoundException {
4442
final PerUserPoolDataSource pupds = new PerUserPoolDataSource();
@@ -60,37 +58,37 @@ protected InstanceKeyDataSource getNewInstance(final Reference ref) throws IOExc
6058
refAddr = ref.get("perUserDefaultAutoCommit");
6159
if (refAddr != null && refAddr.getContent() != null) {
6260
final byte[] serialized = (byte[]) refAddr.getContent();
63-
pupds.setPerUserDefaultAutoCommit((Map<String, Boolean>) deserialize(serialized));
61+
pupds.setPerUserDefaultAutoCommit(deserialize(serialized));
6462
}
6563

6664
refAddr = ref.get("perUserDefaultTransactionIsolation");
6765
if (refAddr != null && refAddr.getContent() != null) {
6866
final byte[] serialized = (byte[]) refAddr.getContent();
69-
pupds.setPerUserDefaultTransactionIsolation((Map<String, Integer>) deserialize(serialized));
67+
pupds.setPerUserDefaultTransactionIsolation(deserialize(serialized));
7068
}
7169

7270
refAddr = ref.get("perUserMaxTotal");
7371
if (refAddr != null && refAddr.getContent() != null) {
7472
final byte[] serialized = (byte[]) refAddr.getContent();
75-
pupds.setPerUserMaxTotal((Map<String, Integer>) deserialize(serialized));
73+
pupds.setPerUserMaxTotal(deserialize(serialized));
7674
}
7775

7876
refAddr = ref.get("perUserMaxIdle");
7977
if (refAddr != null && refAddr.getContent() != null) {
8078
final byte[] serialized = (byte[]) refAddr.getContent();
81-
pupds.setPerUserMaxIdle((Map<String, Integer>) deserialize(serialized));
79+
pupds.setPerUserMaxIdle(deserialize(serialized));
8280
}
8381

8482
refAddr = ref.get("perUserMaxWaitMillis");
8583
if (refAddr != null && refAddr.getContent() != null) {
8684
final byte[] serialized = (byte[]) refAddr.getContent();
87-
pupds.setPerUserMaxWaitMillis((Map<String, Long>) deserialize(serialized));
85+
pupds.setPerUserMaxWaitMillis(deserialize(serialized));
8886
}
8987

9088
refAddr = ref.get("perUserDefaultReadOnly");
9189
if (refAddr != null && refAddr.getContent() != null) {
9290
final byte[] serialized = (byte[]) refAddr.getContent();
93-
pupds.setPerUserDefaultReadOnly((Map<String, Boolean>) deserialize(serialized));
91+
pupds.setPerUserDefaultReadOnly(deserialize(serialized));
9492
}
9593
return pupds;
9694
}

0 commit comments

Comments
 (0)