| 1 25 26 27 package org.objectweb.jonas_ejb.deployment.lib; 28 29 import java.io.File ; 30 import java.io.FileInputStream ; 31 import java.io.FileNotFoundException ; 32 import java.io.IOException ; 33 import java.io.InputStream ; 34 import java.io.InputStreamReader ; 35 import java.io.Reader ; 36 import java.net.MalformedURLException ; 37 import java.net.URL ; 38 import java.net.URLClassLoader ; 39 import java.util.Enumeration ; 40 import java.util.Hashtable ; 41 import java.util.List ; 42 import java.util.StringTokenizer ; 43 import java.util.Vector ; 44 import java.util.zip.ZipEntry ; 45 import java.util.zip.ZipFile ; 46 47 import org.objectweb.jonas_ejb.deployment.api.BeanDesc; 48 import org.objectweb.jonas_ejb.deployment.api.DeploymentDesc; 49 import org.objectweb.jonas_ejb.deployment.api.DeploymentDescEjb1_1; 50 import org.objectweb.jonas_ejb.deployment.api.DeploymentDescEjb2; 51 import org.objectweb.jonas_ejb.deployment.api.EntityDesc; 52 import org.objectweb.jonas_ejb.deployment.api.MessageDrivenDesc; 53 import org.objectweb.jonas_ejb.deployment.api.SessionDesc; 54 import org.objectweb.jonas_ejb.deployment.rules.EjbJarRuleSet; 55 import org.objectweb.jonas_ejb.deployment.rules.JonasEjbJarRuleSet; 56 import org.objectweb.jonas_ejb.deployment.xml.EjbJar; 57 import org.objectweb.jonas_ejb.deployment.xml.JonasEjbJar; 58 import org.objectweb.jonas_ejb.lib.BeanNaming; 59 60 import org.objectweb.jonas_lib.deployment.api.DeploymentDescException; 61 import org.objectweb.jonas_lib.deployment.api.EjbLocalRefDesc; 62 import org.objectweb.jonas_lib.deployment.api.EjbRefDesc; 63 import org.objectweb.jonas_lib.deployment.api.JndiEnvRefsGroup; 64 import org.objectweb.jonas_lib.deployment.api.MessageDestinationRefDesc; 65 import org.objectweb.jonas_lib.deployment.digester.JDigester; 66 import org.objectweb.jonas_lib.deployment.lib.AbsDeploymentDescManager; 67 import org.objectweb.jonas_lib.deployment.xml.JonasMessageDestination; 68 69 import org.objectweb.jonas_ws.deployment.api.PortComponentDesc; 70 import org.objectweb.jonas_ws.deployment.api.PortComponentRefDesc; 71 import org.objectweb.jonas_ws.deployment.api.ServiceRefDesc; 72 import org.objectweb.jonas_ws.deployment.api.WSDeploymentDescException; 73 import org.objectweb.jonas_ws.deployment.lib.WSDeploymentDescManager; 74 75 import org.objectweb.jonas.common.Log; 76 77 import org.objectweb.util.monolog.api.BasicLevel; 78 import org.objectweb.util.monolog.api.Logger; 79 80 92 public class EjbDeploymentDescManager extends AbsDeploymentDescManager { 93 94 97 public static final String EJB_JAR_FILE_NAME = "META-INF/ejb-jar.xml"; 98 99 102 public static final String JONAS_EJB_JAR_FILE_NAME = "META-INF/jonas-ejb-jar.xml"; 103 104 107 private static boolean parsingWithValidation = true; 108 109 112 private static EjbDeploymentDescManager unique; 113 114 115 118 private static JDigester ejbjarDigester = null; 119 120 123 private static JDigester jonasEjbjarDigester = null; 124 125 128 private static EjbJarRuleSet ejbjarRuleSet = new EjbJarRuleSet(); 129 130 133 private static JonasEjbJarRuleSet jonasEjbjarRuleSet = new JonasEjbJarRuleSet(); 134 135 136 139 private WSDeploymentDescManager wsDDManager = null; 140 141 145 private Hashtable urlJarBindings; 146 147 150 private Hashtable urlEarCLBindings; 151 152 155 private Hashtable urlEjbCLBindings; 156 157 160 private Hashtable urlAltDDBindings = null; 161 162 167 private Hashtable earCLEjbLinkJar; 168 169 172 private static Logger logger = Log.getLogger("org.objectweb.jonas_ejb.dd"); 173 174 177 private static Hashtable staticCache = new Hashtable (); 178 179 182 private static String xmlContent = ""; 183 184 187 private static String jonasXmlContent = ""; 188 189 192 private EjbDeploymentDescManager() { 193 urlJarBindings = new Hashtable (); 194 urlEarCLBindings = new Hashtable (); 195 urlEjbCLBindings = new Hashtable (); 196 earCLEjbLinkJar = new Hashtable (); 197 urlAltDDBindings = new Hashtable (); 198 } 199 200 204 public static EjbDeploymentDescManager getInstance() { 205 if (unique == null) { 206 unique = new EjbDeploymentDescManager(); 207 } 208 return unique; 209 } 210 211 220 public static DeploymentDesc getDeploymentDesc(String ejbjar, ClassLoader ejbLoader) 221 throws DeploymentDescException { 222 if (!staticCache.containsKey(ejbjar)) { 223 return getDeploymentDescriptor(ejbjar, ejbLoader, (String ) null); 224 } else { 225 return (DeploymentDesc) staticCache.get(ejbjar); 226 } 227 } 228 229 242 public synchronized DeploymentDesc getDeploymentDesc(URL url, 243 ClassLoader ejbLoader, 244 ClassLoader earLoader) 245 throws DeploymentDescException { 246 if (wsDDManager == null) { 248 wsDDManager = WSDeploymentDescManager.getInstance(); 249 } 250 251 if (earLoader != null) { 254 checkEjbLinkAvailable(earLoader, url); 255 } 256 257 DeploymentDesc dd = (DeploymentDesc) urlJarBindings.get(url.getFile()); 258 if (dd == null) { 259 dd = loadDeploymentDesc(url, ejbLoader, earLoader); 261 } 262 return dd; 263 } 264 265 274 private DeploymentDesc getDeploymentDesc(URL currentUrl, URL urlToLoad) 275 throws DeploymentDescException { 276 DeploymentDesc ejbLinkDD = (DeploymentDesc) urlJarBindings.get(urlToLoad.getFile()); 277 if (ejbLinkDD == null) { 278 String url = currentUrl.getFile(); 280 URLClassLoader earLoader = (URLClassLoader ) urlEarCLBindings.get(url); 281 if (new File (url).isFile()) { 282 if (!url.toLowerCase().endsWith(".jar")) { 283 String err = "File '" + url + "' is not a jar file"; 284 throw new DeploymentDescException(err); 285 } 286 } 287 288 if (earLoader != null) { 289 291 checkEjbLinkAvailable(earLoader, urlToLoad); 294 295 URLClassLoader loaderForCls = (URLClassLoader ) urlEjbCLBindings.get(url); 297 298 if (loaderForCls == null) { 300 loaderForCls = new URLClassLoader (new URL [] {urlToLoad}); 301 } 302 303 URLClassLoader loader = (URLClassLoader ) urlEarCLBindings.get(url); 305 ejbLinkDD = loadDeploymentDesc(urlToLoad, loaderForCls, loader); 306 } else { 307 if (!currentUrl.getFile().equals(urlToLoad)) { 309 String err = "In '" + url + "' ejb-link is not allowed outside the ejb-jar if you are not in an ear application."; 310 throw new DeploymentDescException(err); 311 } 312 } 314 } 315 return ejbLinkDD; 316 } 317 318 329 private DeploymentDesc loadDeploymentDesc(URL url, ClassLoader ejbLoader, ClassLoader earLoader) 330 throws DeploymentDescException { 331 String filename = url.getFile(); 333 File f = new File (filename); 334 if (!f.exists()) { 335 throw new DeploymentDescException(filename + " not found"); 336 } 337 338 DeploymentDesc dd = null; 340 if (f.isDirectory()) { 341 dd = getDeploymentDescriptor(filename + EJB_JAR_FILE_NAME, 342 BeanNaming.getJonasXmlName(filename + EJB_JAR_FILE_NAME), 343 ejbLoader, 344 f.getAbsolutePath()); 345 346 } else if (filename.toLowerCase().endsWith(".xml")) { 347 File parent = f.getParentFile(); 350 dd = getDeploymentDescriptor(filename, 351 BeanNaming.getJonasXmlName(filename), 352 ejbLoader, 353 parent.getAbsolutePath()); 354 } else { 355 String altname = null; 358 URL altDDUrl = (URL ) urlAltDDBindings.get(url.getFile()); 359 if (altDDUrl != null) { 360 altname = altDDUrl.getFile(); 361 } 362 dd = getDeploymentDescriptor(filename, ejbLoader, altname); 363 } 364 365 if (earLoader != null) { 367 urlEjbCLBindings.put(filename, ejbLoader); 368 urlEarCLBindings.put(filename, earLoader); 369 } 370 371 BeanDesc[] bd = dd.getBeanDesc(); 372 for (int j = 0; j < bd.length; j++) { 373 374 EjbRefDesc[] ejbRef = bd[j].getEjbRefDesc(); 376 for (int i = 0; i < ejbRef.length; i++) { 377 String jndiName = ejbRef[i].getJndiName(); 378 String ejbLink = ejbRef[i].getEjbLink(); 379 String ejbRefType = ejbRef[i].getEjbRefType(); 380 if (ejbLink != null && jndiName == null) { 381 String ejbName = getJndiName(url, ejbLink, earLoader, ejbRefType, dd, true); 382 ejbRef[i].setJndiName(ejbName); 383 } 384 } 385 386 EjbLocalRefDesc[] ejbLocalRef = bd[j].getEjbLocalRefDesc(); 388 for (int i = 0; i < ejbLocalRef.length; i++) { 389 String ejblink = ejbLocalRef[i].getEjbLink(); 390 if (ejblink == null) { 391 String err = "Ejb-link must be specified for ejb-local-ref " + ejbLocalRef[i].getEjbRefName(); 392 throw new DeploymentDescException(err); 393 } 394 String ejbRefType = ejbLocalRef[i].getEjbRefType(); 395 String ejbName = getJndiName(url, ejblink, earLoader, ejbRefType, dd, false); 396 ejbLocalRef[i].setJndiLocalName(ejbName); 397 } 398 399 ServiceRefDesc[] serviceRef = bd[j].getServiceRefDesc(); 401 402 for (int i = 0; i < serviceRef.length; i++) { 403 List pcRefs = serviceRef[i].getPortComponentRefs(); 404 405 for (int k = 0; k < pcRefs.size(); k++) { 406 PortComponentRefDesc pcr = (PortComponentRefDesc) pcRefs.get(k); 408 String pclink = pcr.getPortComponentLink(); 409 if (pclink != null) { 410 PortComponentDesc pcDesc = getPCDesc(url, pclink, ejbLoader, earLoader); 412 pcr.setPortComponentDesc(pcDesc); 413 } 414 } 415 } 416 417 MessageDestinationRefDesc[] mdRef = bd[j].getMessageDestinationRefDesc(); 419 for (int i = 0; i < mdRef.length; i++) { 420 String jndiName = mdRef[i].getJndiName(); 421 String mdLink = mdRef[i].getMessageDestinationLink(); 422 String mdType = mdRef[i].getMessageDestinationType(); 423 String mdUsage = mdRef[i].getMessageDestinationUsage(); 424 if (logger.isLoggable(BasicLevel.DEBUG)) { 425 logger.log(BasicLevel.DEBUG, "" + jndiName + " " + mdLink + " " + mdType + " " + mdUsage); 426 } 427 if (mdLink != null && jndiName == null) { 428 String mdName = getMDJndiName(url, mdLink, mdType, mdUsage, dd); 429 mdRef[i].setJndiName(mdName); 430 } 431 } 432 433 } 434 435 if (earLoader != null) { 437 urlJarBindings.put(filename, dd); 439 } 440 441 return dd; 442 } 443 444 445 461 private PortComponentDesc getPCDesc(URL ejbjarURL, 462 String pcLink, 463 ClassLoader moduleLoader, 464 ClassLoader earLoader) 465 throws WSDeploymentDescException { 466 467 return wsDDManager.getPortComponentDesc(ejbjarURL, pcLink, moduleLoader, earLoader); 469 } 470 471 472 487 public String getJndiName(URL currentFile, String ejbLink, ClassLoader earCl, String ejbType, 488 DeploymentDesc deploymentDesc, boolean isEjbRef) 489 throws DeploymentDescException { 490 String ejbJarLink = null; 494 String beanNameLink = null; 495 496 if (ejbLink.indexOf(LINK_SEPARATOR) == -1) { 498 BeanDesc bd = null; 499 if (earCl == null && deploymentDesc == null) { 501 throw new DeploymentDescException("Deployment desc for file ejb-jar '" + currentFile.getFile() + "' not found"); 502 } 503 if (deploymentDesc != null) { 505 bd = deploymentDesc.getBeanDesc(ejbLink); 506 } 507 String url = currentFile.getFile(); 508 URLClassLoader earClassLoader = null; 509 URLClassLoader ejbLoader = (URLClassLoader ) urlEjbCLBindings.get(url); 511 if (earCl != null) { 512 earClassLoader = (URLClassLoader ) earCl; 513 urlEarCLBindings.put(url, earCl); 515 } else { 516 earClassLoader = (URLClassLoader ) urlEarCLBindings.get(url); 517 } 518 519 if ((earClassLoader == null) && (bd == null)) { 521 String err = "Ejb-link " + ejbLink + " not found inside the file " + currentFile.getFile() + ". The bean doesn't exists."; 522 throw new DeploymentDescException(err); 523 } 524 525 if (bd == null) { 527 if (logger.isLoggable(BasicLevel.DEBUG)) { 528 logger.log(BasicLevel.DEBUG, "The bean '" + ejbLink + "' was not found in the current ejbjar, searching on all the ejbjars."); 529 } 530 Vector v = (Vector ) earCLEjbLinkJar.get(earClassLoader); 531 DeploymentDesc lookupDD = null; 532 String fileName = null; 533 URL urlRead = null; 534 String urlReadString = null; 535 boolean found = false; 536 BeanDesc bdtmp = null; 537 for (Enumeration e = v.elements(); e.hasMoreElements();) { 538 urlReadString = (String ) e.nextElement(); 539 File f = new File (urlReadString); 540 try { 541 urlRead = f.toURL(); 542 } catch (Exception ue) { 543 throw new DeploymentDescException("Cannot make an url with the argument'" + urlReadString + "'."); 544 } 545 if (ejbLoader == null) { 547 ejbLoader = (URLClassLoader ) urlEjbCLBindings.get(urlReadString); 548 } 549 if (ejbLoader == null) { 551 throw new DeploymentDescException("Error while resolving ejb-link. The ejb classloader is not found for url '" + urlReadString + "'."); 552 } 553 554 urlEjbCLBindings.put(urlReadString, ejbLoader); 556 557 fileName = urlRead.getFile(); 558 if (!fileName.equals(url)) { 560 if (f.isDirectory()) { 563 lookupDD = getDeploymentDescriptor(fileName + EJB_JAR_FILE_NAME, 564 BeanNaming.getJonasXmlName(fileName + EJB_JAR_FILE_NAME), 565 ejbLoader, 566 f.getAbsolutePath()); 567 } else if (fileName.toLowerCase().endsWith(".xml")) { 568 File parent = f.getParentFile().getParentFile(); 571 lookupDD = getDeploymentDescriptor(fileName, 572 BeanNaming.getJonasXmlName(fileName), 573 ejbLoader, 574 parent.getAbsolutePath()); 575 } else { 576 String altname = null; 579 URL altDDUrl = (URL ) urlAltDDBindings.get(url); 580 if (altDDUrl != null) { 581 altname = altDDUrl.getFile(); 582 } 583 lookupDD = getDeploymentDescriptor(fileName, 584 ejbLoader, 585 altname); 586 } 587 588 if (lookupDD != null) { 589 bdtmp = lookupDD.getBeanDesc(ejbLink); 590 if (bdtmp != null) { 591 if (logger.isLoggable(BasicLevel.DEBUG)) { 592 logger.log(BasicLevel.DEBUG, "Found a BeanDesc in the Deployment Desc." + urlRead); 593 } 594 if (found) { 596 String err = "There are more than one bean with the name '" + ejbLink + "' which were found in all the ejbjars of this EAR."; 597 throw new DeploymentDescException(err); 598 } else { 599 found = true; 600 bd = bdtmp; 601 } 602 } else { 603 if (logger.isLoggable(BasicLevel.DEBUG)) { 604 logger.log(BasicLevel.DEBUG, "No BeanDesc found in the Deployment Desc." + urlRead); 605 } 606 } 607 } 608 } 609 } 610 if (!found) { 611 String err = "No ejblink was found for '" + ejbLink + "' in all the ejbjars of this EAR."; 612 throw new DeploymentDescException(err); 613 } 614 } 615 616 if (logger.isLoggable(BasicLevel.DEBUG)) { 617 logger.log(BasicLevel.DEBUG, "BeanDesc found = " + bd.getEjbName()); 618 } 619 checkType(currentFile, ejbType, bd); 621 622 if (bd == null) { 623 String err = "Ejb-link " + ejbLink + " not found inside the file " + currentFile.getFile() + ". The bean doesn't exists."; 624 throw new DeploymentDescException(err); 625 } 626 627 String jndiname; 628 if (isEjbRef) { 629 jndiname = bd.getJndiName(); 630 } else { 631 jndiname = bd.getJndiLocalName(); 632 } 633 return jndiname; 634 } 635 636 if (earCl != null) { 637 String url = currentFile.getFile(); 638 urlEarCLBindings.put(url, earCl); 640 } 641 642 StringTokenizer st = new StringTokenizer (ejbLink, LINK_SEPARATOR); 644 645 if (st.countTokens() != 2 || ejbLink.startsWith(LINK_SEPARATOR) 648 || ejbLink.endsWith(LINK_SEPARATOR)) { 649 650 String err = "Ejb link " + ejbLink + " has a bad format. Correct format : filename.jar#beanName."; 651 throw new DeploymentDescException(err); 652 } 653 654 ejbJarLink = st.nextToken(); 656 beanNameLink = st.nextToken(); 657 658 if (!ejbJarLink.endsWith(".jar")) { 660 String err = "Ejbjar filename " + ejbJarLink + " from the ejb-link " + ejbLink + " has a bad format. Correct format : filename.jar"; 661 throw new DeploymentDescException(err); 662 } 663 664 665 URL ejbJarLinkUrl = null; 668 try { 669 ejbJarLinkUrl = new File ![]() |