1 31 32 package org.opencms.setup; 33 34 import org.opencms.configuration.CmsConfigurationManager; 35 import org.opencms.configuration.CmsModuleConfiguration; 36 import org.opencms.file.CmsResource; 37 import org.opencms.i18n.CmsEncoder; 38 import org.opencms.main.CmsLog; 39 import org.opencms.main.CmsSystemInfo; 40 import org.opencms.main.OpenCms; 41 import org.opencms.module.CmsModule; 42 import org.opencms.module.CmsModuleVersion; 43 import org.opencms.module.CmsModuleXmlHandler; 44 import org.opencms.report.CmsShellReport; 45 import org.opencms.report.I_CmsReport; 46 import org.opencms.util.CmsStringUtil; 47 import org.opencms.xml.CmsXmlException; 48 49 import java.io.File ; 50 import java.io.FileInputStream ; 51 import java.io.FileOutputStream ; 52 import java.io.IOException ; 53 import java.util.ArrayList ; 54 import java.util.HashMap ; 55 import java.util.HashSet ; 56 import java.util.Iterator ; 57 import java.util.List ; 58 import java.util.Map ; 59 import java.util.Set ; 60 61 import javax.servlet.jsp.JspWriter ; 62 63 import org.apache.commons.logging.Log; 64 65 74 public class CmsUpdateBean extends CmsSetupBean { 75 76 77 public static final String FOLDER_UPDATE = "update" + File.separatorChar; 78 79 80 private static final String C_ADMIN_GROUP = "@ADMIN_GROUP@"; 81 82 83 private static final String C_ADMIN_PWD = "@ADMIN_PWD@"; 84 85 86 private static final String C_ADMIN_USER = "@ADMIN_USER@"; 87 88 89 private static final String C_UPDATE_PROJECT = "@UPDATE_PROJECT@"; 90 91 92 private static final String C_UPDATE_SITE = "@UPDATE_SITE@"; 93 94 95 private static final Log LOG = CmsLog.getLog(CmsUpdateBean.class); 96 97 98 private String m_adminGroup = "_tmpUpdateGroup" + (System.currentTimeMillis() % 1000); 99 100 101 private String m_adminPwd = "admin"; 102 103 104 private String m_adminUser = "Admin"; 105 106 107 private List m_modulesToUpdate; 108 109 110 private String m_updateProject = "_tmpUpdateProject" + (System.currentTimeMillis() % 1000); 111 112 113 private String m_updateSite = CmsResource.VFS_FOLDER_SITES + "/default/"; 114 115 116 private List m_uptodateModules; 117 118 119 private CmsUpdateThread m_workplaceUpdateThread; 120 121 124 public CmsUpdateBean() { 125 126 super(); 127 m_modulesFolder = FOLDER_UPDATE + CmsSystemInfo.FOLDER_MODULES; 128 m_logFile = FOLDER_WEBINF + CmsLog.FOLDER_LOGS + "update.log"; 129 } 130 131 138 public String displayError(String pathPrefix) { 139 140 if (pathPrefix == null) { 141 pathPrefix = ""; 142 } 143 StringBuffer html = new StringBuffer (512); 144 html.append("<table border='0' cellpadding='5' cellspacing='0' style='width: 100%; height: 100%;'>"); 145 html.append("\t<tr>"); 146 html.append("\t\t<td style='vertical-align: middle; height: 100%;'>"); 147 html.append(getHtmlPart("C_BLOCK_START", "Error")); 148 html.append("\t\t\t<table border='0' cellpadding='0' cellspacing='0' style='width: 100%;'>"); 149 html.append("\t\t\t\t<tr>"); 150 html.append("\t\t\t\t\t<td><img SRC='").append(pathPrefix).append("resources/error.png' border='0'></td>"); 151 html.append("\t\t\t\t\t<td> </td>"); 152 html.append("\t\t\t\t\t<td style='width: 100%;'>"); 153 html.append("\t\t\t\t\t\tThe Alkacon OpenCms update wizard has not been started correctly!<br>"); 154 html.append("\t\t\t\t\t\tPlease click <a HREF='").append(pathPrefix); 155 html.append("index.jsp'>here</a> to restart the wizard."); 156 html.append("\t\t\t\t\t</td>"); 157 html.append("\t\t\t\t</tr>"); 158 html.append("\t\t\t</table>"); 159 html.append(getHtmlPart("C_BLOCK_END")); 160 html.append("\t\t</td>"); 161 html.append("\t</tr>"); 162 html.append("</table>"); 163 return html.toString(); 164 } 165 166 171 public String getAdminPwd() { 172 173 return m_adminPwd; 174 } 175 176 181 public String getAdminUser() { 182 183 return m_adminUser; 184 } 185 186 193 public Map getInstalledModules() { 194 195 String file = CmsModuleConfiguration.DEFAULT_XML_FILE_NAME; 196 String basePath = new StringBuffer ("/").append(CmsConfigurationManager.N_ROOT).append("/").append( 198 CmsModuleConfiguration.N_MODULES).append("/").append(CmsModuleXmlHandler.N_MODULE).append("[?]/").toString(); 199 Map modules = new HashMap (); 200 String name = ""; 201 for (int i = 1; name != null; i++) { 202 if (i > 1) { 203 String ver = CmsModuleVersion.DEFAULT_VERSION; 204 try { 205 ver = getXmlHelper().getValue( 206 file, 207 CmsStringUtil.substitute(basePath, "?", "" + (i - 1)) + CmsModuleXmlHandler.N_VERSION); 208 } catch (CmsXmlException e) { 209 } 211 modules.put(name, new CmsModuleVersion(ver)); 212 } 213 try { 214 name = getXmlHelper().getValue( 215 file, 216 CmsStringUtil.substitute(basePath, "?", "" + i) + CmsModuleXmlHandler.N_NAME); 217 } catch (CmsXmlException e) { 218 } 220 } 221 return modules; 222 } 223 224 229 public List getModulesToUpdate() { 230 231 if (m_modulesToUpdate == null) { 232 getUptodateModules(); 233 } 234 return m_modulesToUpdate; 235 } 236 237 242 public String getUpdateProject() { 243 244 return m_updateProject; 245 } 246 247 252 public String getUpdateSite() { 253 254 return m_updateSite; 255 } 256 257 262 public List getUptodateModules() { 263 264 if (m_uptodateModules == null) { 265 m_uptodateModules = new ArrayList (); 266 m_modulesToUpdate = new ArrayList (); 267 Map installedModules = getInstalledModules(); 268 Map availableModules = getAvailableModules(); 269 Iterator itMods = availableModules.keySet().iterator(); 270 while (itMods.hasNext()) { 271 String name = (String )itMods.next(); 272 CmsModuleVersion instVer = (CmsModuleVersion)installedModules.get(name); 273 CmsModuleVersion availVer = ((CmsModule)availableModules.get(name)).getVersion(); 274 boolean uptodate = (instVer != null && instVer.compareTo(availVer) >= 0); 275 if (uptodate) { 276 m_uptodateModules.add(name); 277 } else { 278 m_modulesToUpdate.add(name); 279 } 280 if (LOG.isDebugEnabled()) { 281 LOG.debug(name 282 + " --- installed: " 283 + instVer 284 + " available: " 285 + availVer 286 + " --- uptodate: " 287 + uptodate); 288 } 289 } 290 } 291 return m_uptodateModules; 292 } 293 294 299 public CmsUpdateThread getWorkplaceUpdateThread() { 300 301 return m_workplaceUpdateThread; 302 } 303 304 307 public String htmlModules() { 308 309 StringBuffer html = new StringBuffer (1024); 310 Set uptodate = new HashSet (getUptodateModules()); 311 Iterator itModules = sortModules(getAvailableModules().values()).iterator(); 312 boolean hasModules = false; 313 for (int i = 0; itModules.hasNext(); i++) { 314 String moduleName = (String )itModules.next(); 315 CmsModule module = (CmsModule)getAvailableModules().get(moduleName); 316 if (!uptodate.contains(moduleName)) { 317 html.append(htmlModule(module, i)); 318 hasModules = true; 319 } else { 320 html.append("<input type='hidden' name='availableModules' value='"); 321 html.append(moduleName); 322 html.append("'>\n"); 323 } 324 } 325 if (!hasModules) { 326 html.append("\t<tr>\n"); 327 html.append("\t\t<td style='vertical-align: middle;'>\n"); 328 html.append(Messages.get().getBundle().key(Messages.GUI_WARNING_ALL_MODULES_UPTODATE_0)); 329 html.append("\t\t</td>\n"); 330 html.append("\t</tr>\n"); 331 } 332 return html.toString(); 333 } 334 335 343 public void init(String webAppRfsPath, String servletMapping, String defaultWebApplication) { 344 345 try { 346 super.init(webAppRfsPath, servletMapping, defaultWebApplication); 347 348 if (m_workplaceUpdateThread != null) { 349 if (m_workplaceUpdateThread.isAlive()) { 350 m_workplaceUpdateThread.kill(); 351 } 352 m_workplaceUpdateThread = null; 353 } 354 } catch (Exception e) { 355 e.printStackTrace(); 356 throw new RuntimeException (e); 357 } 358 } 359 360 363 public void prepareUpdateStep5() { 364 365 if (isInitialized()) { 366 try { 367 String fileName = getWebAppRfsPath() + FOLDER_UPDATE + "cmsupdate"; 368 FileInputStream fis = new FileInputStream (fileName + CmsConfigurationManager.POSTFIX_ORI); 370 String script = ""; 371 int readChar = fis.read(); 372 while (readChar > -1) { 373 script += (char)readChar; 374 readChar = fis.read(); 375 } 376 script = CmsStringUtil.substitute(script, C_ADMIN_USER, getAdminUser()); 378 script = CmsStringUtil.substitute(script, C_ADMIN_PWD, getAdminPwd()); 379 script = CmsStringUtil.substitute(script, C_UPDATE_PROJECT, getUpdateProject()); 380 script = CmsStringUtil.substitute(script, C_UPDATE_SITE, getUpdateSite()); 381 script = CmsStringUtil.substitute(script, C_ADMIN_GROUP, getAdminGroup()); 382 FileOutputStream fos = new FileOutputStream (fileName + ".txt"); 384 fos.write(script.getBytes()); 385 fos.close(); 386 } catch (IOException e) { 387 e.printStackTrace(); 388 throw new RuntimeException (e); 389 } 390 } 391 } 392 393 396 public void prepareUpdateStep5b() { 397 398 if (!isInitialized()) { 399 return; 400 } 401 402 if ((m_workplaceUpdateThread != null) && (m_workplaceUpdateThread.isFinished())) { 403 return; 405 } 406 407 if (m_workplaceUpdateThread == null) { 408 m_workplaceUpdateThread = new CmsUpdateThread(this); 409 } 410 411 if (!m_workplaceUpdateThread.isAlive()) { 412 m_workplaceUpdateThread.start(); 413 } 414 } 415 416 423 public void prepareUpdateStep5bOutput(JspWriter out) throws IOException { 424 425 m_oldLoggingOffset = m_newLoggingOffset; 426 m_newLoggingOffset = m_workplaceUpdateThread.getLoggingThread().getMessages().size(); 427 if (isInitialized()) { 428 for (int i = m_oldLoggingOffset; i < m_newLoggingOffset; i++) { 429 String str = m_workplaceUpdateThread.getLoggingThread().getMessages().get(i).toString(); 430 str = CmsEncoder.escapeWBlanks(str, CmsEncoder.ENCODING_UTF_8); 431 out.println("output[" + (i - m_oldLoggingOffset) + "] = \"" + str + "\";"); 432 } 433 } else { 434 out.println("output[0] = 'ERROR';"); 435 } 436 437 boolean threadFinished = m_workplaceUpdateThread.isFinished(); 438 boolean allWritten = m_oldLoggingOffset >= m_workplaceUpdateThread.getLoggingThread().getMessages().size(); 439 440 out.println("function initThread() {"); 441 if (isInitialized()) { 442 out.print("send();"); 443 if (threadFinished && allWritten) { 444 out.println("setTimeout('top.display.finish()', 500);"); 445 } else { 446 int timeout = 5000; 447 if (getWorkplaceUpdateThread().getLoggingThread().getMessages().size() < 20) { 448 timeout = 1000; 449 } 450 out.println("setTimeout('location.reload()', " + timeout + ");"); 451 } 452 } 453 out.println("}"); 454 } 455 456 459 public void prepareUpdateStep6() { 460 461 if (isInitialized()) { 462 lockWizard(); 464 saveProperties(getProperties(), CmsSystemInfo.FILE_PROPERTIES, false); 466 } 467 } 468 469 474 public void setAdminPwd(String adminPwd) { 475 476 m_adminPwd = adminPwd; 477 } 478 479 484 public void setAdminUser(String adminUser) { 485 486 m_adminUser = adminUser; 487 } 488 489 494 public void setUpdateProject(String updateProject) { 495 496 m_updateProject = updateProject; 497 } 498 499 504 public void setUpdateSite(String site) { 505 506 m_updateSite = site; 507 } 508 509 512 public void shellExit() { 513 514 System.out.println(); 515 System.out.println(); 516 System.out.println("The update is finished!\nThe OpenCms system used for the update will now shut down."); 517 } 518 519 522 public void shellStart() { 523 524 System.out.println(); 525 System.out.println("Starting Workplace update for OpenCms!"); 526 527 String [] copy = org.opencms.main.Messages.COPYRIGHT_BY_ALKACON; 528 for (int i = copy.length - 1; i >= 0; i--) { 529 System.out.println(copy[i]); 530 } 531 System.out.println("This is OpenCms " + OpenCms.getSystemInfo().getVersionName()); 532 System.out.println(); 533 System.out.println(); 534 } 535 536 543 public void updateModulesFromUpdateBean() throws Exception { 544 545 554 if (m_cms != null && m_installModules != null) { 555 Set utdModules = new HashSet (getUptodateModules()); 556 I_CmsReport report = new CmsShellReport(m_cms.getRequestContext().getLocale()); 557 for (int i = 0; i < m_installModules.size(); i++) { 558 String name = (String )m_installModules.get(i); 559 if (!utdModules.contains(name)) { 560 String filename = (String )m_moduleFilenames.get(name); 561 try { 562 updateModule(name, filename, report); 563 } catch (Exception e) { 564 e.printStackTrace(System.err); 566 } 567 } else { 568 report.println( 569 Messages.get().container(Messages.RPT_MODULE_UPTODATE_1, name), 570 I_CmsReport.FORMAT_HEADLINE); 571 } 572 } 573 } 574 } 575 576 581 protected String getAdminGroup() { 582 583 return m_adminGroup; 584 } 585 586 591 protected void setAdminGroup(String adminGroup) { 592 593 m_adminGroup = adminGroup; 594 } 595 596 608 protected void updateModule(String moduleName, String importFile, I_CmsReport report) throws Exception { 609 610 String fileName = getModuleFolder() + importFile; 611 612 report.println( 613 Messages.get().container(Messages.RPT_BEGIN_UPDATE_MODULE_1, moduleName), 614 I_CmsReport.FORMAT_HEADLINE); 615 if (OpenCms.getModuleManager().getModule(moduleName) != null) { 616 OpenCms.getModuleManager().deleteModule(m_cms, moduleName, true, report); 617 } 618 OpenCms.getImportExportManager().importData(m_cms, fileName, null, report); 619 report.println( 620 Messages.get().container(Messages.RPT_END_UPDATE_MODULE_1, moduleName), 621 I_CmsReport.FORMAT_HEADLINE); 622 } 623 624 } | Popular Tags |