This is an automated email from the ASF dual-hosted git repository.

garydgregory pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/commons-dbcp.git

commit 9f454440c369b2ebaec691e2062ca4534df122d9
Author: Gary Gregory <[email protected]>
AuthorDate: Sun Nov 22 08:42:42 2026 -0500

    Initialize PoolingDriver builders from a system property
    
    Read the delegate-access system property when constructing a builder.
    Preserve the enabled default when the property is absent or unreadable,
    and allow explicit builder settings to override it.
    
    Use JUnit Pioneer to isolate property tests covering parsing, overrides,
    and configuration snapshots.
    
    Update Javadoc.
---
 pom.xml                                            |   5 +
 src/changes/changes.xml                            |   1 +
 .../org/apache/commons/dbcp2/PoolingDriver.java    |  42 +++++--
 .../apache/commons/dbcp2/TestPoolingDriver.java    |  45 ++++++-
 .../dbcp2/TestUnderlyingConnectionAccess.java      | 134 +++++++++++----------
 5 files changed, 154 insertions(+), 73 deletions(-)

diff --git a/pom.xml b/pom.xml
index c564af9f..63538d11 100644
--- a/pom.xml
+++ b/pom.xml
@@ -68,6 +68,11 @@
       <artifactId>junit-jupiter</artifactId>
       <scope>test</scope>
     </dependency>
+    <dependency>
+      <groupId>org.junit-pioneer</groupId>
+      <artifactId>junit-pioneer</artifactId>
+      <scope>test</scope>
+    </dependency>
     <dependency>
       <groupId>org.mockito</groupId>
       <artifactId>mockito-core</artifactId>
diff --git a/src/changes/changes.xml b/src/changes/changes.xml
index e84d2144..fbf12dca 100644
--- a/src/changes/changes.xml
+++ b/src/changes/changes.xml
@@ -77,6 +77,7 @@ The <action> type attribute can be add,update,fix,remove.
       <!-- ADD -->
       <action type="add" dev="ggregory" due-to="Gary Gregory">Add 
DriverAdapterCPDS.exportPasswordAsRefAddr to optionally omit the password 
address from JNDI References. Password export remains enabled by default for 
compatibility; the setting survives JNDI lookup and Java serialization.</action>
       <action type="add" dev="ggregory" due-to="Gary Gregory">Add 
PoolingDriver.Builder and PoolingDriver.builder() to configure delegate access 
when creating drivers, preserving the existing permissive default.</action>
+      <action type="add" dev="ggregory" due-to="Gary Gregory">Initialize 
PoolingDriver.Builder delegate access from the 
org.apache.commons.dbcp2.PoolingDriver.accessToUnderlyingConnectionAllowed 
system property. Preserve the enabled default when the property is absent or 
unreadable, and allow explicit builder settings to override it.</action>
       <!-- UPDATE -->
       <action type="update" dev="ggregory" due-to="Gary Gregory">Bump 
org.apache.commons:commons-parent from 93 to 107.</action>
       <action type="update" dev="ggregory" due-to="Gary Gregory">Bump 
