Fix the PoolingDriver related generics warnings
authorMark Thomas <markt@apache.org>
Tue, 3 Dec 2013 23:40:32 +0000 (23:40 +0000)
committerMark Thomas <markt@apache.org>
Tue, 3 Dec 2013 23:40:32 +0000 (23:40 +0000)
git-svn-id: https://svn.apache.org/repos/asf/commons/proper/dbcp/trunk@1547646 13f79535-47bb-0310-9956-ffa450edef68

doc/PoolingDriverExample.java
src/java/org/apache/commons/dbcp2/PoolingDriver.java
src/test/org/apache/commons/dbcp2/TestPoolingDriver.java

index c0db6cb..3a486f6 100644 (file)
@@ -5,9 +5,9 @@
  * 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.
@@ -30,6 +30,7 @@ import java.sql.SQLException;
 import org.apache.commons.pool2.ObjectPool;
 import org.apache.commons.pool2.impl.GenericObjectPool;
 import org.apache.commons.dbcp2.ConnectionFactory;
+import org.apache.commons.dbcp2.PoolableConnection;
 import org.apache.commons.dbcp2.PoolingDriver;
 import org.apache.commons.dbcp2.PoolableConnectionFactory;
 import org.apache.commons.dbcp2.DriverManagerConnectionFactory;
