1 17 18 19 package org.apache.catalina.startup; 20 21 22 import java.io.BufferedOutputStream ; 23 import java.io.File ; 24 import java.io.FileOutputStream ; 25 import java.io.IOException ; 26 import java.io.InputStream ; 27 import java.util.ArrayList ; 28 import java.util.HashMap ; 29 import java.util.LinkedHashMap ; 30 import java.util.jar.JarEntry ; 31 import java.util.jar.JarFile ; 32 33 import javax.management.ObjectName ; 34 35 import org.apache.catalina.Container; 36 import org.apache.catalina.Context; 37 import org.apache.catalina.Engine; 38 import org.apache.catalina.Host; 39 import org.apache.catalina.Lifecycle; 40 import org.apache.catalina.LifecycleEvent; 41 import org.apache.catalina.LifecycleListener; 42 import org.apache.catalina.core.ContainerBase; 43 import org.apache.catalina.core.StandardHost; 44 import org.apache.catalina.util.StringManager; 45 import org.apache.tomcat.util.digester.Digester; 46 import org.apache.tomcat.util.modeler.Registry; 47 48 49 57 public class HostConfig 58 implements LifecycleListener { 59 60 protected static org.apache.commons.logging.Log log= 61 org.apache.commons.logging.LogFactory.getLog( HostConfig.class ); 62 63 65 66 69 protected File appBase = null; 70 71 72 75 protected File configBase = null; 76 77 78 81 protected String configClass = "org.apache.catalina.startup.ContextConfig"; 82 83 84 87 protected String contextClass = "org.apache.catalina.core.StandardContext"; 88 89 90 93 protected Host host = null; 94 95 96 99 protected ObjectName oname = null; 100 101 102 105 protected static final StringManager sm = 106 StringManager.getManager(Constants.Package); 107 108 109 112 protected boolean deployXML = false; 113 114 115 119 protected boolean unpackWARs = false; 120 121 122 125 protected HashMap deployed = new HashMap (); 126 127 128 132 protected ArrayList serviced = new ArrayList (); 133 134 135 138 protected boolean xmlValidation = false; 139 140 141 144 protected boolean xmlNamespaceAware = false; 145 146 147 150 protected static Digester digester = createDigester(); 151 152 153 155 156 159 public String getConfigClass() { 160 161 return (this.configClass); 162 163 } 164 165 166 171 public void setConfigClass(String configClass) { 172 173 this.configClass = configClass; 174 175 } 176 177 178 181 public String getContextClass() { 182 183 return (this.contextClass); 184 185 } 186 187 188 193 public void setContextClass(String contextClass) { 194 195 this.contextClass = contextClass; 196 197 } 198 199 200 203 public boolean isDeployXML() { 204 205 return (this.deployXML); 206 207 } 208 209 210 215 public void setDeployXML(boolean deployXML) { 216 217 this.deployXML= deployXML; 218 219 } 220 221 222 225 public boolean isUnpackWARs() { 226 227 return (this.unpackWARs); 228 229 } 230 231 232 237 public void setUnpackWARs(boolean unpackWARs) { 238 239 this.unpackWARs = unpackWARs; 240 241 } 242 243 244 249 public void setXmlValidation(boolean xmlValidation){ 250 this.xmlValidation = xmlValidation; 251 } 252 253 258 public boolean getXmlValidation(){ 259 return xmlValidation; 260 } 261 262 267 public boolean getXmlNamespaceAware(){ 268 return xmlNamespaceAware; 269 } 270 271 272 277 public void setXmlNamespaceAware(boolean xmlNamespaceAware){ 278 this.xmlNamespaceAware=xmlNamespaceAware; 279 } 280 281 282 284 285 290 public void lifecycleEvent(LifecycleEvent event) { 291 292 if (event.getType().equals(Lifecycle.PERIODIC_EVENT)) 293 check(); 294 295 try { 297 host = (Host) event.getLifecycle(); 298 if (host instanceof StandardHost) { 299 setDeployXML(((StandardHost) host).isDeployXML()); 300 setUnpackWARs(((StandardHost) host).isUnpackWARs()); 301 setXmlNamespaceAware(((StandardHost) host).getXmlNamespaceAware()); 302 setXmlValidation(((StandardHost) host).getXmlValidation()); 303 } 304 } catch (ClassCastException e) { 305 log.error(sm.getString("hostConfig.cce", event.getLifecycle()), e); 306 return; 307 } 308 309 if (event.getType().equals(Lifecycle.START_EVENT)) 311 start(); 312 else if (event.getType().equals(Lifecycle.STOP_EVENT)) 313 stop(); 314 315 } 316 317 318 321 public synchronized void addServiced(String name) { 322 serviced.add(name); 323 } 324 325 326 330 public synchronized boolean isServiced(String name) { 331 return (serviced.contains(name)); 332 } 333 334 335 338 public synchronized void removeServiced(String name) { 339 serviced.remove(name); 340 } 341 342 343 348 public long getDeploymentTime(String name) { 349 DeployedApplication app = (DeployedApplication) deployed.get(name); 350 if (app == null) { 351 return 0L; 352 } else { 353 return app.timestamp; 354 } 355 } 356 357 358 365 public boolean isDeployed(String name) { 366 DeployedApplication app = (DeployedApplication) deployed.get(name); 367 if (app == null) { 368 return false; 369 } else { 370 return true; 371 } 372 } 373 374 375 377 378 381 protected static Digester createDigester() { 382 Digester digester = new Digester(); 383 digester.setValidating(false); 384 digester.addObjectCreate("Context", "org.apache.catalina.core.StandardContext", 386 "className"); 387 digester.addSetProperties("Context"); 390 return (digester); 391 } 392 393 394 398 protected File appBase() { 399 400 if (appBase != null) { 401 return appBase; 402 } 403 404 File file = new File (host.getAppBase()); 405 if (!file.isAbsolute()) 406 file = new File (System.getProperty("catalina.base"), 407 host.getAppBase()); 408 try { 409 appBase = file.getCanonicalFile(); 410 } catch (IOException e) { 411 appBase = file; 412 } 413 return (appBase); 414 415 } 416 417 418 422 protected File configBase() { 423 424 if (configBase != null) { 425 return configBase; 426 } 427 428 File file = new File (System.getProperty("catalina.base"), "conf"); 429 Container parent = host.getParent(); 430 if ((parent != null) && (parent instanceof Engine)) { 431 file = new File (file, parent.getName()); 432 } 433 file = new File (file, host.getName()); 434 try { 435 configBase = file.getCanonicalFile(); 436 } catch (IOException e) { 437 configBase = file; 438 } 439 return (configBase); 440 441 } 442 443 447 public String getConfigBaseName() { 448 return configBase().getAbsolutePath(); 449 } 450 451 454 protected String getConfigFile(String path) { 455 String basename = null; 456 if (path.equals("")) { 457 basename = "ROOT"; 458 } else { 459 basename = path.substring(1).replace('/', '#'); 460 } 461 return (basename); 462 } 463 464 465 468 protected String getDocBase(String path) { 469 String basename = null; 470 if (path.equals("")) { 471 basename = "ROOT"; 472 } else { 473 basename = path.substring(1); 474 } 475 return (basename); 476 } 477 478 479 483 protected void deployApps() { 484 485 File appBase = appBase(); 486 File configBase = configBase(); 487 deployDescriptors(configBase, configBase.list()); 489 deployWARs(appBase, appBase.list()); 491 deployDirectories(appBase, appBase.list()); 493 494 } 495 496 497 501 protected void deployApps(String name) { 502 503 File appBase = appBase(); 504 File configBase = configBase(); 505 String baseName = getConfigFile(name); 506 String docBase = getConfigFile(name); 507 508 File xml = new File (configBase, baseName + ".xml"); 510 if (xml.exists()) 511 deployDescriptor(name, xml, baseName + ".xml"); 512 File war = new File (appBase, docBase + ".war"); 514 if (war.exists()) 515 deployWAR(name, war, docBase + ".war"); 516 File dir = new File (appBase, docBase); 518 if (dir.exists()) 519 deployDirectory(name, dir, docBase); 520 521 } 522 523 524 527 protected void deployDescriptors(File configBase, String [] files) { 528 529 if (files == null) 530 return; 531 532 for (int i = 0; i < files.length; i++) { 533 534 if (files[i].equalsIgnoreCase("META-INF")) 535 continue; 536 if (files[i].equalsIgnoreCase("WEB-INF")) 537 continue; 538 File contextXml = new File (configBase, files[i]); 539 if (files[i].toLowerCase().endsWith(".xml")) { 540 541 String nameTmp = files[i].substring(0, files[i].length() - 4); 543 String contextPath = "/" + nameTmp.replace('#', '/'); 544 if (nameTmp.equals("ROOT")) { 545 contextPath = ""; 546 } 547 548 if (isServiced(contextPath)) 549 continue; 550 551 String file = files[i]; 552 553 deployDescriptor(contextPath, contextXml, file); 554 555 } 556 557 } 558 559 } 560 561 562 567 protected void deployDescriptor(String contextPath, File contextXml, String file) { 568 if (deploymentExists(contextPath)) { 569 return; 570 } 571 572 DeployedApplication deployedApp = new DeployedApplication(contextPath); 573 574 if(log.isDebugEnabled()) { 576 log.debug(sm.getString("hostConfig.deployDescriptor", file)); 577 } 578 579 Context context = null; 580 try { 581 synchronized (digester) { 582 try { 583 context = (Context) digester.parse(contextXml); 584 if (context == null) { 585 log.error(sm.getString("hostConfig.deployDescriptor.error", 586 file)); 587 return; 588 } 589 } finally { 590 digester.reset(); 591 } 592 } 593 if (context instanceof Lifecycle) { 594 Class clazz = Class.forName(host.getConfigClass()); 595 LifecycleListener listener = 596 (LifecycleListener) clazz.newInstance(); 597 ((Lifecycle) context).addLifecycleListener(listener); 598 } 599 context.setConfigFile(contextXml.getAbsolutePath()); 600 context.setPath(contextPath); 601 boolean isWar = false; 603 boolean isExternal = false; 604 if (context.getDocBase() != null) { 605 File docBase = new File (context.getDocBase()); 606 if (!docBase.isAbsolute()) { 607 docBase = new File (appBase(), context.getDocBase()); 608 } 609 if (!docBase.getCanonicalPath().startsWith(appBase().getAbsolutePath())) { 611 isExternal = true; 612 deployedApp.redeployResources.put 613 (contextXml.getAbsolutePath(), new Long (contextXml.lastModified())); 614 deployedApp.redeployResources.put(docBase.getAbsolutePath(), 615 new Long (docBase.lastModified())); 616 if (docBase.getAbsolutePath().toLowerCase().endsWith(".war")) { 617 isWar = true; 618 } 619 } else { 620 log.warn(sm.getString("hostConfig.deployDescriptor.localDocBaseSpecified", 621 docBase)); 622 context.setDocBase(null); 624 } 625 } 626 host.addChild(context); 627 String name = null; 629 String path = context.getPath(); 630 if (path.equals("")) { 631 name = "ROOT"; 632 } else { 633 if (path.startsWith("/")) { 634 name = path.substring(1); 635 } else { 636 name = path; 637 } 638 } 639 File expandedDocBase = new File (name); 640 File warDocBase = new File (name + ".war"); 641 if (!expandedDocBase.isAbsolute()) { 642 expandedDocBase = new File (appBase(), name); 643 warDocBase = new File (appBase(), name + ".war"); 644 } 645 if (isWar && unpackWARs) { 648 deployedApp.redeployResources.put(expandedDocBase.getAbsolutePath(), 649 new Long (expandedDocBase.lastModified())); 650 deployedApp.redeployResources.put 651 (contextXml.getAbsolutePath(), new Long (contextXml.lastModified())); 652 addWatchedResources(deployedApp, expandedDocBase.getAbsolutePath(), context); 653 } else { 654 if (warDocBase.exists()) { 656 deployedApp.redeployResources.put(warDocBase.getAbsolutePath(), 657 new Long (warDocBase.lastModified())); 658 } 659 if (expandedDocBase.exists()) { 660 deployedApp.redeployResources.put(expandedDocBase.getAbsolutePath(), 661 new Long (expandedDocBase.lastModified())); 662 addWatchedResources(deployedApp, 663 expandedDocBase.getAbsolutePath(), context); 664 } else { 665 addWatchedResources(deployedApp, null, context); 666 } 667 if (!isExternal) { 669 deployedApp.redeployResources.put 670 (contextXml.getAbsolutePath(), new Long (contextXml.lastModified())); 671 } 672 } 673 } catch (Throwable t) { 674 log.error(sm.getString("hostConfig.deployDescriptor.error", 675 file), t); 676 } 677 678 if (context != null && host.findChild(context.getName()) != null) { 679 deployed.put(contextPath, deployedApp); 680 } 681 } 682 683 684 687 protected void deployWARs(File appBase, String [] files) { 688 689 if (files == null) 690 return; 691 692 for (int i = 0; i < files.length; i++) { 693 694 if (files[i].equalsIgnoreCase("META-INF")) 695 continue; 696 if (files[i].equalsIgnoreCase("WEB-INF")) 697 continue; 698 File dir = new File (appBase, files[i]); 699 if (files[i].toLowerCase().endsWith(".war")) { 700 701 String contextPath = "/" + files[i]; 703 int period = contextPath.lastIndexOf("."); 704 if (period >= 0) 705 contextPath = contextPath.substring(0, period); 706 if (contextPath.equals("/ROOT")) 707 contextPath = ""; 708 709 if (isServiced(contextPath)) 710 continue; 711 712 String file = files[i]; 713 714 deployWAR(contextPath, dir, file); 715 716 } 717 718 } 719 720 } 721 722 723 728 protected void deployWAR(String contextPath, File dir, String file) { 729 730 if (deploymentExists(contextPath)) 731 return; 732 733 JarFile jar = null; 735 JarEntry entry = null; 736 InputStream istream = null; 737 BufferedOutputStream ostream = null; 738 File xml = new File 739 (configBase, file.substring(0, file.lastIndexOf(".")) + ".xml"); 740 if (deployXML && !xml.exists()) { 741 try { 742 jar = new JarFile (dir); 743 entry = jar.getJarEntry(Constants.ApplicationContextXml); 744 if (entry != null) { 745 istream = jar.getInputStream(entry); 746 747 configBase.mkdirs(); 748 749 ostream = 750 new BufferedOutputStream 751 (new FileOutputStream (xml), 1024); 752 byte buffer[] = new byte[1024]; 753 while (true) { 754 int n = istream.read(buffer); 755 if (n < 0) { 756 break; 757 } 758 ostream.write(buffer, 0, n); 759 } 760 ostream.flush(); 761 ostream.close(); 762 ostream = null; 763 istream.close(); 764 istream = null; 765 entry = null; 766 jar.close(); 767 jar = null; 768 } 769 } catch (Exception e) { 770 if (ostream != null) { 772 try { 773 ostream.close(); 774 } catch (Throwable t) { 775 ; 776 } 777 ostream = null; 778 } 779 if (istream != null) { 780 try { 781 istream.close(); 782 } catch (Throwable t) { 783 ; 784 } 785 istream = null; 786 } 787 } finally { 788 entry = null; 789 if (jar != null) { 790 try { 791 jar.close(); 792 } catch (Throwable t) { 793 ; 794 } 795 jar = null; 796 } 797 } 798 } 799 800 DeployedApplication deployedApp = new DeployedApplication(contextPath); 801 802 if(log.isInfoEnabled()) 804 log.info(sm.getString("hostConfig.deployJar", file)); 805 806 deployedApp.redeployResources.put 808 (dir.getAbsolutePath(), new Long (dir.lastModified())); 809 810 try { 811 Context context = (Context) Class.forName(contextClass).newInstance(); 812 if (context instanceof Lifecycle) { 813 Class clazz = Class.forName(host.getConfigClass()); 814 LifecycleListener listener = 815 (LifecycleListener) clazz.newInstance(); 816 ((Lifecycle) context).addLifecycleListener(listener); 817 } 818 context.setPath(contextPath); 819 context.setDocBase(file); 820 if (xml.exists()) { 821 context.setConfigFile(xml.getAbsolutePath()); 822 deployedApp.redeployResources.put 823 (xml.getAbsolutePath(), new Long (xml.lastModified())); 824 } 825 host.addChild(context); 826 if (unpackWARs && (context.getDocBase() != null)) { 829 String name = null; 830 String path = context.getPath(); 831 if (path.equals("")) { 832 name = "ROOT"; 833 } else { 834 if (path.startsWith("/")) { 835 name = path.substring(1); 836 } else { 837 name = path; 838 } 839 } 840 File docBase = new File (name); 841 if (!docBase.isAbsolute()) { 842 docBase = new File (appBase(), name); 843 } 844 deployedApp.redeployResources.put(docBase.getAbsolutePath(), 845 new Long (docBase.lastModified())); 846 addWatchedResources(deployedApp, docBase.getAbsolutePath(), context); 847 } else { 848 addWatchedResources(deployedApp, null, context); 849 } 850 } catch (Throwable t) { 851 log.error(sm.getString("hostConfig.deployJar.error", file), t); 852 } 853 854 deployed.put(contextPath, deployedApp); 855 } 856 857 858 861 protected void deployDirectories(File appBase, String [] files) { 862 863 if (files == null) 864 return; 865 866 for (int i = 0; i < files.length; i++) { 867 868 if (files[i].equalsIgnoreCase("META-INF")) 869 continue; 870 if (files[i].equalsIgnoreCase("WEB-INF")) 871 continue; 872 File dir = new File (appBase, files[i]); 873 if (dir.isDirectory()) { 874 875 String contextPath = "/" + files[i]; 877 if (files[i].equals("ROOT")) 878 contextPath = ""; 879 880 if (isServiced(contextPath)) 881 continue; 882 883 deployDirectory(contextPath, dir, files[i]); 884 885 } 886 887 } 888 889 } 890 891 892 897 protected void deployDirectory(String contextPath, File dir, String file) { 898 DeployedApplication deployedApp = new DeployedApplication(contextPath); 899 900 if (deploymentExists(contextPath)) 901 return; 902 903 if( log.isDebugEnabled() ) 905 log.debug(sm.getString("hostConfig.deployDir", file)); 906 try { 907 Context context = (Context) Class.forName(contextClass).newInstance(); 908 if (context instanceof Lifecycle) { 909 Class clazz = Class.forName(host.getConfigClass()); 910 LifecycleListener listener = 911 (LifecycleListener) clazz.newInstance(); 912 ((Lifecycle) context).addLifecycleListener(listener); 913 } 914 context.setPath(contextPath); 915 context.setDocBase(file); 916 File configFile = new File (dir, Constants.ApplicationContextXml); 917 if (deployXML) { 918 context.setConfigFile(configFile.getAbsolutePath()); 919 } 920 host.addChild(context); 921 deployedApp.redeployResources.put(dir.getAbsolutePath(), 922 new Long (dir.lastModified())); 923 if (deployXML) { 924 deployedApp.redeployResources.put(configFile.getAbsolutePath(), 925 new Long (configFile.lastModified())); 926 } 927 addWatchedResources(deployedApp, dir.getAbsolutePath(), context); 928 } catch (Throwable t) { 929 log.error(sm.getString("hostConfig.deployDir.error", file), t); 930 } 931 932 deployed.put(contextPath, deployedApp); 933 } 934 935 936 941 protected boolean deploymentExists(String contextPath) { 942 return (deployed.containsKey(contextPath) || (host.findChild(contextPath) != null)); 943 } 944 945 946 952 protected void addWatchedResources(DeployedApplication app, String docBase, Context context) { 953 File docBaseFile = null; 956 if (docBase != null) { 957 docBaseFile = new File (docBase); 958 if (!docBaseFile.isAbsolute()) { 959 docBaseFile = new File (appBase(), docBase); 960 } 961 } 962 String [] watchedResources = context.findWatchedResources(); 963 for (int i = 0; i < watchedResources.length; i++) { 964 File resource = new File (watchedResources[i]); 965 if (!resource.isAbsolute()) { 966 if (docBase != null) { 967 resource = new File (docBaseFile, watchedResources[i]); 968 } else { 969 continue; 970 } 971 } 972 app.reloadResources.put(resource.getAbsolutePath(), 973 new Long (resource.lastModified())); 974 } 975 } 976 977 978 981 protected synchronized void checkResources(DeployedApplication app) { 982 String [] resources = (String []) app.redeployResources.keySet().toArray(new String [0]); 983 for (int i = 0; i < resources.length; i++) { 984 File resource = new File (resources[i]); 985 if (log.isDebugEnabled()) 986 log.debug("Checking context[" + app.name + "] redeploy resource " + resource); 987 if (resource.exists()) { 988 long lastModified = ((Long ) app.redeployResources.get(resources[i])).longValue(); 989 if ((!resource.isDirectory()) && resource.lastModified() > lastModified) { 990 if (log.isInfoEnabled()) 992 log.info(sm.getString("hostConfig.undeploy", app.name)); 993 ContainerBase context = (ContainerBase) host.findChild(app.name); 994 try { 995 host.removeChild(context); 996 } catch (Throwable t) { 997 log.warn(sm.getString 998 ("hostConfig.context.remove", app.name), t); 999 } 1000 try { 1001 context.destroy(); 1002 } catch (Throwable t) { 1003 log.warn(sm.getString 1004 ("hostConfig.context.destroy", app.name), t); 1005 } 1006 for (int j = i + 1; j < resources.length; j++) { 1008 try { 1009 File current = new File (resources[j]); 1010 current = current.getCanonicalFile(); 1011 if ((current.getAbsolutePath().startsWith(appBase().getAbsolutePath())) 1012 || (current.getAbsolutePath().startsWith(configBase().getAbsolutePath()))) { 1013 if (log.isDebugEnabled()) 1014 log.debug("Delete " + current); 1015 ExpandWar.delete(current); 1016 } 1017 } catch (IOException e) { 1018 log.warn(sm.getString 1019 ("hostConfig.canonicalizing", app.name), e); 1020 } 1021 } 1022 deployed.remove(app.name); 1023 return; 1024 } 1025 } else { 1026 long lastModified = ((Long ) app.redeployResources.get(resources[i])).longValue(); 1027 if (lastModified == 0L) { 1028 continue; 1029 } 1030 if (log.isInfoEnabled()) 1032 log.info(sm.getString("hostConfig.undeploy", app.name)); 1033 ContainerBase context = (ContainerBase) host.findChild(app.name); 1034 try { 1035 host.removeChild(context); 1036 } catch (Throwable t) { 1037 log.warn(sm.getString 1038 ("hostConfig.context.remove", app.name), t); 1039 } 1040 try { 1041 context.destroy(); 1042 } catch (Throwable t) { 1043 log.warn(sm.getString 1044 ("hostConfig.context.destroy", app.name), t); 1045 } 1046 for (int j = i + 1; j < resources.length; j++) { 1048 try { 1049 File current = new File (resources[j]); 1050 current = current.getCanonicalFile(); 1051 if ((current.getAbsolutePath().startsWith(appBase().getAbsolutePath())) 1052 || (current.getAbsolutePath().startsWith(configBase().getAbsolutePath()))) { 1053 if (log.isDebugEnabled()) 1054 log.debug("Delete " + current); 1055 ExpandWar.delete(current); 1056 } 1057 } catch (IOException e) { 1058 log.warn(sm.getString 1059 ("hostConfig.canonicalizing", app.name), e); 1060 } 1061 } 1062 String [] resources2 = (String []) app.reloadResources.keySet().toArray(new String [0]); 1064 for (int j = 0; j < resources2.length; j++) { 1065 try { 1066 File current = new File (resources2[j]); 1067 current = current.getCanonicalFile(); 1068 if ((current.getAbsolutePath().startsWith(appBase().getAbsolutePath())) 1069 || ((current.getAbsolutePath().startsWith(configBase().getAbsolutePath()) 1070 && (current.getAbsolutePath().endsWith(".xml"))))) { 1071 if (log.isDebugEnabled()) 1072 log.debug("Delete " + current); 1073 ExpandWar.delete(current); 1074 } 1075 } catch (IOException e) { 1076 log.warn(sm.getString 1077 ("hostConfig.canonicalizing", app.name), e); 1078 } 1079 } 1080 deployed.remove(app.name); 1081 return; 1082 } 1083 } 1084 resources = (String []) app.reloadResources.keySet().toArray(new String [0]); 1085 for (int i = 0; i < resources.length; i++) { 1086 File resource = new File (resources[i]); 1087 if (log.isDebugEnabled()) 1088 log.debug("Checking context[" + app.name + "] reload resource " + resource); 1089 long lastModified = ((Long ) app.reloadResources.get(resources[i])).longValue(); 1090 if ((!resource.exists() && lastModified != 0L) 1091 || (resource.lastModified() != lastModified)) { 1092 if(log.isInfoEnabled()) 1094 log.info(sm.getString("hostConfig.reload", app.name)); 1095 Container context = host.findChild(app.name); 1096 try { 1097 ((Lifecycle) context).stop(); 1098 } catch (Exception e) { 1099 log.warn(sm.getString 1100 ("hostConfig.context.restart", app.name), e); 1101 } 1102 try { 1105 ((Lifecycle) context).start(); 1106 } catch (Exception e) { 1107 log.warn(sm.getString 1108 ("hostConfig.context.restart", app.name), e); 1109 } 1110 app.reloadResources.put(resources[i], new Long (resource.lastModified())); 1112 app.timestamp = System.currentTimeMillis(); 1113 return; 1114 } 1115 } 1116 } 1117 1118 1119 1122 public void start() { 1123 1124 if (log.isDebugEnabled()) 1125 log.debug(sm.getString("hostConfig.start")); 1126 1127 try { 1128 ObjectName hostON = new ObjectName (host.getObjectName()); 1129 oname = new ObjectName 1130 (hostON.getDomain() + ":type=Deployer,host=" + host.getName()); 1131 Registry.getRegistry(null, null).registerComponent 1132 (this, oname, this.getClass().getName()); 1133 } catch (Exception e) { 1134 log.error(sm.getString("hostConfig.jmx.register", oname), e); 1135 } 1136 1137 if (host.getDeployOnStartup()) 1138 deployApps(); 1139 1140 } 1141 1142 1143 1146 public void stop() { 1147 1148 if (log.isDebugEnabled()) 1149 log.debug(sm.getString("hostConfig.stop")); 1150 1151 undeployApps(); 1152 1153 if (oname != null) { 1154 try { 1155 Registry.getRegistry(null, null).unregisterComponent(oname); 1156 } catch (Exception e) { 1157 log.error(sm.getString("hostConfig.jmx.unregister", oname), e); 1158 } 1159 } 1160 oname = null; 1161 appBase = null; 1162 configBase = null; 1163 1164 } 1165 1166 1167 1170 protected void undeployApps() { 1171 1172 if (log.isDebugEnabled()) 1173 log.debug(sm.getString("hostConfig.undeploying")); 1174 1175 DeployedApplication[] apps = 1177 (DeployedApplication[]) deployed.values().toArray(new DeployedApplication[0]); 1178 for (int i = 0; i < apps.length; i++) { 1179 try { 1180 host.removeChild(host.findChild(apps[i].name)); 1181 } catch (Throwable t) { 1182 log.warn(sm.getString 1183 ("hostConfig.context.remove", apps[i].name), t); 1184 } 1185 } 1186 1187 deployed.clear(); 1188 1189 } 1190 1191 1192 1195 protected void check() { 1196 1197 if (host.getAutoDeploy()) { 1198 DeployedApplication[] apps = 1200 (DeployedApplication[]) deployed.values().toArray(new DeployedApplication[0]); 1201 for (int i = 0; i < apps.length; i++) { 1202 if (!isServiced(apps[i].name)) 1203 checkResources(apps[i]); 1204 } 1205 deployApps(); 1207 } 1208 1209 } 1210 1211 1212 1215 public void check(String name) { 1216 DeployedApplication app = (DeployedApplication) deployed.get(name); 1217 if (app != null) { 1218 checkResources(app); 1219 } else { 1220 deployApps(name); 1221 } 1222 } 1223 1224 1228 public void manageApp(Context context) { 1229 1230 String contextPath = context.getPath(); 1231 1232 if (deployed.containsKey(contextPath)) 1233 return; 1234 1235 DeployedApplication deployedApp = new DeployedApplication(contextPath); 1236 1237 boolean isWar = false; 1239 if (context.getDocBase() != null) { 1240 File docBase = new File (context.getDocBase()); 1241 if (!docBase.isAbsolute()) { 1242 docBase = new File (appBase(), context.getDocBase()); 1243 } 1244 deployedApp.redeployResources.put(docBase.getAbsolutePath(), 1245 new Long (docBase.lastModified())); 1246 if (docBase.getAbsolutePath().toLowerCase().endsWith(".war")) { 1247 isWar = true; 1248 } 1249 } 1250 host.addChild(context); 1251 if (isWar && unpackWARs) { 1254 String name = null; 1255 String path = context.getPath(); 1256 if (path.equals("")) { 1257 name = "ROOT"; 1258 } else { 1259 if (path.startsWith("/")) { 1260 name = path.substring(1); 1261 } else { 1262 name = path; 1263 } 1264 } 1265 File docBase = new File (name); 1266 if (!docBase.isAbsolute()) { 1267 docBase = new File (appBase(), name); 1268 } 1269 deployedApp.redeployResources.put(docBase.getAbsolutePath(), 1270 new Long (docBase.lastModified())); 1271 addWatchedResources(deployedApp, docBase.getAbsolutePath(), context); 1272 } else { 1273 addWatchedResources(deployedApp, null, context); 1274 } 1275 deployed.put(contextPath, deployedApp); 1276 } 1277 1278 1282 public void unmanageApp(String contextPath) { 1283 if(isServiced(contextPath)) { 1284 deployed.remove(contextPath); 1285 host.removeChild(host.findChild(contextPath)); 1286 } 1287 } 1288 1289 1291 1292 1296 protected class DeployedApplication { 1297 public DeployedApplication(String name) { 1298 this.name = name; 1299 } 1300 1301 1305 public String name; 1306 1307 1314 public LinkedHashMap redeployResources = new LinkedHashMap (); 1315 1316 1323 public HashMap reloadResources = new HashMap (); 1324 1325 1328 public long timestamp = System.currentTimeMillis(); 1329 } 1330 1331} 1332 | Popular Tags |