org.apache.commons:commons-pool2 from 2.13.0 to 2.13.1.</action>
diff --git a/src/main/java/org/apache/commons/dbcp2/PoolingDriver.java 
b/src/main/java/org/apache/commons/dbcp2/PoolingDriver.java
index 5d649711..a6e20c23 100644
--- a/src/main/java/org/apache/commons/dbcp2/PoolingDriver.java
+++ b/src/main/java/org/apache/commons/dbcp2/PoolingDriver.java
@@ -40,17 +40,27 @@ public class PoolingDriver implements Driver {
     /**
      * Builds {@link PoolingDriver} instances.
      * <p>
-     * Access to underlying connections is enabled by default, matching {@link 
PoolingDriver#PoolingDriver()}.
+     * Access to underlying connections is initialized from {@link 
#ACCESS_TO_UNDERLYING_CONNECTION_ALLOWED_SYSTEM_PROPERTY}
+     * when the builder is constructed and is enabled by default, matching 
{@link PoolingDriver#PoolingDriver()}.
      * </p>
      *
      * @since 2.15.0
      */
     public static class Builder implements Supplier<PoolingDriver> {
 
-        private boolean accessToUnderlyingConnectionAllowed = true;
+        private static boolean getDefaultAccessToUnderlyingConnectionAllowed() 
{
+            final boolean defaultValue = true;
+            try {
+                return 
Boolean.parseBoolean(System.getProperty(ACCESS_TO_UNDERLYING_CONNECTION_ALLOWED_SYSTEM_PROPERTY,
 Boolean.toString(defaultValue)));
+            } catch (final SecurityException ignored) {
+                return defaultValue;
+            }
+        }
+
+        private boolean accessToUnderlyingConnectionAllowed = 
getDefaultAccessToUnderlyingConnectionAllowed();
 
         /**
-         * Constructs a new builder.
+         * Constructs a new builder using the current system property value.
          */
         public Builder() {
             // empty
@@ -73,8 +83,9 @@ public class PoolingDriver implements Driver {
         /**
          * Sets whether connections and their statements, result sets, and 
database metadata permit access to delegates.
          * <p>
-         * The default is {@code true}. When {@code false}, delegate getters 
return {@code null} and JDBC unwrapping is limited to
-         * interfaces implemented by the wrapper itself.
+         * This setting overrides the value read from {@link 
#ACCESS_TO_UNDERLYING_CONNECTION_ALLOWED_SYSTEM_PROPERTY} when this builder was
+         * constructed. When {@code false}, delegate getters return {@code 
null} and JDBC unwrapping is limited to interfaces implemented by
+         * the wrapper itself.
          * </p>
          *
          * @param accessToUnderlyingConnectionAllowed whether to allow access 
to delegates.
@@ -128,6 +139,22 @@ public class PoolingDriver implements Driver {
         }
     }
 
+    /**
+     * The system property used to initialize access to underlying 
connections: {@value}.
+     * <p>
+     * Each new {@link Builder} reads this property. If it is absent or cannot 
be read because of a {@link SecurityException}, access is enabled. Otherwise,
+     * access is enabled only when the value equals {@code "true"}, ignoring 
case. Existing builders and drivers are unaffected by later property changes. An
+     * explicit call to {@link 
Builder#setAccessToUnderlyingConnectionAllowed(boolean)} overrides the property 
value.
+     * </p>
+     * <p>
+     * The no-argument constructor uses a new builder. To configure the driver 
registered by this class's static initializer, set the property before this 
class
+     * is initialized, for example on the JVM command line.
+     * </p>
+     *
+     * @since 2.15.0
+     */
+    public static final String 
ACCESS_TO_UNDERLYING_CONNECTION_ALLOWED_SYSTEM_PROPERTY = 
"org.apache.commons.dbcp2.PoolingDriver.accessToUnderlyingConnectionAllowed";
+
     private static final DriverPropertyInfo[] EMPTY_DRIVER_PROPERTY_INFO_ARRAY 
= {};
 
     /* Register myself with the {@link DriverManager}. */
@@ -176,9 +203,10 @@ public class PoolingDriver implements Driver {
     private final boolean accessToUnderlyingConnectionAllowed;
 
     /**
-     * Constructs a new driver with {@code 
accessToUnderlyingConnectionAllowed} enabled.
+     * Constructs a new driver using a new builder's configuration.
      * <p>
-     * This permits delegate getters and JDBC unwrapping on its connections, 
statements, result sets, and database metadata.
+     * Access to underlying connections is initialized from {@link 
#ACCESS_TO_UNDERLYING_CONNECTION_ALLOWED_SYSTEM_PROPERTY} and is enabled by
+     * default. When enabled, this permits delegate getters and JDBC 
unwrapping on connections, statements, result sets, and database metadata.
      * </p>
      */
     public PoolingDriver() {
diff --git a/src/test/java/org/apache/commons/dbcp2/TestPoolingDriver.java 
b/src/test/java/org/apache/commons/dbcp2/TestPoolingDriver.java
index 46d5223c..f8361022 100644
--- a/src/test/java/org/apache/commons/dbcp2/TestPoolingDriver.java
+++ b/src/test/java/org/apache/commons/dbcp2/TestPoolingDriver.java
@@ -45,19 +45,32 @@ import org.apache.commons.pool2.ObjectPool;
 import org.apache.commons.pool2.impl.GenericObjectPool;
 import org.apache.commons.pool2.impl.GenericObjectPoolConfig;
 import org.junit.jupiter.api.AfterEach;
+import org.junit.jupiter.api.BeforeAll;
 import org.junit.jupiter.api.BeforeEach;
 import org.junit.jupiter.api.Test;
+import org.junit.jupiter.params.ParameterizedTest;
+import org.junit.jupiter.params.provider.CsvSource;
+import org.junitpioneer.jupiter.ClearSystemProperty;
+import org.junitpioneer.jupiter.ReadsSystemProperty;
+import org.junitpioneer.jupiter.SetSystemProperty;
 
 /**
  * Tests for a {@link GenericObjectPool} based {@link PoolingDriver}.
  */
+@ReadsSystemProperty
 public class TestPoolingDriver extends TestConnectionPool {
 
+    @BeforeAll
+    static void loadDriver() throws ClassNotFoundException {
+        // Initialize the registered driver before individual tests change the 
builder's default property.
+        Class.forName(PoolingDriver.class.getName());
+    }
+
     private PoolingDriver driver;
 
     @Override
     protected Connection getConnection() throws Exception {
-        return DriverManager.getConnection("jdbc:apache:commons:dbcp:test");
+        return driver.connect(PoolingDriver.URL_PREFIX + "test", null);
     }
 
     @BeforeEach
@@ -144,6 +157,7 @@ public class TestPoolingDriver extends TestConnectionPool {
     }
 
     @Test
+    @ClearSystemProperty(key = 
PoolingDriver.ACCESS_TO_UNDERLYING_CONNECTION_ALLOWED_SYSTEM_PROPERTY)
     void testBuilderDefaults() {
         final Supplier<PoolingDriver> supplier = PoolingDriver.builder();
         final PoolingDriver first = supplier.get();
@@ -156,6 +170,35 @@ public class TestPoolingDriver extends TestConnectionPool {
         
assertFalse(restrictedBuilder.get().isAccessToUnderlyingConnectionAllowed());
     }
 
+    @ParameterizedTest
+    @CsvSource({"true, true", "TRUE, true", "TrUe, true", "false, false", 
"FALSE, false", "'', false", "invalid, false"})
+    @ClearSystemProperty(key = 
PoolingDriver.ACCESS_TO_UNDERLYING_CONNECTION_ALLOWED_SYSTEM_PROPERTY)
+    void testBuilderSystemProperty(final String value, final boolean expected) 
throws Exception {
+        
System.setProperty(PoolingDriver.ACCESS_TO_UNDERLYING_CONNECTION_ALLOWED_SYSTEM_PROPERTY,
 value);
+        final PoolingDriver.Builder builder = PoolingDriver.builder();
+        final PoolingDriver configured = builder.get();
+        assertEquals(expected, 
configured.isAccessToUnderlyingConnectionAllowed());
+        assertEquals(expected, new 
PoolingDriver.Builder().get().isAccessToUnderlyingConnectionAllowed());
+        assertEquals(expected, new 
PoolingDriver().isAccessToUnderlyingConnectionAllowed());
+        try (Connection connection = 
configured.connect(PoolingDriver.URL_PREFIX + "test", null)) {
+            assertEquals(expected, 
connection.isWrapperFor(TesterConnection.class));
+        }
+        assertEquals(!expected, 
builder.setAccessToUnderlyingConnectionAllowed(!expected).get().isAccessToUnderlyingConnectionAllowed());
+        assertEquals(expected, 
configured.isAccessToUnderlyingConnectionAllowed());
+    }
+
+    @Test
+    @SetSystemProperty(key = 
PoolingDriver.ACCESS_TO_UNDERLYING_CONNECTION_ALLOWED_SYSTEM_PROPERTY, value = 
"false")
+    void testBuilderSystemPropertySnapshot() {
+        final PoolingDriver.Builder builder = PoolingDriver.builder();
+        final PoolingDriver configured = builder.get();
+        
System.setProperty(PoolingDriver.ACCESS_TO_UNDERLYING_CONNECTION_ALLOWED_SYSTEM_PROPERTY,
 "true");
+        assertFalse(configured.isAccessToUnderlyingConnectionAllowed());
+        assertFalse(builder.get().isAccessToUnderlyingConnectionAllowed());
+        
assertTrue(PoolingDriver.builder().get().isAccessToUnderlyingConnectionAllowed());
+        assertTrue(new 
PoolingDriver().isAccessToUnderlyingConnectionAllowed());
+    }
+
     @Test
     void testClosePool() throws Exception {
         final Connection conn = 
DriverManager.getConnection("jdbc:apache:commons:dbcp:test");
diff --git 
a/src/test/java/org/apache/commons/dbcp2/TestUnderlyingConnectionAccess.java 
b/src/test/java/org/apache/commons/dbcp2/TestUnderlyingConnectionAccess.java
index 1af74dfc..04c42e53 100644
--- a/src/test/java/org/apache/commons/dbcp2/TestUnderlyingConnectionAccess.java
+++ b/src/test/java/org/apache/commons/dbcp2/TestUnderlyingConnectionAccess.java
@@ -61,10 +61,13 @@ import org.junit.jupiter.params.ParameterizedTest;
 import org.junit.jupiter.params.provider.Arguments;
 import org.junit.jupiter.params.provider.MethodSource;
 import org.junit.jupiter.params.provider.ValueSource;
+import org.junitpioneer.jupiter.ClearSystemProperty;
+import org.junitpioneer.jupiter.ReadsSystemProperty;
 
 /**
  * Exercises delegate access through every pool guard and JDBC wrapper, with 
and without statement pooling and nested wrappers.
  */
+@ReadsSystemProperty
 class TestUnderlyingConnectionAccess {
 
     private enum CloseWith {
@@ -285,51 +288,6 @@ class TestUnderlyingConnectionAccess {
                 Arrays.stream(StatementKind.values()).map(kind -> 
Arguments.of(source, allowed, poolStatements, nested, kind))))));
     }
 
-    @ParameterizedTest
-    @MethodSource("statementArguments")
-    void testAccessAndExecution(final Source source, final boolean allowed, 
final boolean poolStatements, final boolean nested,
-            final StatementKind kind) throws Exception {
-        try (Fixture fixture = createFixture(source, allowed, poolStatements, 
nested)) {
-            final Connection connection = fixture.connection;
-            assertConnectionAccess(connection, allowed);
-            assertFalse(connection.isWrapperFor(String.class));
-            assertThrows(SQLException.class, () -> 
connection.unwrap(String.class));
-            try (Statement statement = kind.create(connection)) {
-                assertSame(connection, statement.getConnection());
-                assertStatementAccess(statement, allowed);
-                assertNotEquals("NULL", statement.toString());
-                final ResultSet resultSet = execute(statement);
-                try (ResultSet ignored = resultSet) {
-                    assertSame(statement, resultSet.getStatement());
-                    assertResultSetAccess(resultSet, allowed);
-                    assertTrue(resultSet.next());
-                    assertEquals(7, resultSet.getInt(1));
-                    if (statement instanceof CallableStatement) {
-                        assertEquals(7, ((CallableStatement) 
statement).getInt(1));
-                    }
-                    if (!allowed && !nested) {
-                        assertNull(((DelegatingConnection<?>) 
connection).getDelegate());
-                        assertNull(((DelegatingStatement) 
statement).getDelegate());
-                        assertNull(((DelegatingResultSet) 
resultSet).getDelegate());
-                    }
-                }
-                assertResultSetAccess(resultSet, allowed);
-            }
-            final DatabaseMetaData metadata = connection.getMetaData();
-            assertSame(connection, metadata.getConnection());
-            assertMetadataAccess(metadata, allowed);
-            if (!allowed && !nested) {
-                assertNull(((DelegatingDatabaseMetaData) 
metadata).getDelegate());
-            }
-            final ResultSet tables = metadata.getTables(null, null, null, 
null);
-            try (ResultSet ignored = tables) {
-                assertResultSetAccess(tables, allowed);
-                assertNull(tables.getStatement());
-            }
-            assertResultSetAccess(tables, allowed);
-        }
-    }
-
     @ParameterizedTest
     @MethodSource("closeArguments")
     void testAccessAfterClose(final Source source, final boolean allowed, 
final boolean nested, final StatementKind kind,
@@ -413,6 +371,51 @@ class TestUnderlyingConnectionAccess {
         }
     }
 
+    @ParameterizedTest
+    @MethodSource("statementArguments")
+    void testAccessAndExecution(final Source source, final boolean allowed, 
final boolean poolStatements, final boolean nested,
+            final StatementKind kind) throws Exception {
+        try (Fixture fixture = createFixture(source, allowed, poolStatements, 
nested)) {
+            final Connection connection = fixture.connection;
+            assertConnectionAccess(connection, allowed);
+            assertFalse(connection.isWrapperFor(String.class));
+            assertThrows(SQLException.class, () -> 
connection.unwrap(String.class));
+            try (Statement statement = kind.create(connection)) {
+                assertSame(connection, statement.getConnection());
+                assertStatementAccess(statement, allowed);
+                assertNotEquals("NULL", statement.toString());
+                final ResultSet resultSet = execute(statement);
+                try (ResultSet ignored = resultSet) {
+                    assertSame(statement, resultSet.getStatement());
+                    assertResultSetAccess(resultSet, allowed);
+                    assertTrue(resultSet.next());
+                    assertEquals(7, resultSet.getInt(1));
+                    if (statement instanceof CallableStatement) {
+                        assertEquals(7, ((CallableStatement) 
statement).getInt(1));
+                    }
+                    if (!allowed && !nested) {
+                        assertNull(((DelegatingConnection<?>) 
connection).getDelegate());
+                        assertNull(((DelegatingStatement) 
statement).getDelegate());
+                        assertNull(((DelegatingResultSet) 
resultSet).getDelegate());
+                    }
+                }
+                assertResultSetAccess(resultSet, allowed);
+            }
+            final DatabaseMetaData metadata = connection.getMetaData();
+            assertSame(connection, metadata.getConnection());
+            assertMetadataAccess(metadata, allowed);
+            if (!allowed && !nested) {
+                assertNull(((DelegatingDatabaseMetaData) 
metadata).getDelegate());
+            }
+            final ResultSet tables = metadata.getTables(null, null, null, 
null);
+            try (ResultSet ignored = tables) {
+                assertResultSetAccess(tables, allowed);
+                assertNull(tables.getStatement());
+            }
+            assertResultSetAccess(tables, allowed);
+        }
+    }
+
     @ParameterizedTest
     @ValueSource(booleans = {false, true})
     void testGeneratedKeys(final boolean allowed) throws Exception {
@@ -432,6 +435,26 @@ class TestUnderlyingConnectionAccess {
         }
     }
 
+    @Test
+    void testGuardHookAndInternalAccess() throws Exception {
+        try (Connection physical = new Driver().connect(newUrl(), new 
Properties());
+                DelegatingConnection<Connection> connection = new 
DelegatingConnection<Connection>(physical) {
+                    @Override
+                    protected boolean isAccessToUnderlyingConnectionAllowed() {
+                        return false;
+                    }
+                }; Statement statement = connection.createStatement(); 
ResultSet resultSet = statement.executeQuery("SELECT 7")) {
+            assertNull(connection.getDelegate());
+            assertConnectionAccess(connection, false);
+            assertStatementAccess(statement, false);
+            assertResultSetAccess(resultSet, false);
+            assertMetadataAccess(connection.getMetaData(), false);
+            // Internal access remains available to pool lifecycle operations.
+            assertSame(physical, connection.getDelegateInternal());
+            assertSame(physical, connection.getInnermostDelegateInternal());
+        }
+    }
+
     @ParameterizedTest
     @ValueSource(booleans = {false, true})
     void testInvalidateWrappedConnection(final boolean allowed) throws 
Exception {
@@ -458,26 +481,6 @@ class TestUnderlyingConnectionAccess {
         }
     }
 
-    @Test
-    void testGuardHookAndInternalAccess() throws Exception {
-        try (Connection physical = new Driver().connect(newUrl(), new 
Properties());
-                DelegatingConnection<Connection> connection = new 
DelegatingConnection<Connection>(physical) {
-                    @Override
-                    protected boolean isAccessToUnderlyingConnectionAllowed() {
-                        return false;
-                    }
-                }; Statement statement = connection.createStatement(); 
ResultSet resultSet = statement.executeQuery("SELECT 7")) {
-            assertNull(connection.getDelegate());
-            assertConnectionAccess(connection, false);
-            assertStatementAccess(statement, false);
-            assertResultSetAccess(resultSet, false);
-            assertMetadataAccess(connection.getMetaData(), false);
-            // Internal access remains available to pool lifecycle operations.
-            assertSame(physical, connection.getDelegateInternal());
-            assertSame(physical, connection.getInnermostDelegateInternal());
-        }
-    }
-
     @ParameterizedTest
     @ValueSource(booleans = {false, true})
     void testManagedTransaction(final boolean allowed) throws Exception {
@@ -547,6 +550,7 @@ class TestUnderlyingConnectionAccess {
     }
 
     @Test
+    @ClearSystemProperty(key = 
PoolingDriver.ACCESS_TO_UNDERLYING_CONNECTION_ALLOWED_SYSTEM_PROPERTY)
     void testPlainWrappersAndDriverDefault() throws Exception {
         assertTrue(new 
PoolingDriver().isAccessToUnderlyingConnectionAllowed());
         try (Connection physical = new Driver().connect(newUrl(), new 
Properties());

Reply via email to