"Fossies" - the Fresh Open Source Software Archive  

Source code changes of the file "log4j-jdbc-dbcp2/src/main/java/org/apache/logging/log4j/core/appender/db/jdbc/PoolingDriverConnectionSource.java" between
apache-log4j-2.11.1-src.tar.gz and apache-log4j-2.11.2-src.tar.gz

About: Apache Log4j 2 is a logging library for Java. Source distribution (Java).

PoolingDriverConnectionSource.java  (apache-log4j-2.11.1-src):PoolingDriverConnectionSource.java  (apache-log4j-2.11.2-src)
skipping to change at line 34 skipping to change at line 34
import org.apache.commons.dbcp2.ConnectionFactory; import org.apache.commons.dbcp2.ConnectionFactory;
import org.apache.commons.dbcp2.DriverManagerConnectionFactory; import org.apache.commons.dbcp2.DriverManagerConnectionFactory;
import org.apache.commons.dbcp2.PoolableConnection; import org.apache.commons.dbcp2.PoolableConnection;
import org.apache.commons.dbcp2.PoolableConnectionFactory; import org.apache.commons.dbcp2.PoolableConnectionFactory;
import org.apache.commons.dbcp2.PoolingDriver; import org.apache.commons.dbcp2.PoolingDriver;
import org.apache.commons.pool2.ObjectPool; import org.apache.commons.pool2.ObjectPool;
import org.apache.commons.pool2.impl.GenericObjectPool; import org.apache.commons.pool2.impl.GenericObjectPool;
import org.apache.logging.log4j.core.Core; import org.apache.logging.log4j.core.Core;
import org.apache.logging.log4j.core.config.Property; import org.apache.logging.log4j.core.config.Property;
import org.apache.logging.log4j.core.config.plugins.Plugin; import org.apache.logging.log4j.core.config.plugins.Plugin;
import org.apache.logging.log4j.core.config.plugins.PluginBuilderAttribute;
import org.apache.logging.log4j.core.config.plugins.PluginBuilderFactory; import org.apache.logging.log4j.core.config.plugins.PluginBuilderFactory;
import org.apache.logging.log4j.core.config.plugins.PluginElement;
/** /**
* A {@link ConnectionSource} that uses a JDBC connection string, a user name, a nd a password to call * A {@link ConnectionSource} that uses a JDBC connection string, a user name, a nd a password to call
* {@link DriverManager#getConnection(String, String, String)}. The connections are served from an * {@link DriverManager#getConnection(String, String, String)}. The connections are served from an
* <a href="http://commons.apache.org/proper/commons-dbcp/">Apache Commons DBCP< /a> pooling driver. * <a href="http://commons.apache.org/proper/commons-dbcp/">Apache Commons DBCP< /a> pooling driver.
*/ */
@Plugin(name = "PoolingDriver", category = Core.CATEGORY_NAME, elementType = "co nnectionSource", printObject = true) @Plugin(name = "PoolingDriver", category = Core.CATEGORY_NAME, elementType = "co nnectionSource", printObject = true)
public final class PoolingDriverConnectionSource extends AbstractDriverManagerCo nnectionSource { public final class PoolingDriverConnectionSource extends AbstractDriverManagerCo nnectionSource {
/** /**
* Builds PoolingDriverConnectionSource instances. * Builds PoolingDriverConnectionSource instances.
* *
* @param <B> * @param <B>
* This builder type or a subclass. * This builder type or a subclass.
*/ */
public static class Builder<B extends Builder<B>> extends AbstractDriverMana gerConnectionSource.Builder<B> public static class Builder<B extends Builder<B>> extends AbstractDriverMana gerConnectionSource.Builder<B>
implements org.apache.logging.log4j.core.util.Builder<PoolingDriverConnectio nSource> { implements org.apache.logging.log4j.core.util.Builder<PoolingDriverConnectio nSource> {
public static final String DEFAULT_POOL_NAME = "example"; public static final String DEFAULT_POOL_NAME = "example";
@PluginElement("PoolableConnectionFactoryConfig")
private PoolableConnectionFactoryConfig poolableConnectionFactoryConfig;
@PluginBuilderAttribute
private String poolName = DEFAULT_POOL_NAME; private String poolName = DEFAULT_POOL_NAME;
@Override @Override
public PoolingDriverConnectionSource build() { public PoolingDriverConnectionSource build() {
try { try {
return new PoolingDriverConnectionSource(getDrive rClassName(), getConnectionString(), getUserName(), return new PoolingDriverConnectionSource(getDrive rClassName(), getConnectionString(), getUserName(),
getPassword(), getProperties(), p oolName); getPassword(), getProperties(), p oolName, poolableConnectionFactoryConfig);
} catch (final SQLException e) { } catch (final SQLException e) {
getLogger().error("Exception constructing {} to ' {}' with {}", PoolingDriverConnectionSource.class, getLogger().error("Exception constructing {} to ' {}' with {}", PoolingDriverConnectionSource.class,
getConnectionString(), this, e); getConnectionString(), this, e);
return null; return null;
} }
} }
public B setPoolName(final String poolName) { public B setPoolableConnectionFactoryConfig(final PoolableConnectionFact
oryConfig poolableConnectionFactoryConfig) {
this.poolableConnectionFactoryConfig = poolableConnectionFactoryConf
ig;
return asBuilder();
}
public B setPoolName(final String poolName) {
this.poolName = poolName; this.poolName = poolName;
return asBuilder(); return asBuilder();
} }
@Override @Override
public String toString() { public String toString() {
return "Builder [poolName=" + poolName + ", connectionStr ing=" + connectionString + ", driverClassName=" return "Builder [poolName=" + poolName + ", connectionStr ing=" + connectionString + ", driverClassName="
+ driverClassName + ", properties=" + Arr ays.toString(properties) + ", userName=" + driverClassName + ", properties=" + Arr ays.toString(properties) + ", userName="
+ Arrays.toString(userName) + "]"; + Arrays.toString(userName) + "]";
} }
} }
public static final String URL_PREFIX = "jdbc:apache:commons:dbcp:"; public static final String URL_PREFIX = "jdbc:apache:commons:dbcp:";
// This method is not named newBuilder() to make the compiler happy. // This method is not named newBuilder() to make the compiler happy.
@PluginBuilderFactory @PluginBuilderFactory
public static <B extends Builder<B>> B newPoolingDriverConnectionSourceBuild er() { public static <B extends Builder<B>> B newPoolingDriverConnectionSourceBuild er() {
return new Builder<B>().asBuilder(); return new Builder<B>().asBuilder();
} }
private final String poolingDriverClassName = "org.apache.commons.dbcp2.Pool ingDriver"; private final String poolingDriverClassName = "org.apache.commons.dbcp2.Pool ingDriver";
private final String poolName; private final String poolName;
/**
* @deprecated Use {@link #newPoolingDriverConnectionSourceBuilder()}.
*/
@Deprecated
public PoolingDriverConnectionSource(final String driverClassName, final Str ing connectionString, public PoolingDriverConnectionSource(final String driverClassName, final Str ing connectionString,
final char[] userName, final char[] password, final Property[] prope rties, final String poolName) final char[] userName, final char[] password, final Property[] prope rties, final String poolName)
throws SQLException { throws SQLException {
super(driverClassName, connectionString, URL_PREFIX + poolName, userName , password, properties); super(driverClassName, connectionString, URL_PREFIX + poolName, userName , password, properties);
this.poolName = poolName; this.poolName = poolName;
setupDriver(connectionString); setupDriver(connectionString, null);
}
private PoolingDriverConnectionSource(final String driverClassName, final St
ring connectionString,
final char[] userName, final char[] password, final Property[] prope
rties, final String poolName,
final PoolableConnectionFactoryConfig poolableConnectionFactoryConfi
g)
throws SQLException {
super(driverClassName, connectionString, URL_PREFIX + poolName, userName
, password, properties);
this.poolName = poolName;
setupDriver(connectionString, poolableConnectionFactoryConfig);
} }
@Override @Override
public String getActualConnectionString() { public String getActualConnectionString() {
// TODO Auto-generated method stub // TODO Auto-generated method stub
return super.getActualConnectionString(); return super.getActualConnectionString();
} }
private PoolingDriver getPoolingDriver() throws SQLException { private PoolingDriver getPoolingDriver() throws SQLException {
final PoolingDriver driver = (PoolingDriver) DriverManager.getDriver(URL _PREFIX); final PoolingDriver driver = (PoolingDriver) DriverManager.getDriver(URL _PREFIX);
if (driver == null) { if (driver == null) {
getLogger().error("No JDBC driver for '{}'", URL_PREFIX); getLogger().error("No JDBC driver for '{}'", URL_PREFIX);
} }
return driver; return driver;
} }
private void setupDriver(final String connectionString) throws SQLException private void setupDriver(final String connectionString,
{ final PoolableConnectionFactoryConfig poolableConnectionFactoryConfi
g) throws SQLException {
// //
// First, we'll create a ConnectionFactory that the // First, we'll create a ConnectionFactory that the
// pool will use to create Connections. // pool will use to create Connections.
// We'll use the DriverManagerConnectionFactory, // We'll use the DriverManagerConnectionFactory,
// using the connect string passed in the command line // using the connect string passed in the command line
// arguments. // arguments.
// //
final Property[] properties = getProperties(); final Property[] properties = getProperties();
final char[] userName = getUserName(); final char[] userName = getUserName();
final char[] password = getPassword(); final char[] password = getPassword();
final ConnectionFactory connectionFactory; final ConnectionFactory connectionFactory;
if (properties != null && properties.length > 0) { if (properties != null && properties.length > 0) {
if (userName != null || password != null) { if (userName != null || password != null) {
throw new SQLException("Either set the userName and password, or set the Properties, but not both."); throw new SQLException("Either set the userName and password, or set the Properties, but not both.");
} }
connectionFactory = new DriverManagerConnectionFactory(connectionStr ing, toProperties(properties)); connectionFactory = new DriverManagerConnectionFactory(connectionStr ing, toProperties(properties));
} else { } else {
connectionFactory = new DriverManagerConnectionFactory(connection String, toString(userName), toString(password)); connectionFactory = new DriverManagerConnectionFactory(connectionStr ing, toString(userName), toString(password));
} }
// //
// Next, we'll create the PoolableConnectionFactory, which wraps // Next, we'll create the PoolableConnectionFactory, which wraps
// the "real" Connections created by the ConnectionFactory with // the "real" Connections created by the ConnectionFactory with
// the classes that implement the pooling functionality. // the classes that implement the pooling functionality.
// //
final PoolableConnectionFactory poolableConnectionFactory = new Poolable ConnectionFactory(connectionFactory, final PoolableConnectionFactory poolableConnectionFactory = new Poolable ConnectionFactory(connectionFactory,
null); null);
if (poolableConnectionFactoryConfig != null) {
poolableConnectionFactoryConfig.init(poolableConnectionFactory);
}
// //
// Now we'll need a ObjectPool that serves as the // Now we'll need a ObjectPool that serves as the
// actual pool of connections. // actual pool of connections.
// //
// We'll use a GenericObjectPool instance, although // We'll use a GenericObjectPool instance, although
// any ObjectPool implementation will suffice. // any ObjectPool implementation will suffice.
// //
@SuppressWarnings("resource") @SuppressWarnings("resource")
// This GenericObjectPool will be closed on shutown // This GenericObjectPool will be closed on shutdown
final ObjectPool<PoolableConnection> connectionPool = new GenericObjectP ool<>(poolableConnectionFactory); final ObjectPool<PoolableConnection> connectionPool = new GenericObjectP ool<>(poolableConnectionFactory);
// Set the factory's pool property to the owning pool // Set the factory's pool property to the owning pool
poolableConnectionFactory.setPool(connectionPool); poolableConnectionFactory.setPool(connectionPool);
loadDriver(poolingDriverClassName); loadDriver(poolingDriverClassName);
final PoolingDriver driver = getPoolingDriver(); final PoolingDriver driver = getPoolingDriver();
if (driver != null) { if (driver != null) {
getLogger().debug("Registering DBCP pool '{}'", poolName); getLogger().debug("Registering DBCP pool '{}' with pooling driver {} : {}", poolName, driver, connectionPool);
driver.registerPool(poolName, connectionPool); driver.registerPool(poolName, connectionPool);
} }
// //
// Now we can just use the connect string "jdbc:apache:commons:dbcp:exam ple" // Now we can just use the connect string "jdbc:apache:commons:dbcp:exam ple"
// to access our pool of Connections. // to access our pool of Connections.
// //
} }
@Override @Override
public boolean stop(long timeout, TimeUnit timeUnit) { public boolean stop(final long timeout, final TimeUnit timeUnit) {
try { try {
final PoolingDriver driver = getPoolingDriver(); final PoolingDriver driver = getPoolingDriver();
if (driver != null) { if (driver != null) {
getLogger().debug("Closing DBCP pool '{}'", poolName); getLogger().debug("Driver {} closing DBCP pool '{}'", driver, po olName);
driver.closePool(poolName); driver.closePool(poolName);
} }
return true; return true;
} catch (Exception e) { } catch (final Exception e) {
getLogger().error("Exception stopping connection source for '{}' → ' {}'", getConnectionString(), getLogger().error("Exception stopping connection source for '{}' → ' {}'", getConnectionString(),
getActualConnectionString(), e); getActualConnectionString(), e);
return false; return false;
} }
} }
} }
 End of changes. 16 change blocks. 
12 lines changed or deleted 47 lines changed or added

Home  |  About  |  Features  |  All  |  Newest  |  Dox  |  Diffs  |  RSS Feeds  |  Screenshots  |  Comments  |  Imprint  |  Privacy  |  HTTP(S)