1 13 package com.tonbeller.jpivot.mondrian; 14 15 import java.sql.Connection ; 16 import java.sql.DriverManager ; 17 import java.sql.ResultSet ; 18 import java.sql.ResultSetMetaData ; 19 import java.sql.SQLException ; 20 import java.sql.Statement ; 21 import java.util.ArrayList ; 22 import java.util.List ; 23 24 import javax.naming.Context ; 25 import javax.naming.InitialContext ; 26 import javax.naming.NamingException ; 27 import javax.sql.DataSource ; 28 29 import mondrian.rolap.RolapConnectionProperties; 30 import mondrian.olap.MemoryLimitExceededException; 31 import mondrian.util.MemoryMonitor; 32 import mondrian.util.MemoryMonitorFactory; 33 34 import org.apache.log4j.Logger; 35 36 import com.tonbeller.wcf.table.AbstractTableModel; 37 import com.tonbeller.wcf.table.DefaultTableRow; 38 import com.tonbeller.wcf.table.TableRow; 39 40 44 45 public class MondrianDrillThroughTableModel extends AbstractTableModel { 46 private static Logger logger = Logger.getLogger(MondrianDrillThroughTableModel.class); 47 private String title = "Drill Through Table"; 48 private String caption = ""; 49 private String sql = ""; 50 private String jdbcUser; 51 private String jdbcUrl; 52 private String jdbcPassword; 53 private String jdbcDriver; 54 private String dataSourceName; 55 56 private DataSource dataSource; 57 private static Context jndiContext; 58 59 private boolean ready = false; 60 61 private TableRow[] rows = new TableRow[0]; 62 private String [] columnTitles = new String [0]; 63 64 public MondrianDrillThroughTableModel() { 65 } 66 67 public int getRowCount() { 68 if ( !ready ) { 69 executeQuery(); 70 } 71 return rows.length; 72 } 73 74 public TableRow getRow(int rowIndex) { 75 if ( !ready ) { 76 executeQuery(); 77 } 78 return rows[rowIndex]; 79 } 80 81 public String getTitle() { 82 return title; 83 } 84 87 public String getSql() { 88 return sql; 89 } 90 91 94 public void setSql(String sql) { 95 this.sql = sql; 96 this.ready = false; 97 } 98 99 102 public void setTitle(String title) { 103 this.title = title; 104 } 105 106 111 public int getColumnCount() { 112 if ( !ready ) { 113 executeQuery(); 114 } 115 return columnTitles.length; 116 } 117 118 public String getColumnTitle(int columnIndex) { 119 if ( !ready ) { 120 executeQuery(); 121 } 122 return columnTitles[columnIndex]; 123 } 124 125 129 private void executeQuery() { 130 Connection con=null; 131 class Listener implements MemoryMonitor.Listener { 132 String oomMsg; 133 Listener() { 134 } 135 public void memoryUsageNotification(long used, long max) { 136 StringBuffer buf = new StringBuffer (200); 137 buf.append("OutOfMemory used="); 138 buf.append(used); 139 buf.append(", max="); 140 buf.append(max); 141 if (dataSourceName != null) { 142 buf.append(" for data source: "); 143 buf.append(dataSourceName); 144 } else if (jdbcUrl != null) { 145 buf.append(" for jcbc URL: "); 146 buf.append(jdbcUrl); 147 } 148 this.oomMsg = buf.toString(); 149 } 150 void check() throws MemoryLimitExceededException { 151 if (oomMsg != null) { 152 throw new MemoryLimitExceededException(oomMsg); 153 } 154 } 155 } 156 Listener listener = new Listener(); 157 MemoryMonitor mm = MemoryMonitorFactory.getMemoryMonitor(); 158 try { 159 mm.addListener(listener); 160 161 con = getConnection(); 162 Statement s = con.createStatement(); 163 ResultSet rs = s.executeQuery(sql); 164 ResultSetMetaData md = rs.getMetaData(); 165 int numCols = md.getColumnCount(); 166 columnTitles = new String [numCols]; 167 168 listener.check(); 170 171 for ( int i = 0; i < numCols; i++ ) { 173 columnTitles[i] = md.getColumnName(i+1); 175 } 176 title = title.concat(" for "+columnTitles[columnTitles.length-1]); 177 List tempRows = new ArrayList (); 179 while (rs.next()) { 180 Object [] row = new Object [numCols]; 181 for ( int i = 0; i < numCols; i++ ) { 183 row[i] = rs.getObject(i+1); 184 } 185 tempRows.add(new DefaultTableRow(row)); 186 187 listener.check(); 189 } 190 rs.close(); 191 rows = (TableRow[]) tempRows.toArray(new TableRow[0]); 192 } catch (Exception e) { 193 e.printStackTrace(); 194 logger.error("?", e); 195 rows = new TableRow[1]; 197 columnTitles = new String [1]; 198 columnTitles[0] = "An error occured"; 199 Object [] row = new Object [1]; 200 row[0] = e.toString(); 201 rows[0] = new DefaultTableRow(row); 202 ready=false; 203 return; 204 } finally { 205 try { 206 con.close(); 207 } catch (Exception e1) { 208 } 210 mm.removeListener(listener); 211 } 212 ready = true; 213 } 214 215 220 private Connection getConnection() throws SQLException { 221 if (dataSourceName == null) { 222 223 if (jdbcUrl == null) { 224 throw new RuntimeException ( 225 "Mondrian Connect string '" + 226 "' must contain either '" + 227 RolapConnectionProperties.Jdbc + 228 "' or '" + 229 RolapConnectionProperties.DataSource + "'"); 230 } 231 return DriverManager.getConnection(jdbcUrl, jdbcUser, jdbcPassword); 232 } else { 233 return getDataSource().getConnection(); 234 } 235 } 236 237 private DataSource getDataSource() { 238 if (dataSource == null) { 239 try { 241 dataSource = (DataSource ) getJndiContext().lookup(dataSourceName); 242 } catch (NamingException e) { 243 throw new RuntimeException ( 244 "Error while looking up data source (" + 245 dataSourceName + ")", e); 246 } 247 } 248 return dataSource; 249 } 250 251 private Context getJndiContext() throws NamingException { 252 if (jndiContext == null) { 253 jndiContext = new InitialContext (); 254 } 255 return jndiContext; 256 } 257 260 public String getJdbcDriver() { 261 return jdbcDriver; 262 } 263 264 267 public void setJdbcDriver(String jdbcDriver) { 268 this.jdbcDriver = jdbcDriver; 269 } 270 271 274 public String getJdbcPassword() { 275 return jdbcPassword; 276 } 277 278 281 public void setJdbcPassword(String jdbcPassword) { 282 this.jdbcPassword = jdbcPassword; 283 } 284 285 288 public String getJdbcUrl() { 289 return jdbcUrl; 290 } 291 292 295 public void setJdbcUrl(String jdbcUrl) { 296 this.jdbcUrl = jdbcUrl; 297 } 298 299 302 public String getJdbcUser() { 303 return jdbcUser; 304 } 305 306 309 public void setJdbcUser(String jdbcUser) { 310 this.jdbcUser = jdbcUser; 311 } 312 313 316 public String getCaption() { 317 return caption; 318 } 319 320 323 public void setCaption(String caption) { 324 this.caption = caption; 325 } 326 327 330 public String getDataSourceName() { 331 return dataSourceName; 332 } 333 334 337 public void setDataSourceName(String string) { 338 dataSourceName = string; 339 } 340 341 } 342 | Popular Tags |