1 2 3 27 28 29 package org.apache.catalina.core; 30 31 32 import java.io.BufferedOutputStream ; 33 import java.io.File ; 34 import java.io.FileOutputStream ; 35 import java.io.InputStream ; 36 import java.io.IOException ; 37 import java.net.URL ; 38 import java.util.Enumeration ; 39 import org.apache.catalina.Container; 40 import org.apache.catalina.Context; 41 import org.apache.catalina.Deployer; 42 import org.apache.catalina.Engine; 43 import org.apache.catalina.Globals; 44 import org.apache.catalina.Lifecycle; 45 import org.apache.catalina.LifecycleException; 46 import org.apache.catalina.LifecycleListener; 47 import org.apache.catalina.Host; 48 import org.apache.catalina.core.StandardServer; 49 import org.apache.catalina.startup.ContextRuleSet; 50 import org.apache.catalina.startup.ExpandWar; 51 import org.apache.catalina.startup.NamingRuleSet; 52 import org.apache.catalina.util.StringManager; 53 import com.sun.org.apache.commons.digester.Digester; 54 import org.xml.sax.SAXParseException ; 55 56 57 64 65 public class StandardHostDeployer implements Deployer { 66 67 private static com.sun.org.apache.commons.logging.Log log= 68 com.sun.org.apache.commons.logging.LogFactory.getLog( StandardHostDeployer.class ); 69 70 72 public StandardHostDeployer() { 73 } 74 75 81 public StandardHostDeployer(StandardHost host) { 82 83 super(); 84 this.host = host; 85 86 } 87 88 89 91 92 96 private ContextRuleSet contextRuleSet = null; 97 98 99 105 private Digester digester = null; 106 107 108 111 protected StandardHost host = null; 112 113 114 118 private NamingRuleSet namingRuleSet = null; 119 120 121 126 private String overrideDocBase = null; 127 128 129 135 private String overrideConfigFile = null; 136 137 138 141 protected static final StringManager sm = 142 StringManager.getManager(Constants.Package); 143 144 145 147 public Host getHost() { 148 return host; 149 } 150 151 public void setHost(Host host) { 152 this.host = (StandardHost)host; 153 } 154 155 158 public String getName() { 159 160 return (host.getName()); 161 162 } 163 164 165 192 public synchronized void install(String contextPath, URL war) 193 throws IOException { 194 195 if (contextPath == null) 197 throw new IllegalArgumentException 198 (sm.getString("standardHost.pathRequired")); 199 if (!contextPath.equals("") && !contextPath.startsWith("/")) 200 throw new IllegalArgumentException 201 (sm.getString("standardHost.pathFormat", contextPath)); 202 if (findDeployedApp(contextPath) != null) 203 throw new IllegalStateException 204 (sm.getString("standardHost.pathUsed", contextPath)); 205 if (war == null) 206 throw new IllegalArgumentException 207 (sm.getString("standardHost.warRequired")); 208 209 log.info(sm.getString("standardHost.installing", 211 contextPath, war.toString())); 212 String url = war.toString(); 213 String docBase = null; 214 boolean isWAR = false; 215 if (url.startsWith("jar:")) { 216 url = url.substring(4, url.length() - 2); 217 if (!url.toLowerCase().endsWith(".war")) { 218 throw new IllegalArgumentException 219 (sm.getString("standardHost.warURL", url)); 220 } 221 isWAR = true; 222 } 223 if (url.startsWith("file://")) 224 docBase = url.substring(7); 225 else if (url.startsWith("file:")) 226 docBase = url.substring(5); 227 else 228 throw new IllegalArgumentException 229 (sm.getString("standardHost.warURL", url)); 230 231 boolean isAppBase = false; 233 File appBase = new File (host.getAppBase()); 234 if (!appBase.isAbsolute()) 235 appBase = new File (System.getProperty("catalina.base"), 236 host.getAppBase()); 237 File contextFile = new File (docBase); 238 File baseDir = contextFile.getParentFile(); 239 if (appBase.getCanonicalPath().equals(baseDir.getCanonicalPath())) { 240 isAppBase = true; 241 } 242 243 if (!host.isDeployXML() && !isAppBase) { 246 throw new IllegalArgumentException 247 (sm.getString("standardHost.installBase", url)); 248 } 249 250 if (isAppBase && host.getAutoDeploy()) { 253 String filename = contextFile.getName(); 254 if (isWAR) { 255 filename = filename.substring(0,filename.length()-4); 256 } 257 if (contextPath.length() == 0) { 258 if (!filename.equals("ROOT")) { 259 throw new IllegalArgumentException 260 (sm.getString("standardHost.pathMatch", "/", "ROOT")); 261 } 262 } else if (!filename.equals(contextPath.substring(1))) { 263 throw new IllegalArgumentException 264 (sm.getString("standardHost.pathMatch", contextPath, filename)); 265 } 266 } 267 268 if (isWAR && host.isUnpackWARs()) { 270 docBase = ExpandWar.expand(host, war, contextPath); 271 } 272 273 try { 275 Class clazz = Class.forName(host.getContextClass()); 276 Context context = (Context) clazz.newInstance(); 277 context.setPath(contextPath); 278 context.setDocBase(docBase); 279 if (context instanceof Lifecycle) { 280 clazz = Class.forName(host.getConfigClass()); 281 LifecycleListener listener = 282 (LifecycleListener) clazz.newInstance(); 283 ((Lifecycle) context).addLifecycleListener(listener); 284 } 285 host.fireContainerEvent(PRE_INSTALL_EVENT, context); 286 host.addChild(context); 287 host.fireContainerEvent(INSTALL_EVENT, context); 288 } catch (ClassNotFoundException e) { 289 log.info("", e); 290 } catch (Exception e) { 291 log.info("Error installing", e); 292 throw new IOException (e.toString()); 293 } 294 295 } 296 297 298 328 public synchronized void install(String contextPath, URL war, 329 String configFile) throws IOException { 330 331 if (contextPath == null) 333 throw new IllegalArgumentException 334 (sm.getString("standardHost.pathRequired")); 335 if (!contextPath.equals("") && !contextPath.startsWith("/")) 336 throw new IllegalArgumentException 337 (sm.getString("standardHost.pathFormat", contextPath)); 338 if (findDeployedApp(contextPath) != null) 339 throw new IllegalStateException 340 (sm.getString("standardHost.pathUsed", contextPath)); 341 if (war == null) 342 throw new IllegalArgumentException 343 (sm.getString("standardHost.warRequired")); 344 345 log.info(sm.getString("standardHost.installing", 347 contextPath, war.toString())); 348 String url = war.toString(); 349 String docBase = null; 350 boolean isWAR = false; 351 if (url.startsWith("jar:")) { 352 url = url.substring(4, url.length() - 2); 353 if (!url.toLowerCase().endsWith(".war")) { 354 throw new IllegalArgumentException 355 (sm.getString("standardHost.warURL", url)); 356 } 357 isWAR = true; 358 } 359 if (url.startsWith("file://")) 360 docBase = url.substring(7); 361 else if (url.startsWith("file:")) 362 docBase = url.substring(5); 363 else 364 throw new IllegalArgumentException 365 (sm.getString("standardHost.warURL", url)); 366 367 if (isWAR && host.isUnpackWARs()) { 369 docBase = ExpandWar.expand(host, war, contextPath); 370 } 371 372 try { 374 Class clazz = Class.forName(host.getContextClass()); 375 Context context = (Context) clazz.newInstance(); 376 context.setPath(contextPath); 377 context.setDocBase(docBase); 378 context.setConfigFile(configFile); 379 if (context instanceof Lifecycle) { 380 clazz = Class.forName(host.getConfigClass()); 381 LifecycleListener listener = 382 (LifecycleListener) clazz.newInstance(); 383 ((Lifecycle) context).addLifecycleListener(listener); 384 } 385 host.fireContainerEvent(PRE_INSTALL_EVENT, context); 386 host.addChild(context); 387 host.fireContainerEvent(INSTALL_EVENT, context); 388 389 Engine engine = (Engine)host.getParent(); 391 StandardServer server = (StandardServer) engine.getService().getServer(); 392 } catch (Exception e) { 394 log.error(sm.getString("standardHost.installError", contextPath), 395 e); 396 throw new IOException (e.toString()); 397 } 398 399 } 400 401 402 429 public synchronized void install(URL config, URL war) throws IOException { 430 431 if (config == null) 433 throw new IllegalArgumentException 434 (sm.getString("standardHost.configRequired")); 435 436 if (!host.isDeployXML()) 437 throw new IllegalArgumentException 438 (sm.getString("standardHost.configNotAllowed")); 439 440 log.info(sm.getString("standardHost.installingXML", config)); 441 442 String docBase = null; boolean isWAR = false; 445 if (war != null) { 446 String url = war.toString(); 447 log.info(sm.getString("standardHost.installingWAR", url)); 448 if (url.startsWith("jar:")) { 450 url = url.substring(4, url.length() - 2); 451 isWAR = true; 452 } 453 if (url.startsWith("file://")) 454 docBase = url.substring(7); 455 else if (url.startsWith("file:")) 456 docBase = url.substring(5); 457 else 458 throw new IllegalArgumentException 459 (sm.getString("standardHost.warURL", url)); 460 461 } 462 463 if (isWAR && host.isUnpackWARs()) { 465 docBase = ExpandWar.expand(host, war); 466 } 467 468 this.overrideDocBase = docBase; 470 if (config.toString().startsWith("file:")) { 471 this.overrideConfigFile = config.getFile(); 472 } 473 474 InputStream stream = null; 475 try { 476 stream = config.openStream(); 477 Digester digester = createDigester(); 478 digester.setDebug(host.getDebug()); 479 digester.setClassLoader(this.getClass().getClassLoader()); 480 digester.clear(); 481 digester.push(this); 482 digester.parse(stream); 483 stream.close(); 484 stream = null; 485 } catch (Exception e) { 486 host.log 487 (sm.getString("standardHost.installError", docBase), e); 488 throw new IOException (e.toString()); 489 } finally { 490 if (stream != null) { 491 try { 492 stream.close(); 493 } catch (Throwable t) { 494 ; 495 } 496 } 497 this.overrideDocBase = null; 498 this.overrideConfigFile = null; 499 } 500 501 } 502 503 504 511 public Context findDeployedApp(String contextPath) { 512 513 return ((Context) host.findChild(contextPath)); 514 515 } 516 517 518 523 public String [] findDeployedApps() { 524 525 Container children[] = host.findChildren(); 526 String results[] = new String [children.length]; 527 for (int i = 0; i < children.length; i++) 528 results[i] = children[i].getName(); 529 return (results); 530 531 } 532 533 534 550 public void remove(String contextPath) throws IOException { 551 552 if (contextPath == null) 554 throw new IllegalArgumentException 555 (sm.getString("standardHost.pathRequired")); 556 if (!contextPath.equals("") && !contextPath.startsWith("/")) 557 throw new IllegalArgumentException 558 (sm.getString("standardHost.pathFormat", contextPath)); 559 560 Context context = findDeployedApp(contextPath); 562 if (context == null) 563 throw new IllegalArgumentException 564 (sm.getString("standardHost.pathMissing", contextPath)); 565 566 log.info(sm.getString("standardHost.removing", contextPath)); 568 try { 569 host.removeChild(context); 570 host.fireContainerEvent(REMOVE_EVENT, context); 571 } catch (Exception e) { 572 log.error(sm.getString("standardHost.removeError", contextPath), e); 573 throw new IOException (e.toString()); 574 } 575 576 } 577 578 579 597 public void remove(String contextPath, boolean undeploy) 598 throws IOException { 599 600 if (contextPath == null) 602 throw new IllegalArgumentException 603 (sm.getString("standardHost.pathRequired")); 604 if (!contextPath.equals("") && !contextPath.startsWith("/")) 605 throw new IllegalArgumentException 606 (sm.getString("standardHost.pathFormat", contextPath)); 607 608 Context context = findDeployedApp(contextPath); 610 if (context == null) 611 throw new IllegalArgumentException 612 (sm.getString("standardHost.pathMissing", contextPath)); 613 614 host.log(sm.getString("standardHost.removing", contextPath)); 616 try { 617 File workDir = 619 (File ) context.getServletContext().getAttribute 620 (Globals.WORK_DIR_ATTR); 621 String configFile = context.getConfigFile(); 622 host.removeChild(context); 623 624 if (undeploy) { 625 628 boolean isAppBase = false; 630 File appBase = new File (host.getAppBase()); 631 if (!appBase.isAbsolute()) 632 appBase = new File (System.getProperty("catalina.base"), 633 host.getAppBase()); 634 File contextFile = new File (context.getDocBase()); 635 File baseDir = contextFile.getParentFile(); 636 if ((baseDir == null) 637 || (appBase.getCanonicalPath().equals 638 (baseDir.getCanonicalPath()))) { 639 isAppBase = true; 640 } 641 642 boolean isWAR = false; 643 if (contextFile.getName().toLowerCase().endsWith(".war")) { 644 isWAR = true; 645 } 646 if (isAppBase && host.getAutoDeploy()) { 649 String filename = contextFile.getName(); 650 if (isWAR) { 651 filename = filename.substring(0,filename.length()-4); 652 } 653 if (contextPath.length() == 0 && filename.equals("ROOT") || 654 filename.equals(contextPath.substring(1))) { 655 if (!isWAR) { 656 long contextLastModified = 657 contextFile.lastModified(); 658 if (contextFile.isDirectory()) { 659 deleteDir(contextFile); 660 } 661 if (host.isUnpackWARs()) { 662 File contextWAR = 663 new File (context.getDocBase() + ".war"); 664 if (contextWAR.exists()) { 665 if (contextLastModified 666 > contextWAR.lastModified()) { 667 contextWAR.delete(); 668 } 669 } 670 } 671 } else { 672 contextFile.delete(); 673 } 674 } 675 if (host.isDeployXML() && (configFile != null)) { 676 File docBaseXml = new File (configFile); 677 docBaseXml.delete(); 678 } 679 } 680 681 if (workDir == null && 683 context instanceof StandardContext && 684 ((StandardContext)context).getWorkDir() != null) { 685 workDir = new File (((StandardContext)context).getWorkPath()); 686 } 687 if (workDir != null && workDir.exists()) { 688 deleteDir(workDir); 689 } 690 } 691 692 host.fireContainerEvent(REMOVE_EVENT, context); 693 } catch (Exception e) { 694 host.log(sm.getString("standardHost.removeError", contextPath), e); 695 throw new IOException (e.toString()); 696 } 697 698 } 699 700 701 714 public void start(String contextPath) throws IOException { 715 716 if (contextPath == null) 718 throw new IllegalArgumentException 719 (sm.getString("standardHost.pathRequired")); 720 if (!contextPath.equals("") && !contextPath.startsWith("/")) 721 throw new IllegalArgumentException 722 (sm.getString("standardHost.pathFormat", contextPath)); 723 Context context = findDeployedApp(contextPath); 724 if (context == null) 725 throw new IllegalArgumentException 726 (sm.getString("standardHost.pathMissing", contextPath)); 727 log.info("standardHost.start " + contextPath); 728 try { 729 ((Lifecycle) context).start(); 730 } catch (LifecycleException e) { 731 log.info("standardHost.start " + contextPath + ": ", e); 732 throw new IllegalStateException 733 ("standardHost.start " + contextPath + ": " + e); 734 } 735 } 736 737 738 751 public void stop(String contextPath) throws IOException { 752 753 if (contextPath == null) 755 throw new IllegalArgumentException 756 (sm.getString("standardHost.pathRequired")); 757 if (!contextPath.equals("") && !contextPath.startsWith("/")) 758 throw new IllegalArgumentException 759 (sm.getString("standardHost.pathFormat", contextPath)); 760 Context context = findDeployedApp(contextPath); 761 if (context == null) 762 throw new IllegalArgumentException 763 (sm.getString("standardHost.pathMissing", contextPath)); 764 log.info("standardHost.stop " + contextPath); 765 try { 766 ((Lifecycle) context).stop(); 767 } catch (LifecycleException e) { 768 log.error("standardHost.stop " + contextPath + ": ", e); 769 throw new IllegalStateException 770 ("standardHost.stop " + contextPath + ": " + e); 771 } 772 773 } 774 775 776 778 779 784 public void addChild(Container child) { 785 786 Context context = (Context) child; 787 String contextPath = context.getPath(); 788 if (contextPath == null) 789 throw new IllegalArgumentException 790 (sm.getString("standardHost.pathRequired")); 791 else if (!contextPath.equals("") && !contextPath.startsWith("/")) 792 throw new IllegalArgumentException 793 (sm.getString("standardHost.pathFormat", contextPath)); 794 if (host.findChild(contextPath) != null) 795 throw new IllegalStateException 796 (sm.getString("standardHost.pathUsed", contextPath)); 797 if (this.overrideDocBase != null) 798 context.setDocBase(this.overrideDocBase); 799 if (this.overrideConfigFile != null) 800 context.setConfigFile(this.overrideConfigFile); 801 host.fireContainerEvent(PRE_INSTALL_EVENT, context); 802 host.addChild(child); 803 host.fireContainerEvent(INSTALL_EVENT, context); 804 805 } 806 807 808 811 public ClassLoader getParentClassLoader() { 812 813 return (host.getParentClassLoader()); 814 815 } 816 817 818 820 821 825 protected Digester createDigester() { 826 if (digester == null) { 827 digester = new Digester(); 828 if (host.getDebug() > 0) 829 digester.setDebug(3); 830 digester.setValidating(false); 831 contextRuleSet = new ContextRuleSet(""); 832 digester.addRuleSet(contextRuleSet); 833 namingRuleSet = new NamingRuleSet("Context/"); 834 digester.addRuleSet(namingRuleSet); 835 } 836 return (digester); 837 838 } 839 840 841 847 protected void deleteDir(File dir) { 848 849 String files[] = dir.list(); 850 if (files == null) { 851 files = new String [0]; 852 } 853 for (int i = 0; i < files.length; i++) { 854 File file = new File (dir, files[i]); 855 if (file.isDirectory()) { 856 deleteDir(file); 857 } else { 858 file.delete(); 859 } 860 } 861 dir.delete(); 862 863 } 864 865 } 866 | Popular Tags |