This is an automated email from the ASF dual-hosted git repository.
markt pushed a commit to branch 7.0.x
in repository https://gitbox.apache.org/repos/asf/tomcat.git
The following commit(s) were added to refs/heads/7.0.x by this push:
new 4819646 Generics: DataSource factories
4819646 is described below
commit 48196467ca0a5a9624c14cdcece222490894f34e
Author: Mark Thomas <[email protected]>
AuthorDate: Mon Mar 18 15:17:07 2019 +0000
Generics: DataSource factories
---
.../dbcp/datasources/CPDSConnectionFactory.java | 136 ++++++++--------
.../dbcp/datasources/InstanceKeyObjectFactory.java | 27 ++--
.../datasources/KeyedCPDSConnectionFactory.java | 180 ++++++++++-----------
3 files changed, 164 insertions(+), 179 deletions(-)
diff --git
a/java/org/apache/tomcat/dbcp/dbcp/datasources/CPDSConnectionFactory.java
b/java/org/apache/tomcat/dbcp/dbcp/datasources/CPDSConnectionFactory.java
index a959de5..47bc497 100644
--- a/java/org/apache/tomcat/dbcp/dbcp/datasources/CPDSConnectionFactory.java
+++ b/java/org/apache/tomcat/dbcp/dbcp/datasources/CPDSConnectionFactory.java
@@ -40,7 +40,7 @@ import org.apache.tomcat.dbcp.pool.PoolableObjectFactory;
* @author John D. McNally
*/
class CPDSConnectionFactory
- implements PoolableObjectFactory, ConnectionEventListener,
PooledConnectionManager {
+ implements PoolableObjectFactory<PooledConnectionAndInfo>,
ConnectionEventListener, PooledConnectionManager {
private static final String NO_KEY_MESSAGE
= "close() was called on a Connection, but "
@@ -49,7 +49,7 @@ class CPDSConnectionFactory
private final ConnectionPoolDataSource _cpds;
private final String _validationQuery;
private final boolean _rollbackAfterValidation;
- private final ObjectPool _pool;
+ private final ObjectPool<PooledConnectionAndInfo> _pool;
private String _username = null;
private String _password = null;
@@ -57,12 +57,13 @@ class CPDSConnectionFactory
* Map of PooledConnections for which close events are ignored.
* Connections are muted when they are being validated.
*/
- private final Map /* <PooledConnection, null> */ validatingMap = new
HashMap();
+ private final Map<PooledConnection, Void> validatingMap = new
HashMap<PooledConnection, Void>();
/**
* Map of PooledConnectionAndInfo instances
*/
- private final WeakHashMap /* <PooledConnection, PooledConnectionAndInfo>
*/ pcMap = new WeakHashMap();
+ private final WeakHashMap <PooledConnection, PooledConnectionAndInfo>
pcMap =
+ new WeakHashMap<PooledConnection, PooledConnectionAndInfo>();
/**
* Create a new <tt>PoolableConnectionFactory</tt>.
@@ -78,7 +79,7 @@ class CPDSConnectionFactory
* @param password
*/
public CPDSConnectionFactory(ConnectionPoolDataSource cpds,
- ObjectPool pool,
+ ObjectPool<PooledConnectionAndInfo> pool,
String validationQuery,
String username,
String password) {
@@ -101,7 +102,7 @@ class CPDSConnectionFactory
* @param password
*/
public CPDSConnectionFactory(ConnectionPoolDataSource cpds,
- ObjectPool pool,
+ ObjectPool<PooledConnectionAndInfo> pool,
String validationQuery,
boolean rollbackAfterValidation,
String username,
@@ -120,13 +121,13 @@ class CPDSConnectionFactory
*
* @return ObjectPool managing pooled connections
*/
- public ObjectPool getPool() {
+ public ObjectPool<PooledConnectionAndInfo> getPool() {
return _pool;
}
@Override
- public synchronized Object makeObject() {
- Object obj;
+ public synchronized PooledConnectionAndInfo makeObject() {
+ PooledConnectionAndInfo obj;
try {
PooledConnection pc = null;
if (_username == null) {
@@ -154,84 +155,77 @@ class CPDSConnectionFactory
* Closes the PooledConnection and stops listening for events from it.
*/
@Override
- public void destroyObject(Object obj) throws Exception {
- if (obj instanceof PooledConnectionAndInfo) {
- PooledConnection pc =
((PooledConnectionAndInfo)obj).getPooledConnection();
- pc.removeConnectionEventListener(this);
- pcMap.remove(pc);
- pc.close();
- }
+ public void destroyObject(PooledConnectionAndInfo obj) throws Exception {
+ PooledConnection pc = obj.getPooledConnection();
+ pc.removeConnectionEventListener(this);
+ pcMap.remove(pc);
+ pc.close();
}
@Override
- public boolean validateObject(Object obj) {
+ public boolean validateObject(PooledConnectionAndInfo obj) {
boolean valid = false;
- if (obj instanceof PooledConnectionAndInfo) {
- PooledConnection pconn =
- ((PooledConnectionAndInfo) obj).getPooledConnection();
- String query = _validationQuery;
- if (null != query) {
- Connection conn = null;
- Statement stmt = null;
- ResultSet rset = null;
- // logical Connection from the PooledConnection must be closed
- // before another one can be requested and closing it will
- // generate an event. Keep track so we know not to return
- // the PooledConnection
- validatingMap.put(pconn, null);
- try {
- conn = pconn.getConnection();
- stmt = conn.createStatement();
- rset = stmt.executeQuery(query);
- if (rset.next()) {
- valid = true;
- } else {
- valid = false;
- }
- if (_rollbackAfterValidation) {
- conn.rollback();
- }
- } catch (Exception e) {
+ PooledConnection pconn = obj.getPooledConnection();
+ String query = _validationQuery;
+ if (null != query) {
+ Connection conn = null;
+ Statement stmt = null;
+ ResultSet rset = null;
+ // logical Connection from the PooledConnection must be closed
+ // before another one can be requested and closing it will
+ // generate an event. Keep track so we know not to return
+ // the PooledConnection
+ validatingMap.put(pconn, null);
+ try {
+ conn = pconn.getConnection();
+ stmt = conn.createStatement();
+ rset = stmt.executeQuery(query);
+ if (rset.next()) {
+ valid = true;
+ } else {
valid = false;
- } finally {
- if (rset != null) {
- try {
- rset.close();
- } catch (Throwable t) {
- // ignore
- }
+ }
+ if (_rollbackAfterValidation) {
+ conn.rollback();
+ }
+ } catch (Exception e) {
+ valid = false;
+ } finally {
+ if (rset != null) {
+ try {
+ rset.close();
+ } catch (Throwable t) {
+ // ignore
}
- if (stmt != null) {
- try {
- stmt.close();
- } catch (Throwable t) {
- // ignore
- }
+ }
+ if (stmt != null) {
+ try {
+ stmt.close();
+ } catch (Throwable t) {
+ // ignore
}
- if (conn != null) {
- try {
- conn.close();
- } catch (Throwable t) {
- // ignore
- }
+ }
+ if (conn != null) {
+ try {
+ conn.close();
+ } catch (Throwable t) {
+ // ignore
}
- validatingMap.remove(pconn);
}
- } else {
- valid = true;
+ validatingMap.remove(pconn);
}
} else {
- valid = false;
+ valid = true;
}
return valid;
}
@Override
- public void passivateObject(Object obj) {
+ public void passivateObject(PooledConnectionAndInfo obj) {
}
@Override
- public void activateObject(Object obj) {
+ public void activateObject(PooledConnectionAndInfo obj) {
}
// ***********************************************************************
@@ -250,7 +244,7 @@ class CPDSConnectionFactory
// if this event occured becase we were validating, ignore it
// otherwise return the connection to the pool.
if (!validatingMap.containsKey(pc)) {
- Object info = pcMap.get(pc);
+ PooledConnectionAndInfo info = pcMap.get(pc);
if (info == null) {
throw new IllegalStateException(NO_KEY_MESSAGE);
}
@@ -286,7 +280,7 @@ class CPDSConnectionFactory
}
pc.removeConnectionEventListener(this);
- Object info = pcMap.get(pc);
+ PooledConnectionAndInfo info = pcMap.get(pc);
if (info == null) {
throw new IllegalStateException(NO_KEY_MESSAGE);
}
@@ -310,7 +304,7 @@ class CPDSConnectionFactory
*/
@Override
public void invalidate(PooledConnection pc) throws SQLException {
- Object info = pcMap.get(pc);
+ PooledConnectionAndInfo info = pcMap.get(pc);
if (info == null) {
throw new IllegalStateException(NO_KEY_MESSAGE);
}
diff --git
a/java/org/apache/tomcat/dbcp/dbcp/datasources/InstanceKeyObjectFactory.java
b/java/org/apache/tomcat/dbcp/dbcp/datasources/InstanceKeyObjectFactory.java
index 6b6562a..7649761 100644
--- a/java/org/apache/tomcat/dbcp/dbcp/datasources/InstanceKeyObjectFactory.java
+++ b/java/org/apache/tomcat/dbcp/dbcp/datasources/InstanceKeyObjectFactory.java
@@ -41,21 +41,19 @@ import javax.naming.spi.ObjectFactory;
abstract class InstanceKeyObjectFactory
implements ObjectFactory
{
- private static final Map instanceMap = new HashMap();
+ private static final Map<String, InstanceKeyDataSource> instanceMap =
+ new HashMap<String, InstanceKeyDataSource>();
synchronized static String registerNewInstance(InstanceKeyDataSource ds) {
int max = 0;
- Iterator i = instanceMap.keySet().iterator();
+ Iterator<String> i = instanceMap.keySet().iterator();
while (i.hasNext()) {
- Object obj = i.next();
- if (obj instanceof String)
- {
- try {
- max = Math.max(max, new Integer((String)obj).intValue());
- }
- catch (NumberFormatException e) {
- // no sweat, ignore those keys
- }
+ String obj = i.next();
+ try {
+ max = Math.max(max, new Integer(obj).intValue());
+ }
+ catch (NumberFormatException e) {
+ // no sweat, ignore those keys
}
}
String instanceKey = String.valueOf(max + 1);
@@ -75,10 +73,9 @@ abstract class InstanceKeyObjectFactory
*/
public static void closeAll() throws Exception {
//Get iterator to loop over all instances of this datasource.
- Iterator instanceIterator = instanceMap.entrySet().iterator();
+ Iterator<Map.Entry<String, InstanceKeyDataSource>> instanceIterator =
instanceMap.entrySet().iterator();
while (instanceIterator.hasNext()) {
- ((InstanceKeyDataSource)
- ((Map.Entry) instanceIterator.next()).getValue()).close();
+ instanceIterator.next().getValue().close();
}
instanceMap.clear();
}
@@ -90,7 +87,7 @@ abstract class InstanceKeyObjectFactory
*/
@Override
public Object getObjectInstance(Object refObj, Name name,
- Context context, Hashtable env)
+ Context context, Hashtable<?, ?> env)
throws IOException, ClassNotFoundException {
// The spec says to return null if we can't create an instance
// of the reference
diff --git
a/java/org/apache/tomcat/dbcp/dbcp/datasources/KeyedCPDSConnectionFactory.java
b/java/org/apache/tomcat/dbcp/dbcp/datasources/KeyedCPDSConnectionFactory.java
index 8bac417..d0b3f2b 100644
---
a/java/org/apache/tomcat/dbcp/dbcp/datasources/KeyedCPDSConnectionFactory.java
+++
b/java/org/apache/tomcat/dbcp/dbcp/datasources/KeyedCPDSConnectionFactory.java
@@ -21,9 +21,10 @@ import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
-import java.util.HashMap;
+import java.util.Collections;
import java.util.Map;
-import java.util.WeakHashMap;
+import java.util.Set;
+import java.util.concurrent.ConcurrentHashMap;
import javax.sql.ConnectionEvent;
import javax.sql.ConnectionEventListener;
@@ -39,8 +40,8 @@ import org.apache.tomcat.dbcp.pool.KeyedPoolableObjectFactory;
*
* @author John D. McNally
*/
-class KeyedCPDSConnectionFactory
- implements KeyedPoolableObjectFactory, ConnectionEventListener,
PooledConnectionManager {
+class KeyedCPDSConnectionFactory implements
KeyedPoolableObjectFactory<UserPassKey, PooledConnectionAndInfo>,
+ ConnectionEventListener, PooledConnectionManager {
private static final String NO_KEY_MESSAGE
= "close() was called on a Connection, but "
@@ -49,18 +50,20 @@ class KeyedCPDSConnectionFactory
private final ConnectionPoolDataSource _cpds;
private final String _validationQuery;
private final boolean _rollbackAfterValidation;
- private final KeyedObjectPool _pool;
-
- /**
+ private final KeyedObjectPool<UserPassKey, PooledConnectionAndInfo> _pool;
+
+ /**
* Map of PooledConnections for which close events are ignored.
* Connections are muted when they are being validated.
*/
- private final Map /* <PooledConnection, null> */ validatingMap = new
HashMap();
-
+ private final Set<PooledConnection> validatingSet =
Collections.newSetFromMap(
+ new ConcurrentHashMap<PooledConnection, Boolean>());
+
/**
* Map of PooledConnectionAndInfo instances
*/
- private final WeakHashMap /* <PooledConnection, PooledConnectionAndInfo>
*/ pcMap = new WeakHashMap();
+ private final Map<PooledConnection, PooledConnectionAndInfo> pcMap =
+ new ConcurrentHashMap<PooledConnection, PooledConnectionAndInfo>();
/**
* Create a new <tt>KeyedPoolableConnectionFactory</tt>.
@@ -70,9 +73,9 @@ class KeyedCPDSConnectionFactory
* Should return at least one row. May be <tt>null</tt>
*/
public KeyedCPDSConnectionFactory(ConnectionPoolDataSource cpds,
- KeyedObjectPool pool,
+ KeyedObjectPool<UserPassKey,
PooledConnectionAndInfo> pool,
String validationQuery) {
- this(cpds , pool, validationQuery, false);
+ this(cpds , pool, validationQuery, false);
}
/**
@@ -86,8 +89,8 @@ class KeyedCPDSConnectionFactory
* @param rollbackAfterValidation whether a rollback should be issued after
* {@link #validateObject validating} {@link Connection}s.
*/
- public KeyedCPDSConnectionFactory(ConnectionPoolDataSource cpds,
- KeyedObjectPool pool,
+ public KeyedCPDSConnectionFactory(ConnectionPoolDataSource cpds,
+ KeyedObjectPool<UserPassKey,
PooledConnectionAndInfo> pool,
String validationQuery,
boolean rollbackAfterValidation) {
_cpds = cpds;
@@ -96,27 +99,26 @@ class KeyedCPDSConnectionFactory
_validationQuery = validationQuery;
_rollbackAfterValidation = rollbackAfterValidation;
}
-
+
/**
* Returns the keyed object pool used to pool connections created by this
factory.
- *
+ *
* @return KeyedObjectPool managing pooled connections
*/
- public KeyedObjectPool getPool() {
+ public KeyedObjectPool<UserPassKey, PooledConnectionAndInfo> getPool() {
return _pool;
}
/**
* Creates a new {@link PooledConnectionAndInfo} from the given {@link
UserPassKey}.
- *
- * @param key {@link UserPassKey} containing user credentials
+ *
+ * @param upkey {@link UserPassKey} containing user credentials
* @throws SQLException if the connection could not be created.
* @see
org.apache.tomcat.dbcp.pool.KeyedPoolableObjectFactory#makeObject(java.lang.Object)
*/
@Override
- public synchronized Object makeObject(Object key) throws Exception {
- Object obj = null;
- UserPassKey upkey = (UserPassKey)key;
+ public synchronized PooledConnectionAndInfo makeObject(UserPassKey upkey)
throws Exception {
+ PooledConnectionAndInfo obj = null;
PooledConnection pc = null;
String username = upkey.getUsername();
@@ -144,91 +146,84 @@ class KeyedCPDSConnectionFactory
* Closes the PooledConnection and stops listening for events from it.
*/
@Override
- public void destroyObject(Object key, Object obj) throws Exception {
- if (obj instanceof PooledConnectionAndInfo) {
- PooledConnection pc =
((PooledConnectionAndInfo)obj).getPooledConnection();
- pc.removeConnectionEventListener(this);
- pcMap.remove(pc);
- pc.close();
- }
+ public void destroyObject(UserPassKey key, PooledConnectionAndInfo obj)
throws Exception {
+ PooledConnection pc = obj.getPooledConnection();
+ pc.removeConnectionEventListener(this);
+ pcMap.remove(pc);
+ pc.close();
}
/**
* Validates a pooled connection.
- *
+ *
* @param key ignored
* @param obj {@link PooledConnectionAndInfo} containing the connection to
validate
* @return true if validation suceeds
*/
@Override
- public boolean validateObject(Object key, Object obj) {
+ public boolean validateObject(UserPassKey key, PooledConnectionAndInfo
obj) {
boolean valid = false;
- if (obj instanceof PooledConnectionAndInfo) {
- PooledConnection pconn =
- ((PooledConnectionAndInfo)obj).getPooledConnection();
- String query = _validationQuery;
- if (null != query) {
- Connection conn = null;
- Statement stmt = null;
- ResultSet rset = null;
- // logical Connection from the PooledConnection must be closed
- // before another one can be requested and closing it will
- // generate an event. Keep track so we know not to return
- // the PooledConnection
- validatingMap.put(pconn, null);
- try {
- conn = pconn.getConnection();
- stmt = conn.createStatement();
- rset = stmt.executeQuery(query);
- if (rset.next()) {
- valid = true;
- } else {
- valid = false;
- }
- if (_rollbackAfterValidation) {
- conn.rollback();
- }
- } catch(Exception e) {
+ PooledConnection pconn = obj.getPooledConnection();
+ String query = _validationQuery;
+ if (null != query) {
+ Connection conn = null;
+ Statement stmt = null;
+ ResultSet rset = null;
+ // logical Connection from the PooledConnection must be closed
+ // before another one can be requested and closing it will
+ // generate an event. Keep track so we know not to return
+ // the PooledConnection
+ validatingSet.add(pconn);
+ try {
+ conn = pconn.getConnection();
+ stmt = conn.createStatement();
+ rset = stmt.executeQuery(query);
+ if (rset.next()) {
+ valid = true;
+ } else {
valid = false;
- } finally {
- if (rset != null) {
- try {
- rset.close();
- } catch (Throwable t) {
- // ignore
- }
+ }
+ if (_rollbackAfterValidation) {
+ conn.rollback();
+ }
+ } catch(Exception e) {
+ valid = false;
+ } finally {
+ if (rset != null) {
+ try {
+ rset.close();
+ } catch (Throwable t) {
+ // ignore
}
- if (stmt != null) {
- try {
- stmt.close();
- } catch (Throwable t) {
- // ignore
- }
+ }
+ if (stmt != null) {
+ try {
+ stmt.close();
+ } catch (Throwable t) {
+ // ignore
}
- if (conn != null) {
- try {
- conn.close();
- } catch (Throwable t) {
- // ignore
- }
+ }
+ if (conn != null) {
+ try {
+ conn.close();
+ } catch (Throwable t) {
+ // ignore
}
- validatingMap.remove(pconn);
}
- } else {
- valid = true;
+ validatingSet.remove(pconn);
}
} else {
- valid = false;
+ valid = true;
}
return valid;
}
@Override
- public void passivateObject(Object key, Object obj) {
+ public void passivateObject(UserPassKey key, PooledConnectionAndInfo obj) {
}
@Override
- public void activateObject(Object key, Object obj) {
+ public void activateObject(UserPassKey key, PooledConnectionAndInfo obj) {
}
// ***********************************************************************
@@ -247,9 +242,8 @@ class KeyedCPDSConnectionFactory
// if this event occurred because we were validating, or if this
// connection has been marked for removal, ignore it
// otherwise return the connection to the pool.
- if (!validatingMap.containsKey(pc)) {
- PooledConnectionAndInfo info =
- (PooledConnectionAndInfo) pcMap.get(pc);
+ if (!validatingSet.contains(pc)) {
+ PooledConnectionAndInfo info = pcMap.get(pc);
if (info == null) {
throw new IllegalStateException(NO_KEY_MESSAGE);
}
@@ -284,7 +278,7 @@ class KeyedCPDSConnectionFactory
}
pc.removeConnectionEventListener(this);
- PooledConnectionAndInfo info = (PooledConnectionAndInfo) pcMap.get(pc);
+ PooledConnectionAndInfo info = pcMap.get(pc);
if (info == null) {
throw new IllegalStateException(NO_KEY_MESSAGE);
}
@@ -295,11 +289,11 @@ class KeyedCPDSConnectionFactory
e.printStackTrace();
}
}
-
+
// ***********************************************************************
// PooledConnectionManager implementation
// ***********************************************************************
-
+
/**
* Invalidates the PooledConnection in the pool. The
KeyedCPDSConnectionFactory
* closes the connection and pool counters are updated appropriately.
@@ -309,7 +303,7 @@ class KeyedCPDSConnectionFactory
*/
@Override
public void invalidate(PooledConnection pc) throws SQLException {
- PooledConnectionAndInfo info = (PooledConnectionAndInfo) pcMap.get(pc);
+ PooledConnectionAndInfo info = pcMap.get(pc);
if (info == null) {
throw new IllegalStateException(NO_KEY_MESSAGE);
}
@@ -321,14 +315,14 @@ class KeyedCPDSConnectionFactory
throw (SQLException) new SQLException("Error invalidating
connection").initCause(ex);
}
}
-
+
/**
* Does nothing. This factory does not cache user credentials.
*/
@Override
public void setPassword(String password) {
}
-
+
/**
* This implementation does not fully close the KeyedObjectPool, as
* this would affect all users. Instead, it clears the pool associated
@@ -340,7 +334,7 @@ class KeyedCPDSConnectionFactory
_pool.clear(new UserPassKey(username, null));
} catch (Exception ex) {
throw (SQLException) new SQLException("Error closing connection
pool").initCause(ex);
- }
+ }
}
-
+
}
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]