1 23 24 package org.infoglue.cmsinstaller; 25 import java.sql.*; 26 import java.io.*; 27 import java.util.*; 28 import java.util.Date ; 29 30 import oracle.sql.BLOB; 31 import oracle.sql.CLOB; 32 33 public class OracleDatabaseCommander extends DatabaseCommander 34 { 35 private final String driver = "oracle.jdbc.driver.OracleDriver"; 36 38 public OracleDatabaseCommander(String driverClass, String databaseHostName, String databasePortNumber, String databaseInstance, String url, String userName, String password, String infoglueDatabaseUserName, String infoglueDatabasePassword, String databaseName, String databaseSuffix, String hostName, boolean createDatabase, boolean createInitialData, boolean createAntSeekHome, boolean createOfficeStand, boolean createOfficeStand2) throws Exception 39 { 40 super(driverClass, databaseHostName, databasePortNumber, databaseInstance, url, userName, password, infoglueDatabaseUserName, infoglueDatabasePassword, databaseName, databaseSuffix, hostName, createDatabase, createInitialData, createAntSeekHome, createOfficeStand, createOfficeStand2); 41 } 43 44 public String getUrl(String hostName, String databasePortNumber, String database) 45 { 46 return "jdbc:oracle:thin:@" + databaseHostName + ":" + databasePortNumber + ":" + databaseName + ""; 47 } 48 49 public String getUnicodeUrl(String hostName, String databasePortNumber, String database) 50 { 51 return "jdbc:oracle:thin:@" + databaseHostName + ":" + databasePortNumber + ":" + databaseName + ""; 52 } 53 54 public void setupDatabase() throws Exception 55 { 56 59 if(this.createInitialData) 60 { 61 createTables(); 63 createInitialData(); 64 setupExamples(); 65 } 66 67 } 68 69 public void deleteDatabase() throws Exception 70 { 71 } 74 75 76 private void createDatabase() throws Exception 77 { 78 Logger.logInfo("Setting up a new database...."); 79 issueCommand(getConnection(), "CREATE DATABASE " + databaseName + ";"); 80 } 81 82 private void createTables() throws Exception 83 { 84 String url = "jdbc:oracle:thin:@" + databaseHostName + ":" + this.databasePortNumber + ":" + databaseName + ""; 85 Connection conn = getConnection(url, this.userName, this.password); 86 87 Logger.logInfo("Setting up all tables...."); 88 89 try 90 { 91 FileInputStream fis = new FileInputStream("oracle_infoglue_core_schema.sql"); 92 StringBuffer sb = new StringBuffer (); 93 int c; 94 while((c = fis.read()) != -1) 95 { 96 char character = (char)c; 97 sb.append(character); 98 } 99 String script = sb.toString(); 100 Logger.logInfo("script:" + script); 101 102 StringTokenizer st = new StringTokenizer(script, ";"); 103 while (st.hasMoreTokens()) 104 { 105 String command = st.nextToken(); 106 issueCommand(conn, command); 108 } 109 110 } 111 catch(Exception e) 112 { 113 System.out.println("Error: " + e); 114 Logger.logInfo("Error: " + e); 115 } 116 } 117 118 private void createInitialData() throws Exception 119 { 120 String url = "jdbc:oracle:thin:@" + databaseHostName + ":" + this.databasePortNumber + ":" + databaseName + ""; 121 Connection conn = getConnection(url, this.userName, this.password); 122 123 Logger.logInfo("Setting up initial data...."); 124 125 try 126 { 127 FileInputStream fis = new FileInputStream("oracle_infoglue_initial_data.sql"); 128 StringBuffer sb = new StringBuffer (); 129 int c; 130 while((c = fis.read()) != -1) 131 { 132 char character = (char)c; 133 sb.append(character); 134 } 135 String script = sb.toString(); 136 138 String [] commands = script.split("--endquery"); 139 Logger.logInfo("Parsed " + commands.length + " commands from script"); 140 141 for(int i=0; i<commands.length; i++) 142 { 143 String command = commands[i]; 144 if(command.indexOf("SPECIAL") > -1) 145 issueSpecialCommand(conn, command.trim()); 146 if(command.indexOf("BLOB") > -1) 147 issueSpecialBlobCommand(conn, command.trim()); 148 else 149 { 150 command = command.trim(); 151 if(!command.equalsIgnoreCase("")) 152 { 153 command = command.substring(0, command.length()-1); 154 issueCommand(conn, command); 155 } 156 } 157 } 158 159 } 160 catch(Exception e) 161 { 162 Logger.logInfo("Error: " + e); 163 } 164 } 165 166 167 171 172 protected void issueSpecialBlobCommand(Connection conn, String originalSql) 173 { 174 String sql = originalSql; 175 177 try 178 { 179 String valuesPart = sql.substring(sql.indexOf("VALUES") + 6).trim(); 180 sql = sql.substring(0, sql.indexOf("VALUES") + 6); 181 184 String tableName = null; 185 int blobColumn = 0; 186 List columns = null; 187 List columnValues = null; 188 189 StringTokenizer st = new StringTokenizer(sql, " "); 190 int i = 0; 191 while (st.hasMoreTokens()) 192 { 193 String part = st.nextToken(); 194 196 if(i == 1) 197 blobColumn = new Integer (part).intValue(); 198 if(i == 4) 199 tableName = part; 200 if(i == 5) 201 { 202 columns = parseColumns(part); 203 } 204 205 i++; 206 } 207 208 columnValues = parseValues(valuesPart); 209 210 String columnsString = ""; 211 String valuesString = ""; 212 Iterator columnsIterator = columns.iterator(); 213 while(columnsIterator.hasNext()) 214 { 215 columnsString += (columnsString.equals("")) ? (String )columnsIterator.next() : "," + columnsIterator.next(); 216 valuesString += (valuesString.equals("")) ? "?" : ",?"; 217 } 218 219 sql = "INSERT INTO " + tableName + "(" + columnsString + ") VALUES (" + valuesString + ")"; 220 221 222 PreparedStatement ps = conn.prepareStatement(sql); 223 224 int index = 1; 225 int loopCount = 0; 226 Iterator columnValuesIterator = columnsIterator = columns.iterator(); 227 while(columnsIterator.hasNext()) 228 { 229 columnsIterator.next(); 230 String value = (String )columnValues.get(loopCount); 231 232 if(index == 1 || value.indexOf("'") == -1) 233 { 234 ps.setInt(index, new Integer (value).intValue()); 235 } 236 else if(index == blobColumn) 237 { 238 value = value.substring(1, value.length() - 1); 240 242 if(value.indexOf("assetBlob:") > -1) 243 { 244 String fileName = value.substring(10); 245 FileInputStream fis = new FileInputStream(fileName); 246 247 BLOB bl = BLOB.createTemporary(conn, true, BLOB.DURATION_CALL); 248 bl.open(BLOB.MODE_READWRITE); 249 250 BufferedOutputStream out = new BufferedOutputStream(bl.getBinaryOutputStream()); 251 252 byte[] buffer = new byte[1024]; 253 int len; 254 255 while((len = fis.read(buffer)) >= 0) 256 out.write(buffer, 0, len); 257 258 out.flush(); 259 fis.close(); 260 out.close(); 261 262 ps.setBlob(index, bl); 263 } 264 else 265 { 266 CLOB cl = CLOB.createTemporary(conn, true, CLOB.DURATION_CALL); 267 cl.putString(1,value); 268 ps.setClob(index, cl); 269 } 270 } 271 else if(value.indexOf("date:") > -1) 272 { 273 value = value.substring(6); 274 Date date = parseDate(value, "yyyy-MM-dd HH:mm:ss"); 275 276 ps.setDate(index, new java.sql.Date (date.getTime())); 277 } 278 else 279 { 280 value = value.substring(1, value.length() - 1); 282 ps.setString(index, value); 284 } 285 286 index++; 287 loopCount++; 288 } 289 290 ps.executeUpdate(); 291 } 292 catch(Exception ex) 293 { 294 Logger.logInfo("Command failed: " + ex.getMessage()); 295 Logger.logInfo("SQL: " + originalSql); 296 ex.printStackTrace(); 297 System.err.println("SQLException: " + ex.getMessage()); 298 } 299 } 300 301 public void upgradeTo1_3(Connection conn) throws Exception 302 { 303 } 304 305 public void upgradeTo1_3_2(Connection conn) throws Exception 306 { 307 Logger.logInfo("Upgrading to 1.3.2"); 308 309 try 310 { 311 FileInputStream fis = new FileInputStream("oracle_upgrade1_3_to1_3_2.sql"); 312 StringBuffer sb = new StringBuffer (); 313 int c; 314 while((c = fis.read()) != -1) 315 { 316 char character = (char)c; 317 sb.append(character); 318 } 319 String script = sb.toString(); 320 Logger.logInfo("script:" + script); 321 322 StringTokenizer st = new StringTokenizer(script, ";"); 323 while (st.hasMoreTokens()) 324 { 325 String command = st.nextToken(); 326 issueCommand(conn, command + ";"); 328 } 329 } 330 catch(Exception e) 331 { 332 Logger.logInfo("Error: " + e); 333 } 334 } 335 336 public void upgradeTo2_0(Connection conn) throws Exception 337 { 338 Logger.logInfo("Upgrading to 2.0"); 339 340 try 341 { 342 FileInputStream fis = new FileInputStream("oracle_upgrade1_3_2_to2_0.sql"); 343 StringBuffer sb = new StringBuffer (); 344 int c; 345 while((c = fis.read()) != -1) 346 { 347 char character = (char)c; 348 sb.append(character); 349 } 350 String script = sb.toString(); 351 Logger.logInfo("script:" + script); 352 353 StringTokenizer st = new StringTokenizer(script, ";"); 354 while (st.hasMoreTokens()) 355 { 356 String command = st.nextToken(); 357 issueCommand(conn, command + ";"); 359 } 360 } 361 catch(Exception e) 362 { 363 Logger.logInfo("Error: " + e); 364 } 365 } 366 367 public void upgradeTo2_1(Connection conn) throws Exception 368 { 369 Logger.logInfo("Upgrading to 2.1"); 370 371 try 372 { 373 FileInputStream fis = new FileInputStream("oracle_upgrade2_0_to2_1.sql"); 374 StringBuffer sb = new StringBuffer (); 375 int c; 376 while((c = fis.read()) != -1) 377 { 378 char character = (char)c; 379 sb.append(character); 380 } 381 String script = sb.toString(); 382 Logger.logInfo("script:" + script); 383 384 StringTokenizer st = new StringTokenizer(script, ";"); 385 while (st.hasMoreTokens()) 386 { 387 String command = st.nextToken(); 388 issueCommand(conn, command + ";"); 390 } 391 } 392 catch(Exception e) 393 { 394 Logger.logInfo("Error: " + e); 395 } 396 } 397 398 public void upgradeTo2_3(Connection conn) throws Exception 399 { 400 Logger.logInfo("Upgrading to 2.3"); 401 402 try 403 { 404 FileInputStream fis = new FileInputStream("oracle_upgrade2_1_to2_3.sql"); 405 StringBuffer sb = new StringBuffer (); 406 int c; 407 while((c = fis.read()) != -1) 408 { 409 char character = (char)c; 410 sb.append(character); 411 } 412 String script = sb.toString(); 413 Logger.logInfo("script:" + script); 414 415 StringTokenizer st = new StringTokenizer(script, ";"); 416 while (st.hasMoreTokens()) 417 { 418 String command = st.nextToken(); 419 issueCommand(conn, command + ";"); 421 } 422 } 423 catch(Exception e) 424 { 425 Logger.logInfo("Error: " + e); 426 } 427 } 428 429 private void createUsers() throws Exception 430 { 431 Logger.logInfo("Setting up all users...."); 432 433 String url = "jdbc:oracle:thin:@" + databaseHostName + ""; 434 Connection conn = getConnection(url, this.userName, this.password); 435 443 } 444 445 446 private void dropDatabase() throws Exception 447 { 448 Logger.logInfo("Deleting the database...."); 449 issueCommand(getConnection(), "DROP DATABASE " + databaseName + ";"); 450 } 451 452 453 private void dropUsers() throws Exception 454 { 455 Logger.logInfo("Dropping all users...."); 456 457 String url = "jdbc:mysql://" + databaseHostName + "/mysql"; 458 Connection conn = getConnection(url, this.userName, this.password); 459 460 issueCommand(conn, "DELETE FROM mysql.columns_priv WHERE User = '" + this.databaseSuffix + "'"); 461 issueCommand(conn, "DELETE FROM mysql.tables_priv WHERE User = '" + this.databaseSuffix + "'"); 462 issueCommand(conn, "DELETE FROM mysql.db WHERE User = '" + this.infoglueDatabaseUserName + "'"); 463 issueCommand(conn, "DELETE FROM mysql.user WHERE User = '" + this.infoglueDatabaseUserName + "'"); 464 } 465 466 469 470 private void issueCommand(Connection conn, String sql) 471 { 472 if(sql == null || sql.trim().length() == 0) 473 return; 474 475 try 476 { 477 System.out.println("issueCommand: " + sql); 479 480 PreparedStatement pstmt = conn.prepareStatement(sql); 481 pstmt.execute(); 482 pstmt.close(); 483 485 } 489 catch(SQLException ex) 490 { 491 Logger.logInfo("Command failed: " + ex.getMessage()); 492 Logger.logInfo("SQL: " + sql); 493 System.err.println("SQLException: " + ex.getMessage()); 494 } 495 } 496 497 498 public void createCastorFile() throws Exception 499 { 500 StringBuffer xmlFile = new StringBuffer (); 501 502 xmlFile.append("<!DOCTYPE database PUBLIC \"-//EXOLAB/Castor JDO Configuration DTD Version 1.0//EN\" \"http://castor.codehaus.org/jdo-conf.dtd\">\n"); 503 xmlFile.append("<jdo-conf>\n"); 504 xmlFile.append("<database name=\"INFOGLUE_CMS\" engine=\"oracle\">\n"); 505 xmlFile.append("<data-source class-name=\"org.apache.commons.dbcp.BasicDataSource\">\n"); 506 xmlFile.append("<param name=\"driver-class-name\" value=\"oracle.jdbc.driver.OracleDriver\"/>\n"); 507 xmlFile.append("<param name=\"username\" value=\"" + this.infoglueDatabaseUserName + "\"/>\n"); 508 xmlFile.append("<param name=\"password\" value=\"" + this.infoglueDatabasePassword + "\"/>\n"); 509 xmlFile.append("<param name=\"url\" value=\"jdbc:oracle:thin:@" + databaseHostName + ":" + this.databasePortNumber + ":" + databaseName + "\"/>\n"); 510 xmlFile.append("<param name=\"max-active\" value=\"20\"/>\n"); 511 xmlFile.append("<param name=\"connection-properties\" value=\"SetBigStringTryClob=true\"/>\n"); 512 xmlFile.append("</data-source>\n"); 513 xmlFile.append("<mapping HREF=\"classes/oracle_mapping.xml\"/>\n"); 514 xmlFile.append("</database>\n"); 515 xmlFile.append("<transaction-demarcation mode=\"local\" />\n"); 516 xmlFile.append("</jdo-conf>\n"); 517 518 532 533 PrintWriter pw = new PrintWriter(new FileWriter("localConfigs/database.xml")); 534 pw.println(xmlFile.toString()); 535 pw.close(); 536 } 537 538 public void createCastorRootFile() throws Exception 539 { 540 StringBuffer xmlFile = new StringBuffer (); 541 542 xmlFile.append("<!DOCTYPE database PUBLIC \"-//EXOLAB/Castor JDO Configuration DTD Version 1.0//EN\" \"http://castor.codehaus.org/jdo-conf.dtd\">\n"); 543 xmlFile.append("<jdo-conf>\n"); 544 xmlFile.append("<database name=\"INFOGLUE_CMS\" engine=\"oracle\">\n"); 545 xmlFile.append("<data-source class-name=\"org.apache.commons.dbcp.BasicDataSource\">\n"); 546 xmlFile.append("<param name=\"driver-class-name\" value=\"oracle.jdbc.driver.OracleDriver\"/>\n"); 547 xmlFile.append("<param name=\"username\" value=\"" + this.userName + "\"/>\n"); 548 xmlFile.append("<param name=\"password\" value=\"" + this.password + "\"/>\n"); 549 xmlFile.append("<param name=\"url\" value=\"jdbc:oracle:thin:@" + databaseHostName + ":" + this.databasePortNumber + ":" + databaseName + "\"/>\n"); 550 xmlFile.append("<param name=\"max-active\" value=\"20\"/>\n"); 551 xmlFile.append("<param name=\"connection-properties\" value=\"\"/>\n"); 552 xmlFile.append("</data-source>\n"); 553 xmlFile.append("<mapping HREF=\"classes/oracle_mapping.xml\"/>\n"); 554 xmlFile.append("</database>\n"); 555 xmlFile.append("<transaction-demarcation mode=\"local\" />\n"); 556 xmlFile.append("</jdo-conf>\n"); 557 558 572 573 PrintWriter pw = new PrintWriter(new FileWriter("localConfigs/database.xml")); 574 pw.println(xmlFile.toString()); 575 pw.close(); 576 } 577 578 624 625 public void createOSPropertiesFile() throws Exception 626 { 627 StringBuffer xmlFile = new StringBuffer (); 628 629 xmlFile.append("<propertysets>"); 630 xmlFile.append("<propertyset name=\"aggregate\" class=\"com.opensymphony.module.propertyset.aggregate.AggregatePropertySet\"/>"); 631 xmlFile.append("<propertyset name=\"cached\" class=\"com.opensymphony.module.propertyset.cached.CachingPropertySet\"/>"); 632 xmlFile.append("<propertyset name=\"jdbc\" class=\"org.infoglue.cms.util.workflow.InfoGlueJDBCPropertySet\">"); 633 xmlFile.append("<arg name=\"username\" value=\"" + this.infoglueDatabaseUserName + "\"/>"); 634 xmlFile.append("<arg name=\"password\" value=\"" + this.infoglueDatabasePassword + "\"/>"); 635 xmlFile.append("<arg name=\"driverClassName\" value=\"oracle.jdbc.driver.OracleDriver\"/>"); 636 xmlFile.append("<arg name=\"url\" value=\"jdbc:oracle:thin:@" + databaseHostName + ":" + this.databasePortNumber + ":" + databaseName + "\"/>"); 637 638 xmlFile.append("<arg name=\"table.name\" value=\"OS_PROPERTYENTRY\"/>"); 639 649 xmlFile.append("<arg name=\"col.globalKey\" value=\"entity_name\"/>"); 650 xmlFile.append("<arg name=\"col.entityId\" value=\"entity_id\"/>"); 651 xmlFile.append("<arg name=\"col.itemKey\" value=\"entity_key\"/>"); 652 xmlFile.append("<arg name=\"col.itemType\" value=\"key_type\"/>"); 653 xmlFile.append("<arg name=\"col.booleanVal\" value=\"boolean_val\"/>"); 654 xmlFile.append("<arg name=\"col.string\" value=\"string_val\"/>"); 655 xmlFile.append("<arg name=\"col.date\" value=\"date_val\"/>"); 656 xmlFile.append("<arg name=\"col.data\" value=\"data_val\"/>"); 657 xmlFile.append("<arg name=\"col.float\" value=\"double_val\"/>"); 658 xmlFile.append("<arg name=\"col.number\" value=\"int_val\"/>"); 659 660 xmlFile.append("</propertyset>"); 661 xmlFile.append("<propertyset name=\"ejb\" class=\"com.opensymphony.module.propertyset.ejb.EJBPropertySet\"/>"); 662 xmlFile.append("<propertyset name=\"javabeans\" class=\"com.opensymphony.module.propertyset.javabeans.BeanIntrospectorPropertySet\"/>"); 663 xmlFile.append("<propertyset name=\"map\" class=\"com.opensymphony.module.propertyset.map.MapPropertySet\"/>"); 664 xmlFile.append("<propertyset name=\"memory\" class=\"com.opensymphony.module.propertyset.memory.MemoryPropertySet\"/>"); 665 xmlFile.append("<propertyset name=\"serializable\" class=\"com.opensymphony.module.propertyset.memory.SerializablePropertySet\"/>"); 666 xmlFile.append("<propertyset name=\"ofbiz\" class=\"com.opensymphony.module.propertyset.ofbiz.OFBizPropertySet\"/>"); 667 xmlFile.append("<propertyset name=\"hibernate\" class=\"org.infoglue.cms.util.workflow.hibernate.InfoGlueHibernatePropertySet\"/>"); 668 xmlFile.append("<propertyset name=\"xml\" class=\"com.opensymphony.module.propertyset.xml.XMLPropertySet\"/>"); 669 xmlFile.append("</propertysets>"); 670 671 PrintWriter pw = new PrintWriter(new FileWriter("localConfigs/propertyset.xml")); 672 pw.println(xmlFile.toString()); 673 pw.close(); 674 } 675 676 public void testSetupDummyDatabase() throws Exception 677 { 678 PreparedStatement pstmt = getConnection().prepareStatement("CREATE TABLE SYSTEM.DUMMYTABLE(\"ID\" NUMBER(10) NOT NULL)"); 679 pstmt.execute(); 680 pstmt.close(); 681 682 pstmt = getConnection().prepareStatement("DROP TABLE SYSTEM.DUMMYTABLE"); 683 pstmt.execute(); 684 pstmt.close(); 685 } 686 687 688 public void testConnectDatabase() throws Exception 689 { 690 System.out.println(this.infoglueDatabaseUserName + ":" + this.infoglueDatabasePassword); 691 getConnection(getUrl(this.databaseHostName, this.databasePortNumber, this.databaseName + this.databaseSuffix), this.infoglueDatabaseUserName, this.infoglueDatabasePassword); 692 } 694 695 public String getDriver() 696 { 697 return driver; 698 } 699 700 public String getDatabaseVendor() throws Exception 701 { 702 return "Oracle"; 703 } 704 705 } | Popular Tags |