1 31 32 package org.opencms.setup; 33 34 import org.opencms.main.CmsException; 35 import org.opencms.util.CmsStringUtil; 36 37 import java.io.File ; 38 import java.io.FileNotFoundException ; 39 import java.io.FileReader ; 40 import java.io.InputStreamReader ; 41 import java.io.LineNumberReader ; 42 import java.io.Reader ; 43 import java.io.StringReader ; 44 import java.sql.Connection ; 45 import java.sql.DriverManager ; 46 import java.sql.ResultSet ; 47 import java.sql.SQLException ; 48 import java.sql.Statement ; 49 import java.util.Iterator ; 50 import java.util.Map ; 51 import java.util.StringTokenizer ; 52 import java.util.Vector ; 53 54 64 public class CmsSetupDb extends Object { 65 66 67 public static final String SETUP_DATA_FOLDER = "WEB-INF/setupdata/"; 68 69 70 public static final String SETUP_FOLDER = "setup/"; 71 72 private String m_basePath; 73 private Connection m_con; 74 private boolean m_errorLogging; 75 private Vector m_errors; 76 77 82 public CmsSetupDb(String basePath) { 83 84 m_errors = new Vector (); 85 m_basePath = basePath; 86 m_errorLogging = true; 87 } 88 89 92 public void clearErrors() { 93 94 m_errors.clear(); 95 } 96 97 100 public void closeConnection() { 101 102 try { 103 m_con.close(); 104 } catch (Exception e) { 105 } 107 } 108 109 115 public void createDatabase(String database, Map replacer) { 116 117 m_errorLogging = true; 118 executeSql(database, "create_db.sql", replacer, true); 119 } 120 121 128 public void createDatabase(String database, Map replacer, boolean abortOnError) { 129 130 m_errorLogging = true; 131 executeSql(database, "create_db.sql", replacer, abortOnError); 132 } 133 134 140 public void createTables(String database, Map replacer) { 141 142 m_errorLogging = true; 143 executeSql(database, "create_tables.sql", replacer, true); 144 } 145 146 153 public void createTables(String database, Map replacer, boolean abortOnError) { 154 155 m_errorLogging = true; 156 executeSql(database, "create_tables.sql", replacer, abortOnError); 157 } 158 159 165 public void dropDatabase(String database, Map replacer) { 166 167 m_errorLogging = true; 168 executeSql(database, "drop_db.sql", replacer, false); 169 } 170 171 178 public void dropDatabase(String database, Map replacer, boolean abortOnError) { 179 180 m_errorLogging = true; 181 executeSql(database, "drop_db.sql", replacer, abortOnError); 182 } 183 184 189 public void dropTables(String database) { 190 191 m_errorLogging = true; 192 executeSql(database, "drop_tables.sql", null, false); 193 } 194 195 201 public void dropTables(String database, Map replacer) { 202 203 m_errorLogging = true; 204 executeSql(database, "drop_tables.sql", replacer, false); 205 } 206 207 214 public void dropTables(String database, Map replacer, boolean abortOnError) { 215 216 m_errorLogging = true; 217 executeSql(database, "drop_tables.sql", replacer, abortOnError); 218 } 219 220 225 public Vector getErrors() { 226 227 return m_errors; 228 } 229 230 235 public boolean noErrors() { 236 237 return m_errors.isEmpty(); 238 } 239 240 245 public void setConnection(Connection conn) { 246 247 m_con = conn; 248 } 249 250 259 public void setConnection(String DbDriver, String DbConStr, String DbConStrParams, String DbUser, String DbPwd) { 260 261 String jdbcUrl = DbConStr; 262 try { 263 if (DbConStrParams != null) { 264 jdbcUrl += DbConStrParams; 265 } 266 Class.forName(DbDriver).newInstance(); 267 m_con = DriverManager.getConnection(jdbcUrl, DbUser, DbPwd); 268 } catch (ClassNotFoundException e) { 269 m_errors.addElement(Messages.get().getBundle().key(Messages.ERR_LOAD_JDBC_DRIVER_1, DbDriver)); 270 m_errors.addElement(CmsException.getStackTraceAsString(e)); 271 } catch (Exception e) { 272 m_errors.addElement(Messages.get().getBundle().key(Messages.ERR_DB_CONNECT_1, DbConStr)); 273 m_errors.addElement(CmsException.getStackTraceAsString(e)); 274 } 275 } 276 277 284 public String checkVariables(String db) { 285 286 StringBuffer html = new StringBuffer (512); 287 if (m_con == null) { 288 return null; } 290 SQLException exception = null; 291 if (db.equals("mysql")) { String statement = "SELECT @@max_allowed_packet;"; 293 Statement stmt = null; 294 ResultSet rs = null; 295 long map = 0; 296 try { 297 stmt = m_con.createStatement(); 298 rs = stmt.executeQuery(statement); 299 if (rs.next()) { 300 map = rs.getLong(1); 301 } 302 } catch (SQLException e) { 303 exception = e; 304 } finally { 305 if (stmt != null) { 306 try { 307 stmt.close(); 308 } catch (SQLException e) { 309 } 311 } 312 } 313 if (exception == null) { 314 if (map > 0) { 315 html.append("MySQL system variable <code>'max_allowed_packet'</code> is set to "); 316 html.append(map); 317 html.append(" Bytes.<p>\n"); 318 } 319 html.append("Please, note that it will not be possible for OpenCms to handle files bigger than this value.<p>\n"); 320 if (map < 15 * 1024 * 1024) { 321 m_errors.addElement("<b>Your <code>'max_allowed_packet'</code> variable is set to less than 16Mb (" 322 + map 323 + ").</b>\n" 324 + "The recommended value for running OpenCms is 16Mb." 325 + "Please change your MySQL configuration (in your <code>mi.ini</code> or <code>my.cnf</code> file).\n"); 326 } 327 } 328 } 329 if (exception != null || db.equals("mysql_3")) { 330 html.append("<i>OpenCms was not able to detect the value of your <code>'max_allowed_packet'</code> variable.</i><p>\n"); 331 html.append("Please, note that it will not be possible for OpenCms to handle files bigger than this value.<p>\n"); 332 html.append("<b>The recommended value for running OpenCms is 16Mb, please set it in your MySQL configuration (in your <code>mi.ini</code> or <code>my.cnf</code> file).</b>\n"); 333 if (exception != null) { 334 html.append(CmsException.getStackTraceAsString(exception)); 335 } 336 } 337 if (html.length() == 0) { 338 return null; 339 } 340 return html.toString(); 341 } 342 343 349 public void updateDatabase(String updateScript, Map replacers) { 350 351 StringReader reader = new StringReader (updateScript); 352 executeSql(reader, replacers, true); 353 } 354 355 362 public void updateDatabase(String updateScript, Map replacers, boolean abortOnError) { 363 364 StringReader reader = new StringReader (updateScript); 365 executeSql(reader, replacers, abortOnError); 366 } 367 368 371 protected void finalize() throws Throwable { 372 373 try { 374 closeConnection(); 375 } catch (Throwable t) { 376 } 378 super.finalize(); 379 } 380 381 388 private void executeSql(Reader inputReader, Map replacers, boolean abortOnError) { 389 390 String statement = ""; 391 LineNumberReader reader = null; 392 String line = null; 393 394 try { 396 reader = new LineNumberReader (inputReader); 397 line = null; 398 399 while (true) { 400 line = reader.readLine(); 401 if (line == null) { 402 break; 403 } 404 StringTokenizer st = new StringTokenizer (line); 405 406 while (st.hasMoreTokens()) { 407 String currentToken = st.nextToken(); 408 409 if (currentToken.startsWith("#")) { 411 break; 412 } 413 414 if (currentToken.startsWith("prompt")) { 416 break; 417 } 418 419 statement += " " + currentToken; 421 422 if (currentToken.endsWith(";")) { 424 statement = statement.substring(0, (statement.length() - 1)); 426 427 try { 429 if (replacers != null) { 430 statement = replaceTokens(statement, replacers); 431 executeStatement(statement); 432 } else { 433 executeStatement(statement); 434 } 435 } catch (SQLException e) { 436 if (!abortOnError) { 437 if (m_errorLogging) { 438 m_errors.addElement("Error executing SQL statement: " + statement); 439 m_errors.addElement(CmsException.getStackTraceAsString(e)); 440 } 441 } else { 442 throw e; 443 } 444 } 445 446 statement = ""; 448 } 449 } 450 451 statement += " \n"; 452 } 453 } catch (SQLException e) { 454 if (m_errorLogging) { 455 m_errors.addElement("Error executing SQL statement: " + statement); 456 m_errors.addElement(CmsException.getStackTraceAsString(e)); 457 } 458 } catch (Exception e) { 459 if (m_errorLogging) { 460 m_errors.addElement("Error parsing database setup SQL script in line: " + line); 461 m_errors.addElement(CmsException.getStackTraceAsString(e)); 462 } 463 } finally { 464 try { 465 if (reader != null) { 466 reader.close(); 467 } 468 } catch (Exception e) { 469 } 471 } 472 } 473 474 482 private void executeSql(String databaseKey, String sqlScript, Map replacers, boolean abortOnError) { 483 484 String filename = null; 485 InputStreamReader reader = null; 486 try { 487 filename = m_basePath 488 + "setup" 489 + File.separator 490 + "database" 491 + File.separator 492 + databaseKey 493 + File.separator 494 + sqlScript; 495 executeSql(new FileReader (filename), replacers, abortOnError); 496 } catch (FileNotFoundException e) { 497 if (m_errorLogging) { 498 m_errors.addElement("Database setup SQL script not found: " + filename); 499 m_errors.addElement(CmsException.getStackTraceAsString(e)); 500 } 501 } finally { 502 try { 503 if (reader != null) { 504 reader.close(); 505 } 506 } catch (Exception e) { 507 } 509 } 510 } 511 512 519 private void executeStatement(String statement) throws SQLException { 520 521 Statement stmt = null; 522 523 try { 524 stmt = m_con.createStatement(); 525 stmt.execute(statement); 526 } finally { 527 if (stmt != null) { 528 stmt.close(); 529 } 530 } 531 } 532 533 540 private String replaceTokens(String sql, Map replacers) { 541 542 Iterator keys = replacers.keySet().iterator(); 543 while (keys.hasNext()) { 544 545 String key = (String )keys.next(); 546 String value = (String )replacers.get(key); 547 548 sql = CmsStringUtil.substitute(sql, key, value); 549 } 550 551 return sql; 552 } 553 } | Popular Tags |