You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@commons.apache.org by gg...@apache.org on 2021/08/23 17:09:53 UTC
[commons-dbcp] branch master updated (922eb26 -> 2555c38)
This is an automated email from the ASF dual-hosted git repository.
ggregory pushed a change to branch master
in repository https://gitbox.apache.org/repos/asf/commons-dbcp.git.
from 922eb26 Remove dead comment.
new 329d4f9 Less boiler plate code.
new 5e2f996 Don't be so verbose.
new 2555c38 Fix possible NullPointerException in BasicDataSourceFactory.validatePropertyNames(Reference, Name, List, List)
The 3 revisions listed above as "new" are entirely new to this
repository and will be described in separate emails. The revisions
listed as "add" were already present in the repository and have only
been added to this reference.
Summary of changes:
src/changes/changes.xml | 35 +-
.../commons/dbcp2/BasicDataSourceFactory.java | 358 ++++++---------------
2 files changed, 106 insertions(+), 287 deletions(-)
[commons-dbcp] 01/03: Less boiler plate code.
Posted by gg...@apache.org.
This is an automated email from the ASF dual-hosted git repository.
ggregory pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/commons-dbcp.git
commit 329d4f98d9c5d28245a7136ca532d8d1cf964822
Author: Gary Gregory <ga...@gmail.com>
AuthorDate: Mon Aug 23 12:55:22 2021 -0400
Less boiler plate code.
Also fix exception in private method signature.
Javdoc.
---
.../commons/dbcp2/BasicDataSourceFactory.java | 333 ++++++---------------
1 file changed, 90 insertions(+), 243 deletions(-)
diff --git a/src/main/java/org/apache/commons/dbcp2/BasicDataSourceFactory.java b/src/main/java/org/apache/commons/dbcp2/BasicDataSourceFactory.java
index a3e6b04..4ab0cc4 100644
--- a/src/main/java/org/apache/commons/dbcp2/BasicDataSourceFactory.java
+++ b/src/main/java/org/apache/commons/dbcp2/BasicDataSourceFactory.java
@@ -18,6 +18,7 @@
package org.apache.commons.dbcp2;
import java.io.ByteArrayInputStream;
+import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.sql.Connection;
import java.time.Duration;
@@ -29,8 +30,12 @@ import java.util.Hashtable;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
+import java.util.Objects;
+import java.util.Optional;
import java.util.Properties;
import java.util.StringTokenizer;
+import java.util.function.Consumer;
+import java.util.function.Function;
import javax.naming.Context;
import javax.naming.Name;
@@ -44,11 +49,9 @@ import org.apache.commons.pool2.impl.BaseObjectPoolConfig;
import org.apache.commons.pool2.impl.GenericObjectPoolConfig;
/**
- * <p>
* JNDI object factory that creates an instance of <code>BasicDataSource</code> that has been configured based on the
* <code>RefAddr</code> values of the specified <code>Reference</code>, which must match the names and data types of the
* <code>BasicDataSource</code> bean properties with the following exceptions:
- * </p>
* <ul>
* <li><code>connectionInitSqls</code> must be passed to this factory as a single String using semicolon to delimit the
* statements whereas <code>BasicDataSource</code> requires a collection of Strings.</li>
@@ -182,6 +185,30 @@ public class BasicDataSourceFactory implements ObjectFactory {
}
+ private static <V> void accept(final Properties properties, final String name, final Function<String, V> parser, final Consumer<V> consumer) {
+ getOptional(properties, name).ifPresent(v -> consumer.accept(parser.apply(v)));
+ }
+
+ private static void acceptBoolean(final Properties properties, final String name, final Consumer<Boolean> consumer) {
+ accept(properties, name, Boolean::parseBoolean, consumer);
+ }
+
+ private static void acceptDurationOfMillis(final Properties properties, final String name, final Consumer<Duration> consumer) {
+ accept(properties, name, s -> Duration.ofMillis(Long.parseLong(s)), consumer);
+ }
+
+ private static void acceptDurationOfSeconds(final Properties properties, final String name, final Consumer<Duration> consumer) {
+ accept(properties, name, s -> Duration.ofSeconds(Long.parseLong(s)), consumer);
+ }
+
+ private static void acceptInt(final Properties properties, final String name, final Consumer<Integer> consumer) {
+ accept(properties, name, Integer::parseInt, consumer);
+ }
+
+ private static void acceptString(final Properties properties, final String name, final Consumer<String> consumer) {
+ accept(properties, name, Function.identity(), consumer);
+ }
+
/**
* Creates and configures a {@link BasicDataSource} instance based on the given properties.
*
@@ -193,18 +220,10 @@ public class BasicDataSourceFactory implements ObjectFactory {
*/
public static BasicDataSource createDataSource(final Properties properties) throws Exception {
final BasicDataSource dataSource = new BasicDataSource();
- String value = properties.getProperty(PROP_DEFAULT_AUTO_COMMIT);
- if (value != null) {
- dataSource.setDefaultAutoCommit(Boolean.valueOf(value));
- }
+ acceptBoolean(properties, PROP_DEFAULT_AUTO_COMMIT, dataSource::setDefaultAutoCommit);
+ acceptBoolean(properties, PROP_DEFAULT_READ_ONLY, dataSource::setDefaultReadOnly);
- value = properties.getProperty(PROP_DEFAULT_READ_ONLY);
- if (value != null) {
- dataSource.setDefaultReadOnly(Boolean.valueOf(value));
- }
-
- value = properties.getProperty(PROP_DEFAULT_TRANSACTION_ISOLATION);
- if (value != null) {
+ getOptional(properties, PROP_DEFAULT_TRANSACTION_ISOLATION).ifPresent(value -> {
int level = PoolableConnectionFactory.UNKNOWN_TRANSACTION_ISOLATION;
if ("NONE".equalsIgnoreCase(value)) {
level = Connection.TRANSACTION_NONE;
@@ -227,232 +246,60 @@ public class BasicDataSourceFactory implements ObjectFactory {
}
}
dataSource.setDefaultTransactionIsolation(level);
- }
-
- value = properties.getProperty(PROP_DEFAULT_CATALOG);
- if (value != null) {
- dataSource.setDefaultCatalog(value);
- }
-
- value = properties.getProperty(PROP_DEFAULT_SCHEMA);
- if (value != null) {
- dataSource.setDefaultSchema(value);
- }
-
- value = properties.getProperty(PROP_CACHE_STATE);
- if (value != null) {
- dataSource.setCacheState(Boolean.parseBoolean(value));
- }
-
- value = properties.getProperty(PROP_DRIVER_CLASS_NAME);
- if (value != null) {
- dataSource.setDriverClassName(value);
- }
-
- value = properties.getProperty(PROP_LIFO);
- if (value != null) {
- dataSource.setLifo(Boolean.parseBoolean(value));
- }
-
- value = properties.getProperty(PROP_MAX_TOTAL);
- if (value != null) {
- dataSource.setMaxTotal(Integer.parseInt(value));
- }
-
- value = properties.getProperty(PROP_MAX_IDLE);
- if (value != null) {
- dataSource.setMaxIdle(Integer.parseInt(value));
- }
-
- value = properties.getProperty(PROP_MIN_IDLE);
- if (value != null) {
- dataSource.setMinIdle(Integer.parseInt(value));
- }
-
- value = properties.getProperty(PROP_INITIAL_SIZE);
- if (value != null) {
- dataSource.setInitialSize(Integer.parseInt(value));
- }
-
- value = properties.getProperty(PROP_MAX_WAIT_MILLIS);
- if (value != null) {
- dataSource.setMaxWait(Duration.ofMillis(Long.parseLong(value)));
- }
-
- value = properties.getProperty(PROP_TEST_ON_CREATE);
- if (value != null) {
- dataSource.setTestOnCreate(Boolean.parseBoolean(value));
- }
-
- value = properties.getProperty(PROP_TEST_ON_BORROW);
- if (value != null) {
- dataSource.setTestOnBorrow(Boolean.parseBoolean(value));
- }
-
- value = properties.getProperty(PROP_TEST_ON_RETURN);
- if (value != null) {
- dataSource.setTestOnReturn(Boolean.parseBoolean(value));
- }
-
- value = properties.getProperty(PROP_TIME_BETWEEN_EVICTION_RUNS_MILLIS);
- if (value != null) {
- dataSource.setDurationBetweenEvictionRuns(Duration.ofMillis(Long.parseLong(value)));
- }
-
- value = properties.getProperty(PROP_NUM_TESTS_PER_EVICTION_RUN);
- if (value != null) {
- dataSource.setNumTestsPerEvictionRun(Integer.parseInt(value));
- }
-
- value = properties.getProperty(PROP_MIN_EVICTABLE_IDLE_TIME_MILLIS);
- if (value != null) {
- dataSource.setMinEvictableIdle(Duration.ofMillis(Long.parseLong(value)));
- }
-
- value = properties.getProperty(PROP_SOFT_MIN_EVICTABLE_IDLE_TIME_MILLIS);
- if (value != null) {
- dataSource.setSoftMinEvictableIdle(Duration.ofMillis(Long.parseLong(value)));
- }
-
- value = properties.getProperty(PROP_EVICTION_POLICY_CLASS_NAME);
- if (value != null) {
- dataSource.setEvictionPolicyClassName(value);
- }
-
- value = properties.getProperty(PROP_TEST_WHILE_IDLE);
- if (value != null) {
- dataSource.setTestWhileIdle(Boolean.parseBoolean(value));
- }
-
- value = properties.getProperty(PROP_PASSWORD);
- if (value != null) {
- dataSource.setPassword(value);
- }
-
- value = properties.getProperty(PROP_URL);
- if (value != null) {
- dataSource.setUrl(value);
- }
-
- value = properties.getProperty(PROP_USER_NAME);
- if (value != null) {
- dataSource.setUsername(value);
- }
-
- value = properties.getProperty(PROP_VALIDATION_QUERY);
- if (value != null) {
- dataSource.setValidationQuery(value);
- }
-
- value = properties.getProperty(PROP_VALIDATION_QUERY_TIMEOUT);
- if (value != null) {
- dataSource.setValidationQueryTimeout(Duration.ofSeconds(Integer.parseInt(value)));
- }
-
- value = properties.getProperty(PROP_ACCESS_TO_UNDERLYING_CONNECTION_ALLOWED);
- if (value != null) {
- dataSource.setAccessToUnderlyingConnectionAllowed(Boolean.parseBoolean(value));
- }
-
- value = properties.getProperty(PROP_REMOVE_ABANDONED_ON_BORROW);
- if (value != null) {
- dataSource.setRemoveAbandonedOnBorrow(Boolean.parseBoolean(value));
- }
-
- value = properties.getProperty(PROP_REMOVE_ABANDONED_ON_MAINTENANCE);
- if (value != null) {
- dataSource.setRemoveAbandonedOnMaintenance(Boolean.parseBoolean(value));
- }
-
- value = properties.getProperty(PROP_REMOVE_ABANDONED_TIMEOUT);
- if (value != null) {
- dataSource.setRemoveAbandonedTimeout(Duration.ofSeconds(Integer.parseInt(value)));
- }
-
- value = properties.getProperty(PROP_LOG_ABANDONED);
- if (value != null) {
- dataSource.setLogAbandoned(Boolean.parseBoolean(value));
- }
-
- value = properties.getProperty(PROP_ABANDONED_USAGE_TRACKING);
- if (value != null) {
- dataSource.setAbandonedUsageTracking(Boolean.parseBoolean(value));
- }
-
- value = properties.getProperty(PROP_POOL_PREPARED_STATEMENTS);
- if (value != null) {
- dataSource.setPoolPreparedStatements(Boolean.parseBoolean(value));
- }
-
- value = properties.getProperty(PROP_CLEAR_STATEMENT_POOL_ON_RETURN);
- if (value != null) {
- dataSource.setClearStatementPoolOnReturn(Boolean.parseBoolean(value));
- }
-
- value = properties.getProperty(PROP_MAX_OPEN_PREPARED_STATEMENTS);
- if (value != null) {
- dataSource.setMaxOpenPreparedStatements(Integer.parseInt(value));
- }
-
- value = properties.getProperty(PROP_CONNECTION_INIT_SQLS);
- if (value != null) {
- dataSource.setConnectionInitSqls(parseList(value, ';'));
- }
-
- value = properties.getProperty(PROP_CONNECTION_PROPERTIES);
- if (value != null) {
- final Properties p = getProperties(value);
- final Enumeration<?> e = p.propertyNames();
- while (e.hasMoreElements()) {
- final String propertyName = (String) e.nextElement();
- dataSource.addConnectionProperty(propertyName, p.getProperty(propertyName));
+ });
+
+ acceptString(properties, PROP_DEFAULT_SCHEMA, dataSource::setDefaultSchema);
+ acceptString(properties, PROP_DEFAULT_CATALOG, dataSource::setDefaultCatalog);
+ acceptBoolean(properties, PROP_CACHE_STATE, dataSource::setCacheState);
+ acceptString(properties, PROP_DRIVER_CLASS_NAME, dataSource::setDriverClassName);
+ acceptBoolean(properties, PROP_LIFO, dataSource::setLifo);
+ acceptInt(properties, PROP_MAX_TOTAL, dataSource::setMaxTotal);
+ acceptInt(properties, PROP_MAX_IDLE, dataSource::setMaxIdle);
+ acceptInt(properties, PROP_MIN_IDLE, dataSource::setMinIdle);
+ acceptInt(properties, PROP_INITIAL_SIZE, dataSource::setInitialSize);
+ acceptDurationOfMillis(properties, PROP_MAX_WAIT_MILLIS, dataSource::setMaxWait);
+ acceptBoolean(properties, PROP_TEST_ON_CREATE, dataSource::setTestOnCreate);
+ acceptBoolean(properties, PROP_TEST_ON_BORROW, dataSource::setTestOnBorrow);
+ acceptBoolean(properties, PROP_TEST_ON_RETURN, dataSource::setTestOnReturn);
+ acceptDurationOfMillis(properties, PROP_TIME_BETWEEN_EVICTION_RUNS_MILLIS, dataSource::setDurationBetweenEvictionRuns);
+ acceptInt(properties, PROP_NUM_TESTS_PER_EVICTION_RUN, dataSource::setNumTestsPerEvictionRun);
+ acceptDurationOfMillis(properties, PROP_MIN_EVICTABLE_IDLE_TIME_MILLIS, dataSource::setMinEvictableIdle);
+ acceptDurationOfMillis(properties, PROP_SOFT_MIN_EVICTABLE_IDLE_TIME_MILLIS, dataSource::setSoftMinEvictableIdle);
+ acceptString(properties, PROP_EVICTION_POLICY_CLASS_NAME, dataSource::setEvictionPolicyClassName);
+ acceptBoolean(properties, PROP_TEST_WHILE_IDLE, dataSource::setTestWhileIdle);
+ acceptString(properties, PROP_PASSWORD, dataSource::setPassword);
+ acceptString(properties, PROP_URL, dataSource::setUrl);
+ acceptString(properties, PROP_USER_NAME, dataSource::setUsername);
+ acceptString(properties, PROP_VALIDATION_QUERY, dataSource::setValidationQuery);
+ acceptDurationOfSeconds(properties, PROP_VALIDATION_QUERY_TIMEOUT, dataSource::setValidationQueryTimeout);
+ acceptBoolean(properties, PROP_ACCESS_TO_UNDERLYING_CONNECTION_ALLOWED, dataSource::setAccessToUnderlyingConnectionAllowed);
+ acceptBoolean(properties, PROP_REMOVE_ABANDONED_ON_BORROW, dataSource::setRemoveAbandonedOnBorrow);
+ acceptBoolean(properties, PROP_REMOVE_ABANDONED_ON_MAINTENANCE, dataSource::setRemoveAbandonedOnMaintenance);
+ acceptDurationOfSeconds(properties, PROP_REMOVE_ABANDONED_TIMEOUT, dataSource::setRemoveAbandonedTimeout);
+ acceptBoolean(properties, PROP_LOG_ABANDONED, dataSource::setLogAbandoned);
+ acceptBoolean(properties, PROP_ABANDONED_USAGE_TRACKING, dataSource::setAbandonedUsageTracking);
+ acceptBoolean(properties, PROP_POOL_PREPARED_STATEMENTS, dataSource::setPoolPreparedStatements);
+ acceptBoolean(properties, PROP_CLEAR_STATEMENT_POOL_ON_RETURN, dataSource::setClearStatementPoolOnReturn);
+ acceptInt(properties, PROP_MAX_OPEN_PREPARED_STATEMENTS, dataSource::setMaxOpenPreparedStatements);
+ getOptional(properties, PROP_CONNECTION_INIT_SQLS).ifPresent(v -> dataSource.setConnectionInitSqls(parseList(v, ';')));
+
+ final String value = properties.getProperty(PROP_CONNECTION_PROPERTIES);
+ if (value != null) {
+ for (final Object key : getProperties(value).keySet()) {
+ final String propertyName = Objects.toString(key, null);
+ dataSource.addConnectionProperty(propertyName, getProperties(value).getProperty(propertyName));
}
}
- value = properties.getProperty(PROP_MAX_CONN_LIFETIME_MILLIS);
- if (value != null) {
- dataSource.setMaxConn(Duration.ofMillis(Long.parseLong(value)));
- }
-
- value = properties.getProperty(PROP_LOG_EXPIRED_CONNECTIONS);
- if (value != null) {
- dataSource.setLogExpiredConnections(Boolean.parseBoolean(value));
- }
-
- value = properties.getProperty(PROP_JMX_NAME);
- if (value != null) {
- dataSource.setJmxName(value);
- }
-
- value = properties.getProperty(PROP_ENABLE_AUTO_COMMIT_ON_RETURN);
- if (value != null) {
- dataSource.setAutoCommitOnReturn(Boolean.parseBoolean(value));
- }
-
- value = properties.getProperty(PROP_ROLLBACK_ON_RETURN);
- if (value != null) {
- dataSource.setRollbackOnReturn(Boolean.parseBoolean(value));
- }
-
- value = properties.getProperty(PROP_DEFAULT_QUERY_TIMEOUT);
- if (value != null) {
- dataSource.setDefaultQueryTimeout(Duration.ofSeconds(Integer.valueOf(value)));
- }
-
- value = properties.getProperty(PROP_FAST_FAIL_VALIDATION);
- if (value != null) {
- dataSource.setFastFailValidation(Boolean.parseBoolean(value));
- }
-
- value = properties.getProperty(PROP_DISCONNECTION_SQL_CODES);
- if (value != null) {
- dataSource.setDisconnectionSqlCodes(parseList(value, ','));
- }
-
- value = properties.getProperty(PROP_CONNECTION_FACTORY_CLASS_NAME);
- if (value != null) {
- dataSource.setConnectionFactoryClassName(value);
- }
+ acceptDurationOfMillis(properties, PROP_MAX_CONN_LIFETIME_MILLIS, dataSource::setMaxConn);
+ acceptBoolean(properties, PROP_LOG_EXPIRED_CONNECTIONS, dataSource::setLogExpiredConnections);
+ acceptString(properties, PROP_JMX_NAME, dataSource::setJmxName);
+ acceptBoolean(properties, PROP_ENABLE_AUTO_COMMIT_ON_RETURN, dataSource::setAutoCommitOnReturn);
+ acceptBoolean(properties, PROP_ROLLBACK_ON_RETURN, dataSource::setRollbackOnReturn);
+ acceptDurationOfSeconds(properties, PROP_DEFAULT_QUERY_TIMEOUT, dataSource::setDefaultQueryTimeout);
+ acceptBoolean(properties, PROP_FAST_FAIL_VALIDATION, dataSource::setFastFailValidation);
+ getOptional(properties, PROP_DISCONNECTION_SQL_CODES).ifPresent(v -> dataSource.setDisconnectionSqlCodes(parseList(v, ',')));
+ acceptString(properties, PROP_CONNECTION_FACTORY_CLASS_NAME, dataSource::setConnectionFactoryClassName);
// DBCP-215
// Trick to make sure that initialSize connections are created
@@ -464,16 +311,18 @@ public class BasicDataSourceFactory implements ObjectFactory {
return dataSource;
}
+ private static Optional<String> getOptional(final Properties properties, final String name) {
+ return Optional.ofNullable(properties.getProperty(name));
+ }
+
/**
- * <p>
* Parse properties from the string. Format of the string must be [propertyName=property;]*
- * <p>
*
* @param propText
* @return Properties
- * @throws Exception
+ * @throws IOException
*/
- private static Properties getProperties(final String propText) throws Exception {
+ private static Properties getProperties(final String propText) throws IOException {
final Properties p = new Properties();
if (propText != null) {
p.load(new ByteArrayInputStream(propText.replace(';', '\n').getBytes(StandardCharsets.ISO_8859_1)));
@@ -500,10 +349,8 @@ public class BasicDataSourceFactory implements ObjectFactory {
}
/**
- * <p>
- * Create and return a new <code>BasicDataSource</code> instance. If no instance can be created, return
+ * Creates and return a new <code>BasicDataSource</code> instance. If no instance can be created, return
* <code>null</code> instead.
- * </p>
*
* @param obj
* The possibly null object containing location or reference information that can be used in creating an
[commons-dbcp] 02/03: Don't be so verbose.
Posted by gg...@apache.org.
This is an automated email from the ASF dual-hosted git repository.
ggregory pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/commons-dbcp.git
commit 5e2f99645a2cef32f9b8956b245e277e7d68c618
Author: Gary Gregory <ga...@gmail.com>
AuthorDate: Mon Aug 23 12:55:36 2021 -0400
Don't be so verbose.
---
src/changes/changes.xml | 29 -----------------------------
1 file changed, 29 deletions(-)
diff --git a/src/changes/changes.xml b/src/changes/changes.xml
index 22f713b..a236b87 100644
--- a/src/changes/changes.xml
+++ b/src/changes/changes.xml
@@ -82,35 +82,6 @@ The <action> type attribute can be add,update,fix,remove.
- Add and use Duration versions of now deprecated APIs that use ints and longs.
- Internally track durations with Duration objects instead of ints and longs.
- See the JApiCmp report for the complete list.
- - A non-exhaustive list:
-
- - Add BasicDataSource.getMaxConnDuration(), deprecate getMaxConnLifetimeMillis()
- - Add BasicDataSource.getValidationQueryTimeoutDuration(), deprecate - Add BasicDataSource.getValidationQueryTimeout()
- - Add BasicDataSource.setMaxConn(Duration), deprecate setMaxConnLifetimeMillis(long)
- - Add BasicDataSource.setRemoveAbandonedTimeout(Duration), deprecate setRemoveAbandonedTimeout(int)
- - Add BasicDataSource.setValidationQueryTimeout(Duration), deprecate setValidationQueryTimeout(int)
-
- - Add InstanceKeyDataSource.getDefaultDurationBetweenEvictionRuns(), deprecate getDefaultTimeBetweenEvictionRunsMillis()
- - Add InstanceKeyDataSource.getDefaultMinEvictableIdleDuration(), deprecate getDefaultMinEvictableIdleTimeMillis()
- - Add InstanceKeyDataSource.getDefaultSoftMinEvictableIdleDuration(), deprecate getDefaultSoftMinEvictableIdleTimeMillis
- - Add InstanceKeyDataSource.getLoginTimeoutDuration(), deprecate getLoginTimeout()
- - Add InstanceKeyDataSource.getMaxConnDuration(), deprecate getMaxConnLifetime()
- - Add InstanceKeyDataSource.getValidationQueryTimeoutDuration(), deprecate getValidationQueryTimeout()
- - Add InstanceKeyDataSource.setDefaultDurationBetweenEvictionRuns(Duration), deprecate setDefaultTimeBetweenEvictionRunsMillis(long)
- - Add InstanceKeyDataSource.setDefaultMinEvictableIdle(Duration), deprecate setDefaultMinEvictableIdleTimeMillis(long)
- - Add InstanceKeyDataSource.setDefaultSoftMinEvictableIdle(Duration), deprecate setDefaultSoftMinEvictableIdleTimeMillis(long)
- - Add InstanceKeyDataSource.setLoginTimeout(Duration), deprecate setLoginTimeout(int)
- - Add InstanceKeyDataSource.setValidationQueryTimeout(Duration), deprecate setValidationQueryTimeout(int)
-
- - Add PerUserPoolDataSource.getPerUserDurationBetweenEvictionRuns(String), deprecate getPerUserTimeBetweenEvictionRunsMillis(String)
- - Add PerUserPoolDataSource.getPerUserMaxWaitDuration(String), deprecate getPerUserMaxWaitMillis(String)
- - Add PerUserPoolDataSource.getPerUserMinEvictableIdleDuration(String), deprecate getPerUserMinEvictableIdleTimeMillis(String)
- - Add PerUserPoolDataSource.getPerUserSoftMinEvictableIdleDuration(String), deprecate getPerUserSoftMinEvictableIdleTimeMillis(String)
- - Add PerUserPoolDataSource.setPerUserMaxWaitDuration(String,Duration), deprecate setPerUserMaxWaitMillis(String, Long)
- - Add PerUserPoolDataSource.setPerUserMinEvictableIdle(Duration), deprecate setPerUserMinEvictableIdleTimeMillis(String, Long)
- - Add PerUserPoolDataSource.setPerUserSoftMinEvictableIdle(Duration), deprecate setPerUserSoftMinEvictableIdleTimeMillis(String, Long)
- - Add PerUserPoolDataSource.setPerUserSoftMinEvictableIdle(Duration), deprecate setPerUserSoftMinEvictableIdleTimeMillis(String, Long)
- - Add PerUserPoolDataSource.setPerUserDurationBetweenEvictionRuns(Duration), deprecate setPerUserTimeBetweenEvictionRunsMillis(String, Long)
</action>
<!-- UPDATES -->
<action dev="ggregory" type="update" due-to="Dependabot">
[commons-dbcp] 03/03: Fix possible NullPointerException in
BasicDataSourceFactory.validatePropertyNames(Reference, Name, List, List)
Posted by gg...@apache.org.
This is an automated email from the ASF dual-hosted git repository.
ggregory pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/commons-dbcp.git
commit 2555c38ec47b7bc499c3a0274339d7fcf9b86309
Author: Gary Gregory <ga...@gmail.com>
AuthorDate: Mon Aug 23 13:09:51 2021 -0400
Fix possible NullPointerException in
BasicDataSourceFactory.validatePropertyNames(Reference, Name, List,
List)
- Fix possible NullPointerException in
BasicDataSourceFactory.getObjectInstance().
- Use lambdas.
---
src/changes/changes.xml | 6 ++++++
.../commons/dbcp2/BasicDataSourceFactory.java | 25 +++++++++-------------
2 files changed, 16 insertions(+), 15 deletions(-)
diff --git a/src/changes/changes.xml b/src/changes/changes.xml
index a236b87..16e6f26 100644
--- a/src/changes/changes.xml
+++ b/src/changes/changes.xml
@@ -74,6 +74,12 @@ The <action> type attribute can be add,update,fix,remove.
<action dev="ggregory" type="fix" due-to="Gary Gregory">
Migrate away from deprecated APIs in Apache Commons Pool.
</action>
+ <action dev="ggregory" type="fix" due-to="Gary Gregory">
+ Fix possible NullPointerException in BasicDataSourceFactory.validatePropertyNames().
+ </action>
+ <action dev="ggregory" type="fix" due-to="Gary Gregory">
+ Fix possible NullPointerException in BasicDataSourceFactory.getObjectInstance().
+ </action>
<!-- ADDS -->
<action dev="ggregory" type="add" due-to="Gary Gregory">
Add and use AbandonedTrace#setLastUsed(Instant).
diff --git a/src/main/java/org/apache/commons/dbcp2/BasicDataSourceFactory.java b/src/main/java/org/apache/commons/dbcp2/BasicDataSourceFactory.java
index 4ab0cc4..b932ee1 100644
--- a/src/main/java/org/apache/commons/dbcp2/BasicDataSourceFactory.java
+++ b/src/main/java/org/apache/commons/dbcp2/BasicDataSourceFactory.java
@@ -381,22 +381,17 @@ public class BasicDataSourceFactory implements ObjectFactory {
}
// Check property names and log warnings about obsolete and / or unknown properties
- final List<String> warnings = new ArrayList<>();
+ final List<String> warnMessages = new ArrayList<>();
final List<String> infoMessages = new ArrayList<>();
- validatePropertyNames(ref, name, warnings, infoMessages);
- for (final String warning : warnings) {
- log.warn(warning);
- }
- for (final String infoMessage : infoMessages) {
- log.info(infoMessage);
- }
+ validatePropertyNames(ref, name, warnMessages, infoMessages);
+ warnMessages.forEach(log::warn);
+ infoMessages.forEach(log::info);
final Properties properties = new Properties();
for (final String propertyName : ALL_PROPERTIES) {
final RefAddr ra = ref.get(propertyName);
if (ra != null) {
- final String propertyValue = ra.getContent().toString();
- properties.setProperty(propertyName, propertyValue);
+ properties.setProperty(propertyName, Objects.toString(ra.getContent(), null));
}
}
@@ -411,12 +406,12 @@ public class BasicDataSourceFactory implements ObjectFactory {
* Reference to check properties of
* @param name
* Name provided to getObject
- * @param warnings
+ * @param warnMessages
* container for warning messages
* @param infoMessages
* container for info messages
*/
- private void validatePropertyNames(final Reference ref, final Name name, final List<String> warnings,
+ private void validatePropertyNames(final Reference ref, final Name name, final List<String> warnMessages,
final List<String> infoMessages) {
final List<String> allPropsAsList = Arrays.asList(ALL_PROPERTIES);
final String nameString = name != null ? "Name = " + name.toString() + " " : "";
@@ -425,11 +420,11 @@ public class BasicDataSourceFactory implements ObjectFactory {
final RefAddr ra = ref.get(propertyName);
if (ra != null && !allPropsAsList.contains(ra.getType())) {
final StringBuilder stringBuilder = new StringBuilder(nameString);
- final String propertyValue = ra.getContent().toString();
+ final String propertyValue = Objects.toString(ra.getContent(), null);
stringBuilder.append(NUPROP_WARNTEXT.get(propertyName)).append(" You have set value of \"")
.append(propertyValue).append("\" for \"").append(propertyName)
.append("\" property, which is being ignored.");
- warnings.add(stringBuilder.toString());
+ warnMessages.add(stringBuilder.toString());
}
}
}
@@ -442,7 +437,7 @@ public class BasicDataSourceFactory implements ObjectFactory {
// and it is not in the "silent" list, tell user we are ignoring it.
if (!(allPropsAsList.contains(propertyName) || NUPROP_WARNTEXT.containsKey(propertyName)
|| SILENT_PROPERTIES.contains(propertyName))) {
- final String propertyValue = ra.getContent().toString();
+ final String propertyValue = Objects.toString(ra.getContent(), null);
final StringBuilder stringBuilder = new StringBuilder(nameString);
stringBuilder.append("Ignoring unknown property: ").append("value of \"").append(propertyValue)
.append("\" for \"").append(propertyName).append("\" property");