1 5 6 package com.raptus.owxv3; 7 8 import java.util.*; 9 import java.sql.*; 10 import javax.sql.*; 11 12 36 public class SQLDataManager extends PropertiesDataManager 37 { 38 protected static final String PROPERTY_DATATYPE = "data.type"; 39 protected static final String PROPERTY_TYPE_NAMESPACED = "namespaced"; 40 protected static final String PROPERTY_TYPE_DIRECT = "direct"; 41 protected static final String PROPERTY_SQLSETTINGS = "data.sql.settings"; 42 protected static final String PROPERTY_SQLTABLES = "data.sql.tables"; 43 protected static final String PROPERTY_SQLTABLE = "data.sql.table."; 44 protected static final String PROPERTY_SQLTABLE_CREATE = ".createstmt"; 45 protected static final String PROPERTY_SQLTABLE_STATEMENT = ".statement"; 46 47 50 protected Hashtable sqlTables = new Hashtable(); 51 protected String settings = null; 52 53 54 63 protected Hashtable vmoduleTables= new Hashtable(); 64 65 68 protected boolean namespaced = false; 69 70 73 protected javax.sql.DataSource ds = null; 74 75 78 public boolean initialize(String prefix, DataSource ds) 79 { 80 LoggingManager.log("SQLDataManager initialise!, prefix="+prefix, this); 81 84 this.ds = ds; 85 86 process(); 87 return true; 88 } 89 90 93 public boolean initialize(VModule m) 94 { 95 LoggingManager.log("SQLDataManager.initialise, vm="+m.getIdentification(), this); 96 if(!super.initialize(m)) 97 { 98 LoggingManager.log("super.initialise failed!", this); 99 } 101 102 ds = vmodule.getDatasource(); 103 104 process(m); 105 return true; 106 } 107 108 109 110 113 public void process() 114 { 115 process(null); 116 117 } 118 119 122 public void process(VModule vm) 123 { 124 LoggingManager.log("SQLDataManager.process(),vm="+vm, this); 125 XMLConfigManager xmlc = XMLConfigManager.getInstance(); 126 Vector tableStatements = new Vector(); 127 128 int tableCount = 0; 129 int stmtCount = 0; 130 131 if(vm == null) 132 { 133 LoggingManager.log("Processing in SQLDataManager() with vm=null", this); 134 String tablesprop = xmlc.getPropertyByTree("virtualhost/global_resource/property?name=tables", "value"); 135 136 LoggingManager.log("Tables are :"+tablesprop, this); 137 138 if(tablesprop != null && tablesprop.length() > 0) 139 { 140 StringTokenizer st = new StringTokenizer(tablesprop, ","); 141 while(st.hasMoreTokens()) 142 { 143 String tableName = st.nextToken(); 144 String type = xmlc.getPropertyByTree("virtualhost/global_resource/table?name="+tableName, "type"); 145 if(type != null && type.compareToIgnoreCase(PROPERTY_TYPE_NAMESPACED) == 0) 146 namespaced = true; 147 LoggingManager.log("Table is namespaced", this); 148 String statements = xmlc.getPropertyByTree("virtualhost/global_resource/table?name="+tableName, "statements"); 149 LoggingManager.log("statements="+statements, this); 150 int i = 1; 152 StringTokenizer stt = new StringTokenizer(statements, ","); 153 while(stt.hasMoreTokens()) 154 { 155 String createStmt = xmlc.getPropertyByTree("virtualhost/global_resource/table?name="+tableName+"/statement?name="+stt.nextToken(), ""); 156 LoggingManager.log("Statement="+createStmt, this); 157 if(createStmt != null && createStmt.length() > 0) 158 { 159 if(namespaced) 160 createStmt = parseAndPrefixTablestatement(createStmt, tableName); 161 tableStatements.add(createStmt); 162 } 163 164 stmtCount ++; 165 } 166 sqlTables.put(tableName, tableStatements); 168 tableCount ++; 169 } 182 183 LoggingManager.log("Found " + tableCount + " tables and " + 184 stmtCount + " statements", this); 185 } 186 else 187 LoggingManager.log("Failed to find property " + PROPERTY_SQLTABLES, this); 188 191 } 192 else 193 { 194 LoggingManager.log("Processing in SQLDataManager() with vm="+vm, this); 196 String tablesprop = xmlc.getPropertyByTree("virtualhost/vmodules/vmodule?name="+vm.getIdentification()+"/properties/property?name=tables", "value"); 197 LoggingManager.log("Tables are :"+tablesprop, this); 198 if(tablesprop == null && vm.isVirtual()) 199 { 200 tablesprop = xmlc.getPropertyByTree("virtualhost/vmodules/vmodule?name="+vm.getBaseModule()+"/properties/property?name=tables", "value"); 201 LoggingManager.log("Tables are (from basemodule):"+tablesprop, this); 202 } 203 204 if( tablesprop== null) 205 { 206 tablesprop=""; 207 } 208 209 StringTokenizer st = new StringTokenizer(tablesprop, ","); 210 while(st.hasMoreTokens()) 211 { 212 213 String tableName = st.nextToken(); 214 String type = xmlc.getPropertyByTree("virtualhost/vmodules/vmodule?name="+vm.getIdentification()+"/table?name="+tableName, "type"); 215 if(type == null&&vm.isVirtual()) 216 { 217 type = xmlc.getPropertyByTree("virtualhost/vmodules/vmodule?name="+vm.getBaseModule()+"/table?name="+tableName, "type"); 219 LoggingManager.log("Got table type from base module "+type, this); 220 } 221 if(type != null && type.compareToIgnoreCase(PROPERTY_TYPE_NAMESPACED) == 0) 222 namespaced = true; 223 LoggingManager.log("Table is namespaced", this); 224 225 String statements = xmlc.getPropertyByTree("virtualhost/vmodules/vmodule?name="+vm.getIdentification()+"/table?name="+tableName, "statements"); 226 if(statements == null && vm.isVirtual()) 227 { 228 statements = xmlc.getPropertyByTree("virtualhost/vmodules/vmodule?name="+vm.getBaseModule()+"/table?name="+tableName, "statements"); 229 LoggingManager.log("Got statements from base module!", this); 230 } 231 LoggingManager.log("statements="+statements, this); 232 233 int i = 1; 235 StringTokenizer stt = new StringTokenizer(statements, ","); 236 while(stt.hasMoreTokens()) 237 { 238 String stok = stt.nextToken(); 239 String createStmt = xmlc.getPropertyByTree("virtualhost/vmodules/vmodule?name="+vm.getIdentification()+"/table?name="+tableName+"/statement?name="+stok, ""); 240 if(createStmt == null && vm.isVirtual()) 241 { 242 createStmt = xmlc.getPropertyByTree("virtualhost/vmodules/vmodule?name="+vm.getBaseModule()+"/table?name="+tableName+"/statement?name="+stok, ""); 243 LoggingManager.log("Got create statement from base module!", this); 244 } 245 LoggingManager.log("Statement="+createStmt, this); 246 if(createStmt != null && createStmt.length() > 0) 247 { 248 if(namespaced) 249 createStmt = parseAndPrefixTablestatement(createStmt, tableName); 250 tableStatements.add(createStmt); 251 } 252 253 stmtCount ++; 254 } 255 sqlTables.put(tableName, tableStatements); 257 tableCount ++; 258 if(vm!=null){ 262 263 vmoduleTables.put(tableName+vm.getIdentification(),vm.getIdentification()); 265 LoggingManager.log("Putting "+tableName+vm.getIdentification()+" for vmodule "+ vm.getIdentification(), this); 266 } 267 } 271 272 LoggingManager.log("Found " + tableCount + " tables and " + 273 stmtCount + " statements", this); 274 276 278 281 283 288 } 353 } 354 355 369 protected String parseAndPrefixTablestatement(String stmt, String tableName) 370 { 371 String vmid = vmodule.getIdentification(); 372 LoggingManager.log("parseAndPrefixTablestatement,vmid=" + 373 vmid, this); 374 int count = 0; 375 int spos = 0; 376 while( (spos = stmt.indexOf(tableName, spos)) != -1 ) 377 { 378 int epos = spos + tableName.length(); 379 String tmp = stmt.substring(0, epos) + vmid; 380 stmt = tmp + stmt.substring(epos); 381 spos = epos; 382 count++; 383 } 384 385 return stmt; 386 } 387 388 391 public boolean prepareData() 392 { 393 LoggingManager.log("in SQLDataManager.prepareData()", this); 394 boolean retval = super.prepareData(); 395 396 Connection con = null; 398 DatabaseMetaData dbmd = null; 399 try 400 { 401 con = ds.getConnection(); 402 dbmd = con.getMetaData(); 403 404 if(settings != null && settings.length() > 0) 405 { 406 Statement stmt = con.createStatement(); 407 int retcount = stmt.executeUpdate(settings); 408 stmt.close(); 409 410 LoggingManager.log("ExecuteUpdated SQL settings. Return value: " + retcount, this); 411 } 412 } 413 catch(SQLException e) { 414 LoggingManager.log("Cannot get connection to database or its metadata. " + 415 e.getMessage(), this); 416 return false; 417 } 418 419 Enumeration tablenames = sqlTables.keys(); 421 while(tablenames.hasMoreElements()) 422 { 423 String table = (String ) tablenames.nextElement(); 424 Vector tableStmts = (Vector) sqlTables.get(table); 425 426 boolean exist = true; 428 String tblId = table + ((namespaced) ? propPrefix : ""); 429 LoggingManager.log("checking if table " + tblId + " exists.", this); 430 try 431 { 432 ResultSet rs = dbmd.getTables(null, null, tblId, null); 434 436 exist = false; 437 while(rs.next()) 438 { 439 if(tblId.equals(rs.getString("TABLE_NAME"))) 440 { 441 exist = true; 442 } 443 } 444 } 445 catch(SQLException e) { 446 LoggingManager.log("SQLException while checking if table " + tblId + " exists.", this); 447 } 448 449 if(!exist) 450 { 451 int count = 0; 453 Iterator stmts = tableStmts.iterator(); 454 while(stmts.hasNext()) 455 { 456 count++; 457 458 String strstmt = (String ) stmts.next(); 460 try 461 { 462 if(strstmt != null && strstmt.length() > 0) 463 { 464 Statement stmt = con.createStatement(); 465 int retcount = stmt.executeUpdate(strstmt); 466 stmt.close(); 467 LoggingManager.log("ExecuteUpdated SQL statement #" + count + 468 ". Return value: " + retcount, this); 469 } 470 else 471 LoggingManager.log("Missing SQL statement #" + count, this); 472 } 473 catch(SQLException e) { 474 LoggingManager.log("SQLException while preparing table " + table + " " + 475 e.getMessage() + "(Stmt: " + strstmt + ")", this); 476 } 477 } 478 } 479 else 480 LoggingManager.log("Table " + tblId + " exists. Doing nothing.", this); 481 } 482 483 try 485 { 486 con.commit(); 487 con.close(); 488 } 489 catch(SQLException e) { 490 LoggingManager.log("SQLException while closing connection to database. " + 491 e.getMessage(), this); 492 } 493 494 return retval; 495 } 496 497 public void setProperty(String property, 499 String value) { super.setProperty(property, value); } 500 501 public void saveProperties() { super.saveProperties(); } 502 503 504 508 509 public Hashtable getTableNames(){ 510 return vmoduleTables; 511 } 512 } 513 514 515 | Popular Tags |