You are viewing a plain text version of this content. The canonical link for it is here.
Posted to scm@geronimo.apache.org by dj...@apache.org on 2008/06/10 10:18:01 UTC

svn commit: r666003 - /geronimo/samples/trunk/samples/dbtester/dbtester-war/src/main/java/org/apache/geronimo/samples/dbtester/beans/DBManagerBean.java

Author: djencks
Date: Tue Jun 10 01:18:01 2008
New Revision: 666003

URL: http://svn.apache.org/viewvc?rev=666003&view=rev
Log:
minor code cleanup

Modified:
    geronimo/samples/trunk/samples/dbtester/dbtester-war/src/main/java/org/apache/geronimo/samples/dbtester/beans/DBManagerBean.java

Modified: geronimo/samples/trunk/samples/dbtester/dbtester-war/src/main/java/org/apache/geronimo/samples/dbtester/beans/DBManagerBean.java
URL: http://svn.apache.org/viewvc/geronimo/samples/trunk/samples/dbtester/dbtester-war/src/main/java/org/apache/geronimo/samples/dbtester/beans/DBManagerBean.java?rev=666003&r1=666002&r2=666003&view=diff
==============================================================================
--- geronimo/samples/trunk/samples/dbtester/dbtester-war/src/main/java/org/apache/geronimo/samples/dbtester/beans/DBManagerBean.java (original)
+++ geronimo/samples/trunk/samples/dbtester/dbtester-war/src/main/java/org/apache/geronimo/samples/dbtester/beans/DBManagerBean.java Tue Jun 10 01:18:01 2008
@@ -25,7 +25,6 @@
 import java.sql.SQLException;
 import java.util.ArrayList;
 import java.util.HashMap;
-import java.util.Iterator;
 import java.util.List;
 import java.util.Map;
 import java.util.Set;
@@ -34,43 +33,27 @@
 
 import org.apache.geronimo.gbean.AbstractName;
 import org.apache.geronimo.gbean.AbstractNameQuery;
-import org.apache.geronimo.kernel.GBeanNotFoundException;
-import org.apache.geronimo.kernel.InternalKernelException;
 import org.apache.geronimo.kernel.Kernel;
 import org.apache.geronimo.kernel.KernelRegistry;
