1 22 package org.jboss.web.deployers; 23 24 import java.net.MalformedURLException ; 25 import java.net.URL ; 26 import java.net.URLClassLoader ; 27 import java.security.Policy ; 28 import java.util.ArrayList ; 29 import java.util.HashSet ; 30 import java.util.Iterator ; 31 import java.util.Set ; 32 33 import javax.management.MBeanServer ; 34 import javax.management.ObjectName ; 35 import javax.naming.Context ; 36 import javax.naming.InitialContext ; 37 import javax.naming.LinkRef ; 38 import javax.naming.NamingException ; 39 import javax.security.jacc.PolicyConfiguration ; 40 import javax.security.jacc.PolicyConfigurationFactory ; 41 import javax.security.jacc.PolicyContextException ; 42 43 import org.jboss.deployers.spi.deployer.DeploymentUnit; 44 import org.jboss.deployers.spi.structure.DeploymentContext; 45 import org.jboss.ejb.EjbUtil50; 46 import org.jboss.logging.Logger; 47 import org.jboss.metadata.EjbLocalRefMetaData; 48 import org.jboss.metadata.EjbRefMetaData; 49 import org.jboss.metadata.EnvEntryMetaData; 50 import org.jboss.metadata.MessageDestinationMetaData; 51 import org.jboss.metadata.MessageDestinationRefMetaData; 52 import org.jboss.metadata.ResourceEnvRefMetaData; 53 import org.jboss.metadata.ResourceRefMetaData; 54 import org.jboss.metadata.WebMetaData; 55 import org.jboss.mx.loading.LoaderRepositoryFactory; 56 import org.jboss.naming.NonSerializableFactory; 57 import org.jboss.naming.Util; 58 import org.jboss.security.AuthorizationManager; 59 import org.jboss.security.authorization.PolicyRegistration; 60 import org.jboss.web.WebApplication; 61 import org.jboss.web.WebPermissionMapping; 62 import org.omg.CORBA.ORB ; 63 64 89 public abstract class AbstractWarDeployment 90 { 91 public static final String ERROR = "org.jboss.web.AbstractWebContainer.error"; 92 protected Logger log; 93 94 protected MBeanServer server; 95 98 protected boolean java2ClassLoadingCompliance = false; 99 102 protected boolean unpackWars = true; 103 107 protected boolean lenientEjbLink = false; 108 109 112 protected String defaultSecurityDomain; 113 114 public AbstractWarDeployment() 115 { 116 log = Logger.getLogger(getClass()); 117 } 118 119 124 public static String shortNameFromDeploymentName(String name) 125 { 126 String shortName = name.trim(); 127 String [] parts = name.split("/|\\.|\\!"); 128 if( parts.length > 1 ) 129 { 130 if( parts[parts.length-1].equals("war") ) 132 shortName = parts[parts.length-2]; 133 else 135 shortName = parts[parts.length-1]; 136 } 137 return shortName; 138 } 139 146 public static String shortWarUrlFromServerHome(String warUrl) 147 { 148 String serverHomeUrl = System.getProperty(org.jboss.system.server.ServerConfig.SERVER_HOME_URL); 149 150 if (warUrl == null || serverHomeUrl == null) 151 return warUrl; 152 153 if (warUrl.startsWith(serverHomeUrl)) 154 return ".../" + warUrl.substring(serverHomeUrl.length()); 155 else 156 return warUrl; 157 } 158 159 164 public abstract void init(Object containerConfig) throws Exception ; 165 166 public MBeanServer getServer() 167 { 168 return server; 169 } 170 171 public void setServer(MBeanServer server) 172 { 173 this.server = server; 174 } 175 176 182 public boolean getJava2ClassLoadingCompliance() 183 { 184 return java2ClassLoadingCompliance; 185 } 186 187 193 public void setJava2ClassLoadingCompliance(boolean flag) 194 { 195 java2ClassLoadingCompliance = flag; 196 } 197 198 205 public boolean getUnpackWars() 206 { 207 return unpackWars; 208 } 209 210 217 public void setUnpackWars(boolean flag) 218 { 219 this.unpackWars = flag; 220 } 221 222 228 public boolean getLenientEjbLink() 229 { 230 return lenientEjbLink; 231 } 232 233 238 public void setLenientEjbLink(boolean flag) 239 { 240 lenientEjbLink = flag; 241 } 242 243 249 public String getDefaultSecurityDomain() 250 { 251 if(defaultSecurityDomain == null) 252 throw new IllegalStateException ("Default Security Domain is null"); 253 return defaultSecurityDomain; 254 } 255 256 263 public void setDefaultSecurityDomain(String defaultSecurityDomain) 264 { 265 this.defaultSecurityDomain = defaultSecurityDomain; 266 } 267 268 300 public synchronized WebApplication start(DeploymentUnit di, WebMetaData metaData) 301 throws Exception 302 { 303 Thread thread = Thread.currentThread(); 304 ClassLoader appClassLoader = thread.getContextClassLoader(); 305 WebApplication webApp = null; 306 try 307 { 308 URL [] empty = {}; 310 URLClassLoader warLoader = URLClassLoader.newInstance(empty, di.getClassLoader()); 311 thread.setContextClassLoader(warLoader); 312 String webContext = metaData.getContextRoot(); 313 314 URL warURL = di.getAttachment("org.jboss.web.expandedWarURL", URL .class); 317 if( warURL == null ) 318 warURL = di.getAttachment("jbossws.expanded.war.url", URL .class); 319 if (warURL == null) 320 warURL = di.getDeploymentContext().getRoot().toURL(); 321 322 String warURLString = warURL.toString(); 324 if( warURLString.startsWith("jar:") ) 325 warURLString = warURLString.substring(4, warURLString.length()-2); 326 327 log.debug("webContext: " + webContext); 328 log.debug("warURL: " + warURLString); 329 330 String contextID = metaData.getJaccContextID(); 332 if( contextID == null ) 333 contextID = shortNameFromDeploymentName(di.getName()); 334 metaData.setJaccContextID(contextID); 335 PolicyConfigurationFactory pcFactory = PolicyConfigurationFactory.getPolicyConfigurationFactory(); 336 PolicyConfiguration pc = pcFactory.getPolicyConfiguration(contextID, true); 337 createPermissions(metaData, pc); 338 DeploymentContext current = di.getDeploymentContext(); 340 while (current.getParent() != null) 341 current = current.getParent(); 342 PolicyConfiguration parentPC = 343 current.getTransientAttachments().getAttachment(PolicyConfiguration .class); 344 if (parentPC != null && parentPC != pc) 345 parentPC.linkConfiguration(pc); 346 347 pc.commit(); 349 Policy.getPolicy().refresh(); 351 352 webApp = new WebApplication(metaData); 353 webApp.setClassLoader(warLoader); 354 webApp.setDeploymentUnit(di); 355 performDeploy(webApp, warURLString); 356 } 357 finally 358 { 359 thread.setContextClassLoader(appClassLoader); 360 } 361 return webApp; 362 } 363 364 370 public synchronized void stop(DeploymentUnit di, WebApplication webApp) 371 throws Exception 372 { 373 URL warURL = webApp.getURL(); 374 String warUrl = warURL.toString(); 375 performUndeploy(webApp, warUrl); 376 WebMetaData metaData = webApp.getMetaData(); 378 String contextID = metaData.getJaccContextID(); 379 PolicyConfigurationFactory pcFactory = PolicyConfigurationFactory.getPolicyConfigurationFactory(); 380 PolicyConfiguration pc = pcFactory.getPolicyConfiguration(contextID, true); 381 pc.delete(); 382 String prefixedSecurityDomain = webApp.getMetaData().getSecurityDomain(); 384 if(prefixedSecurityDomain != null) 385 { 386 AuthorizationManager authzmgr = 387 org.jboss.security.Util.getAuthorizationManager(prefixedSecurityDomain); 388 if(authzmgr instanceof PolicyRegistration) 389 { 390 PolicyRegistration xam = (PolicyRegistration)authzmgr; 391 xam.deRegisterPolicy(contextID); 392 } 393 } 394 } 395 396 405 protected abstract void performDeploy(WebApplication webApp, String warUrl) throws Exception ; 406 414 protected abstract void performUndeploy(WebApplication webApp, String warUrl) 415 throws Exception ; 416 417 424 protected void processEnc(ClassLoader loader, WebApplication webApp) 425 throws Exception 426 { 427 if(loader == null) 428 throw new IllegalArgumentException ("Classloader passed to process ENC refs is null"); 429 log.debug("AbstractWebContainer.parseWebAppDescriptors, Begin"); 430 InitialContext iniCtx = new InitialContext (); 431 Context envCtx = null; 432 Thread currentThread = Thread.currentThread(); 433 ClassLoader currentLoader = currentThread.getContextClassLoader(); 434 WebMetaData metaData = webApp.getMetaData(); 435 try 436 { 437 log.debug("Creating ENC using ClassLoader: " + loader); 439 ClassLoader parent = loader.getParent(); 440 while (parent != null) 441 { 442 log.debug(".." + parent); 443 parent = parent.getParent(); 444 } 445 currentThread.setContextClassLoader(loader); 447 metaData.setENCLoader(loader); 448 envCtx = (Context ) iniCtx.lookup("java:comp"); 449 450 ORB orb = null; 452 try 453 { 454 ObjectName ORB_NAME = new ObjectName ("jboss:service=CorbaORB"); 455 orb = (ORB ) server.getAttribute(ORB_NAME, "ORB"); 456 if (orb != null) 458 { 459 NonSerializableFactory.rebind(envCtx, "ORB", orb); 460 log.debug("Bound java:comp/ORB"); 461 } 462 } 463 catch (Throwable t) 464 { 465 log.debug("Unable to retrieve orb" + t.toString()); 466 } 467 468 envCtx.bind("UserTransaction", new LinkRef ("UserTransaction")); 470 log.debug("Linked java:comp/UserTransaction to JNDI name: UserTransaction"); 471 envCtx = envCtx.createSubcontext("env"); 472 processEncReferences(webApp, envCtx); 473 } 474 finally 475 { 476 currentThread.setContextClassLoader(currentLoader); 477 } 478 479 String securityDomain = metaData.getSecurityDomain(); 480 log.debug("linkSecurityDomain"); 481 linkSecurityDomain(securityDomain, envCtx); 482 log.debug("AbstractWebContainer.parseWebAppDescriptors, End"); 483 } 484 485 protected void processEncReferences(WebApplication webApp, Context envCtx) 486 throws ClassNotFoundException , NamingException 487 { 488 DeploymentUnit unit = webApp.getDeploymentUnit(); 489 WebMetaData metaData = webApp.getMetaData(); 490 Iterator envEntries = metaData.getEnvironmentEntries(); 491 log.debug("addEnvEntries"); 492 addEnvEntries(envEntries, envCtx); 493 Iterator resourceEnvRefs = metaData.getResourceEnvReferences(); 494 log.debug("linkResourceEnvRefs"); 495 linkResourceEnvRefs(resourceEnvRefs, envCtx); 496 Iterator resourceRefs = metaData.getResourceReferences(); 497 log.debug("linkResourceRefs"); 498 linkResourceRefs(resourceRefs, envCtx); 499 log.debug("linkMessageDestinationRefs"); 500 linkMessageDestinationRefs(unit, metaData, envCtx); 501 Iterator ejbRefs = metaData.getEjbReferences(); 502 log.debug("linkEjbRefs"); 503 linkEjbRefs(unit, ejbRefs, envCtx); 504 Iterator ejbLocalRefs = metaData.getEjbLocalReferences(); 505 log.debug("linkEjbLocalRefs"); 506 linkEjbLocalRefs(unit, ejbLocalRefs, envCtx); 507 Iterator serviceRefs = metaData.getServiceReferences(); 508 log.debug("linkServiceRefs"); 509 } 511 512 protected void addEnvEntries(Iterator envEntries, Context envCtx) 513 throws ClassNotFoundException , NamingException 514 { 515 while (envEntries.hasNext()) 516 { 517 EnvEntryMetaData entry = (EnvEntryMetaData) envEntries.next(); 518 log.debug("Binding env-entry: " + entry.getName() + " of type: " + 519 entry.getType() + " to value:" + entry.getValue()); 520 EnvEntryMetaData.bindEnvEntry(envCtx, entry); 521 } 522 } 523 524 protected void linkResourceEnvRefs(Iterator resourceEnvRefs, Context envCtx) 525 throws NamingException 526 { 527 while (resourceEnvRefs.hasNext()) 528 { 529 ResourceEnvRefMetaData ref = (ResourceEnvRefMetaData) resourceEnvRefs.next(); 530 String resourceName = ref.getJndiName(); 531 String refName = ref.getRefName(); 532 if (ref.getType().equals("java.net.URL")) 533 { 534 try 535 { 536 log.debug("Binding '" + refName + "' to URL: " + resourceName); 537 URL url = new URL (resourceName); 538 Util.bind(envCtx, refName, url); 539 } 540 catch (MalformedURLException e) 541 { 542 throw new NamingException ("Malformed URL:" + e.getMessage()); 543 } 544 } 545 else if (resourceName != null) 546 { 547 log.debug("Linking '" + refName + "' to JNDI name: " + resourceName); 548 Util.bind(envCtx, refName, new LinkRef (resourceName)); 549 } 550 else 551 { 552 throw new NamingException ("resource-env-ref: " + refName 553 + " has no valid JNDI binding. Check the jboss-web/resource-env-ref."); 554 } 555 } 556 } 557 558 protected void linkResourceRefs(Iterator resourceRefs, Context envCtx) 559 throws NamingException 560 { 561 while (resourceRefs.hasNext()) 562 { 563 ResourceRefMetaData ref = (ResourceRefMetaData) resourceRefs.next(); 564 String jndiName = ref.getJndiName(); 565 String refName = ref.getRefName(); 566 if (ref.getType().equals("java.net.URL")) 567 { 568 try 569 { 570 String resURL = ref.getResURL(); 571 if (ref.getResURL() != null) 572 { 573 log.debug("Binding '" + refName + "' to URL: " + resURL); 574 URL url = new URL (resURL); 575 Util.bind(envCtx, refName, url); 576 } 577 else 578 { 579 log.debug("Linking '" + refName + "' to URL: " + resURL); 580 LinkRef urlLink = new LinkRef (jndiName); 581 Util.bind(envCtx, refName, urlLink); 582 } 583 } 584 catch (MalformedURLException e) 585 { 586 throw new NamingException ("Malformed URL:" + e.getMessage()); 587 } 588 } 589 else if (jndiName != null) 590 { 591 log.debug("Linking '" + refName + "' to JNDI name: " + jndiName); 592 Util.bind(envCtx, refName, new LinkRef (jndiName)); 593 } 594 else 595 { 596 throw new NamingException ("resource-ref: " + refName 597 + " has no valid JNDI binding. Check the jboss-web/resource-ref."); 598 } 599 } 600 } 601 602 protected void linkMessageDestinationRefs(DeploymentUnit unit, WebMetaData metaData, Context envCtx) 603 throws NamingException 604 { 605 Iterator i = metaData.getMessageDestinationReferences(); 606 607 while (i.hasNext()) 608 { 609 MessageDestinationRefMetaData ref = (MessageDestinationRefMetaData) i.next(); 610 611 String refName = ref.getRefName(); 612 String jndiName = ref.getJNDIName(); 613 String link = ref.getLink(); 614 if (link != null) 615 { 616 if (jndiName == null) 617 { 618 MessageDestinationMetaData messageDestination = EjbUtil50.findMessageDestination(server, unit, link); 619 if (messageDestination == null) 620 throw new NamingException ("message-destination-ref '" + refName + 621 "' message-destination-link '" + link + "' not found and no jndi-name in jboss-web.xml"); 622 else 623 { 624 String linkJNDIName = messageDestination.getJNDIName(); 625 if (linkJNDIName == null) 626 log.warn("message-destination '" + link + "' has no jndi-name in jboss-web.xml"); 627 else 628 jndiName = linkJNDIName; 629 } 630 } 631 else 632 log.warn("message-destination-ref '" + refName + 633 "' ignoring message-destination-link '" + link + "' because it has a jndi-name in jboss-web.xml"); 634 } 635 else if (jndiName == null) 636 throw new NamingException ("message-destination-ref '" + refName + 637 "' has no message-destination-link in web.xml and no jndi-name in jboss-web.xml"); 638 Util.bind(envCtx, refName, new LinkRef (jndiName)); 639 } 640 } 641 642 protected void linkEjbRefs(DeploymentUnit unit, Iterator ejbRefs, Context envCtx) 643 throws NamingException 644 { 645 while (ejbRefs.hasNext()) 646 { 647 EjbRefMetaData ejb = (EjbRefMetaData) ejbRefs.next(); 648 String name = ejb.getName(); 649 String linkName = ejb.getLink(); 650 String jndiName = null; 651 652 if (linkName != null) 654 { 655 jndiName = EjbUtil50.findEjbLink(server, unit, linkName); 656 657 if ((jndiName == null) && !(getLenientEjbLink())) 659 throw new NamingException ("ejb-ref: " + name + ", no ejb-link match"); 660 } 661 662 663 if (jndiName == null) 665 { 666 jndiName = ejb.getJndiName(); 667 if (jndiName == null) 668 throw new NamingException ("ejb-ref: " + name + ", no ejb-link in web.xml and no jndi-name in jboss-web.xml"); 669 } 670 671 log.debug("Linking ejb-ref: " + name + " to JNDI name: " + jndiName); 672 Util.bind(envCtx, name, new LinkRef (jndiName)); 673 } 674 } 675 676 protected void linkEjbLocalRefs(DeploymentUnit unit, Iterator ejbRefs, Context envCtx) 677 throws NamingException 678 { 679 while (ejbRefs.hasNext()) 680 { 681 EjbLocalRefMetaData ejb = (EjbLocalRefMetaData) ejbRefs.next(); 682 String name = ejb.getName(); 683 String linkName = ejb.getLink(); 684 String jndiName = null; 685 686 if (linkName != null) 688 { 689 jndiName = EjbUtil50.findLocalEjbLink(server, unit, linkName); 690 691 if ((jndiName == null) && !(getLenientEjbLink())) 693 throw new NamingException ("ejb-ref: " + name + ", no ejb-link match"); 694 } 695 696 697 if (jndiName == null) 698 { 699 jndiName = ejb.getJndiName(); 700 if (jndiName == null) 701 { 702 String msg = null; 703 if (linkName == null) 704 { 705 msg = "ejb-local-ref: '" + name + "', no ejb-link in web.xml and " 706 + "no local-jndi-name in jboss-web.xml"; 707 } 708 else 709 { 710 msg = "ejb-local-ref: '" + name + "', with web.xml ejb-link: '" 711 + linkName + "' failed to resolve to an ejb with a LocalHome"; 712 } 713 throw new NamingException (msg); 714 } 715 } 716 717 log.debug("Linking ejb-local-ref: " + name + " to JNDI name: " + jndiName); 718 Util.bind(envCtx, name, new LinkRef (jndiName)); 719 } 720 } 721 722 731 protected void linkSecurityDomain(String securityDomain, Context envCtx) 732 throws NamingException 733 { 734 if (securityDomain == null) 735 { 736 securityDomain = getDefaultSecurityDomain(); 737 log.debug("No security-domain given, using default: " + securityDomain); 738 } 739 log.debug("Linking security/securityMgr to JNDI name: " + securityDomain); 740 Util.bind(envCtx, "security/securityMgr", new LinkRef (securityDomain)); 741 Util.bind(envCtx, "security/realmMapping", new LinkRef (securityDomain+"/realmMapping")); 742 Util.bind(envCtx, "security/authorizationMgr", new LinkRef (securityDomain+"/authorizationMgr")); 743 Util.bind(envCtx, "security/security-domain", new LinkRef (securityDomain)); 744 Util.bind(envCtx, "security/subject", new LinkRef (securityDomain + "/subject")); 745 } 746 747 754 public String [] getStandardCompileClasspath(ClassLoader loader) 755 { 756 String [] jspResources = { 757 "javax/servlet/resources/web-app_2_3.dtd", 758 "org/apache/jasper/resources/jsp12.dtd", 759 "javax/ejb/EJBHome.class" 760 }; 761 ArrayList tmp = new ArrayList (); 762 for (int j = 0; j < jspResources.length; j++) 763 { 764 URL rsrcURL = loader.getResource(jspResources[j]); 765 if (rsrcURL != null) 766 { 767 String url = rsrcURL.toExternalForm(); 768 if (rsrcURL.getProtocol().equals("jar")) 769 { 770 url = url.substring(4); 772 int seperator = url.indexOf('!'); 773 url = url.substring(0, seperator); 774 } 775 tmp.add(url); 776 } 777 else 778 { 779 log.warn("Failed to fin jsp rsrc: " + jspResources[j]); 780 } 781 } 782 log.trace("JSP StandardCompileClasspath: " + tmp); 783 String [] cp = new String [tmp.size()]; 784 tmp.toArray(cp); 785 return cp; 786 } 787 788 793 public String [] getCompileClasspath(ClassLoader loader) 794 { 795 HashSet tmp = new HashSet (); 796 ClassLoader cl = loader; 797 while (cl != null) 798 { 799 URL [] urls = AbstractWarDeployer.getClassLoaderURLs(cl); 800 addURLs(tmp, urls); 801 cl = cl.getParent(); 802 } 803 try 804 { 805 URL [] globalUrls = (URL []) server.getAttribute(LoaderRepositoryFactory.DEFAULT_LOADER_REPOSITORY, 806 "URLs"); 807 addURLs(tmp, globalUrls); 808 } 809 catch (Exception e) 810 { 811 log.warn("Could not get global URL[] from default loader repository!", e); 812 } log.trace("JSP CompileClasspath: " + tmp); 814 String [] cp = new String [tmp.size()]; 815 tmp.toArray(cp); 816 return cp; 817 } 818 819 private void addURLs(Set urlSet, URL [] urls) 820 { 821 for (int u = 0; u < urls.length; u++) 822 { 823 URL url = urls[u]; 824 urlSet.add(url.toExternalForm()); 825 } 826 } 827 828 835 protected void createPermissions(WebMetaData metaData, PolicyConfiguration pc) 836 throws PolicyContextException 837 { 838 WebPermissionMapping.createPermissions(metaData, pc); 839 } 840 841 } 842 | Popular Tags |