C:\code\src\poolman\src\main\com\codestudio\bean\struts\PoolManAction.java

1    /* 
2     *  PoolMan Java Object Pooling and Caching Library 
3     *  Copyright (C) 1999-2001 The Code Studio 
4     * 
5     *  This library is free software; you can redistribute it and/or 
6     *  modify it under the terms of the GNU Lesser General Public 
7     *  License as published by the Free Software Foundation; either 
8     *  version 2 of the License, or (at your option) any later version. 
9     * 
10    *  This library is distributed in the hope that it will be useful, 
11    *  but WITHOUT ANY WARRANTY; without even the implied warranty of 
12    *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU 
13    *  Lesser General Public License for more details. 
14    * 
15    *  The full license is located at the root of this distribution 
16    *  in the LICENSE file. 
17    */ 
18   package com.codestudio.bean.struts; 
19    
20   import com.codestudio.sql.PoolMan; 
21   import com.codestudio.util.JDBCPool; 
22   import com.codestudio.util.SQLManager; 
23   import org.apache.struts.action.Action; 
24   import org.apache.struts.action.ActionForm; 
25   import org.apache.struts.action.ActionForward; 
26   import org.apache.struts.action.ActionMapping; 
27    
28   import javax.servlet.ServletException; 
29   import javax.servlet.http.HttpServletRequest; 
30   import javax.servlet.http.HttpServletResponse; 
31   import javax.servlet.http.HttpSession; 
32   import javax.sql.DataSource; 
33   import java.io.IOException; 
34   import java.sql.*; 
35   import java.util.ArrayList; 
36    
37    
38   /** 
39    * A struts Action for handling the poolman JSP form. 
40    * 
41    * @author PS Neville 
42    */ 
43   public final class PoolManAction 
44           extends Action { 
45    
46       /** 
47        *  Perform the Struts action associated with the poolman struts form. 
48        */ 
49       public ActionForward perform(ActionMapping mapping, 
50                                    ActionForm form, 
51                                    HttpServletRequest request, 
52                                    HttpServletResponse response) 
53               throws IOException, ServletException { 
54    
55           // retrieve the request data necessary to perform the action 
56           String sqlQuery = ((PoolManActionFormBean) form).getSql(); 
57           String dbName = ((PoolManActionFormBean) form).getCurrentDatabaseName(); 
58    
59           // create or retrieve the results and metrics beans 
60           HttpSession session = request.getSession(); 
61           PoolManResultsBean resultsBean = getResultsBean(session); 
62           PoolManMetricsBean metricsBean = getMetricsBean(session); 
63    
64           // execute the query 
65           if ((sqlQuery != null) && (sqlQuery.length() > 0)) { 
66               try { 
67                   executeSQL(dbName, sqlQuery, resultsBean, metricsBean); 
68               } catch (Exception sqle) { 
69                   resultsBean.setError(sqle.toString()); 
70               } 
71           } 
72    
73           // prepare the Metrics Bean using the DataSource's pool 
74            metricsBean.setJDBCPool((JDBCPool) SQLManager.getInstance().getPool(dbName)); 
75    
76           // set the beans 
77           session.setAttribute("poolmanResults", resultsBean); 
78           session.setAttribute("poolmanMetrics", metricsBean); 
79    
80           // forward control to the specified success URI 
81           return (mapping.findForward("poolman")); 
82    
83       } 
84    
85       /** 
86        * Execute a generic sql statement without knowing much about it or its destination. 
87        * A real-world query would be less generic, making the code simpler than this example. 
88        */ 
89       protected void executeSQL(String dbName, String sqlQuery, 
90                                 PoolManResultsBean resultsBean, 
91                                 PoolManMetricsBean metricsBean) 
92               throws SQLException { 
93    
94           // clear previous data 
95           resultsBean.setError(null); 
96           resultsBean.setHeaderColumns(null); 
97           resultsBean.setRows(null); 
98    
99           // prepare the variables outside the loop to ensure closing 
100          DataSource ds = null; 
101          Connection con = null; 
102          Statement st = null; 
103          ResultSet res = null; 
104   
105          metricsBean.incrementQueryCount(); 
106   
107          try { 
108   
109              // start the method timer 
110              long start = System.currentTimeMillis(); 
111   
112              // get the DataSource 
113              ds = PoolMan.findDataSource(dbName); 
114   
115              // get the Connection 
116              con = ds.getConnection(); 
117   
118              // create the Statement 
119              st = con.createStatement(); 
120   
121              if (st.execute(sqlQuery)) { 
122   
123                  // done executing query, set time 
124                  metricsBean.setLastQueryDuration(System.currentTimeMillis() - start); 
125   
126                  // there was a ResultSet 
127                  res = st.getResultSet(); 
128   
129                  //get the header 
130                  ResultSetMetaData meta = res.getMetaData(); 
131                  ArrayList header = new ArrayList(); 
132                  for (int n = 1; n <= meta.getColumnCount(); n++) { 
133                      header.add(meta.getColumnLabel(n)); 
134                  } 
135                  resultsBean.setHeaderColumns(header); 
136   
137                  while (res.next()) { 
138                      ArrayList row = new ArrayList(); 
139                      for (int n = 1; n <= meta.getColumnCount(); n++) { 
140                          Object value = null; 
141                          try { 
142                              switch (meta.getColumnType(n)) { 
143                                  case Types.CHAR: 
144                                      try { 
145                                          value = new String(res.getBytes(n)); 
146                                      } catch (Exception _e) { 
147                                          value = res.getObject(n); 
148                                      } 
149                                      break; 
150                                  default: 
151                                      value = res.getObject(n); 
152                                      break; 
153                              } 
154                          } catch (Exception ee) { 
155                          } 
156                          if (value == null) 
157                              value = new String("NULL"); 
158                          row.add(value); 
159                      } 
160                      resultsBean.addRow(row); 
161                  } 
162              } 
163   
164              else { 
165   
166                  // done executing query, set time 
167                  metricsBean.setLastQueryDuration(System.currentTimeMillis() - start); 
168   
169                  // no ResultSet 
170                  String resultsMessage = null; 
171                  int num = st.getUpdateCount(); 
172                  switch (num) { 
173                      case 0: 
174                          resultsMessage = "No rows affected"; 
175                          break; 
176                      case 1: 
177                          resultsMessage = "1 row affected"; 
178                          break; 
179                      default: 
180                          resultsMessage = num + " rows affected"; 
181                  } 
182                  ArrayList row = new ArrayList(); 
183                  row.add(resultsMessage); 
184                  resultsBean.addRow(row); 
185              } 
186   
187          } catch (SQLException e) { 
188              throw e; 
189          } 
190          finally { 
191              JDBCPool.closeResources(con, st, res); 
192          } 
193      } 
194   
195      protected PoolManMetricsBean getMetricsBean(HttpSession session) { 
196          PoolManMetricsBean metricsBean = null; 
197          try { 
198              metricsBean = (PoolManMetricsBean) session.getAttribute("poolmanMetrics"); 
199          } catch (Exception e) { 
200          } 
201          if (metricsBean == null) 
202              metricsBean = new PoolManMetricsBean(); 
203          return metricsBean; 
204      } 
205   
206      protected PoolManResultsBean getResultsBean(HttpSession session) { 
207          PoolManResultsBean resultsBean = null; 
208          try { 
209              resultsBean = (PoolManResultsBean) session.getAttribute("poolmanResults"); 
210          } catch (Exception e) { 
211          } 
212          if (resultsBean == null) { 
213              resultsBean = new PoolManResultsBean(); 
214          } 
215          return resultsBean; 
216      } 
217  } 
218   
219