-import org.apache.geronimo.kernel.NoSuchOperationException;
 import org.apache.geronimo.naming.ResourceSource;
 
 public class DBManagerBean {
 
-    private Map poolMap;
-    private Map tableMap;
+    private final Map<String, DataSource> poolMap = new HashMap<String, DataSource>();
 
     public DBManagerBean() {
-        poolMap = new HashMap();
-        init();
-    }
-
-    private void init() {
         Kernel kernel = KernelRegistry.getSingleKernel();
-        Set cfList = kernel.listGBeans(new AbstractNameQuery(ResourceSource.class.getName()));
+        Set<AbstractName> cfList = kernel.listGBeans(new AbstractNameQuery(ResourceSource.class.getName()));
 
-        for (Iterator iterator = cfList.iterator(); iterator.hasNext();) {
+        for (AbstractName name : cfList) {
 
-            AbstractName name = (AbstractName) iterator.next();
             try {
                 Object rs = kernel.invoke(name, "$getResource", new Object[]{}, new String[]{});
 
-                if (rs instanceof javax.sql.DataSource) {
+                if (rs instanceof DataSource) {
                     DataSource ds = (DataSource) rs;
                     poolMap.put(name.getArtifact().getArtifactId(), ds);
                 }
-            } catch (GBeanNotFoundException e) {
-                e.printStackTrace();
-            } catch (NoSuchOperationException e) {
-                e.printStackTrace();
-            } catch (InternalKernelException e) {
-                e.printStackTrace();
             } catch (Exception e) {
                 e.printStackTrace();
             }
@@ -83,10 +66,10 @@
 
     public Map getTableList(String poolName) throws SQLException {
 
-        tableMap = new HashMap();
+        Map<String, List<String>> tableMap = new HashMap<String, List<String>>();
 
         if (poolMap.containsKey(poolName)) {
-            DataSource ds = (DataSource) poolMap.get(poolName);
+            DataSource ds = poolMap.get(poolName);
             Connection con = null;
             try {
 
@@ -99,19 +82,17 @@
                 while (rs.next()) {
                     String schemaName = rs.getString("TABLE_SCHEM");
                     String tableName = rs.getString("TABLE_NAME");
-                    ArrayList tableList = null;
+                    List<String> tableList;
 
                     if (tableMap.containsKey(schemaName)) {
-                        tableList = (ArrayList) tableMap.get(schemaName);
+                        tableList = tableMap.get(schemaName);
                         tableList.add(tableName);
                     } else {
-                        tableList = new ArrayList();
+                        tableList = new ArrayList<String>();
                         tableList.add(tableName);
                     }
                     tableMap.put(schemaName, tableList);
                 }
-            } catch (SQLException e) {
-                throw e;
             } finally {
                 if (con != null) {
                     try {
@@ -126,43 +107,56 @@
         return tableMap;
     }
 
-    public List getTableContents(String poolName, String schemaName, String tableName) throws SQLException {
-
-        ArrayList matrix = new ArrayList();
-
-        DataSource ds = (DataSource) poolMap.get(poolName);
-        Connection con = null;
-
-        con = ds.getConnection();
+    public List<List<String>> getTableContents(String poolName, String schemaName, String tableName) throws SQLException {
 
-        String SQL = "SELECT * FROM ";
+        List<List<String>> matrix = new ArrayList<List<String>>();
 
-        if (schemaName == null || schemaName.equals("null")) {
-            SQL += tableName; //Problem comes with MySQL
-        } else {
-            SQL += (schemaName + "." + tableName);
+        DataSource ds = poolMap.get(poolName);
+        if (ds == null) {
+            throw new IllegalArgumentException("no pool named: " + poolName);
         }
+        Connection con = ds.getConnection();
 
-        PreparedStatement pStmt = con.prepareStatement(SQL);
-        ResultSet rs = pStmt.executeQuery();
+        try {
+            String SQL = "SELECT * FROM ";
 
-        ResultSetMetaData metaData = rs.getMetaData();
-        int columns = metaData.getColumnCount();
+            if (schemaName == null || schemaName.equals("null")) {
+                SQL += tableName; //Problem comes with MySQL
+            } else {
+                SQL += (schemaName + "." + tableName);
+            }
 
-        ArrayList header = new ArrayList();
-        for (int i = 1; i <= columns; i++) {
-            String colName = metaData.getColumnLabel(i);
-            header.add(colName);
-        }
-        matrix.add(header);
+            PreparedStatement pStmt = con.prepareStatement(SQL);
+            try {
+                ResultSet rs = pStmt.executeQuery();
+
+                try {
+                    ResultSetMetaData metaData = rs.getMetaData();
+                    int columns = metaData.getColumnCount();
+
+                    ArrayList<String> header = new ArrayList<String>();
+                    for (int i = 1; i <= columns; i++) {
+                        String colName = metaData.getColumnLabel(i);
+                        header.add(colName);
+                    }
+                    matrix.add(header);
 
-        while (rs.next()) {
-            ArrayList row = new ArrayList();
+                    while (rs.next()) {
+                        ArrayList<String> row = new ArrayList<String>();
 
-            for (int i = 1; i <= columns; i++) {
-                row.add(rs.getString(i));
+                        for (int i = 1; i <= columns; i++) {
+                            row.add(rs.getString(i));
+                        }
+                        matrix.add(row);
+                    }
+                } finally {
+                    rs.close();
+                }
+            } finally {
+                pStmt.close();
             }
-            matrix.add(row);
+        } finally {
+            con.close();
         }
 
         return matrix;