1 42 43 44 package org.jahia.admin.database; 45 46 import java.io.File ; 47 import java.io.FileNotFoundException ; 48 import java.io.IOException ; 49 import java.sql.PreparedStatement ; 50 import java.sql.ResultSet ; 51 import java.sql.ResultSetMetaData ; 52 import java.sql.SQLException ; 53 import java.sql.Statement ; 54 import java.sql.Types ; 55 import java.util.Enumeration ; 56 import java.util.HashMap ; 57 import java.util.Iterator ; 58 import java.util.TreeSet ; 59 import java.util.Vector ; 60 61 import javax.servlet.ServletException ; 62 import javax.servlet.http.HttpServletRequest ; 63 import javax.servlet.http.HttpServletResponse ; 64 import javax.servlet.http.HttpSession ; 65 66 import org.jahia.bin.Jahia; 67 import org.jahia.bin.JahiaAdministration; 68 import org.jahia.data.JahiaData; 69 import org.jahia.exceptions.JahiaException; 70 import org.jahia.params.ParamBean; 71 import org.jahia.resourcebundle.JahiaResourceBundle; 72 import org.jahia.services.usermanager.JahiaUser; 73 import org.jahia.utils.JahiaTools; 74 import org.jahia.utils.properties.PropertiesManager; 75 import org.jahia.utils.PathResolver; 76 77 89 public class ManageDatabase { 90 91 92 private static org.apache.log4j.Logger logger = 93 org.apache.log4j.Logger.getLogger (ManageDatabase.class); 94 95 private static final String CLASS_NAME = JahiaAdministration.CLASS_NAME; 96 private static final String JSP_PATH = JahiaAdministration.JSP_PATH; 97 98 private static DatabaseConnection dbConnect = new DatabaseConnection (); 99 private static DatabaseScripts scriptsManager; 100 private static PropertiesManager properties; 101 private static PathResolver pathResolver; 102 103 private static byte[] mLock = new byte[1]; 104 105 113 public ManageDatabase (HttpServletRequest request, 114 HttpServletResponse response, 115 HttpSession session, 116 PathResolver pathResolver) 117 throws Throwable { 118 this.pathResolver = pathResolver; 119 properties = new PropertiesManager (Jahia.getJahiaPropertiesFileName ()); 120 scriptsManager = new DatabaseScripts (); 121 userRequestDispatcher (request, response, session); 122 } 124 125 132 private void userRequestDispatcher (HttpServletRequest request, 133 HttpServletResponse response, 134 HttpSession session) 135 throws Throwable { 136 String operation = request.getParameter ("sub"); 137 138 if (operation.equals ("display")) { 139 displayChoice (request, response, session); 140 } else if (operation.equals ("choice")) { 141 processChoice (request, response, session); 142 } else if (operation.equals ("backup")) { 143 processBackup (request, response, session); 144 } else if (operation.equals ("restore")) { 145 processRestore (request, response, session); 146 } else if (operation.equals ("flush")) { 147 processFlush (request, response, session); 148 } else if (operation.equals ("transfer")) { 149 processSettings (request, response, session, true); 150 } else if (operation.equals ("change")) { 151 processSettings (request, response, session, false); 152 } 153 } 155 156 166 private void displayChoice (HttpServletRequest request, 167 HttpServletResponse response, 168 HttpSession session) 169 throws IOException , ServletException { 170 JahiaAdministration.doRedirect (request, response, session, JSP_PATH + "config_database_which.jsp"); 171 } 173 174 182 private void displayBackup (HttpServletRequest request, 183 HttpServletResponse response, 184 HttpSession session) 185 throws IOException , ServletException { 186 String jahiaBackupName = (String )session.getAttribute (CLASS_NAME + "jahiaBackupName"); 188 String jahiaBackupDesc = (String )session.getAttribute (CLASS_NAME + "jahiaBackupDesc"); 189 String jahiaBackupType = (String )session.getAttribute (CLASS_NAME + "jahiaBackupType"); 190 191 if (jahiaBackupName == null) { 193 jahiaBackupName = ""; 194 } 195 if (jahiaBackupDesc == null) { 196 jahiaBackupDesc = ""; 197 } 198 if (jahiaBackupType == null) { 199 jahiaBackupType = "manual"; 200 } 201 202 request.setAttribute ("jahiaBackupName", jahiaBackupName); 204 request.setAttribute ("jahiaBackupDesc", jahiaBackupDesc); 205 request.setAttribute ("jahiaBackupType", jahiaBackupType); 206 207 JahiaAdministration.doRedirect (request, response, session, JSP_PATH + "backup_database.jsp"); 208 } 210 211 219 private void displayRestore (HttpServletRequest request, 220 HttpServletResponse response, 221 HttpSession session) 222 throws IOException , ServletException { 223 int backupCount = 0; 224 225 StringBuffer constructBackupPath = new StringBuffer (); 227 constructBackupPath.append (properties.getProperty ("jahiaVarDiskPath").trim ()); 228 constructBackupPath.append (File.separator); 229 constructBackupPath.append ("backup"); 230 String backupJahiaFilesPath = JahiaTools.convertContexted (constructBackupPath.toString (), pathResolver); 231 232 TreeSet backupListTree = new TreeSet (); 234 File backupJahiaFilesObject = new File (backupJahiaFilesPath); 235 File [] filesInThisDirectory = backupJahiaFilesObject.listFiles (); 236 try { 237 for (int i = 0; i < filesInThisDirectory.length; i++) { 238 if (filesInThisDirectory[i].isFile ()) { 239 backupListTree.add (filesInThisDirectory[i].getPath ()); 240 } 241 } 242 } catch (NullPointerException npe) { 243 } 244 245 Iterator iteration = backupListTree.iterator (); 247 Vector backupListVector = new Vector (); 248 while (iteration.hasNext ()) { 249 String pathBackupFile = (String )iteration.next (); 250 HashMap backupHash = new HashMap (); 251 PropertiesManager backupProperties = new PropertiesManager (pathBackupFile); 252 253 String epochTime = backupProperties.getProperty ("epoch"); 254 backupHash.put ("backup.epoch", epochTime); 255 backupHash.put ("backup.date", JahiaTools.formatDateFromEpoch (epochTime)); 256 backupHash.put ("backup.name", backupProperties.getProperty ("name")); 257 backupHash.put ("backup.desc", backupProperties.getProperty ("desc")); 258 backupHash.put ("backup.type", backupProperties.getProperty ("type")); 259 if (backupProperties.getProperty ("build") == null) { 260 backupHash.put ("backup.build", "0"); 261 } else { 262 backupHash.put ("backup.build", backupProperties.getProperty ("build")); 263 } 264 if (backupProperties.getProperty ("release") == null) { 265 backupHash.put ("backup.release", "0.0"); 266 } else { 267 backupHash.put ("backup.release", backupProperties.getProperty ("release")); 268 } 269 270 backupListVector.add (backupHash); 271 backupCount++; 272 } 273 backupListVector = JahiaTools.inverseVector (backupListVector); 274 275 request.setAttribute ("jahiaBackupList", backupListVector.elements ()); 277 request.setAttribute ("jahiaBackupCount", new Integer (backupCount)); 278 279 JahiaAdministration.doRedirect (request, response, session, JSP_PATH + "restore_database.jsp"); 280 } 282 283 294 private void displaySettings (HttpServletRequest request, 295 HttpServletResponse response, 296 HttpSession session) 297 throws IOException , ServletException { 298 Integer jahiaDBWhichAction = (Integer )session.getAttribute (CLASS_NAME + "jahiaDBWhichAction"); 300 301 Vector scriptsListInfos = scriptsManager.getDatabaseScriptsInfos (scriptsManager.getDatabaseScriptsFileObjects (), pathResolver); 303 304 String jahiaDBScript = (String )session.getAttribute (CLASS_NAME + "jahiaDBScript"); 306 String jahiaDBDriver = (String )session.getAttribute (CLASS_NAME + "jahiaDBDriver"); 307 String jahiaDBUrl = (String )session.getAttribute (CLASS_NAME + "jahiaDBUrl"); 308 String jahiaDBUsername = (String )session.getAttribute (CLASS_NAME + "jahiaDBUsername"); 309 String jahiaDBPassword = (String )session.getAttribute (CLASS_NAME + "jahiaDBPassword"); 310 String jahiaDBMinConnections = (String )session.getAttribute (CLASS_NAME + "jahiaDBMinConnections"); 311 String jahiaDBMaxConnections = (String )session.getAttribute (CLASS_NAME + "jahiaDBMaxConnections"); 312 String jahiaDBWaitIfBusy = (String )session.getAttribute (CLASS_NAME + "jahiaDBWaitIfBusy"); 313 String jahiaDBVerbose = (String )session.getAttribute (CLASS_NAME + "jahiaDBVerbose"); 314 315 if (jahiaDBScript == null) { 317 jahiaDBScript = properties.getProperty ("db_script"); 318 } 319 if (jahiaDBDriver == null) { 320 jahiaDBDriver = properties.getProperty ("db_driver"); 321 } 322 if (jahiaDBUrl == null) { 323 jahiaDBUrl = properties.getProperty ("db_url"); 324 } 325 if (jahiaDBUsername == null) { 326 jahiaDBUsername = properties.getProperty ("db_username"); 327 } 328 if (jahiaDBPassword == null) { 329 jahiaDBPassword = properties.getProperty ("db_password"); 330 } 331 if (jahiaDBMinConnections == null) { 332 jahiaDBMinConnections = properties.getProperty ("db_min_connections"); 333 } 334 if (jahiaDBMaxConnections == null) { 335 jahiaDBMaxConnections = properties.getProperty ("db_max_connections"); 336 } 337 if (jahiaDBWaitIfBusy == null) { 338 jahiaDBWaitIfBusy = properties.getProperty ("db_waitIfBusy"); 339 } 340 if (jahiaDBVerbose == null) { 341 jahiaDBVerbose = properties.getProperty ("db_verbose"); 342 } 343 344 request.setAttribute ("jahiaDBWhichAction", jahiaDBWhichAction); 346 request.setAttribute ("jahiaScriptsInfos", scriptsListInfos.elements ()); 347 request.setAttribute ("jahiaScriptsJavaScript", scriptsListInfos.elements ()); 348 request.setAttribute ("jahiaDBScript", jahiaDBScript); 349 request.setAttribute ("jahiaDBDriver", jahiaDBDriver); 350 request.setAttribute ("jahiaDBUrl", jahiaDBUrl); 351 request.setAttribute ("jahiaDBUsername", jahiaDBUsername); 352 request.setAttribute ("jahiaDBPassword", jahiaDBPassword); 353 request.setAttribute ("jahiaDBMinConnections", jahiaDBMinConnections); 354 request.setAttribute ("jahiaDBMaxConnections", jahiaDBMaxConnections); 355 request.setAttribute ("jahiaDBWaitIfBusy", jahiaDBWaitIfBusy); 356 request.setAttribute ("jahiaDBVerbose", jahiaDBVerbose); 357 358 JahiaAdministration.doRedirect (request, response, session, JSP_PATH + "config_database.jsp"); 359 } 361 362 372 private void processChoice (HttpServletRequest request, 373 HttpServletResponse response, 374 HttpSession session) 375 throws IOException , ServletException { 376 Integer action = new Integer (request.getParameter ("which_action").trim ()); 378 379 session.setAttribute (CLASS_NAME + "jahiaDBWhichAction", action); 381 382 if (action.intValue () == 3) { 384 displayBackup (request, response, session); 385 } else if (action.intValue () == 4) { 386 displayRestore (request, response, session); 387 } else { 388 displaySettings (request, response, session); 389 } 390 } 392 393 401 private void processBackup (HttpServletRequest request, 402 HttpServletResponse response, 403 HttpSession session) 404 throws IOException , ServletException { 405 406 synchronized (mLock) { 407 408 JahiaUser user = (JahiaUser)session.getAttribute (ParamBean.SESSION_USER); 409 410 JahiaData jData = (JahiaData)request.getAttribute ("org.jahia.data.JahiaData"); 411 ParamBean jParams = null; 412 if (jData != null) { 413 jParams = jData.params (); 414 } 415 416 if (user == null || !user.isAdminMember (0)) { 417 String dspMsg = JahiaResourceBundle.getAdminResource ("org.jahia.admin.JahiaDisplayMessage.illegalAccessOperation.label", 418 jParams, jParams.getLocale ()); 419 session.setAttribute (CLASS_NAME + "jahiaDisplayMessage", dspMsg); 420 JahiaAdministration.doRedirect (request, response, session, JSP_PATH + "menu.jsp"); 421 return; 422 } 423 424 byte[] jahiaLock = null; 425 426 try { 427 jahiaLock = Jahia.getLock (user, session); 428 if (jahiaLock == null) { 429 String dspMsg = JahiaResourceBundle.getAdminResource ("org.jahia.admin.JahiaDisplayMessage.notExclusiveLock.label", 430 jParams, jParams.getLocale ()); 431 session.setAttribute (CLASS_NAME + "jahiaDisplayMessage", dspMsg); 432 JahiaAdministration.doRedirect (request, response, session, JSP_PATH + "menu.jsp"); 433 return; 434 } 435 } catch (JahiaException je) { 436 session.setAttribute (CLASS_NAME + "jahiaDisplayMessage", je.getMessage ()); 437 JahiaAdministration.doRedirect (request, response, session, JSP_PATH + "menu.jsp"); 438 return; 439 } 440 441 try { 442 443 444 boolean processError = true; 445 446 String jahiaBackupName = request.getParameter ("backupname").trim (); 448 String jahiaBackupDesc = request.getParameter ("backupdesc").trim (); 449 String jahiaBackupType = request.getParameter ("backuptype").trim (); 450 451 if (jahiaBackupName.length () == 0) { 453 String dspMsg = JahiaResourceBundle.getAdminResource ("org.jahia.admin.JahiaDisplayMessage.backupName_mustSet.label", 454 jParams, jParams.getLocale ()); 455 session.setAttribute (this.getClass().getName() + "jahiaDisplayMessage", dspMsg); 456 } else { 457 processError = false; 458 } 459 460 if (processError) { 461 session.setAttribute (this.getClass().getName() + "jahiaBackupName", jahiaBackupName); 462 displayBackup (request, response, session); 463 } else { 464 465 String sqlGrab = grabDatabaseData (properties.getProperty ("db_driver"), properties.getProperty ("db_url"), properties.getProperty ("db_username"), properties.getProperty ("db_password")); 467 468 471 String epochTime = new Long (JahiaTools.getCurrentDateInMs ()).toString (); 473 474 String originDataJahiaFilesPath = JahiaTools.convertContexted (properties.getProperty ("jahiaFilesBigTextDiskPath").trim (), pathResolver); 476 String originTemplatesJahiaFilesPath = JahiaTools.convertContexted (properties.getProperty ("jahiaFilesTemplatesDiskPath").trim (), pathResolver); 477 String originTemplatesJSPPath = JahiaTools.convertContexted ("$context/" + properties.getProperty ("jahiaTemplatesDiskPath").trim (), pathResolver); 478 String backupJahiaFilesPath = JahiaTools.convertContexted (properties.getProperty ("jahiaVarDiskPath").trim () + File.separator + "backup", pathResolver); 479 String thisBackupJahiaFilesPath = backupJahiaFilesPath + File.separator + epochTime; 480 String backupDataJahiaFilesPath = thisBackupJahiaFilesPath + File.separator + "data"; 481 String backupTemplatesJahiaFilesPath = thisBackupJahiaFilesPath + File.separator + "templates" + File.separator + "xml"; 482 String backupTemplatesJSPPath = thisBackupJahiaFilesPath + File.separator + "templates" + File.separator + "jsp"; 483 484 JahiaTools.copyFolderContent (originDataJahiaFilesPath, backupDataJahiaFilesPath); 486 JahiaTools.copyFolderContent (originTemplatesJahiaFilesPath, backupTemplatesJahiaFilesPath); 487 JahiaTools.copyFolderContent (originTemplatesJSPPath, backupTemplatesJSPPath); 488 489 StringBuffer nfoText = new StringBuffer (); 491 nfoText.append ("epoch=" + epochTime + "\n"); 492 nfoText.append ("name=" + JahiaTools.string2Property (jahiaBackupName) + "\n"); 493 nfoText.append ("desc=" + JahiaTools.string2Property (jahiaBackupDesc) + "\n"); 494 nfoText.append ("type=" + jahiaBackupType + "\n"); 495 nfoText.append ("build=" + Jahia.BUILD_NUMBER + "\n"); 496 nfoText.append ("release=" + Jahia.RELEASE_NUMBER + "\n"); 497 498 JahiaTools.writeStringInFile (thisBackupJahiaFilesPath + File.separator + "sqldata.backup", sqlGrab); 500 JahiaTools.writeStringInFile (backupJahiaFilesPath + File.separator + "backup_" + epochTime + ".info", nfoText.toString ()); 501 502 508 String dspMsg = JahiaResourceBundle.getAdminResource ("org.jahia.admin.JahiaDisplayMessage.dbBackupCompleted.label", 509 jParams, jParams.getLocale ()); 510 session.setAttribute (CLASS_NAME + "jahiaDisplayMessage", dspMsg); 511 displayRestore (request, response, session); 512 } 513 } catch (Throwable t) { 514 t.printStackTrace (); 515 String dspMsg = JahiaResourceBundle.getAdminResource ("org.jahia.admin.JahiaDisplayMessage.dbError.label", 516 jParams, jParams.getLocale ()); 517 session.setAttribute (CLASS_NAME + "jahiaDisplayMessage", dspMsg); 518 JahiaAdministration.doRedirect (request, response, session, JSP_PATH + "menu.jsp"); 519 return; 520 521 } finally { 522 Jahia.releaseLock (jahiaLock); 523 } 524 } 525 526 } 528 529 536 private void processRestore (HttpServletRequest request, 537 HttpServletResponse response, 538 HttpSession session) 539 throws IOException , ServletException { 540 541 synchronized (mLock) { 542 543 byte[] jahiaLock = null; 544 boolean freeLock = true; 545 546 JahiaUser user = (JahiaUser)session.getAttribute (ParamBean.SESSION_USER); 547 JahiaData jData = (JahiaData)request.getAttribute ("org.jahia.data.JahiaData"); 548 ParamBean jParams = null; 549 if (jData != null) { 550 jParams = jData.params (); 551 } 552 553 if (user == null || !user.isAdminMember (0)) { 554 String dspMsg = JahiaResourceBundle.getAdminResource ("org.jahia.admin.JahiaDisplayMessage.illegalAccessOperation.label", 555 jParams, jParams.getLocale ()); 556 session.setAttribute (CLASS_NAME + "jahiaDisplayMessage", dspMsg); 557 JahiaAdministration.doRedirect (request, response, session, JSP_PATH + "menu.jsp"); 558 return; 559 } 560 561 try { 562 jahiaLock = Jahia.getLock (user, session); 563 if (jahiaLock == null) { 564 String dspMsg = JahiaResourceBundle.getAdminResource ("org.jahia.admin.JahiaDisplayMessage.noJahiaLock.label", 565 jParams, jParams.getLocale ()); 566 session.setAttribute (CLASS_NAME + "jahiaDisplayMessage", dspMsg); 567 JahiaAdministration.doRedirect (request, response, session, JSP_PATH + "menu.jsp"); 568 return; 569 } 570 } catch (JahiaException je) { 571 session.setAttribute (CLASS_NAME + "jahiaDisplayMessage", je.getMessage ()); 572 JahiaAdministration.doRedirect (request, response, session, JSP_PATH + "menu.jsp"); 573 return; 574 } 575 576 try { 577 578 String jahiaBackupEpoch = ""; 580 if (request.getParameter ("epoch") != null) { 581 jahiaBackupEpoch = request.getParameter ("epoch").trim (); 582 } 583 584 String backupBuild = ""; 586 if (request.getParameter ("build") != null) { 587 backupBuild = request.getParameter ("build").trim (); 588 } 589 590 String backupRelease = ""; 591 if (request.getParameter ("release") != null) { 592 backupRelease = request.getParameter ("release").trim (); 593 } 594 595 if (backupBuild == null || backupBuild.equals ("")) { 596 backupBuild = "-1"; 597 } 598 if (backupRelease == null || backupRelease.equals ("")) { 599 backupRelease = "-1.0"; 600 } 601 602 603 String originDataJahiaFilesPath = JahiaTools.convertContexted (properties.getProperty ("jahiaFilesBigTextDiskPath").trim (), pathResolver); 605 String originTemplatesJahiaFilesPath = JahiaTools.convertContexted (properties.getProperty ("jahiaFilesTemplatesDiskPath").trim (), pathResolver); 606 String originTemplatesJSPPath = JahiaTools.convertContexted ("$context/" + properties.getProperty ("jahiaTemplatesDiskPath").trim (), pathResolver); 607 String backupJahiaFilesPath = JahiaTools.convertContexted (properties.getProperty ("jahiaVarDiskPath").trim () + File.separator + "backup", pathResolver); 608 String thisBackupJahiaFilesPath = backupJahiaFilesPath + File.separator + jahiaBackupEpoch; 609 String backupDataJahiaFilesPath = thisBackupJahiaFilesPath + File.separator + "data"; 610 String backupTemplatesJahiaFilesPath = thisBackupJahiaFilesPath + File.separator + "templates" + File.separator + "xml"; 611 String backupTemplatesJSPPath = thisBackupJahiaFilesPath + File.separator + "templates" + File.separator + "jsp"; 612 613 JahiaTools.deleteFile (new File (originDataJahiaFilesPath)); 615 JahiaTools.deleteFile (new File (originTemplatesJahiaFilesPath)); 616 JahiaTools.deleteFile (new File (originTemplatesJSPPath)); 617 JahiaTools.copyFolderContent (backupDataJahiaFilesPath, originDataJahiaFilesPath); 618 JahiaTools.copyFolderContent (backupTemplatesJahiaFilesPath, originTemplatesJahiaFilesPath); 619 JahiaTools.copyFolderContent (backupTemplatesJSPPath, originTemplatesJSPPath); 620 621 Enumeration allGrabbedData = JahiaTools.string2Enumeration (thisBackupJahiaFilesPath + File.separator + "sqldata.backup", true); 623 insertDatabaseData (allGrabbedData, properties.getProperty ("db_driver"), properties.getProperty ("db_url"), properties.getProperty ("db_username"), properties.getProperty ("db_password")); 624 625 626 String patchPath = JahiaTools.convertContexted (properties.getProperty ("jahiaVarDiskPath").trim () + File.separator + "patch", pathResolver); 628 File f = new File (patchPath); 629 File [] files = f.listFiles (); 630 for (int i = 0; i < files.length; i++) { 631 if (files[i].getName ().endsWith (".property")) { 632 logger.debug("Find patch file " + files[i].getName ()); 633 PropertiesManager patch = new PropertiesManager (files[i].getAbsolutePath ()); 634 try { 635 if (Integer.parseInt (patch.getProperty ("build").trim ()) > Integer.parseInt (backupBuild)) { 636 patch = null; 637 logger.debug ("Delete patch file " + files[i].getName ()); 638 files[i].delete (); 639 } 640 } catch (Throwable t) { 641 logger.warn (t); 642 } 643 } 644 } 645 646 647 freeLock = false; 650 651 String dspMsg = JahiaResourceBundle.getAdminResource ("org.jahia.admin.processMessage.dbRestoreCompleted.label", 653 jParams, jParams.getLocale ()); 654 request.setAttribute ("processMessage", dspMsg); 655 session.setAttribute (CLASS_NAME + "jahiaDisplayMessage", 656 JahiaResourceBundle.getAdminResource("org.jahia.admin.copyright", 657 jParams, jParams.getLocale())); 658 JahiaAdministration.doRedirect (request, response, session, JSP_PATH + "finish.jsp"); 659 660 661 } catch (Throwable t) { 662 t.printStackTrace (); 663 String dspMsg = JahiaResourceBundle.getAdminResource ("org.jahia.admin.JahiaDisplayMessage.dbError.label", 664 jParams, jParams.getLocale ()); 665 session.setAttribute (CLASS_NAME + "jahiaDisplayMessage", dspMsg); 666 JahiaAdministration.doRedirect (request, response, session, JSP_PATH + "menu.jsp"); 667 return; 668 669 } finally { 670 if (freeLock) { 671 Jahia.releaseLock (jahiaLock); 672 } 673 } 674 } 675 676 677 } 679 680 687 private void processFlush (HttpServletRequest request, 688 HttpServletResponse response, 689 HttpSession session) 690 throws IOException , ServletException 691 { 692 synchronized (mLock) { 693 String jahiaFlushEpoch = request.getParameter ("epoch").trim (); 695 696 String backupJahiaFilesPath = JahiaTools.convertContexted (properties.getProperty ("jahiaVarDiskPath").trim () + File.separator + "backup", pathResolver); 698 String thisBackupJahiaFilesPath = backupJahiaFilesPath + File.separator + jahiaFlushEpoch; 699 700 File f = new File (backupJahiaFilesPath + File.separator + "backup_" + jahiaFlushEpoch + ".info"); 702 f.delete (); 703 logger.debug ("Delete file " + f.getAbsolutePath ()); 704 JahiaTools.deleteFile (new File (backupJahiaFilesPath + File.separator + "backup_" + jahiaFlushEpoch + ".info")); 705 JahiaTools.deleteFile (new File (thisBackupJahiaFilesPath)); 706 707 JahiaData jData = (JahiaData)request.getAttribute ("org.jahia.data.JahiaData"); 709 ParamBean jParams = null; 710 if (jData != null) { 711 jParams = jData.params (); 712 } 713 String dspMsg = JahiaResourceBundle.getAdminResource ("org.jahia.admin.processMessage.selectedBackupFlushed.label", 714 jParams, jParams.getLocale ()); 715 request.setAttribute ("processMessage", dspMsg); 716 session.setAttribute (CLASS_NAME + "jahiaDisplayMessage", 717 JahiaResourceBundle.getAdminResource("org.jahia.admin.copyright", 718 jParams, jParams.getLocale())); 719 displayRestore (request, response, session); 720 } 721 } 722 723 724 736 private void processSettings (HttpServletRequest request, 737 HttpServletResponse response, 738 HttpSession session, 739 boolean transfer) 740 throws IOException , ServletException { 741 742 synchronized (mLock) { 743 744 boolean processError = true; 745 746 String runtimeTestSQL = ""; 747 int jahiaDBMinConnectionsInt = 0; 748 int jahiaDBMaxConnectionsInt = 0; 749 750 String jahiaDBScript = request.getParameter ("dbtype").trim (); 752 String jahiaDBDriver = request.getParameter ("dbdriver").trim (); 753 String jahiaDBUrl = request.getParameter ("dburl").trim (); 754 String jahiaDBUsername = request.getParameter ("dbusername").trim (); 755 String jahiaDBPassword = request.getParameter ("dbpassword").trim (); 756 String jahiaDBMinConnections = request.getParameter ("dbminconnect"); 757 String jahiaDBMaxConnections = request.getParameter ("dbmaxconnect"); 758 String jahiaDBWaitIfBusy = request.getParameter ("dbwait"); 759 String jahiaDBVerbose = request.getParameter ("dbverbose"); 760 761 if (!transfer) { 763 try { 764 jahiaDBMinConnectionsInt = Integer.parseInt (jahiaDBMinConnections.trim ()); 765 } catch (NumberFormatException nfe) { 766 jahiaDBMinConnectionsInt = 0; 767 } 768 try { 769 jahiaDBMaxConnectionsInt = Integer.parseInt (jahiaDBMaxConnections.trim ()); 770 } catch (NumberFormatException nfe) { 771 jahiaDBMaxConnectionsInt = 0; 772 } 773 } 774 775 JahiaData jData = (JahiaData)request.getAttribute ("org.jahia.data.JahiaData"); 777 ParamBean jParams = null; 778 if (jData != null) { 779 jParams = jData.params (); 780 } 781 if (jahiaDBDriver.length () == 0) { 782 String dspMsg = JahiaResourceBundle.getAdminResource ("org.jahia.admin.JahiaDisplayMessage.dbDriver_mustSet.label", 783 jParams, jParams.getLocale ()); 784 session.setAttribute (CLASS_NAME + "jahiaDisplayMessage", dspMsg); 785 } else if (jahiaDBUrl.length () == 0) { 786 String dspMsg = JahiaResourceBundle.getAdminResource ("org.jahia.admin.JahiaDisplayMessage.dbURL_mustSet.label", 787 jParams, jParams.getLocale ()); 788 session.setAttribute (CLASS_NAME + "jahiaDisplayMessage", dspMsg); 789 } else { 790 if (!transfer) { 791 if (jahiaDBMinConnections.length () == 0) { 792 String dspMsg = JahiaResourceBundle.getAdminResource ("org.jahia.admin.JahiaDisplayMessage.dbMinConn_mustSet.label", 793 jParams, jParams.getLocale ()); 794 session.setAttribute (CLASS_NAME + "jahiaDisplayMessage", dspMsg); 795 } else if (jahiaDBMinConnectionsInt < 1) { 796 String dspMsg = JahiaResourceBundle.getAdminResource ("org.jahia.admin.JahiaDisplayMessage.dbMinConn_higherZero.label", 797 jParams, jParams.getLocale ()); 798 session.setAttribute (CLASS_NAME + "jahiaDisplayMessage", dspMsg); 799 } else if (jahiaDBMaxConnections.length () == 0) { 800 String dspMsg = JahiaResourceBundle.getAdminResource ("org.jahia.admin.JahiaDisplayMessage.dbMaxConn_mustSet.label", 801 jParams, jParams.getLocale ()); 802 session.setAttribute (CLASS_NAME + "jahiaDisplayMessage", dspMsg); 803 } else if (jahiaDBMaxConnectionsInt < 1) { 804 String dspMsg = JahiaResourceBundle.getAdminResource ("org.jahia.admin.JahiaDisplayMessage.dbMaxConn_higherZero.label", 805 jParams, jParams.getLocale ()); 806 session.setAttribute (CLASS_NAME + "jahiaDisplayMessage", dspMsg); 807 } else if (jahiaDBMaxConnectionsInt <= jahiaDBMinConnectionsInt) { 808 String dspMsg = JahiaResourceBundle.getAdminResource ("org.jahia.admin.JahiaDisplayMessage.dbMaxConn_higherMin.label", 809 jParams, jParams.getLocale ()); 810 session.setAttribute (CLASS_NAME + "jahiaDisplayMessage", dspMsg); 811 } else { 812 processError = false; 813 } 814 } else { 815 processError = false; 816 } 817 } 818 819 StringBuffer constructDBScript = new StringBuffer (); 821 constructDBScript.append (Jahia.jahiaDatabaseScriptsPath); 822 constructDBScript.append (File.separator); 823 constructDBScript.append (jahiaDBScript); 824 825 try { 827 File scriptsFileObject = new File (constructDBScript.toString ()); 828 Enumeration scriptsListRuntime = scriptsManager.getDatabaseScriptsRuntime (scriptsFileObject); 829 runtimeTestSQL = (String )scriptsListRuntime.nextElement (); 830 } catch (FileNotFoundException fnfe) { 831 String dspMsg = JahiaResourceBundle.getAdminResource ("org.jahia.admin.JahiaDisplayMessage.dbNotOpen.label", 832 jParams, jParams.getLocale ()); 833 session.setAttribute (CLASS_NAME + "jahiaDisplayMessage", dspMsg); 834 processError = true; 835 } 836 837 if (!processError) { 839 HashMap testDatabaseConnectionHashMap = dbConnect.databaseTest (jahiaDBScript, jahiaDBDriver, jahiaDBUrl, jahiaDBUsername, jahiaDBPassword, runtimeTestSQL, false, true); 840 Boolean testDatabaseConnectionError = (Boolean )testDatabaseConnectionHashMap.get ("testDatabaseConnectionError"); 841 String testDatabaseConnectionMessage = (String )testDatabaseConnectionHashMap.get ("testDatabaseConnectionMessage"); 842 843 if (testDatabaseConnectionError.booleanValue ()) { 844 logger.debug ("Database test failed.\n"); 845 session.setAttribute (CLASS_NAME + "jahiaDisplayMessage", testDatabaseConnectionMessage); 846 processError = true; 847 } else { 848 logger.debug ("Database test passed successfully!\n"); 849 } 850 } 851 852 session.setAttribute (CLASS_NAME + "jahiaDBScript", jahiaDBScript); 854 session.setAttribute (CLASS_NAME + "jahiaDBDriver", jahiaDBDriver); 855 session.setAttribute (CLASS_NAME + "jahiaDBUrl", jahiaDBUrl); 856 session.setAttribute (CLASS_NAME + "jahiaDBUsername", jahiaDBUsername); 857 session.setAttribute (CLASS_NAME + "jahiaDBPassword", jahiaDBPassword); 858 if (!transfer) { 859 session.setAttribute (CLASS_NAME + "jahiaDBMinConnections", jahiaDBMinConnections); 860 session.setAttribute (CLASS_NAME + "jahiaDBMaxConnections", jahiaDBMaxConnections); 861 session.setAttribute (CLASS_NAME + "jahiaDBWaitIfBusy", jahiaDBWaitIfBusy); 862 session.setAttribute (CLASS_NAME + "jahiaDBVerbose", jahiaDBVerbose); 863 } 864 865 if (processError) { 866 displaySettings (request, response, session); 867 } else { if (!transfer) { 869 storeSettings (request, response, session, transfer); 870 } else { 871 if (!checkSameDatabase (jahiaDBUrl)) { 872 877 insertDatabaseTables (jahiaDBScript, jahiaDBDriver, jahiaDBUrl, jahiaDBUsername, jahiaDBPassword); 879 880 DatabaseConnection fromDB = null; 881 DatabaseConnection toDB = null; 882 try { 883 fromDB = new DatabaseConnection(); 884 fromDB.databaseOpen (properties.getProperty ("db_driver"), 885 properties.getProperty ("db_url"), 886 properties.getProperty ("db_username"), 887 properties.getProperty ("db_password")); 888 889 toDB = new DatabaseConnection(); 890 toDB.databaseOpen (jahiaDBDriver, jahiaDBUrl, 891 jahiaDBUsername, jahiaDBPassword); 892 893 transfertDatabaseData(fromDB,toDB); 894 895 } catch (Exception e) { 896 e.printStackTrace(); 897 } finally { 898 if ( fromDB != null ){ 899 fromDB.databaseClose(); 900 } 901 if ( toDB != null ){ 902 toDB.databaseClose(); 903 } 904 } 905 storeSettings (request, response, session, transfer); 906 } else { 907 String dspMsg = JahiaResourceBundle.getAdminResource ("org.jahia.admin.JahiaDisplayMessage.useCurrentDB.label", 908 jParams, jParams.getLocale ()); 909 session.setAttribute (CLASS_NAME + "jahiaDisplayMessage", dspMsg); 910 displaySettings (request, response, session); 911 } 912 } 913 } 914 } 915 916 } 918 926 private void transfertDatabaseData(DatabaseConnection fromDB, 927 DatabaseConnection toDB){ 928 try { 929 ResultSet tablesResultSet = fromDB.getConnection() 930 .getMetaData().getTables (null, null, null, 931 new String []{"TABLE"}); 932 933 StringBuffer sqlInsertCurrentValues = new StringBuffer (); 934 Vector tablesResultSetVector = new Vector (); 935 936 while (tablesResultSet.next ()) { 937 tablesResultSetVector.add (tablesResultSet.getString ("TABLE_NAME").toLowerCase ()); 938 } 939 tablesResultSet.close (); 940 941 Enumeration tablesResultSetEnum = tablesResultSetVector.elements (); 942 PreparedStatement pstmt = null; 943 while (tablesResultSetEnum.hasMoreElements ()) { 944 String table_name = (String )tablesResultSetEnum.nextElement (); 945 ResultSet columnsResultSet = null; 946 try { 947 if (table_name.toLowerCase().startsWith("jahia_")) { 948 StringBuffer sqlColumnsList = new StringBuffer (); 949 columnsResultSet = fromDB.getStatement() 950 .executeQuery ("SELECT * FROM " + table_name); 951 ResultSetMetaData columnsMetaData = columnsResultSet.getMetaData (); 952 int columnCount = columnsMetaData.getColumnCount (); 953 954 StringBuffer valuesBuff = new StringBuffer (" VALUES ("); 956 StringBuffer buff = new StringBuffer ("INSERT INTO "); 957 buff.append(table_name); 958 buff.append(" ("); 959 for (int column = 1; column <= columnCount; column++) { 960 if (column > 1) { 961 buff.append(", "); 962 valuesBuff.append(", "); 963 } 964 buff.append (columnsMetaData.getColumnLabel(column).toLowerCase ()); 965 valuesBuff.append("?"); 966 } 967 buff.append(" )"); 968 valuesBuff.append(" )"); 969 String sqlInsertStatement = buff.toString() + valuesBuff.toString(); 970 while (columnsResultSet.next ()) { 973 try { 974 pstmt = toDB.getConnection().prepareStatement(sqlInsertStatement); 975 for (int column = 1; column <= columnCount; column++) { 976 String column_name = columnsMetaData.getColumnLabel(column).toLowerCase(); 977 Object column_value = columnsResultSet.getObject(column); 978 986 pstmt.setObject(column,column_value); 987 } 988 pstmt.executeUpdate(); 990 } catch ( Throwable t ){ 991 logger.debug(t); 992 } finally { 993 if ( pstmt != null ){ 994 try { 995 pstmt.close(); 996 } catch ( Throwable t ){ 997 logger.debug(t); 998 } 999 } 1000 } 1001 } 1002 } 1003 } catch (IndexOutOfBoundsException ioobe) { 1004 logger.debug(ioobe); 1005 } catch (SQLException sqle) { 1006 logger.debug(sqle); 1007 } finally { 1008 if ( columnsResultSet != null ){ 1009 try { 1010 columnsResultSet.close(); 1011 } catch ( Throwable t ){ 1012 logger.debug(t); 1013 } 1014 } 1015 } 1016 } 1017 } catch (Exception e) { 1018 logger.debug(e); 1019 } 1020 } 1021 1022 1032 private void storeSettings (HttpServletRequest request, 1033 HttpServletResponse response, 1034 HttpSession session, 1035 boolean transfer) 1036 throws IOException , ServletException { 1037 1038 properties.setProperty ("db_script", (String )session.getAttribute (CLASS_NAME + "jahiaDBScript")); 1040 properties.setProperty ("db_driver", (String )session.getAttribute (CLASS_NAME + "jahiaDBDriver")); 1041 properties.setProperty ("db_url", (String )session.getAttribute (CLASS_NAME + "jahiaDBUrl")); 1042 properties.setProperty ("db_username", (String )session.getAttribute (CLASS_NAME + "jahiaDBUsername")); 1043 properties.setProperty ("db_password", (String )session.getAttribute (CLASS_NAME + "jahiaDBPassword")); 1044 if (!transfer) { 1045 properties.setProperty ("db_min_connections", (String )session.getAttribute (CLASS_NAME + "jahiaDBMinConnections")); 1046 properties.setProperty ("db_max_connections", (String )session.getAttribute (CLASS_NAME + "jahiaDBMaxConnections")); 1047 properties.setProperty ("db_waitIfBusy", (String )session.getAttribute (CLASS_NAME + "jahiaDBWaitIfBusy")); 1048 properties.setProperty ("db_verbose", (String )session.getAttribute (CLASS_NAME + "jahiaDBVerbose")); 1049 } 1050 1051 properties.storeProperties (); 1053 1054 JahiaData jData = (JahiaData)request.getAttribute ("org.jahia.data.JahiaData"); 1055 ParamBean jParams = null; 1056 if (jData != null) { 1057 jParams = jData.params (); 1058 } 1059 String dspMsg = JahiaResourceBundle.getAdminResource ("org.jahia.admin.processMessage.restartJahiaAfterDBChange.label", 1060 jParams, jParams.getLocale ()); 1061 request.setAttribute ("processMessage", dspMsg); 1062 session.setAttribute (CLASS_NAME + "jahiaDisplayMessage", 1063 JahiaResourceBundle.getAdminResource("org.jahia.admin.copyright", 1064 jParams, jParams.getLocale())); 1065 JahiaAdministration.doRedirect (request, response, session, JSP_PATH + "finish.jsp"); 1066 } 1068 1069 1079 private String grabDatabaseData (String driver, 1080 String url, 1081 String username, 1082 String password) { 1083 String sqlOutput = ""; 1084 1085 try { 1086 dbConnect.databaseOpen (driver, url, username, password); 1087 1088 ResultSet tablesResultSet = dbConnect.getConnection ().getMetaData ().getTables (null, null, null, new String []{"TABLE"}); 1089 StringBuffer sqlInsertCurrentValues = new StringBuffer (); 1090 Vector tablesResultSetVector = new Vector (); 1091 1092 while (tablesResultSet.next ()) { 1093 tablesResultSetVector.add (tablesResultSet.getString ("TABLE_NAME").toLowerCase ()); 1094 } 1095 tablesResultSet.close (); 1096 1097 Enumeration tablesResultSetEnum = tablesResultSetVector.elements (); 1098 1099 String pattern = "\\" + "n"; 1100 1101 while (tablesResultSetEnum.hasMoreElements ()) { 1102 String table_name = (String )tablesResultSetEnum.nextElement (); 1103 try { 1104 if (table_name.substring (0, 6).equals ("jahia_")) { 1105 StringBuffer sqlColumnsList = new StringBuffer (); 1106 ResultSet columnsResultSet = dbConnect.getStatement ().executeQuery ("SELECT * FROM " + table_name); 1107 ResultSetMetaData columnsMetaData = columnsResultSet.getMetaData (); 1108 int columnCount = columnsMetaData.getColumnCount (); 1109 1110 for (int column = 1; column <= columnCount; column++) { 1111 if (column > 1) { 1112 sqlColumnsList.append (", "); 1113 } 1114 sqlColumnsList.append (columnsMetaData.getColumnLabel (column).toLowerCase ()); 1115 } 1116 1117 while (columnsResultSet.next ()) { 1118 StringBuffer sqlValuesList = new StringBuffer (); 1119 for (int column = 1; column <= columnCount; column++) { 1120 String column_name = columnsResultSet.getString (column); 1121 int column_type = columnsMetaData.getColumnType (column); 1122 boolean isText = false; 1123 if (column > 1) { 1124 sqlValuesList.append (", "); 1125 } 1126 if (column_name != null) { 1127 if ((column_type == Types.VARCHAR) || (column_type == Types.CHAR) || (column_type == Types.BLOB) || (column_type == Types.LONGVARCHAR)) { 1128 isText = true; 1129 } else if (column_type == Types.OTHER) { 1130 try { 1131 int testConversion = Integer.parseInt (column_name); 1132 } catch (NumberFormatException nfe) { 1133 isText = true; 1134 } 1135 } 1136 } 1137 1138 if (isText) { 1139 sqlValuesList.append ("'"); 1140 } 1141 sqlValuesList.append (column_name); 1142 if (isText) { 1143 sqlValuesList.append ("'"); 1144 } 1145 } 1146 1147 String parsedString = JahiaTools.replacePattern (sqlValuesList.toString (), "\n", pattern); 1148 parsedString = JahiaTools.replacePattern (parsedString, "\r", ""); 1149 1150 sqlInsertCurrentValues.append ("INSERT INTO " + table_name + "(" + sqlColumnsList.toString ().toLowerCase () + ") VALUES(" + parsedString + ")\n"); 1151 } 1152 columnsResultSet.close (); 1153 } 1154 } catch (IndexOutOfBoundsException ioobe) { 1155 } catch (SQLException sqle) { 1156 } 1157 } 1158 dbConnect.databaseClose (); 1159 1160 sqlOutput = sqlInsertCurrentValues.toString (); 1161 1162 } catch (Exception e) { 1163 } 1164 1165 return sqlOutput; 1166 } 1168 1169 1178 private void insertDatabaseData (Enumeration allGrabbedData, 1179 String driver, 1180 String url, 1181 String username, 1182 String password) { 1183 String lastTableName = ""; 1184 1185 try { 1187 dbConnect.databaseOpen (driver, url, username, password); 1188 1189 while (allGrabbedData.hasMoreElements ()) { 1190 try { 1191 String runtimeSQL = (String )allGrabbedData.nextElement (); 1192 String runtimeTableName = runtimeSQL.substring (runtimeSQL.indexOf ("jahia_"), runtimeSQL.indexOf ("(")).trim (); 1193 try { 1194 if (!runtimeTableName.equals (lastTableName)) { 1195 Statement stmt = dbConnect.getStatement (); 1196 stmt.executeUpdate ("DELETE FROM " + runtimeTableName); 1197 } 1198 } catch (SQLException sqle) { 1199 } 1200 try { 1201 Statement stmt = dbConnect.getStatement (); 1202 stmt.executeUpdate (JahiaTools.replacePattern (runtimeSQL, "\\n", "\n")); 1203 } catch (SQLException sqle) { 1204 } 1205 lastTableName = runtimeTableName; 1206 } catch (IndexOutOfBoundsException ioobe) { 1207 } 1208 } 1209 1210 dbConnect.databaseClose (); 1211 } catch (NullPointerException npe) { 1212 logger.error ("Error:", npe); 1213 } catch (ClassNotFoundException cnfe) { 1214 logger.error ("Database driver not found :", cnfe); 1215 } catch (SQLException sqle) { 1216 logger.error ("Error while executing query : ", sqle); 1217 } 1218 } 1220 1221 1231 private void insertDatabaseTables (String scriptFileName, 1232 String driver, 1233 String url, 1234 String username, 1235 String password) 1236 { 1237 try { 1239 dbConnect.databaseOpen (driver, url, username, password); 1240 1241 File scriptsFileObject = new File (Jahia.jahiaDatabaseScriptsPath + File.separator + scriptFileName); 1243 Enumeration scriptsListRuntime = scriptsManager.getDatabaseScriptsRuntime (scriptsFileObject); 1244 1245 while (scriptsListRuntime.hasMoreElements ()) { 1246 try { 1247 String runtimeSQL = (String )scriptsListRuntime.nextElement (); 1248 String runtimeTableName = runtimeSQL.substring (runtimeSQL.indexOf ("jahia_"), runtimeSQL.indexOf ("(")).trim (); 1249 try { 1250 dbConnect.getStatement ().execute ("DROP TABLE " + runtimeTableName); 1251 } catch (SQLException sqle) { 1252 } 1253 try { 1254 dbConnect.getStatement ().execute (runtimeSQL); 1255 } catch (SQLException sqle) { 1256 } 1257 } catch (IndexOutOfBoundsException ioobe) { 1258 } 1259 } 1260 1261 dbConnect.databaseClose (); 1262 } catch (NullPointerException npe) { 1263 logger.error ("Null pointer exception:", npe); 1264 } catch (IOException ioe) { 1265 logger.error ("IOException:", ioe); 1266 } catch (ClassNotFoundException cnfe) { 1267 logger.error ("Error while using database driver :", cnfe); 1268 } catch (SQLException sqle) { 1269 logger.error ("SQL Exception :", sqle); 1270 } 1271 } 1273 1274 1283 private boolean checkSameDatabase (String db_url) 1284 throws IOException , ServletException { 1285 boolean theSameDatabase = false; 1286 if (db_url.equals (properties.getProperty ("db_url"))) { 1287 theSameDatabase = true; 1288 } 1289 return theSameDatabase; 1290 } 1291 1292 1293} 1294 | Popular Tags |