1 20 21 package org.jahia.services.webapps_deployer; 22 23 24 import org.jahia.data.applications.ApplicationBean; 25 import org.jahia.data.webapps.*; 26 import org.jahia.exceptions.JahiaException; 27 import org.jahia.exceptions.JahiaInitializationException; 28 import org.jahia.registries.ServicesRegistry; 29 import org.jahia.services.JahiaService; 30 import org.jahia.services.acl.ACLNotFoundException; 31 import org.jahia.services.acl.JahiaBaseACL; 32 import org.jahia.services.sites.JahiaSite; 33 import org.jahia.settings.SettingsBean; 34 import org.jahia.utils.JahiaTools; 35 import org.xml.sax.EntityResolver ; 36 37 import java.io.File ; 38 import java.util.Enumeration ; 39 import java.util.Hashtable ; 40 import java.util.Vector ; 41 42 43 49 public abstract class JahiaWebAppsDeployerService extends JahiaService { 50 51 private static org.apache.log4j.Logger logger = 52 org.apache.log4j.Logger.getLogger (JahiaWebAppsDeployerService.class); 53 54 private static final String CLASS_NAME = JahiaWebAppsDeployerService.class.getName (); 55 56 57 protected String m_ServerType = ""; 58 59 60 protected String m_ServerHomeDiskPath = ""; 61 62 63 protected static String m_WebAppRootPath = ""; 64 65 66 protected static String m_NewWebAppPath = ""; 67 68 69 protected static String m_JahiaHomeDiskPath = ""; 70 71 72 private static String m_SharedComponentsPath = ""; 73 74 75 76 protected int m_AppRight = 1; 78 79 protected int m_VisibleStatus = 1; 81 82 protected static String m_TempFolderDiskPath = ""; 83 84 85 protected static String m_TempFolderPrefix = "todelete_"; 86 87 88 protected static EntityResolver m_Resolver; 89 90 91 92 protected static final String m_WEB_INF = "WEB-INF"; 93 94 95 protected static final String m_META_INF = "Meta-Inf"; 96 97 98 105 protected static Hashtable m_WebAppsPackage = new Hashtable (); 106 107 108 109 private static final String WEB_XML_FILE = "WEB-INF/web.xml"; 110 111 112 117 public void init (SettingsBean jSettings) 118 throws JahiaInitializationException { 119 120 if (!isInitialized ()) { 121 122 m_ServerType = jSettings.getServer (); 123 m_ServerHomeDiskPath = jSettings.getServerHomeDiskPath (); 124 m_WebAppRootPath = jSettings.getJahiaWebAppsDiskPath (); 125 m_NewWebAppPath = jSettings.getJahiaNewWebAppsDiskPath (); 126 m_JahiaHomeDiskPath = jSettings.getJahiaHomeDiskPath (); 127 m_SharedComponentsPath = jSettings.getJahiaSharedComponentsDiskPath (); 128 m_Resolver = (EntityResolver ) jSettings.getDtdEntityResolver (); 129 130 logger.debug (" jahiaHomeDiskPath= " + m_JahiaHomeDiskPath); 131 132 File f = new File (m_NewWebAppPath); 134 File parent = f.getParentFile (); 135 if (parent != null) { 136 File tmpFolder = new File (parent.getAbsolutePath () + File.separator + "tmp"); 137 tmpFolder.mkdirs (); 138 if (tmpFolder == null || !tmpFolder.isDirectory ()) { 139 String errMsg = " cannot create a temporaty folder "; 140 logger.error (errMsg); 141 throw new JahiaInitializationException (errMsg); 142 } 143 m_TempFolderDiskPath = tmpFolder.getAbsolutePath (); 144 } 145 146 File sComponents = new File (m_SharedComponentsPath); 148 sComponents.mkdirs (); 149 150 mIsServiceInitialized = true; 151 } 152 153 } 155 156 157 164 165 166 179 public abstract boolean deploy (JahiaSite site, String context, String filePath) 180 throws JahiaException; 181 182 183 190 public abstract boolean undeploy (ApplicationBean app) throws JahiaException; 191 192 193 200 public abstract boolean deploy (JahiaSite site, Vector files); 201 202 208 public abstract boolean canDeploy (); 209 210 211 216 217 public static String getNewWebAppsPath () { 218 219 return m_NewWebAppPath; 220 221 } 222 223 224 public static String getWebAppsRootPath () { 225 226 return m_WebAppRootPath; 227 228 } 229 230 public static String getJahiaHomeDiskPath () { 231 232 return m_JahiaHomeDiskPath; 233 234 } 235 236 public static String getSharedComponentsPath () { 237 238 return m_SharedComponentsPath; 239 240 } 241 242 243 251 public void registerWebApps (JahiaSite site, String appContext, String filename, 252 Vector webApps) throws JahiaException { 253 254 int size = webApps.size (); 255 256 258 for (int i = 0; i < size; i++) { 259 260 JahiaWebAppDef webAppDef = (JahiaWebAppDef) webApps.get (i); 261 262 ApplicationBean theWebApp = ServicesRegistry.getInstance (). 264 getJahiaApplicationsManagerService (). 265 getApplication ("/" + appContext); 266 267 if (theWebApp == null) { 268 269 int parentAclID = site.getAclID (); 271 272 JahiaBaseACL acl = new JahiaBaseACL (); 274 if (acl != null) { 275 try { 276 acl.create (parentAclID); 277 } catch (ACLNotFoundException ex) { 278 throw new JahiaException ("Could not create the page def.", 279 "The parent ACL ID [" + parentAclID + "] could not be found," + 280 " while trying to create a new page def.", 281 JahiaException.TEMPLATE_ERROR, JahiaException.ERROR_SEVERITY); 282 } 283 } else { 284 throw new JahiaException ("Could not create page def.", 285 "Could not instanciate a new ACL object while trying to create a new page def.", 286 JahiaException.TEMPLATE_ERROR, JahiaException.CRITICAL_SEVERITY); 287 } 288 289 theWebApp = new ApplicationBean ( 291 0, site.getID (), 293 webAppDef.getName (), 294 "/" + appContext, 295 m_VisibleStatus, 296 false, acl.getID (), 298 filename, 299 webAppDef.getdesc () 300 ); 301 302 ServicesRegistry.getInstance ().getJahiaApplicationsManagerService () 303 .addDefinition (theWebApp); 304 305 307 } 308 } 309 } 310 311 321 public JahiaWebAppsPackage loadWebAppInfo (String path) { 322 323 synchronized (m_WebAppsPackage) { 324 325 JahiaWebAppsPackage pack = null; 326 327 if (!JahiaTools.checkFileNameCaseSensitive (path)) { 329 return null; 330 } 331 332 File f = new File (path); 333 334 try { 335 long fLength = f.length (); 337 Thread.sleep (500); 338 while (fLength != f.length ()) { 339 fLength = f.length (); 340 Thread.sleep (500); 341 } 342 } catch (Throwable tr) { 343 return null; 344 } 345 346 if (f != null && (f.isFile () || f.isDirectory ())) { 347 if (f.isDirectory ()) { 348 try { 349 pack = loadWebAppInfoFromDirectory (path); 350 } catch (JahiaException je) { 351 return null; 352 } 353 } else { 354 try { 355 pack = loadWebAppInfoFromFile (path); 356 } catch (JahiaException je) { 357 return null; 358 } 359 } 360 } 361 return pack; 362 } 363 } 364 365 366 373 protected JahiaWebAppsPackage loadWebAppInfoFromDirectory (String path) 374 throws JahiaException { 375 376 Web_App_Xml doc = null; 377 Vector vec = new Vector (); 378 JahiaWebAppsPackage pack = null; 379 380 doc = new Web_App_Xml (path + File.separator + WEB_XML_FILE); 382 383 doc.extractDocumentData (); 384 385 File tmpFile = new File (path); 387 388 389 String contextRoot = tmpFile.getName (); 390 int endIndex = contextRoot.lastIndexOf("."); 391 if(endIndex>0) 392 contextRoot = contextRoot.substring(0,endIndex); 393 pack = new JahiaWebAppsPackage (contextRoot); 395 396 Vector servlets = doc.getServlets (); 397 int size = servlets.size (); 398 399 Servlet_Element servlet = null; 400 401 JahiaWebAppDef webAppDef = new JahiaWebAppDef (doc.getDisplayName (), 402 contextRoot 403 ); 404 webAppDef.addRoles (doc.getRoles ()); 405 406 for (int i = 0; i < size; i++) { 407 408 servlet = (Servlet_Element) servlets.get (i); 409 webAppDef.addServlet (servlet); 410 } 411 412 pack.addWebAppDef (webAppDef); 413 414 if (pack.getWebApps ().size () > 0) { 415 416 pack.setFileName (tmpFile.getName ()); 417 pack.setFilePath (path); 418 if(tmpFile.isDirectory()) { 419 pack.setType(JahiaWebAppsPackage.DIR); 420 } 421 } else { 422 return null; 423 } 424 425 return pack; 426 } 427 428 429 436 protected JahiaWebAppsPackage loadWebAppInfoFromFile (String path) throws JahiaException { 437 438 File fileItem = new File (path); 439 440 JahiaWebAppsPackage pack = null; 441 442 if (fileItem != null && fileItem.getName ().endsWith (".war")) { 443 444 JahiaWebAppsWarPackage warPackage = loadWebAppInfoFromWar ( 445 fileItem.getAbsolutePath ()); 446 447 if (warPackage != null && warPackage.getWebApps ().size () > 0) { 448 449 pack = new JahiaWebAppsPackage (warPackage.getContextRoot ()); 450 pack.addWebAppDef (warPackage.getWebApps ()); 451 pack.setFileName (fileItem.getName ()); 452 pack.setFilePath (path); 453 } 454 455 } else if (fileItem != null && fileItem.getName ().endsWith (".ear")) { 456 457 JahiaWebAppsEarPackage earPackage = loadWebAppInfoFromEar ( 458 fileItem.getAbsolutePath ()); 459 460 if (earPackage != null && earPackage.getWebApps ().size () > 0) { 461 462 pack = new JahiaWebAppsPackage (earPackage.getContextRoot ()); 463 pack.addWebAppDef (earPackage.getWebApps ()); 464 pack.setFileName (fileItem.getName ()); 465 pack.setFilePath (path); 466 } 467 } 468 return pack; 469 } 470 471 472 479 protected JahiaWebAppsWarPackage loadWebAppInfoFromWar (String path) throws JahiaException { 480 481 File fileItem = new File (path); 482 483 if (fileItem != null && fileItem.getName ().endsWith (".war")) { 484 485 JahiaWebAppsWarHandler wah = null; 487 488 try { 489 wah = new JahiaWebAppsWarHandler (path); 490 JahiaWebAppsWarPackage warPackage = wah.getWebAppsPackage (); 491 return warPackage; 492 } finally { 493 494 if (wah != null) { 495 wah.closeArchiveFile (); 496 } 497 } 498 499 } 500 return null; 501 } 502 503 504 511 protected JahiaWebAppsEarPackage loadWebAppInfoFromEar (String path) throws JahiaException { 512 513 File fileItem = new File (path); 514 515 if (fileItem != null && fileItem.getName ().endsWith (".ear")) { 516 517 JahiaEarFileHandler earh = null; 519 520 try { 521 522 earh = new JahiaEarFileHandler (path); 523 524 JahiaWebAppsEarPackage earPackage = new JahiaWebAppsEarPackage ( 525 JahiaTools.removeFileExtension (fileItem.getName (), ".ear")); 526 527 Vector webComponents = earh.getWebComponents (); 529 530 int size = webComponents.size (); 531 532 Web_Component webComp = null; 533 String webURI = null; JahiaWebAppsWarPackage warPackage = null; 535 536 for (int i = 0; i < size; i++) { 537 538 webComp = (Web_Component) webComponents.get (i); 539 webURI = webComp.getWebURI (); 540 541 if (webURI != null && (webURI.length () > 0)) { 542 543 545 earh.extractEntry (webURI, m_TempFolderDiskPath); 547 548 File warFile = new File ( 550 m_TempFolderDiskPath + File.separator + webURI); 551 552 warPackage = loadWebAppInfoFromWar (warFile.getAbsolutePath ()); 554 if (warPackage != null && (warPackage.getWebApps ().size () > 0)) { 555 earPackage.addWebAppDefs ((Vector ) warPackage.getWebApps ()); 556 } 557 558 warFile.delete (); 559 } 560 } 561 562 return earPackage; 563 564 } catch (JahiaException e) { 565 566 String errMsg = "Failed handling webApps file "; 567 logger.error (errMsg, e); 568 if (earh != null) { 569 earh.closeArchiveFile (); 570 } 571 572 throw new JahiaException ( 573 "JahiaWebAppsDeployerBaseService::loadWebAppInfoFromEar()", 574 "JahiaWebAppsDeployerBaseService" + errMsg, 575 JahiaException.SERVICE_ERROR, JahiaException.ERROR_SEVERITY); 576 } finally { 577 578 if (earh != null) { 581 earh.closeArchiveFile (); 582 } 583 } 584 585 } 586 return null; 587 } 588 589 592 public EntityResolver getDtdEntityResolver () { 593 return (EntityResolver ) m_Resolver; 594 } 595 596 597 598 599 619 620 621 622 631 public boolean deletePackage (JahiaSite site, String path) { 632 633 634 synchronized (m_WebAppsPackage) { 635 636 File tmpFile = new File (path); 637 StringBuffer filename = new StringBuffer (site.getSiteKey ()); 638 filename.append ("_"); 639 filename.append (tmpFile.getName ()); 640 641 643 if (tmpFile != null && tmpFile.isFile ()) { 644 if (tmpFile.delete ()) { 645 m_WebAppsPackage.remove (filename.toString ()); 646 return true; 647 } else { 648 return false; 649 } 650 } else if (tmpFile != null && tmpFile.isDirectory ()) { 651 652 StringBuffer buff = new StringBuffer (m_TempFolderDiskPath); 653 buff.append (File.separator); 654 buff.append (m_TempFolderPrefix); 655 buff.append (filename.toString ()); 656 buff.append (JahiaTools.getUniqueDirName ()); 657 File tmpFolder = new File (buff.toString ()); 658 if (tmpFile.renameTo (tmpFolder)) { 660 m_WebAppsPackage.remove (filename.toString ()); 661 JahiaTools.deleteFile (tmpFolder); 663 return true; 664 } 665 666 } else { 667 m_WebAppsPackage.remove (filename.toString ()); 668 return true; 669 } 670 return false; 671 } 672 } 673 674 675 683 public void addNewFile (JahiaSite site, String path) { 684 685 686 synchronized (m_WebAppsPackage) { 687 688 File tmpFile = new File (path); 689 StringBuffer filename = new StringBuffer (site.getSiteKey ()); 690 filename.append ("_"); 691 filename.append (tmpFile.getName ()); 692 if (tmpFile != null) { 693 694 JahiaWebAppsPackage pack = null; 695 696 if (m_WebAppsPackage.get (filename.toString ()) == null) { 697 698 pack = loadWebAppInfo (path); 699 if (pack != null) { 700 m_WebAppsPackage.remove (filename.toString ()); 701 m_WebAppsPackage.put (filename.toString (), pack); 702 } 703 } 704 } 705 } 706 } 707 708 709 714 public Enumeration getWebAppsPackageKeys () { 715 716 synchronized (m_WebAppsPackage) { 717 return m_WebAppsPackage.keys (); 718 } 719 720 } 721 722 727 public Enumeration getWebAppsPackageKeys (String siteKey) { 728 729 Enumeration enumeration = null; 730 Vector result = new Vector (); 731 synchronized (m_WebAppsPackage) { 732 733 enumeration = m_WebAppsPackage.keys (); 734 String name = null; 735 String siteIdent = siteKey + "_"; 736 while (enumeration.hasMoreElements ()) { 737 name = (String ) enumeration.nextElement (); 738 if (name.startsWith (siteIdent)) { 739 result.add (siteIdent); 740 } 741 } 742 return result.elements (); 743 } 744 } 745 746 747 752 public Enumeration getWebAppsPackages () { 753 754 return m_WebAppsPackage.elements (); 755 } 756 757 758 763 public Enumeration getWebAppsPackages (String siteKey) { 764 765 Enumeration enumeration = null; 766 Vector result = new Vector (); 767 synchronized (m_WebAppsPackage) { 768 769 enumeration = m_WebAppsPackage.keys (); 770 String name = null; 771 String siteIdent = siteKey + "_"; 772 while (enumeration.hasMoreElements ()) { 773 name = (String ) enumeration.nextElement (); 774 if (name.startsWith (siteIdent)) { 775 result.add (m_WebAppsPackage.get (name)); 776 } 777 } 778 return result.elements (); 779 } 780 } 781 782 783 791 public Object getWebAppsPackage (String theKey) { 792 793 synchronized (m_WebAppsPackage) { 794 return m_WebAppsPackage.get (theKey); 795 } 796 } 797 798 799 807 public void scanDirectory (String path) 808 throws JahiaException { 809 810 synchronized (m_WebAppsPackage) { 811 812 JahiaWebAppsPackage pack = null; 813 814 File dir = new File (path); 815 if (dir != null && dir.isDirectory ()) { 816 817 File [] files = dir.listFiles (); 818 int size = files.length; 819 820 for (int i = 0; i < size; i++) { 821 822 if (files[i].canWrite ()) { 823 824 pack = loadWebAppInfo (files[i].getAbsolutePath ()); 825 if (pack != null) { 826 m_WebAppsPackage.remove (files[i].getName ()); 829 m_WebAppsPackage.put (files[i].getName (), pack); 831 } 832 } 833 } 834 } 835 } 836 } 837 838 839 840 841 849 protected boolean removeMetaInfFolder (String parentPath) { 850 851 if (parentPath == null) { 852 return false; 853 } 854 855 File f = new File (parentPath); 856 File [] files = f.listFiles (); 857 for (int i = 0; i < files.length; i++) { 858 if (files[i].getName ().equalsIgnoreCase (m_META_INF)) { 859 try { 860 return JahiaTools.deleteFile (files[i], false); 861 } catch (Throwable t) { 862 t.printStackTrace (); 863 return false; 864 } 865 } 866 } 867 868 return false; 869 } 870 871 872 } | Popular Tags |