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
The following commit(s) were added to refs/heads/master by this push: new 6758c426 Make package-private classes final 6758c426 is described below commit 6758c426aa908ea061bbcb41f9fe77a0b9e2a500 Author: Gary Gregory <gardgreg...@gmail.com> AuthorDate: Sat Jul 9 18:44:20 2022 -0400 Make package-private classes final --- .../commons/dbcp2/ConnectionFactoryFactory.java | 152 ++--- .../org/apache/commons/dbcp2/DriverFactory.java | 160 +++--- .../commons/dbcp2/LifetimeExceededException.java | 2 +- .../apache/commons/dbcp2/ObjectNameWrapper.java | 208 +++---- .../commons/dbcp2/cpdsadapter/ConnectionImpl.java | 614 ++++++++++----------- .../dbcp2/cpdsadapter/PooledConnectionImpl.java | 2 +- .../dbcp2/datasources/CPDSConnectionFactory.java | 2 +- .../datasources/KeyedCPDSConnectionFactory.java | 2 +- .../apache/commons/dbcp2/datasources/PoolKey.java | 134 ++--- .../commons/dbcp2/datasources/UserPassKey.java | 238 ++++---- 10 files changed, 757 insertions(+), 757 deletions(-) diff --git a/src/main/java/org/apache/commons/dbcp2/ConnectionFactoryFactory.java b/src/main/java/org/apache/commons/dbcp2/ConnectionFactoryFactory.java index b008413c..451edf1a 100644 --- a/src/main/java/org/apache/commons/dbcp2/ConnectionFactoryFactory.java +++ b/src/main/java/org/apache/commons/dbcp2/ConnectionFactoryFactory.java @@ -1,76 +1,76 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one or more - * contributor license agreements. See the NOTICE file distributed with - * this work for additional information regarding copyright ownership. - * The ASF licenses this file to You under the Apache License, Version 2.0 - * (the "License"); you may not use this file except in compliance with - * the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.apache.commons.dbcp2; - -import java.sql.Driver; -import java.sql.SQLException; -import java.util.Properties; - -/* - * Creates {@link ConnectionFactory} instances. - * - * @since 2.7.0 - */ -class ConnectionFactoryFactory { - - /** - * Creates a new {@link DriverConnectionFactory} allowing for an override through - * {@link BasicDataSource#getDriverClassName()}. - * - * @param basicDataSource Configures creation. - * @param driver The JDBC driver. - * @return a new {@link DriverConnectionFactory} allowing for a {@link BasicDataSource#getDriverClassName()} - * override. - * @throws SQLException Thrown when instantiation fails. - */ - static ConnectionFactory createConnectionFactory(final BasicDataSource basicDataSource, final Driver driver) - throws SQLException { - final Properties connectionProperties = basicDataSource.getConnectionProperties(); - final String url = basicDataSource.getUrl(); - // Set up the driver connection factory we will use - final String user = basicDataSource.getUsername(); - if (user != null) { - connectionProperties.put(Constants.KEY_USER, user); - } else { - basicDataSource.log("DBCP DataSource configured without a 'username'"); - } - - final String pwd = basicDataSource.getPassword(); - if (pwd != null) { - connectionProperties.put("password", pwd); - } else { - basicDataSource.log("DBCP DataSource configured without a 'password'"); - } - final String connectionFactoryClassName = basicDataSource.getConnectionFactoryClassName(); - if (connectionFactoryClassName != null) { - try { - final Class<?> connectionFactoryFromCCL = Class.forName(connectionFactoryClassName); - return (ConnectionFactory) connectionFactoryFromCCL - .getConstructor(Driver.class, String.class, Properties.class) - .newInstance(driver, url, connectionProperties); - } catch (final Exception t) { - final String message = "Cannot load ConnectionFactory implementation '" + connectionFactoryClassName - + "'"; - basicDataSource.log(message, t); - throw new SQLException(message, t); - } - } - // Defaults to DriverConnectionFactory - return new DriverConnectionFactory(driver, url, connectionProperties); - } - -} +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.commons.dbcp2; + +import java.sql.Driver; +import java.sql.SQLException; +import java.util.Properties; + +/* + * Creates {@link ConnectionFactory} instances. + * + * @since 2.7.0 + */ +final class ConnectionFactoryFactory { + + /** + * Creates a new {@link DriverConnectionFactory} allowing for an override through + * {@link BasicDataSource#getDriverClassName()}. + * + * @param basicDataSource Configures creation. + * @param driver The JDBC driver. + * @return a new {@link DriverConnectionFactory} allowing for a {@link BasicDataSource#getDriverClassName()} + * override. + * @throws SQLException Thrown when instantiation fails. + */ + static ConnectionFactory createConnectionFactory(final BasicDataSource basicDataSource, final Driver driver) + throws SQLException { + final Properties connectionProperties = basicDataSource.getConnectionProperties(); + final String url = basicDataSource.getUrl(); + // Set up the driver connection factory we will use + final String user = basicDataSource.getUsername(); + if (user != null) { + connectionProperties.put(Constants.KEY_USER, user); + } else { + basicDataSource.log("DBCP DataSource configured without a 'username'"); + } + + final String pwd = basicDataSource.getPassword(); + if (pwd != null) { + connectionProperties.put("password", pwd); + } else { + basicDataSource.log("DBCP DataSource configured without a 'password'"); + } + final String connectionFactoryClassName = basicDataSource.getConnectionFactoryClassName(); + if (connectionFactoryClassName != null) { + try { + final Class<?> connectionFactoryFromCCL = Class.forName(connectionFactoryClassName); + return (ConnectionFactory) connectionFactoryFromCCL + .getConstructor(Driver.class, String.class, Properties.class) + .newInstance(driver, url, connectionProperties); + } catch (final Exception t) { + final String message = "Cannot load ConnectionFactory implementation '" + connectionFactoryClassName + + "'"; + basicDataSource.log(message, t); + throw new SQLException(message, t); + } + } + // Defaults to DriverConnectionFactory + return new DriverConnectionFactory(driver, url, connectionProperties); + } + +} diff --git a/src/main/java/org/apache/commons/dbcp2/DriverFactory.java b/src/main/java/org/apache/commons/dbcp2/DriverFactory.java index 28d57051..77704e48 100644 --- a/src/main/java/org/apache/commons/dbcp2/DriverFactory.java +++ b/src/main/java/org/apache/commons/dbcp2/DriverFactory.java @@ -1,80 +1,80 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one or more - * contributor license agreements. See the NOTICE file distributed with - * this work for additional information regarding copyright ownership. - * The ASF licenses this file to You under the Apache License, Version 2.0 - * (the "License"); you may not use this file except in compliance with - * the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.apache.commons.dbcp2; - -import java.sql.Driver; -import java.sql.DriverManager; -import java.sql.SQLException; - -/* - * Creates {@link Driver} instances. - * - * @since 2.7.0 - */ -class DriverFactory { - - static Driver createDriver(final BasicDataSource basicDataSource) throws SQLException { - // Load the JDBC driver class - Driver driverToUse = basicDataSource.getDriver(); - final String driverClassName = basicDataSource.getDriverClassName(); - final ClassLoader driverClassLoader = basicDataSource.getDriverClassLoader(); - final String url = basicDataSource.getUrl(); - - if (driverToUse == null) { - Class<?> driverFromCCL = null; - if (driverClassName != null) { - try { - try { - if (driverClassLoader == null) { - driverFromCCL = Class.forName(driverClassName); - } else { - driverFromCCL = Class.forName(driverClassName, true, driverClassLoader); - } - } catch (final ClassNotFoundException cnfe) { - driverFromCCL = Thread.currentThread().getContextClassLoader().loadClass(driverClassName); - } - } catch (final Exception t) { - final String message = "Cannot load JDBC driver class '" + driverClassName + "'"; - basicDataSource.log(message, t); - throw new SQLException(message, t); - } - } - - try { - if (driverFromCCL == null) { - driverToUse = DriverManager.getDriver(url); - } else { - // Usage of DriverManager is not possible, as it does not - // respect the ContextClassLoader - // N.B. This cast may cause ClassCastException which is - // handled below - driverToUse = (Driver) driverFromCCL.getConstructor().newInstance(); - if (!driverToUse.acceptsURL(url)) { - throw new SQLException("No suitable driver", "08001"); - } - } - } catch (final Exception t) { - final String message = "Cannot create JDBC driver of class '" - + (driverClassName != null ? driverClassName : "") + "' for connect URL '" + url + "'"; - basicDataSource.log(message, t); - throw new SQLException(message, t); - } - } - return driverToUse; - } - -} +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.commons.dbcp2; + +import java.sql.Driver; +import java.sql.DriverManager; +import java.sql.SQLException; + +/* + * Creates {@link Driver} instances. + * + * @since 2.7.0 + */ +final class DriverFactory { + + static Driver createDriver(final BasicDataSource basicDataSource) throws SQLException { + // Load the JDBC driver class + Driver driverToUse = basicDataSource.getDriver(); + final String driverClassName = basicDataSource.getDriverClassName(); + final ClassLoader driverClassLoader = basicDataSource.getDriverClassLoader(); + final String url = basicDataSource.getUrl(); + + if (driverToUse == null) { + Class<?> driverFromCCL = null; + if (driverClassName != null) { + try { + try { + if (driverClassLoader == null) { + driverFromCCL = Class.forName(driverClassName); + } else { + driverFromCCL = Class.forName(driverClassName, true, driverClassLoader); + } + } catch (final ClassNotFoundException cnfe) { + driverFromCCL = Thread.currentThread().getContextClassLoader().loadClass(driverClassName); + } + } catch (final Exception t) { + final String message = "Cannot load JDBC driver class '" + driverClassName + "'"; + basicDataSource.log(message, t); + throw new SQLException(message, t); + } + } + + try { + if (driverFromCCL == null) { + driverToUse = DriverManager.getDriver(url); + } else { + // Usage of DriverManager is not possible, as it does not + // respect the ContextClassLoader + // N.B. This cast may cause ClassCastException which is + // handled below + driverToUse = (Driver) driverFromCCL.getConstructor().newInstance(); + if (!driverToUse.acceptsURL(url)) { + throw new SQLException("No suitable driver", "08001"); + } + } + } catch (final Exception t) { + final String message = "Cannot create JDBC driver of class '" + + (driverClassName != null ? driverClassName : "") + "' for connect URL '" + url + "'"; + basicDataSource.log(message, t); + throw new SQLException(message, t); + } + } + return driverToUse; + } + +} diff --git a/src/main/java/org/apache/commons/dbcp2/LifetimeExceededException.java b/src/main/java/org/apache/commons/dbcp2/LifetimeExceededException.java index ff48941f..cfc2bec9 100644 --- a/src/main/java/org/apache/commons/dbcp2/LifetimeExceededException.java +++ b/src/main/java/org/apache/commons/dbcp2/LifetimeExceededException.java @@ -23,7 +23,7 @@ import java.sql.SQLException; * * @since 2.1 */ -class LifetimeExceededException extends SQLException { +final class LifetimeExceededException extends SQLException { private static final long serialVersionUID = -3783783104516492659L; diff --git a/src/main/java/org/apache/commons/dbcp2/ObjectNameWrapper.java b/src/main/java/org/apache/commons/dbcp2/ObjectNameWrapper.java index e0ffd1f0..189b79e5 100644 --- a/src/main/java/org/apache/commons/dbcp2/ObjectNameWrapper.java +++ b/src/main/java/org/apache/commons/dbcp2/ObjectNameWrapper.java @@ -1,104 +1,104 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one or more - * contributor license agreements. See the NOTICE file distributed with - * this work for additional information regarding copyright ownership. - * The ASF licenses this file to You under the Apache License, Version 2.0 - * (the "License"); you may not use this file except in compliance with - * the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.apache.commons.dbcp2; - -import java.lang.management.ManagementFactory; -import java.util.Objects; - -import javax.management.MBeanServer; -import javax.management.MalformedObjectNameException; -import javax.management.ObjectName; - -import org.apache.commons.logging.Log; -import org.apache.commons.logging.LogFactory; - -/** - * Internal wrapper class that allows JMX to be a noop if absent or disabled. - * - * @since 2.2.1 - */ -class ObjectNameWrapper { - - private static final Log log = LogFactory.getLog(ObjectNameWrapper.class); - - private static final MBeanServer MBEAN_SERVER = getPlatformMBeanServer(); - - private static MBeanServer getPlatformMBeanServer() { - try { - return ManagementFactory.getPlatformMBeanServer(); - } catch (final LinkageError | Exception e) { - // ignore - JMX not available - log.debug("Failed to get platform MBeanServer", e); - return null; - } - } - - public static ObjectName unwrap(final ObjectNameWrapper wrapper) { - return wrapper == null ? null : wrapper.unwrap(); - } - - public static ObjectNameWrapper wrap(final ObjectName objectName) { - return new ObjectNameWrapper(objectName); - } - - public static ObjectNameWrapper wrap(final String name) throws MalformedObjectNameException { - return wrap(new ObjectName(name)); - } - - private final ObjectName objectName; - - public ObjectNameWrapper(final ObjectName objectName) { - this.objectName = objectName; - } - - public void registerMBean(final Object object) { - if (MBEAN_SERVER == null || objectName == null) { - return; - } - try { - MBEAN_SERVER.registerMBean(object, objectName); - } catch (final LinkageError | Exception e) { - log.warn("Failed to complete JMX registration for " + objectName, e); - } - } - - /** - * @since 2.7.0 - */ - @Override - public String toString() { - return Objects.toString(objectName); - } - - public void unregisterMBean() { - if (MBEAN_SERVER == null || objectName == null) { - return; - } - if (MBEAN_SERVER.isRegistered(objectName)) { - try { - MBEAN_SERVER.unregisterMBean(objectName); - } catch (final LinkageError | Exception e) { - log.warn("Failed to complete JMX unregistration for " + objectName, e); - } - } - } - - public ObjectName unwrap() { - return objectName; - } - -} +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.commons.dbcp2; + +import java.lang.management.ManagementFactory; +import java.util.Objects; + +import javax.management.MBeanServer; +import javax.management.MalformedObjectNameException; +import javax.management.ObjectName; + +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; + +/** + * Internal wrapper class that allows JMX to be a noop if absent or disabled. + * + * @since 2.2.1 + */ +final class ObjectNameWrapper { + + private static final Log log = LogFactory.getLog(ObjectNameWrapper.class); + + private static final MBeanServer MBEAN_SERVER = getPlatformMBeanServer(); + + private static MBeanServer getPlatformMBeanServer() { + try { + return ManagementFactory.getPlatformMBeanServer(); + } catch (final LinkageError | Exception e) { + // ignore - JMX not available + log.debug("Failed to get platform MBeanServer", e); + return null; + } + } + + public static ObjectName unwrap(final ObjectNameWrapper wrapper) { + return wrapper == null ? null : wrapper.unwrap(); + } + + public static ObjectNameWrapper wrap(final ObjectName objectName) { + return new ObjectNameWrapper(objectName); + } + + public static ObjectNameWrapper wrap(final String name) throws MalformedObjectNameException { + return wrap(new ObjectName(name)); + } + + private final ObjectName objectName; + + public ObjectNameWrapper(final ObjectName objectName) { + this.objectName = objectName; + } + + public void registerMBean(final Object object) { + if (MBEAN_SERVER == null || objectName == null) { + return; + } + try { + MBEAN_SERVER.registerMBean(object, objectName); + } catch (final LinkageError | Exception e) { + log.warn("Failed to complete JMX registration for " + objectName, e); + } + } + + /** + * @since 2.7.0 + */ + @Override + public String toString() { + return Objects.toString(objectName); + } + + public void unregisterMBean() { + if (MBEAN_SERVER == null || objectName == null) { + return; + } + if (MBEAN_SERVER.isRegistered(objectName)) { + try { + MBEAN_SERVER.unregisterMBean(objectName); + } catch (final LinkageError | Exception e) { + log.warn("Failed to complete JMX unregistration for " + objectName, e); + } + } + } + + public ObjectName unwrap() { + return objectName; + } + +} diff --git a/src/main/java/org/apache/commons/dbcp2/cpdsadapter/ConnectionImpl.java b/src/main/java/org/apache/commons/dbcp2/cpdsadapter/ConnectionImpl.java index ee5be1b1..22f1b523 100644 --- a/src/main/java/org/apache/commons/dbcp2/cpdsadapter/ConnectionImpl.java +++ b/src/main/java/org/apache/commons/dbcp2/cpdsadapter/ConnectionImpl.java @@ -1,307 +1,307 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one or more - * contributor license agreements. See the NOTICE file distributed with - * this work for additional information regarding copyright ownership. - * The ASF licenses this file to You under the Apache License, Version 2.0 - * (the "License"); you may not use this file except in compliance with - * the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.apache.commons.dbcp2.cpdsadapter; - -import java.sql.CallableStatement; -import java.sql.Connection; -import java.sql.PreparedStatement; -import java.sql.SQLException; - -import org.apache.commons.dbcp2.DelegatingCallableStatement; -import org.apache.commons.dbcp2.DelegatingConnection; -import org.apache.commons.dbcp2.DelegatingPreparedStatement; - -/** - * This class is the {@code Connection} that will be returned from - * {@code PooledConnectionImpl.getConnection()}. Most methods are wrappers around the JDBC 1.x - * {@code Connection}. A few exceptions include preparedStatement and close. In accordance with the JDBC - * specification this Connection cannot be used after closed() is called. Any further usage will result in an - * SQLException. - * <p> - * ConnectionImpl extends DelegatingConnection to enable access to the underlying connection. - * </p> - * - * @since 2.0 - */ -class ConnectionImpl extends DelegatingConnection<Connection> { - - private final boolean accessToUnderlyingConnectionAllowed; - - /** The object that instantiated this object */ - private final PooledConnectionImpl pooledConnection; - - /** - * Creates a {@code ConnectionImpl}. - * - * @param pooledConnection - * The PooledConnection that is calling the ctor. - * @param connection - * The JDBC 1.x Connection to wrap. - * @param accessToUnderlyingConnectionAllowed - * if true, then access is allowed to the underlying connection - */ - ConnectionImpl(final PooledConnectionImpl pooledConnection, final Connection connection, - final boolean accessToUnderlyingConnectionAllowed) { - super(connection); - this.pooledConnection = pooledConnection; - this.accessToUnderlyingConnectionAllowed = accessToUnderlyingConnectionAllowed; - } - - /** - * Marks the Connection as closed, and notifies the pool that the pooled connection is available. - * <p> - * In accordance with the JDBC specification this Connection cannot be used after closed() is called. Any further - * usage will result in an SQLException. - * </p> - * - * @throws SQLException - * The database connection couldn't be closed. - */ - @Override - public void close() throws SQLException { - if (!isClosedInternal()) { - try { - passivate(); - } finally { - setClosedInternal(true); - pooledConnection.notifyListeners(); - } - } - } - - /** - * Get the delegated connection, if allowed. - * - * @return the internal connection, or null if access is not allowed. - * @see #isAccessToUnderlyingConnectionAllowed() - */ - @Override - public Connection getDelegate() { - if (isAccessToUnderlyingConnectionAllowed()) { - return getDelegateInternal(); - } - return null; - } - - /** - * Get the innermost connection, if allowed. - * - * @return the innermost internal connection, or null if access is not allowed. - * @see #isAccessToUnderlyingConnectionAllowed() - */ - @Override - public Connection getInnermostDelegate() { - if (isAccessToUnderlyingConnectionAllowed()) { - return super.getInnermostDelegateInternal(); - } - return null; - } - - /** - * If false, getDelegate() and getInnermostDelegate() will return null. - * - * @return true if access is allowed to the underlying connection - * @see ConnectionImpl - */ - public boolean isAccessToUnderlyingConnectionAllowed() { - return accessToUnderlyingConnectionAllowed; - } - - /** - * If pooling of {@code CallableStatement}s is turned on in the {@link DriverAdapterCPDS}, a pooled object may - * be returned, otherwise delegate to the wrapped JDBC 1.x {@link java.sql.Connection}. - * - * @param sql - * an SQL statement that may contain one or more '?' parameter placeholders. Typically this statement is - * specified using JDBC call escape syntax. - * @return a default {@code CallableStatement} object containing the pre-compiled SQL statement. - * @throws SQLException - * Thrown if a database access error occurs or this method is called on a closed connection. - * @since 2.4.0 - */ - @Override - public CallableStatement prepareCall(final String sql) throws SQLException { - checkOpen(); - try { - return new DelegatingCallableStatement(this, pooledConnection.prepareCall(sql)); - } catch (final SQLException e) { - handleException(e); // Does not return - return null; - } - } - - /** - * If pooling of {@code CallableStatement}s is turned on in the {@link DriverAdapterCPDS}, a pooled object may - * be returned, otherwise delegate to the wrapped JDBC 1.x {@link java.sql.Connection}. - * - * @param sql - * a {@code String} object that is the SQL statement to be sent to the database; may contain on or - * more '?' parameters. - * @param resultSetType - * a result set type; one of {@code ResultSet.TYPE_FORWARD_ONLY}, - * {@code ResultSet.TYPE_SCROLL_INSENSITIVE}, or {@code ResultSet.TYPE_SCROLL_SENSITIVE}. - * @param resultSetConcurrency - * a concurrency type; one of {@code ResultSet.CONCUR_READ_ONLY} or - * {@code ResultSet.CONCUR_UPDATABLE}. - * @return a {@code CallableStatement} object containing the pre-compiled SQL statement that will produce - * {@code ResultSet} objects with the given type and concurrency. - * @throws SQLException - * Thrown if a database access error occurs, this method is called on a closed connection or the given - * parameters are not {@code ResultSet} constants indicating type and concurrency. - * @since 2.4.0 - */ - @Override - public CallableStatement prepareCall(final String sql, final int resultSetType, final int resultSetConcurrency) - throws SQLException { - checkOpen(); - try { - return new DelegatingCallableStatement(this, - pooledConnection.prepareCall(sql, resultSetType, resultSetConcurrency)); - } catch (final SQLException e) { - handleException(e); // Does not return - return null; - } - } - - /** - * If pooling of {@code CallableStatement}s is turned on in the {@link DriverAdapterCPDS}, a pooled object may - * be returned, otherwise delegate to the wrapped JDBC 1.x {@link java.sql.Connection}. - * - * @param sql - * a {@code String} object that is the SQL statement to be sent to the database; may contain on or - * more '?' parameters. - * @param resultSetType - * one of the following {@code ResultSet} constants: {@code ResultSet.TYPE_FORWARD_ONLY}, - * {@code ResultSet.TYPE_SCROLL_INSENSITIVE}, or {@code ResultSet.TYPE_SCROLL_SENSITIVE}. - * @param resultSetConcurrency - * one of the following {@code ResultSet} constants: {@code ResultSet.CONCUR_READ_ONLY} or - * {@code ResultSet.CONCUR_UPDATABLE}. - * @param resultSetHoldability - * one of the following {@code ResultSet} constants: {@code ResultSet.HOLD_CURSORS_OVER_COMMIT} - * or {@code ResultSet.CLOSE_CURSORS_AT_COMMIT}. - * @return a new {@code CallableStatement} object, containing the pre-compiled SQL statement, that will - * generate {@code ResultSet} objects with the given type, concurrency, and holdability. - * @throws SQLException - * Thrown if a database access error occurs, this method is called on a closed connection or the given - * parameters are not {@code ResultSet} constants indicating type, concurrency, and holdability. - * @since 2.4.0 - */ - @Override - public CallableStatement prepareCall(final String sql, final int resultSetType, final int resultSetConcurrency, - final int resultSetHoldability) throws SQLException { - checkOpen(); - try { - return new DelegatingCallableStatement(this, - pooledConnection.prepareCall(sql, resultSetType, resultSetConcurrency, resultSetHoldability)); - } catch (final SQLException e) { - handleException(e); // Does not return - return null; - } - } - - /** - * If pooling of {@code PreparedStatement}s is turned on in the {@link DriverAdapterCPDS}, a pooled object may - * be returned, otherwise delegate to the wrapped JDBC 1.x {@link java.sql.Connection}. - * - * @param sql - * SQL statement to be prepared - * @return the prepared statement - * @throws SQLException - * if this connection is closed or an error occurs in the wrapped connection. - */ - @Override - public PreparedStatement prepareStatement(final String sql) throws SQLException { - checkOpen(); - try { - return new DelegatingPreparedStatement(this, pooledConnection.prepareStatement(sql)); - } catch (final SQLException e) { - handleException(e); // Does not return - return null; - } - } - - @Override - public PreparedStatement prepareStatement(final String sql, final int autoGeneratedKeys) throws SQLException { - checkOpen(); - try { - return new DelegatingPreparedStatement(this, pooledConnection.prepareStatement(sql, autoGeneratedKeys)); - } catch (final SQLException e) { - handleException(e); - return null; - } - } - - /** - * If pooling of {@code PreparedStatement}s is turned on in the {@link DriverAdapterCPDS}, a pooled object may - * be returned, otherwise delegate to the wrapped JDBC 1.x {@link java.sql.Connection}. - * - * @throws SQLException - * if this connection is closed or an error occurs in the wrapped connection. - */ - @Override - public PreparedStatement prepareStatement(final String sql, final int resultSetType, final int resultSetConcurrency) - throws SQLException { - checkOpen(); - try { - return new DelegatingPreparedStatement(this, - pooledConnection.prepareStatement(sql, resultSetType, resultSetConcurrency)); - } catch (final SQLException e) { - handleException(e); - return null; - } - } - - // - // Methods for accessing the delegate connection - // - - @Override - public PreparedStatement prepareStatement(final String sql, final int resultSetType, final int resultSetConcurrency, - final int resultSetHoldability) throws SQLException { - checkOpen(); - try { - return new DelegatingPreparedStatement(this, - pooledConnection.prepareStatement(sql, resultSetType, resultSetConcurrency, resultSetHoldability)); - } catch (final SQLException e) { - handleException(e); - return null; - } - } - - @Override - public PreparedStatement prepareStatement(final String sql, final int[] columnIndexes) throws SQLException { - checkOpen(); - try { - return new DelegatingPreparedStatement(this, pooledConnection.prepareStatement(sql, columnIndexes)); - } catch (final SQLException e) { - handleException(e); - return null; - } - } - - @Override - public PreparedStatement prepareStatement(final String sql, final String[] columnNames) throws SQLException { - checkOpen(); - try { - return new DelegatingPreparedStatement(this, pooledConnection.prepareStatement(sql, columnNames)); - } catch (final SQLException e) { - handleException(e); - return null; - } - } - -} +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.commons.dbcp2.cpdsadapter; + +import java.sql.CallableStatement; +import java.sql.Connection; +import java.sql.PreparedStatement; +import java.sql.SQLException; + +import org.apache.commons.dbcp2.DelegatingCallableStatement; +import org.apache.commons.dbcp2.DelegatingConnection; +import org.apache.commons.dbcp2.DelegatingPreparedStatement; + +/** + * This class is the {@code Connection} that will be returned from + * {@code PooledConnectionImpl.getConnection()}. Most methods are wrappers around the JDBC 1.x + * {@code Connection}. A few exceptions include preparedStatement and close. In accordance with the JDBC + * specification this Connection cannot be used after closed() is called. Any further usage will result in an + * SQLException. + * <p> + * ConnectionImpl extends DelegatingConnection to enable access to the underlying connection. + * </p> + * + * @since 2.0 + */ +final class ConnectionImpl extends DelegatingConnection<Connection> { + + private final boolean accessToUnderlyingConnectionAllowed; + + /** The object that instantiated this object */ + private final PooledConnectionImpl pooledConnection; + + /** + * Creates a {@code ConnectionImpl}. + * + * @param pooledConnection + * The PooledConnection that is calling the ctor. + * @param connection + * The JDBC 1.x Connection to wrap. + * @param accessToUnderlyingConnectionAllowed + * if true, then access is allowed to the underlying connection + */ + ConnectionImpl(final PooledConnectionImpl pooledConnection, final Connection connection, + final boolean accessToUnderlyingConnectionAllowed) { + super(connection); + this.pooledConnection = pooledConnection; + this.accessToUnderlyingConnectionAllowed = accessToUnderlyingConnectionAllowed; + } + + /** + * Marks the Connection as closed, and notifies the pool that the pooled connection is available. + * <p> + * In accordance with the JDBC specification this Connection cannot be used after closed() is called. Any further + * usage will result in an SQLException. + * </p> + * + * @throws SQLException + * The database connection couldn't be closed. + */ + @Override + public void close() throws SQLException { + if (!isClosedInternal()) { + try { + passivate(); + } finally { + setClosedInternal(true); + pooledConnection.notifyListeners(); + } + } + } + + /** + * Get the delegated connection, if allowed. + * + * @return the internal connection, or null if access is not allowed. + * @see #isAccessToUnderlyingConnectionAllowed() + */ + @Override + public Connection getDelegate() { + if (isAccessToUnderlyingConnectionAllowed()) { + return getDelegateInternal(); + } + return null; + } + + /** + * Get the innermost connection, if allowed. + * + * @return the innermost internal connection, or null if access is not allowed. + * @see #isAccessToUnderlyingConnectionAllowed() + */ + @Override + public Connection getInnermostDelegate() { + if (isAccessToUnderlyingConnectionAllowed()) { + return super.getInnermostDelegateInternal(); + } + return null; + } + + /** + * If false, getDelegate() and getInnermostDelegate() will return null. + * + * @return true if access is allowed to the underlying connection + * @see ConnectionImpl + */ + public boolean isAccessToUnderlyingConnectionAllowed() { + return accessToUnderlyingConnectionAllowed; + } + + /** + * If pooling of {@code CallableStatement}s is turned on in the {@link DriverAdapterCPDS}, a pooled object may + * be returned, otherwise delegate to the wrapped JDBC 1.x {@link java.sql.Connection}. + * + * @param sql + * an SQL statement that may contain one or more '?' parameter placeholders. Typically this statement is + * specified using JDBC call escape syntax. + * @return a default {@code CallableStatement} object containing the pre-compiled SQL statement. + * @throws SQLException + * Thrown if a database access error occurs or this method is called on a closed connection. + * @since 2.4.0 + */ + @Override + public CallableStatement prepareCall(final String sql) throws SQLException { + checkOpen(); + try { + return new DelegatingCallableStatement(this, pooledConnection.prepareCall(sql)); + } catch (final SQLException e) { + handleException(e); // Does not return + return null; + } + } + + /** + * If pooling of {@code CallableStatement}s is turned on in the {@link DriverAdapterCPDS}, a pooled object may + * be returned, otherwise delegate to the wrapped JDBC 1.x {@link java.sql.Connection}. + * + * @param sql + * a {@code String} object that is the SQL statement to be sent to the database; may contain on or + * more '?' parameters. + * @param resultSetType + * a result set type; one of {@code ResultSet.TYPE_FORWARD_ONLY}, + * {@code ResultSet.TYPE_SCROLL_INSENSITIVE}, or {@code ResultSet.TYPE_SCROLL_SENSITIVE}. + * @param resultSetConcurrency + * a concurrency type; one of {@code ResultSet.CONCUR_READ_ONLY} or + * {@code ResultSet.CONCUR_UPDATABLE}. + * @return a {@code CallableStatement} object containing the pre-compiled SQL statement that will produce + * {@code ResultSet} objects with the given type and concurrency. + * @throws SQLException + * Thrown if a database access error occurs, this method is called on a closed connection or the given + * parameters are not {@code ResultSet} constants indicating type and concurrency. + * @since 2.4.0 + */ + @Override + public CallableStatement prepareCall(final String sql, final int resultSetType, final int resultSetConcurrency) + throws SQLException { + checkOpen(); + try { + return new DelegatingCallableStatement(this, + pooledConnection.prepareCall(sql, resultSetType, resultSetConcurrency)); + } catch (final SQLException e) { + handleException(e); // Does not return + return null; + } + } + + /** + * If pooling of {@code CallableStatement}s is turned on in the {@link DriverAdapterCPDS}, a pooled object may + * be returned, otherwise delegate to the wrapped JDBC 1.x {@link java.sql.Connection}. + * + * @param sql + * a {@code String} object that is the SQL statement to be sent to the database; may contain on or + * more '?' parameters. + * @param resultSetType + * one of the following {@code ResultSet} constants: {@code ResultSet.TYPE_FORWARD_ONLY}, + * {@code ResultSet.TYPE_SCROLL_INSENSITIVE}, or {@code ResultSet.TYPE_SCROLL_SENSITIVE}. + * @param resultSetConcurrency + * one of the following {@code ResultSet} constants: {@code ResultSet.CONCUR_READ_ONLY} or + * {@code ResultSet.CONCUR_UPDATABLE}. + * @param resultSetHoldability + * one of the following {@code ResultSet} constants: {@code ResultSet.HOLD_CURSORS_OVER_COMMIT} + * or {@code ResultSet.CLOSE_CURSORS_AT_COMMIT}. + * @return a new {@code CallableStatement} object, containing the pre-compiled SQL statement, that will + * generate {@code ResultSet} objects with the given type, concurrency, and holdability. + * @throws SQLException + * Thrown if a database access error occurs, this method is called on a closed connection or the given + * parameters are not {@code ResultSet} constants indicating type, concurrency, and holdability. + * @since 2.4.0 + */ + @Override + public CallableStatement prepareCall(final String sql, final int resultSetType, final int resultSetConcurrency, + final int resultSetHoldability) throws SQLException { + checkOpen(); + try { + return new DelegatingCallableStatement(this, + pooledConnection.prepareCall(sql, resultSetType, resultSetConcurrency, resultSetHoldability)); + } catch (final SQLException e) { + handleException(e); // Does not return + return null; + } + } + + /** + * If pooling of {@code PreparedStatement}s is turned on in the {@link DriverAdapterCPDS}, a pooled object may + * be returned, otherwise delegate to the wrapped JDBC 1.x {@link java.sql.Connection}. + * + * @param sql + * SQL statement to be prepared + * @return the prepared statement + * @throws SQLException + * if this connection is closed or an error occurs in the wrapped connection. + */ + @Override + public PreparedStatement prepareStatement(final String sql) throws SQLException { + checkOpen(); + try { + return new DelegatingPreparedStatement(this, pooledConnection.prepareStatement(sql)); + } catch (final SQLException e) { + handleException(e); // Does not return + return null; + } + } + + @Override + public PreparedStatement prepareStatement(final String sql, final int autoGeneratedKeys) throws SQLException { + checkOpen(); + try { + return new DelegatingPreparedStatement(this, pooledConnection.prepareStatement(sql, autoGeneratedKeys)); + } catch (final SQLException e) { + handleException(e); + return null; + } + } + + /** + * If pooling of {@code PreparedStatement}s is turned on in the {@link DriverAdapterCPDS}, a pooled object may + * be returned, otherwise delegate to the wrapped JDBC 1.x {@link java.sql.Connection}. + * + * @throws SQLException + * if this connection is closed or an error occurs in the wrapped connection. + */ + @Override + public PreparedStatement prepareStatement(final String sql, final int resultSetType, final int resultSetConcurrency) + throws SQLException { + checkOpen(); + try { + return new DelegatingPreparedStatement(this, + pooledConnection.prepareStatement(sql, resultSetType, resultSetConcurrency)); + } catch (final SQLException e) { + handleException(e); + return null; + } + } + + // + // Methods for accessing the delegate connection + // + + @Override + public PreparedStatement prepareStatement(final String sql, final int resultSetType, final int resultSetConcurrency, + final int resultSetHoldability) throws SQLException { + checkOpen(); + try { + return new DelegatingPreparedStatement(this, + pooledConnection.prepareStatement(sql, resultSetType, resultSetConcurrency, resultSetHoldability)); + } catch (final SQLException e) { + handleException(e); + return null; + } + } + + @Override + public PreparedStatement prepareStatement(final String sql, final int[] columnIndexes) throws SQLException { + checkOpen(); + try { + return new DelegatingPreparedStatement(this, pooledConnection.prepareStatement(sql, columnIndexes)); + } catch (final SQLException e) { + handleException(e); + return null; + } + } + + @Override + public PreparedStatement prepareStatement(final String sql, final String[] columnNames) throws SQLException { + checkOpen(); + try { + return new DelegatingPreparedStatement(this, pooledConnection.prepareStatement(sql, columnNames)); + } catch (final SQLException e) { + handleException(e); + return null; + } + } + +} diff --git a/src/main/java/org/apache/commons/dbcp2/cpdsadapter/PooledConnectionImpl.java b/src/main/java/org/apache/commons/dbcp2/cpdsadapter/PooledConnectionImpl.java index 331de90b..57bcecd4 100644 --- a/src/main/java/org/apache/commons/dbcp2/cpdsadapter/PooledConnectionImpl.java +++ b/src/main/java/org/apache/commons/dbcp2/cpdsadapter/PooledConnectionImpl.java @@ -46,7 +46,7 @@ import org.apache.commons.pool2.impl.DefaultPooledObject; * * @since 2.0 */ -class PooledConnectionImpl +final class PooledConnectionImpl implements PooledConnection, KeyedPooledObjectFactory<PStmtKey, DelegatingPreparedStatement> { private static final String CLOSED = "Attempted to use PooledConnection after closed() was called."; diff --git a/src/main/java/org/apache/commons/dbcp2/datasources/CPDSConnectionFactory.java b/src/main/java/org/apache/commons/dbcp2/datasources/CPDSConnectionFactory.java index 21e14000..76ea6db6 100644 --- a/src/main/java/org/apache/commons/dbcp2/datasources/CPDSConnectionFactory.java +++ b/src/main/java/org/apache/commons/dbcp2/datasources/CPDSConnectionFactory.java @@ -42,7 +42,7 @@ import org.apache.commons.pool2.impl.DefaultPooledObject; * * @since 2.0 */ -class CPDSConnectionFactory +final class CPDSConnectionFactory implements PooledObjectFactory<PooledConnectionAndInfo>, ConnectionEventListener, PooledConnectionManager { private static final String NO_KEY_MESSAGE = "close() was called on a Connection, but I have no record of the underlying PooledConnection."; diff --git a/src/main/java/org/apache/commons/dbcp2/datasources/KeyedCPDSConnectionFactory.java b/src/main/java/org/apache/commons/dbcp2/datasources/KeyedCPDSConnectionFactory.java index eadaa14b..f49b8ce9 100644 --- a/src/main/java/org/apache/commons/dbcp2/datasources/KeyedCPDSConnectionFactory.java +++ b/src/main/java/org/apache/commons/dbcp2/datasources/KeyedCPDSConnectionFactory.java @@ -43,7 +43,7 @@ import org.apache.commons.pool2.impl.DefaultPooledObject; * * @since 2.0 */ -class KeyedCPDSConnectionFactory implements KeyedPooledObjectFactory<UserPassKey, PooledConnectionAndInfo>, +final class KeyedCPDSConnectionFactory implements KeyedPooledObjectFactory<UserPassKey, PooledConnectionAndInfo>, ConnectionEventListener, PooledConnectionManager { private static final String NO_KEY_MESSAGE = "close() was called on a Connection, but " diff --git a/src/main/java/org/apache/commons/dbcp2/datasources/PoolKey.java b/src/main/java/org/apache/commons/dbcp2/datasources/PoolKey.java index deab7c0f..01d8675a 100644 --- a/src/main/java/org/apache/commons/dbcp2/datasources/PoolKey.java +++ b/src/main/java/org/apache/commons/dbcp2/datasources/PoolKey.java @@ -1,67 +1,67 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one or more - * contributor license agreements. See the NOTICE file distributed with - * this work for additional information regarding copyright ownership. - * The ASF licenses this file to You under the Apache License, Version 2.0 - * (the "License"); you may not use this file except in compliance with - * the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.apache.commons.dbcp2.datasources; - -import java.io.Serializable; -import java.util.Objects; - -/** - * @since 2.0 - */ -class PoolKey implements Serializable { - private static final long serialVersionUID = 2252771047542484533L; - - private final String dataSourceName; - private final String userName; - - PoolKey(final String dataSourceName, final String userName) { - this.dataSourceName = dataSourceName; - this.userName = userName; - } - - @Override - public boolean equals(final Object obj) { - if (this == obj) { - return true; - } - if (obj == null) { - return false; - } - if (getClass() != obj.getClass()) { - return false; - } - final PoolKey other = (PoolKey) obj; - if (!Objects.equals(dataSourceName, other.dataSourceName)) { - return false; - } - return Objects.equals(userName, other.userName); - } - - @Override - public int hashCode() { - return Objects.hash(dataSourceName, userName); - } - - @Override - public String toString() { - final StringBuilder sb = new StringBuilder(50); - sb.append("PoolKey("); - sb.append(dataSourceName); - sb.append(')'); - return sb.toString(); - } -} +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.commons.dbcp2.datasources; + +import java.io.Serializable; +import java.util.Objects; + +/** + * @since 2.0 + */ +final class PoolKey implements Serializable { + private static final long serialVersionUID = 2252771047542484533L; + + private final String dataSourceName; + private final String userName; + + PoolKey(final String dataSourceName, final String userName) { + this.dataSourceName = dataSourceName; + this.userName = userName; + } + + @Override + public boolean equals(final Object obj) { + if (this == obj) { + return true; + } + if (obj == null) { + return false; + } + if (getClass() != obj.getClass()) { + return false; + } + final PoolKey other = (PoolKey) obj; + if (!Objects.equals(dataSourceName, other.dataSourceName)) { + return false; + } + return Objects.equals(userName, other.userName); + } + + @Override + public int hashCode() { + return Objects.hash(dataSourceName, userName); + } + + @Override + public String toString() { + final StringBuilder sb = new StringBuilder(50); + sb.append("PoolKey("); + sb.append(dataSourceName); + sb.append(')'); + return sb.toString(); + } +} diff --git a/src/main/java/org/apache/commons/dbcp2/datasources/UserPassKey.java b/src/main/java/org/apache/commons/dbcp2/datasources/UserPassKey.java index a8d75408..94643c80 100644 --- a/src/main/java/org/apache/commons/dbcp2/datasources/UserPassKey.java +++ b/src/main/java/org/apache/commons/dbcp2/datasources/UserPassKey.java @@ -1,119 +1,119 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one or more - * contributor license agreements. See the NOTICE file distributed with - * this work for additional information regarding copyright ownership. - * The ASF licenses this file to You under the Apache License, Version 2.0 - * (the "License"); you may not use this file except in compliance with - * the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.apache.commons.dbcp2.datasources; - -import java.io.Serializable; -import java.util.Objects; - -import org.apache.commons.pool2.KeyedObjectPool; - -/** - * <p> - * Holds a user name and password pair. Serves as a poolable object key for the {@link KeyedObjectPool} backing a - * {@link SharedPoolDataSource}. Two instances with the same user name are considered equal. This ensures that there - * will be only one keyed pool for each user in the pool. The password is used (along with the user name) by the - * {@code KeyedCPDSConnectionFactory} when creating new connections. - * </p> - * - * <p> - * {@link InstanceKeyDataSource#getConnection(String, String)} validates that the password used to create a connection - * matches the password provided by the client. - * </p> - * - * @since 2.0 - */ -class UserPassKey implements Serializable { - private static final long serialVersionUID = 5142970911626584817L; - - private final CharArray name; - private final CharArray password; - - UserPassKey(final char[] userName, final char[] password) { - this(new CharArray(userName), new CharArray(password)); - } - - UserPassKey(final CharArray userName, final CharArray userPassword) { - this.name = userName; - this.password = userPassword; - } - - UserPassKey(final String userName) { - this(new CharArray(userName), CharArray.NULL); - } - - UserPassKey(final String userName, final char[] password) { - this(new CharArray(userName), new CharArray(password)); - } - - UserPassKey(final String userName, final String userPassword) { - this(new CharArray(userName), new CharArray(userPassword)); - } - - /** - * Only takes the user name into account. - */ - @Override - public boolean equals(final Object obj) { - if (this == obj) { - return true; - } - if (obj == null) { - return false; - } - if (getClass() != obj.getClass()) { - return false; - } - final UserPassKey other = (UserPassKey) obj; - return Objects.equals(name, other.name); - } - - /** - * Gets the value of password. - * - * @return value of password. - */ - String getPassword() { - return password.asString(); - } - - /** - * Gets the value of password. - * - * @return value of password. - */ - char[] getPasswordCharArray() { - return password.get(); - } - - /** - * Gets the value of user name. - * - * @return value of user name. - */ - String getUserName() { - return name.asString(); - } - - /** - * Only takes the user name into account. - */ - @Override - public int hashCode() { - return Objects.hash(name); - } - -} +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.commons.dbcp2.datasources; + +import java.io.Serializable; +import java.util.Objects; + +import org.apache.commons.pool2.KeyedObjectPool; + +/** + * <p> + * Holds a user name and password pair. Serves as a poolable object key for the {@link KeyedObjectPool} backing a + * {@link SharedPoolDataSource}. Two instances with the same user name are considered equal. This ensures that there + * will be only one keyed pool for each user in the pool. The password is used (along with the user name) by the + * {@code KeyedCPDSConnectionFactory} when creating new connections. + * </p> + * + * <p> + * {@link InstanceKeyDataSource#getConnection(String, String)} validates that the password used to create a connection + * matches the password provided by the client. + * </p> + * + * @since 2.0 + */ +final class UserPassKey implements Serializable { + private static final long serialVersionUID = 5142970911626584817L; + + private final CharArray name; + private final CharArray password; + + UserPassKey(final char[] userName, final char[] password) { + this(new CharArray(userName), new CharArray(password)); + } + + UserPassKey(final CharArray userName, final CharArray userPassword) { + this.name = userName; + this.password = userPassword; + } + + UserPassKey(final String userName) { + this(new CharArray(userName), CharArray.NULL); + } + + UserPassKey(final String userName, final char[] password) { + this(new CharArray(userName), new CharArray(password)); + } + + UserPassKey(final String userName, final String userPassword) { + this(new CharArray(userName), new CharArray(userPassword)); + } + + /** + * Only takes the user name into account. + */ + @Override + public boolean equals(final Object obj) { + if (this == obj) { + return true; + } + if (obj == null) { + return false; + } + if (getClass() != obj.getClass()) { + return false; + } + final UserPassKey other = (UserPassKey) obj; + return Objects.equals(name, other.name); + } + + /** + * Gets the value of password. + * + * @return value of password. + */ + String getPassword() { + return password.asString(); + } + + /** + * Gets the value of password. + * + * @return value of password. + */ + char[] getPasswordCharArray() { + return password.get(); + } + + /** + * Gets the value of user name. + * + * @return value of user name. + */ + String getUserName() { + return name.asString(); + } + + /** + * Only takes the user name into account. + */ + @Override + public int hashCode() { + return Objects.hash(name); + } + +}