1 26 27 package org.objectweb.jonas_web.deployment.lib; 28 29 import java.io.File ; 30 import java.io.FileInputStream ; 31 import java.io.IOException ; 32 import java.io.InputStream ; 33 import java.io.InputStreamReader ; 34 import java.io.Reader ; 35 import java.net.MalformedURLException ; 36 import java.net.URL ; 37 import java.net.URLClassLoader ; 38 import java.util.Enumeration ; 39 import java.util.Hashtable ; 40 import java.util.List ; 41 import java.util.StringTokenizer ; 42 import java.util.jar.JarFile ; 43 import java.util.zip.ZipEntry ; 44 45 import org.objectweb.jonas_ejb.deployment.lib.EjbDeploymentDescManager; 46 47 import org.objectweb.jonas_lib.deployment.api.DeploymentDescException; 48 import org.objectweb.jonas_lib.deployment.api.EjbLocalRefDesc; 49 import org.objectweb.jonas_lib.deployment.api.EjbRefDesc; 50 import org.objectweb.jonas_lib.deployment.api.MessageDestinationRefDesc; 51 import org.objectweb.jonas_lib.deployment.digester.JDigester; 52 import org.objectweb.jonas_lib.deployment.lib.AbsDeploymentDescManager; 53 import org.objectweb.jonas_lib.deployment.xml.JonasMessageDestination; 54 55 import org.objectweb.jonas_web.deployment.api.WebContainerDeploymentDesc; 56 import org.objectweb.jonas_web.deployment.api.WebContainerDeploymentDescException; 57 import org.objectweb.jonas_web.deployment.rules.JonasWebAppRuleSet; 58 import org.objectweb.jonas_web.deployment.rules.WebAppRuleSet; 59 import org.objectweb.jonas_web.deployment.xml.JonasWebApp; 60 import org.objectweb.jonas_web.deployment.xml.WebApp; 61 62 import org.objectweb.jonas_ws.deployment.api.PortComponentDesc; 63 import org.objectweb.jonas_ws.deployment.api.PortComponentRefDesc; 64 import org.objectweb.jonas_ws.deployment.api.ServiceRefDesc; 65 import org.objectweb.jonas_ws.deployment.api.WSDeploymentDescException; 66 import org.objectweb.jonas_ws.deployment.lib.WSDeploymentDescManager; 67 68 import org.objectweb.jonas.common.Log; 69 70 import org.objectweb.util.monolog.api.BasicLevel; 71 import org.objectweb.util.monolog.api.Logger; 72 73 80 public class WebDeploymentDescManager extends AbsDeploymentDescManager { 81 82 85 public static final String WEB_FILE_NAME = "WEB-INF/web.xml"; 86 87 90 public static final String JONAS_WEB_FILE_NAME = "WEB-INF/jonas-web.xml"; 91 92 95 private static boolean parsingWithValidation = true; 96 97 100 private static JDigester webAppDigester = null; 101 102 105 private static JDigester jonasWebAppDigester = null; 106 107 110 private static WebAppRuleSet webAppRuleSet = new WebAppRuleSet(); 111 112 115 private static JonasWebAppRuleSet jonasWebAppRuleSet = new JonasWebAppRuleSet(); 116 117 120 private static WebDeploymentDescManager unique; 121 122 125 private EjbDeploymentDescManager ejbDDManager = null; 126 127 130 private WSDeploymentDescManager wsDDManager = null; 131 132 135 private static Logger logger = Log.getLogger(Log.JONAS_WEB_PREFIX); 136 137 138 private static Hashtable staticCache = new Hashtable (); 139 140 143 private WebDeploymentDescManager() { 144 ejbDDManager = EjbDeploymentDescManager.getInstance(); 145 earCLAltDDBindings = new Hashtable (); 146 } 147 148 152 private Hashtable earCLAltDDBindings = null; 153 154 158 public static WebDeploymentDescManager getInstance() { 159 if (unique == null) { 160 unique = new WebDeploymentDescManager(); 161 } 162 return unique; 163 } 164 165 174 public WebContainerDeploymentDesc getDeploymentDesc(URL url, ClassLoader loaderForCls, ClassLoader earLoader) 175 throws DeploymentDescException { 176 177 if (wsDDManager == null) { 179 wsDDManager = WSDeploymentDescManager.getInstance(); 180 } 181 182 if (!new File (url.getFile()).exists()) { 184 String err = "Cannot get the deployment descriptor for "; 185 err = err + "'" + url.getFile() + "'. The file doesn't exist."; 186 throw new WebContainerDeploymentDescException(err); 187 } 188 189 URL altDDUrl = null; 191 192 Hashtable urlAltddBindings = null; 194 if (earLoader != null) { 195 urlAltddBindings = (Hashtable ) earCLAltDDBindings.get(earLoader); 197 if (urlAltddBindings == null) { 198 String err = "Cannot find if there is alt-dd for '" + url.getFile() 201 + "', the setAltDD function was badly called"; 202 throw new WebContainerDeploymentDescException(err); 203 } 204 altDDUrl = (URL ) urlAltddBindings.get(url); 206 } 207 208 WebContainerDeploymentDesc webDD = null; 212 try { 213 if (altDDUrl != null) { 214 webDD = getInstance(url.getFile(), loaderForCls, altDDUrl.getFile()); 215 } else { 216 webDD = getInstance(url.getFile(), loaderForCls); 217 } 218 } catch (DeploymentDescException dde) { 219 throw new WebContainerDeploymentDescException(dde); 220 } 221 222 EjbRefDesc[] ejbRef = webDD.getEjbRefDesc(); 224 for (int i = 0; i < ejbRef.length; i++) { 225 if (ejbRef[i].getJndiName() == null) { 226 String ejbLink = ejbRef[i].getEjbLink(); 227 String ejbRefType = ejbRef[i].getEjbRefType(); 228 if (ejbLink != null) { 229 if (earLoader == null) { 230 throw new WebContainerDeploymentDescException( 231 "Ejb-link is not authorized from a single war. The war must be in an ear."); 232 } else { 233 String jndiName = getJndiName(url, ejbLink, earLoader, ejbRefType, true); 234 ejbRef[i].setJndiName(jndiName); 235 } 236 } 237 } 238 } 239 240 EjbLocalRefDesc[] ejbLocalRef = webDD.getEjbLocalRefDesc(); 242 for (int i = 0; i < ejbLocalRef.length; i++) { 243 String ejblink = ejbLocalRef[i].getEjbLink(); 244 if (earLoader == null) { 245 throw new WebContainerDeploymentDescException( 246 "Ejb-link is not authorized from a single war. The war must be in an ear."); 247 } 248 String ejbRefType = ejbLocalRef[i].getEjbRefType(); 249 String ejbName = getJndiName(url, ejblink, earLoader, ejbRefType, false); 250 ejbLocalRef[i].setJndiLocalName(ejbName); 251 } 252 253 ServiceRefDesc[] serviceRef = webDD.getServiceRefDesc(); 255 256 for (int i = 0; i < serviceRef.length; i++) { 257 258 List pcRefs = serviceRef[i].getPortComponentRefs(); 259 for (int j = 0; j < pcRefs.size(); j++) { 260 PortComponentRefDesc pcr = (PortComponentRefDesc) pcRefs.get(j); 262 String pclink = pcr.getPortComponentLink(); 263 if (pclink != null) { 264 PortComponentDesc pcDesc = getPCDesc(url, pclink, loaderForCls, earLoader); 266 pcr.setPortComponentDesc(pcDesc); 267 } 268 } 269 } 270 271 MessageDestinationRefDesc[] mdRef = webDD.getMessageDestinationRefDesc(); 273 for (int i = 0; i < mdRef.length; i++) { 274 if (mdRef[i].getJndiName() == null) { 275 String jndiName = mdRef[i].getJndiName(); 276 String mdLink = mdRef[i].getMessageDestinationLink(); 277 String mdType = mdRef[i].getMessageDestinationType(); 278 String mdUsage = mdRef[i].getMessageDestinationUsage(); 279 if (mdLink != null) { 280 if (earLoader == null) { 281 throw new WebContainerDeploymentDescException( 282 "Message-destination-link is not authorized from a single client jar. The client jar must be in an ear."); 283 } else { 284 String mdName = getMDJndiName(url, mdLink, mdType, mdUsage, earLoader); 285 mdRef[i].setJndiName(jndiName); 286 } 287 } 288 } 289 } 290 291 return webDD; 292 } 293 294 306 private PortComponentDesc getPCDesc(URL warURL, String pcLink, ClassLoader moduleLoader, ClassLoader earLoader) 307 throws WSDeploymentDescException { 308 309 return wsDDManager.getPortComponentDesc(warURL, pcLink, moduleLoader, earLoader); 311 } 312 313 327 private String getJndiName(URL warURL, String ejbLink, ClassLoader earLoader, String ejbType, boolean isEjbRef) 328 throws DeploymentDescException { 329 330 return ejbDDManager.getJndiName(warURL, ejbLink, earLoader, ejbType, null, isEjbRef); 332 } 333 334 349 private String getMDJndiName(URL warURL, String mdLink, String mdType, String mdUsage, ClassLoader earLoader) 350 throws WebContainerDeploymentDescException { 351 352 String ejbJarLink = null; 356 String destNameLink = null; 357 org.objectweb.jonas_ejb.deployment.api.DeploymentDesc dd = null; 358 359 if (mdLink.toLowerCase().indexOf(".jar#") == -1) { 361 String err = "Message-destination-link " + mdLink 362 + " has a bad format. Correct format : filename.jar#messageDestinationName"; 363 throw new WebContainerDeploymentDescException(err); 364 } 365 366 StringTokenizer st = new StringTokenizer (mdLink, LINK_SEPARATOR); 367 368 if (st.countTokens() != 2 || mdLink.startsWith(LINK_SEPARATOR) || mdLink.endsWith(LINK_SEPARATOR)) { 371 372 String err = "Message-destination-link " + mdLink 373 + " has a bad format. Correct format : filename.jar#messageDestinationName."; 374 throw new WebContainerDeploymentDescException(err); 375 } 376 377 ejbJarLink = st.nextToken(); 379 destNameLink = st.nextToken(); 380 381 if (!ejbJarLink.endsWith(".jar")) { 383 String err = "Ejbjar filename " + ejbJarLink + " from the message-destination-link " + mdLink 384 + " has a bad format. Correct format : filename.jar"; 385 throw new WebContainerDeploymentDescException(err); 386 } 387 388 URL ejbJarLinkUrl = null; 391 try { 392 ejbJarLinkUrl = new File (new File (warURL.getFile()).getParent() + File.separator + ejbJarLink) 393 .getCanonicalFile().toURL(); 394 } catch (MalformedURLException mue) { 395 String err = "Error when creating an url for the ejb jar filename. Error :" + mue.getMessage(); 396 throw new WebContainerDeploymentDescException(err); 397 } catch (IOException ioe) { 398 String err = "Error when creating/accessing a file. Error :" + ioe.getMessage(); 399 throw new WebContainerDeploymentDescException(err); 400 } 401 402 if (!new File (ejbJarLinkUrl.getFile()).exists()) { 404 String err = "Cannot get the deployment descriptor for '" + ejbJarLinkUrl.getFile() 405 + "'. The file doesn't exist."; 406 throw new WebContainerDeploymentDescException(err); 407 } 408 409 URL [] ddURL = new URL [1]; 412 ddURL[0] = ejbJarLinkUrl; 413 URLClassLoader loaderForClsEjb = new URLClassLoader (ddURL, earLoader); 414 try { 415 dd = ejbDDManager.getDeploymentDesc(ejbJarLinkUrl, loaderForClsEjb, earLoader); 416 } catch (DeploymentDescException e) { 417 String err = "Cannot get the deployment descriptor for '" + ejbJarLinkUrl.getFile() + "'."; 418 throw new WebContainerDeploymentDescException(err, e); 419 } 420 421 JonasMessageDestination md = dd.getJonasMessageDestination(mdLink); 422 423 if (md == null) { 424 String err = "No message-destination-link was found for '" + mdLink + "' in the file " + warURL.getFile() 425 + " specified."; 426 throw new WebContainerDeploymentDescException(err); 427 } 428 429 432 return md.getJndiName(); 433 } 434 435 441 public void removeCache(ClassLoader earClassLoader) { 442 earCLAltDDBindings.remove(earClassLoader); 444 445 ejbDDManager.removeCache(earClassLoader); 447 } 448 449 459 public void setAltDD(ClassLoader earClassLoader, URL [] urls, URL [] altDDs) { 460 461 Hashtable urlAltddBindings = new Hashtable (); 463 464 for (int i = 0; i < urls.length; i++) { 466 if (altDDs[i] != null) { 467 urlAltddBindings.put(urls[i], altDDs[i]); 468 } 469 } 470 471 earCLAltDDBindings.put(earClassLoader, urlAltddBindings); 473 474 } 475 476 481 public int getCacheSize() { 482 int bufferSize = 0; 483 484 Enumeration classLoaders = earCLAltDDBindings.keys(); 485 while (classLoaders.hasMoreElements()) { 486 ClassLoader loader = (ClassLoader ) classLoaders.nextElement(); 487 Hashtable hashtab = (Hashtable ) earCLAltDDBindings.get(loader); 488 bufferSize = bufferSize + hashtab.size(); 489 } 490 491 return bufferSize; 492 } 493 494 503 public static WebContainerDeploymentDesc getDeploymentDesc(String filename, ClassLoader loader) 504 throws WebContainerDeploymentDescException { 505 506 WebContainerDeploymentDesc wcdd = null; 507 if (staticCache.containsKey(filename)) { 509 wcdd = (WebContainerDeploymentDesc) staticCache.get(filename); 510 } else { 511 if (!new File (filename).exists()) { 513 String err = "Cannot get the deployment descriptor for "; 514 err += "'" + filename + "'. The file doesn't exist."; 515 throw new WebContainerDeploymentDescException(err); 516 } 517 try { 519 wcdd = getInstance(filename, loader); 520 } catch (DeploymentDescException dde) { 521 throw new WebContainerDeploymentDescException(dde); 522 } 523 staticCache.put(filename, wcdd); 525 } 526 527 return wcdd; 528 } 529 530 543 public static WebContainerDeploymentDesc getInstance(String warFileName, ClassLoader classLoaderForCls, 544 String altWebXmlFilename) throws DeploymentDescException { 545 546 String xmlContent = ""; 548 String jonasXmlContent = ""; 549 550 JarFile warFile = null; 552 553 InputStream webInputStream = null; 555 InputStream jonasWebInputStream = null; 556 557 ZipEntry webZipEntry = null; 559 ZipEntry jonasWebZipEntry = null; 560 561 WebApp webApp; 563 JonasWebApp jonasWebApp; 564 565 File fWar = new File (warFileName); 567 568 if (!(fWar.exists())) { 570 String err = "' " + warFileName + "' was not found."; 571 throw new WebContainerDeploymentDescException(err); 572 } 573 574 if ((altWebXmlFilename != null) && (!new File (altWebXmlFilename).exists())) { 577 String err = "The file for the altdd tag for the EAR case '" + altWebXmlFilename + "' was not found."; 578 throw new WebContainerDeploymentDescException(err); 579 } 580 581 try { 584 585 if (altWebXmlFilename == null) { 587 588 if (fWar.isDirectory()) { 591 File webXmlF = new File (warFileName, WEB_FILE_NAME); 593 if (!webXmlF.exists()) { 594 String err = "You have choose to deploy a war directory but there is no " + WEB_FILE_NAME 595 + " file in the directory " + warFileName; 596 throw new WebContainerDeploymentDescException(err); 597 } 598 webInputStream = new FileInputStream (webXmlF); 599 xmlContent = xmlContent(webInputStream); 600 webInputStream = new FileInputStream (webXmlF); 601 } else { 602 warFile = new JarFile (warFileName); 603 webZipEntry = warFile.getEntry(WEB_FILE_NAME); 606 if (webZipEntry == null) { 607 throw new WebContainerDeploymentDescException("The entry '" + WEB_FILE_NAME 608 + "' was not found in the file '" + warFileName + "'."); 609 } 610 webInputStream = warFile.getInputStream(webZipEntry); 612 xmlContent = xmlContent(webInputStream); 613 webInputStream = warFile.getInputStream(webZipEntry); 614 } 615 } else { 616 webInputStream = new FileInputStream (altWebXmlFilename); 617 xmlContent = xmlContent(webInputStream); 618 webInputStream = new FileInputStream (altWebXmlFilename); 619 } 620 621 if (fWar.isDirectory()) { 623 File webJXmlF = new File (warFileName, JONAS_WEB_FILE_NAME); 625 if (webJXmlF.exists()) { 626 jonasWebInputStream = new FileInputStream (webJXmlF); 627 jonasXmlContent = xmlContent(jonasWebInputStream); 628 jonasWebInputStream = new FileInputStream (webJXmlF); 629 } 630 } else { 631 if (warFile == null) { 632 warFile = new JarFile (warFileName); 633 } 634 635 jonasWebZipEntry = warFile.getEntry(JONAS_WEB_FILE_NAME); 637 638 if (jonasWebZipEntry != null) { 640 jonasWebInputStream = warFile.getInputStream(jonasWebZipEntry); 641 jonasXmlContent = xmlContent(jonasWebInputStream); 642 jonasWebInputStream = warFile.getInputStream(jonasWebZipEntry); 643 644 } 645 } 646 647 } catch (Exception e) { 648 if (warFile != null) { 649 try { 650 warFile.close(); 651 } catch (IOException ioe) { 652 logger.log(BasicLevel.WARN, "Can't close file '" + warFileName + "'"); 654 } 655 } 656 throw new WebContainerDeploymentDescException( 657 "Cannot read the XML deployment descriptors of the war file '" + warFileName + "'.", e); 658 } 659 660 webApp = loadWebApp(new InputStreamReader (webInputStream), WEB_FILE_NAME); 661 try { 662 webInputStream.close(); 663 } catch (IOException e) { 664 logger.log(BasicLevel.WARN, "Can't close InputStream of web.xml from '" + warFileName + "'"); 666 } 667 668 if (jonasWebInputStream != null) { 671 jonasWebApp = loadJonasWebApp(new InputStreamReader (jonasWebInputStream), JONAS_WEB_FILE_NAME); 672 try { 673 jonasWebInputStream.close(); 674 } catch (IOException e) { 675 logger.log(BasicLevel.WARN, "Can't close InputStream of jonas-web.xml from '" + warFileName + "'"); 677 } 678 } else { 679 jonasWebApp = new JonasWebApp(); 680 } 681 682 if (warFile != null) { 684 try { 685 warFile.close(); 686 } catch (IOException ioe) { 687 logger.log(BasicLevel.WARN, "Can't close file '" + warFileName + "'"); 689 } 690 } 691 692 WebContainerDeploymentDesc webDD = new WebContainerDeploymentDesc(warFileName, classLoaderForCls, webApp, 694 jonasWebApp); 695 webDD.setXmlContent(xmlContent); 696 webDD.setJOnASXmlContent(jonasXmlContent); 697 return webDD; 698 } 699 700 711 public static WebContainerDeploymentDesc getInstance(String warFileName, ClassLoader classLoaderForCls) 712 throws DeploymentDescException { 713 714 return getInstance(warFileName, classLoaderForCls, null); 715 } 716 717 725 public static WebApp loadWebApp(Reader reader, String fileName) throws DeploymentDescException { 726 727 WebApp webApp = new WebApp(); 728 729 if (webAppDigester == null) { 731 webAppDigester = new JDigester(webAppRuleSet, getParsingWithValidation(), true, new WebAppDTDs(), 732 new WebAppSchemas()); 733 } 734 735 try { 736 webAppDigester.parse(reader, fileName, webApp); 737 } catch (DeploymentDescException e) { 738 throw e; 739 } finally { 740 webAppDigester.push(null); 741 } 742 return webApp; 743 } 744 745 753 public static JonasWebApp loadJonasWebApp(Reader reader, String fileName) throws DeploymentDescException { 754 755 JonasWebApp jonasWebApp = new JonasWebApp(); 756 757 if (jonasWebAppDigester == null) { 759 jonasWebAppDigester = new JDigester(jonasWebAppRuleSet, getParsingWithValidation(), true, 760 new JonasWebAppDTDs(), new JonasWebAppSchemas()); 761 } 762 763 try { 764 jonasWebAppDigester.parse(reader, fileName, jonasWebApp); 765 } catch (DeploymentDescException e) { 766 throw e; 767 } finally { 768 jonasWebAppDigester.push(null); 769 } 770 return jonasWebApp; 771 } 772 773 777 public static boolean getParsingWithValidation() { 778 return parsingWithValidation; 779 } 780 781 785 public static void setParsingWithValidation(boolean validation) { 786 WebDeploymentDescManager.parsingWithValidation = validation; 787 } 788 789 } | Popular Tags |