@@ -171,8 +172,8 @@ public class PoolingDriverExample {
         // We'll use a GenericObjectPool instance, although
         // any ObjectPool implementation will suffice.
         //
-        ObjectPool connectionPool =
-            new GenericObjectPool(poolableConnectionFactory);
+        ObjectPool<PoolableConnection> connectionPool =
+            new GenericObjectPool<>(poolableConnectionFactory);
 
         //
         // Finally, we create the PoolingDriver itself...
@@ -193,8 +194,8 @@ public class PoolingDriverExample {
 
     public static void printDriverStats() throws Exception {
         PoolingDriver driver = (PoolingDriver) DriverManager.getDriver("jdbc:apache:commons:dbcp:");
-        ObjectPool connectionPool = driver.getConnectionPool("example");
-        
+        ObjectPool<? extends Connection> connectionPool = driver.getConnectionPool("example");
+
         System.out.println("NumActive: " + connectionPool.getNumActive());
         System.out.println("NumIdle: " + connectionPool.getNumIdle());
     }
index ba3d915..ada8350 100644 (file)
@@ -50,7 +50,7 @@ public class PoolingDriver implements Driver {
     }
 
     /** The map of registered pools. */
-    protected static final HashMap<String,ObjectPool<Connection>> _pools =
+    protected static final HashMap<String,ObjectPool<? extends Connection>> _pools =
             new HashMap<>();
 
     /** Controls access to the underlying connection */
@@ -66,8 +66,8 @@ public class PoolingDriver implements Driver {
     protected PoolingDriver(boolean accessToUnderlyingConnectionAllowed) {
         this.accessToUnderlyingConnectionAllowed = accessToUnderlyingConnectionAllowed;
     }
-    
-    
+
+
     /**
      * Returns the value of the accessToUnderlyingConnectionAllowed property.
      *
@@ -77,9 +77,9 @@ public class PoolingDriver implements Driver {
         return accessToUnderlyingConnectionAllowed;
     }
 
-    public synchronized ObjectPool<Connection> getConnectionPool(String name)
+    public synchronized ObjectPool<? extends Connection> getConnectionPool(String name)
             throws SQLException {
-        ObjectPool<Connection> pool = _pools.get(name);
+        ObjectPool<? extends Connection> pool = _pools.get(name);
         if (null == pool) {
             throw new SQLException("Pool not registered.");
         }
@@ -87,12 +87,12 @@ public class PoolingDriver implements Driver {
     }
 
     public synchronized void registerPool(String name,
-            ObjectPool<Connection> pool) {
+            ObjectPool<? extends Connection> pool) {
         _pools.put(name,pool);
     }
 
     public synchronized void closePool(String name) throws SQLException {
-        ObjectPool<Connection> pool = _pools.get(name);
+        ObjectPool<? extends Connection> pool = _pools.get(name);
         if (pool != null) {
             _pools.remove(name);
             try {
@@ -121,17 +121,17 @@ public class PoolingDriver implements Driver {
     @Override
     public Connection connect(String url, Properties info) throws SQLException {
         if(acceptsURL(url)) {
-            ObjectPool<Connection> pool =
+            ObjectPool<? extends Connection> pool =
                 getConnectionPool(url.substring(URL_PREFIX_LEN));
             if(null == pool) {
                 throw new SQLException("No pool found for " + url + ".");
             } else {
                 try {
                     Connection conn = pool.borrowObject();
-                    if (conn != null) {
-                        conn = new PoolGuardConnectionWrapper(pool, conn);
+                    if (conn == null) {
+                        return null;
                     }
-                    return conn;
+                    return new PoolGuardConnectionWrapper(pool, conn);
                 } catch(SQLException e) {
                     throw e;
                 } catch(NoSuchElementException e) {
@@ -164,7 +164,8 @@ public class PoolingDriver implements Driver {
     public void invalidateConnection(Connection conn) throws SQLException {
         if (conn instanceof PoolGuardConnectionWrapper) { // normal case
             PoolGuardConnectionWrapper pgconn = (PoolGuardConnectionWrapper) conn;
-            ObjectPool<Connection> pool = pgconn.pool;
+            @SuppressWarnings("unchecked")
+            ObjectPool<Connection> pool = (ObjectPool<Connection>) pgconn.pool;
             try {
                 pool.invalidateObject(pgconn.getDelegateInternal());
             }
@@ -208,11 +209,11 @@ public class PoolingDriver implements Driver {
      * PoolGuardConnectionWrapper is a Connection wrapper that makes sure a
      * closed connection cannot be used anymore.
      */
-    private class PoolGuardConnectionWrapper extends DelegatingConnection {
+    private class PoolGuardConnectionWrapper extends DelegatingConnection<Connection> {
 
-        private final ObjectPool<Connection> pool;
+        private final ObjectPool<? extends Connection> pool;
 
-        PoolGuardConnectionWrapper(ObjectPool<Connection> pool,
+        PoolGuardConnectionWrapper(ObjectPool<? extends Connection> pool,
                 Connection delegate) {
             super(delegate);
             this.pool = pool;
index 4568ac3..d5a95ee 100644 (file)
@@ -76,7 +76,7 @@ public class TestPoolingDriver extends TestConnectionPool {
         poolConfig.setNumTestsPerEvictionRun(5);
         poolConfig.setMinEvictableIdleTimeMillis(5000L);
 
-        GenericObjectPool pool = new GenericObjectPool(pcf, poolConfig);
+        GenericObjectPool<PoolableConnection> pool = new GenericObjectPool<>(pcf, poolConfig);
         pcf.setPool(pool);
 
         assertNotNull(pcf);
@@ -107,7 +107,7 @@ public class TestPoolingDriver extends TestConnectionPool {
             new PoolableConnectionFactory(connectionFactory);
         pcf.setDefaultReadOnly(false);
         pcf.setDefaultAutoCommit(true);
-        GenericObjectPool connectionPool = new GenericObjectPool(pcf);
+        GenericObjectPool<PoolableConnection> connectionPool = new GenericObjectPool<>(pcf);
         PoolingDriver driver2 = new PoolingDriver();
         driver2.registerPool("example",connectionPool);
     }
@@ -147,8 +147,8 @@ public class TestPoolingDriver extends TestConnectionPool {
             new PoolableConnectionFactory(connectionFactory);
         poolableConnectionFactory.setDefaultReadOnly(false);
         poolableConnectionFactory.setDefaultAutoCommit(true);
-        ObjectPool connectionPool =
-            new GenericObjectPool(poolableConnectionFactory,config);
+        ObjectPool<PoolableConnection> connectionPool =
+                new GenericObjectPool<>(poolableConnectionFactory,config);
         poolableConnectionFactory.setPool(connectionPool);
         assertNotNull(poolableConnectionFactory);
         PoolingDriver driver2 = new PoolingDriver();