1 20 21 package org.jahia.services.webapps_deployer; 22 23 24 import org.jahia.data.applications.ApplicationBean; 25 import org.jahia.data.constants.JahiaConstants; 26 import org.jahia.data.webapps.JahiaEarFileHandler; 27 import org.jahia.data.webapps.JahiaWebAppsPackage; 28 import org.jahia.data.webapps.JahiaWebAppsWarHandler; 29 import org.jahia.data.webapps.Web_Component; 30 import org.jahia.exceptions.JahiaException; 31 import org.jahia.exceptions.JahiaInitializationException; 32 import org.jahia.services.sites.JahiaSite; 33 import org.jahia.services.webapps_deployer.tomcat.TomcatWebAppsDeployer; 34 import org.jahia.services.webapps_deployer.tomcat.Tomcat_Users_Xml; 35 import org.jahia.settings.SettingsBean; 36 import org.jahia.utils.JahiaTools; 37 import org.jahia.utils.keygenerator.JahiaKeyGen; 38 39 import java.io.File ; 40 import java.util.Vector ; 41 42 49 public class JahiaTomcatWebAppsDeployerBaseService extends JahiaWebAppsDeployerService { 50 51 private static org.apache.log4j.Logger logger = 52 org.apache.log4j.Logger.getLogger (JahiaTomcatWebAppsDeployerBaseService.class); 53 54 private static final String CLASS_NAME = JahiaWebAppsDeployerService.class.getName (); 55 56 private static JahiaTomcatWebAppsDeployerBaseService m_Instance = null; 57 58 59 60 private String m_TomcatUsersXmlFilePath; 61 62 63 64 65 private static String m_TomcatUserName = "Jahia"; 66 67 68 private static String m_TomcatUserPassword = ""; 69 70 71 private static String m_TomcatUserRoles = "manager"; 72 73 74 private static String m_JahiaWebAppsDeployerBaseURL = ""; 75 76 77 private boolean m_TomcatInitErr = false; 78 79 80 83 protected JahiaTomcatWebAppsDeployerBaseService () { 84 85 logger.debug ("Starting the Jahia Tomcat WebApps Deployer Base Service"); 86 87 } 88 89 90 93 public static synchronized JahiaTomcatWebAppsDeployerBaseService getInstance () { 94 95 if (m_Instance == null) { 96 m_Instance = new JahiaTomcatWebAppsDeployerBaseService (); 97 } 98 return m_Instance; 99 } 100 101 102 108 public void init (SettingsBean jSettings) 109 throws JahiaInitializationException { 110 111 if (!isInitialized ()) { 112 113 super.init (jSettings); 114 115 m_JahiaWebAppsDeployerBaseURL = jSettings.getJahiaWebAppsDeployerBaseURL (); 116 117 m_TomcatUsersXmlFilePath = 120 m_ServerHomeDiskPath + "conf" + File.separator + "tomcat-users.xml"; 121 122 File tomcatUsersXmlFile = new File (m_TomcatUsersXmlFilePath); 123 if (tomcatUsersXmlFile == null || !tomcatUsersXmlFile.isFile () 124 || !tomcatUsersXmlFile.canWrite ()) { 125 logger.debug ( 126 "WARNING: " + tomcatUsersXmlFile.getAbsolutePath () + " file cannot be accessed or doesn't exist, application deployment might not work! "); 127 m_TomcatInitErr = true; 129 } else { 130 131 if (!addManagerUser (m_TomcatUsersXmlFilePath)) { 132 logger.debug ( 133 "WARNING: cannot create the tomcat manager user for web apps, application deployment might not work"); 134 m_TomcatInitErr = true; 135 136 } 137 } 138 139 mIsServiceInitialized = true; 140 } 141 142 } 144 145 159 public boolean deploy (JahiaSite site, String context, String filePath) 160 throws JahiaException { 161 162 if (!canDeploy ()) { 163 return false; 164 } 165 166 boolean success = false; 167 if (filePath.endsWith (".ear")) { 168 success = handleEarFile (site, filePath); 169 } else if (filePath.endsWith (".war")) { 170 success = deployWarFile (site, context, filePath); 171 } else { 172 File tmpFile = new File (filePath); 173 if (tmpFile != null && tmpFile.isDirectory ()) { 174 success = deployDirectory (site, context, filePath); 175 } 176 } 177 178 if (success) { 179 deletePackage (site, filePath); 180 return true; 181 } else { 182 return false; 183 } 184 } 185 186 187 195 196 public boolean undeploy (ApplicationBean app) throws JahiaException { 197 198 if (!canDeploy ()) { 199 return false; 200 } 201 202 if (app != null) { 203 204 undeployWebApp (app.getContext ()); 206 207 JahiaTools.deleteFile (new File (m_WebAppRootPath + app.getContext ())); 209 210 211 222 223 return true; 224 } 225 226 return false; 227 } 228 229 230 238 protected boolean deployWarFile (JahiaSite site, String webContext, String filePath) 239 throws JahiaException { 240 241 Vector webApps = new Vector (); 242 StringBuffer webContextDiskPath = new StringBuffer (m_WebAppRootPath); 243 webContextDiskPath.append (File.separator); 244 webContextDiskPath.append (webContext); 245 webContextDiskPath.append ("_"); 246 webContextDiskPath.append (site.getSiteKey ()); 247 248 StringBuffer webContextBuff = new StringBuffer (webContext); 250 webContextBuff.append ("_"); 251 webContextBuff.append (site.getSiteKey ()); 252 253 webApps = handleWarFile (webContextDiskPath.toString (), filePath); 254 255 activateWebApp (webContextBuff.toString (), webContextDiskPath.toString ()); 257 258 File f = new File (filePath); 260 registerWebApps (site, webContextBuff.toString (), f.getName (), webApps); 261 262 File warFile = new File (filePath); 264 warFile.delete (); 265 266 275 276 return true; 277 278 } 279 280 281 289 protected synchronized boolean deployDirectory (JahiaSite site, String webContext, 290 String filePath) throws JahiaException { 291 292 String webContextDiskPath = m_WebAppRootPath + webContext; 293 File tmpFile = new File (webContextDiskPath); 294 File curFile = new File (filePath); 295 boolean sameDir = false; 296 297 sameDir = tmpFile.equals (curFile); 298 299 302 303 304 if (sameDir || !tmpFile.isDirectory ()) { 305 306 JahiaWebAppsPackage aPackage = loadWebAppInfoFromDirectory (filePath); 307 if (aPackage != null && (aPackage.getWebApps ().size () > 0)) { 308 309 File dir = new File (filePath); 310 311 if (!sameDir) { 312 313 if (dir != null && dir.renameTo (tmpFile)) { 314 return false; 315 } 316 } 317 318 activateWebApp (webContext, webContextDiskPath); 320 321 registerWebApps (site, webContext, "", aPackage.getWebApps ()); 323 324 return true; 325 } 326 } 327 328 return false; 329 330 } 331 332 333 343 public boolean deploy (JahiaSite site, Vector files) { 344 345 346 synchronized (files) { 347 348 int size = files.size (); 349 File fileItem = null; 350 351 for (int i = 0; i < size; i++) { 352 353 fileItem = (File ) files.get (i); 354 355 if (fileItem != null && fileItem.isFile ()) { 356 logger.debug ("Found new application to deploy : " + fileItem.getName ()); 357 try { 358 359 JahiaWebAppsPackage pack = null; 360 if (site.getWebAppsAutoDeployMode () && canDeploy ()) { 361 if (fileItem.getName ().endsWith (".ear") || fileItem.getName () 362 .endsWith (".war")) { 363 pack = loadWebAppInfo (fileItem.getAbsolutePath ()); 364 if (pack != null) { 365 if (!deploy (site, pack.getContextRoot (), 366 fileItem.getAbsolutePath ())) { 367 fileItem.delete (); 368 return false; 369 } 370 } else { 371 try { 372 File newFile = new File ( 373 fileItem.getAbsolutePath () + "_error"); 374 fileItem.renameTo (newFile); 376 } catch (Throwable t) { 377 logger.error ( 378 "Error renaming error file " + 379 fileItem.toString () + " to " + 380 fileItem.getAbsolutePath () + "_error", 381 t); 382 } 383 } 384 } 385 } else { 386 if (!site.getWebAppsAutoDeployMode ()) { 387 logger.warn ( 388 "Site automatic web application deployment is deactivated. Rescheduling for later in case setting changes."); 389 } 390 addNewFile (site, fileItem.getAbsolutePath ()); 391 } 392 393 } catch (JahiaException e) { 394 String errMsg = "Failed deploying Application file " + fileItem.getName (); 395 logger.error (errMsg, e); 396 fileItem.delete (); 397 } 398 } 399 } 400 } 401 return true; 402 } 403 404 405 414 protected boolean handleEarFile (JahiaSite site, String filePath) throws JahiaException { 415 416 418 JahiaEarFileHandler earh = null; 420 421 try { 422 423 earh = new JahiaEarFileHandler (filePath); 424 425 Vector webComponents = earh.getWebComponents (); 427 int size = webComponents.size (); 428 429 if (size <= 0) { 430 return false; 431 } 432 433 Web_Component webComp = null; 435 String webURI = null; 437 for (int i = 0; i < size; i++) { 438 439 webComp = (Web_Component) webComponents.get (i); 440 441 webURI = webComp.getWebURI (); 442 443 444 if (webURI != null && (webURI.length () > 0)) { 445 446 448 earh.extractEntry (webURI, m_TempFolderDiskPath); 450 451 File warFile = new File (m_TempFolderDiskPath + File.separator + webURI); 453 454 String deployContext = webComp.getContextRoot(); 455 if (deployContext == null) { 456 deployContext = JahiaTools.removeFileExtension (warFile.getName (), ".war"); 457 } else { 458 if (deployContext.startsWith("/")) { 459 deployContext = deployContext.substring(1); 460 } 461 } 462 463 if (!deployWarFile (site, 464 deployContext, 465 warFile.getAbsolutePath ())) { 466 return false; 467 } 468 469 } 470 471 } 472 473 474 } catch (JahiaException e) { 475 476 String errMsg = "Failed handling webApps file "; 477 logger.error (errMsg + "\n" + e.toString (), e); 478 throw new JahiaException ("JahiaTomcatWebAppsDeployerBaseService::deployEarFile()", 479 "JahiaTomcatWebAppsDeployerBaseService" + errMsg, 480 JahiaException.SERVICE_ERROR, JahiaException.ERROR_SEVERITY); 481 482 } finally { 483 484 if (earh != null) { 487 earh.closeArchiveFile (); 488 } 489 } 490 491 return true; 492 } 493 494 495 496 506 protected Vector handleWarFile (String webAppContext, String filePath) 507 throws JahiaException { 508 509 JahiaWebAppsWarHandler wah = null; 511 512 Vector webApps = new Vector (); 513 514 try { 515 516 wah = new JahiaWebAppsWarHandler (filePath); 517 518 webApps = wah.getWebAppsPackage ().getWebApps (); 519 int size = webApps.size (); 520 521 if (size > 0) { 522 523 File f = new File (webAppContext); 525 if (!f.isDirectory () && !f.mkdirs ()) { 526 527 String errMsg = "Failed creating the war context root dir " + f.getAbsolutePath (); 528 logger.error (errMsg + "\n"); 529 throw new JahiaException ( 530 "JahiaTomcatWebAppsDeployerBaseService::deployWarFile()", 531 "JahiaTomcatWebAppsDeployerBaseService" + errMsg, 532 JahiaException.SERVICE_ERROR, JahiaException.ERROR_SEVERITY); 533 } 534 535 removeMetaInfFolder (f.getAbsolutePath ()); 537 538 wah.unzip (webAppContext); 540 541 542 } 543 544 } catch (JahiaException e) { 545 546 String errMsg = "Failed handling webApps file "; 547 logger.error (errMsg + "\n" + e.toString (), e); 548 throw new JahiaException ("JahiaTomcatWebAppsDeployerBaseService::deployWarFile()", 549 "JahiaTomcatWebAppsDeployerBaseService" + errMsg, 550 JahiaException.SERVICE_ERROR, JahiaException.ERROR_SEVERITY, e); 551 552 } finally { 553 554 if (wah != null) { 557 wah.closeArchiveFile (); 558 } 559 } 560 561 return webApps; 562 } 563 564 565 566 574 protected boolean activateWebApp ( 575 String context, 576 String webAppDiskPath 577 ) throws JahiaException { 578 579 TomcatWebAppsDeployer deployer = new TomcatWebAppsDeployer ( 580 m_ServerType, 581 m_JahiaWebAppsDeployerBaseURL, 582 m_TomcatUserName, 583 m_TomcatUserPassword 584 ); 585 File tmpFile = new File (webAppDiskPath); 586 String fileUrl = null; 587 try { 588 fileUrl = tmpFile.toURL ().toString (); 589 } catch (java.net.MalformedURLException ue) { 590 logger.debug ( 591 "Error while activating app with context=" + context + " and webAppDiskPath=" + webAppDiskPath, 592 ue); 593 return false; 594 } 595 596 return deployer.deploy ("/" + context, fileUrl); 597 598 } 599 600 601 608 protected boolean undeployWebApp (String context 609 ) throws JahiaException { 610 611 TomcatWebAppsDeployer deployer = new TomcatWebAppsDeployer ( 612 m_ServerType, 613 m_JahiaWebAppsDeployerBaseURL, 614 m_TomcatUserName, 615 m_TomcatUserPassword 616 ); 617 622 boolean doStop = (!m_ServerType.endsWith (JahiaConstants.SERVER_TOMCAT4_BETA1)); 623 624 if (context.startsWith ("/")) { 625 if (doStop) { 626 deployer.stop (context); 627 } 628 return deployer.undeploy (context); 629 } else { 630 if (doStop) { 631 deployer.stop ("/" + context); 632 } 633 return deployer.undeploy ("/" + context); 634 } 635 636 } 637 638 639 647 protected String createWebContext (String webContext) { 648 649 StringBuffer strBuf = new StringBuffer (1024); 650 strBuf.append (m_WebAppRootPath); 651 strBuf.append (webContext); 652 653 String webContextPath = strBuf.toString (); 655 656 File f = new File (webContextPath); 657 658 if (!f.isDirectory () && !f.mkdirs ()) { 659 660 return null; 661 } 662 663 return webContextPath; 664 } 665 666 667 675 protected boolean addManagerUser (String docPath) { 676 677 678 try { 679 680 String password = null; 681 m_TomcatUserPassword = JahiaKeyGen.getKey (15); 682 Tomcat_Users_Xml doc = new Tomcat_Users_Xml (docPath); 683 684 password = doc.getUserPassword (m_TomcatUserName, m_TomcatUserRoles); 685 if (password == null) { 686 doc.addUser (m_TomcatUserName, m_TomcatUserPassword, m_TomcatUserRoles); 687 doc.write (); 688 } else if (password.equals ("")) { 689 doc.updateUser (m_TomcatUserName, m_TomcatUserPassword, m_TomcatUserRoles); 690 doc.write (); 691 } else { 692 m_TomcatUserPassword = password; 693 } 694 } catch (JahiaException e) { 695 logger.debug ("Exception while modifying Tomcat user file", e); 696 return false; 697 } 698 return true; 699 } 700 701 702 703 710 public boolean canDeploy () { 711 if (m_TomcatInitErr) { 712 logger.debug ( 713 "Cannot deploy application because of failure of the initialization of the deployment service. Check your logs for warnings or errors."); 714 } 715 716 return (!m_TomcatInitErr); 717 718 } 719 720 721 } | Popular Tags |