1 26 27 package org.objectweb.jonas_client.deployment.lib; 28 29 import java.io.File ; 31 import java.io.FileInputStream ; 32 import java.io.FileNotFoundException ; 33 import java.io.IOException ; 34 import java.io.InputStream ; 35 import java.io.InputStreamReader ; 36 import java.io.Reader ; 37 import java.net.MalformedURLException ; 38 import java.net.URL ; 39 import java.net.URLClassLoader ; 40 import java.util.Enumeration ; 41 import java.util.Hashtable ; 42 import java.util.List ; 43 import java.util.StringTokenizer ; 44 import java.util.jar.JarFile ; 45 import java.util.zip.ZipEntry ; 46 47 import org.objectweb.jonas_client.deployment.api.ClientContainerDeploymentDesc; 48 import org.objectweb.jonas_client.deployment.api.ClientContainerDeploymentDescException; 49 import org.objectweb.jonas_client.deployment.rules.ApplicationClientRuleSet; 50 import org.objectweb.jonas_client.deployment.rules.JonasClientRuleSet; 51 import org.objectweb.jonas_client.deployment.xml.ApplicationClient; 52 import org.objectweb.jonas_client.deployment.xml.JonasClient; 53 54 import org.objectweb.jonas_ejb.deployment.api.DeploymentDesc; 55 import org.objectweb.jonas_ejb.deployment.lib.EjbDeploymentDescManager; 56 57 import org.objectweb.jonas_lib.deployment.api.DeploymentDescException; 58 import org.objectweb.jonas_lib.deployment.api.EjbRefDesc; 59 import org.objectweb.jonas_lib.deployment.api.MessageDestinationRefDesc; 60 import org.objectweb.jonas_lib.deployment.digester.JDigester; 61 import org.objectweb.jonas_lib.deployment.lib.AbsDeploymentDescManager; 62 import org.objectweb.jonas_lib.deployment.xml.JonasMessageDestination; 63 import org.objectweb.jonas_lib.loader.WebappClassLoader; 64 65 import org.objectweb.jonas_ws.deployment.api.PortComponentDesc; 66 import org.objectweb.jonas_ws.deployment.api.PortComponentRefDesc; 67 import org.objectweb.jonas_ws.deployment.api.ServiceDesc; 68 import org.objectweb.jonas_ws.deployment.api.ServiceRefDesc; 69 import org.objectweb.jonas_ws.deployment.api.WSDeploymentDesc; 70 import org.objectweb.jonas_ws.deployment.lib.WSDeploymentDescManager; 71 72 76 public class ClientDeploymentDescManager extends AbsDeploymentDescManager { 77 78 81 private static ClientDeploymentDescManager unique; 82 83 86 private EjbDeploymentDescManager ejbDDManager = null; 87 88 92 private Hashtable earCLAltDDBindings = null; 93 94 97 public static final String CLIENT_FILE_NAME = "META-INF/application-client.xml"; 98 99 102 public static final String JONAS_CLIENT_FILE_NAME = "META-INF/jonas-client.xml"; 103 104 107 private static JDigester appClientDigester = null; 108 109 112 private static JDigester jonasAppClientDigester = null; 113 114 117 private static boolean parsingWithValidation = true; 118 119 122 private static ApplicationClientRuleSet appClientRuleSet = new ApplicationClientRuleSet(); 123 124 127 private static JonasClientRuleSet jonasAppClientRuleSet = new JonasClientRuleSet(); 128 129 132 private ClientDeploymentDescManager() { 133 ejbDDManager = EjbDeploymentDescManager.getInstance(); 134 earCLAltDDBindings = new Hashtable (); 135 } 136 137 141 public static ClientDeploymentDescManager getInstance() { 142 if (unique == null) { 143 unique = new ClientDeploymentDescManager(); 144 } 145 return unique; 146 } 147 148 158 public ClientContainerDeploymentDesc getDeploymentDesc(URL url, ClassLoader loaderForCls, ClassLoader earLoader) 159 throws DeploymentDescException { 160 161 if (!new File (url.getFile()).exists()) { 163 String err = "Cannot get the deployment descriptor for "; 164 err = err + "'" + url.getFile() + "'. The file doesn't exist."; 165 throw new ClientContainerDeploymentDescException(err); 166 } 167 168 URL altDDUrl = null; 170 171 Hashtable urlAltddBindings = null; 173 if (earLoader != null) { 174 urlAltddBindings = (Hashtable ) earCLAltDDBindings.get(earLoader); 176 if (urlAltddBindings == null) { 177 String err = "Cannot find if there is alt-dd for '" + url.getFile() 180 + "', the setAltDD function was badly called"; 181 throw new ClientContainerDeploymentDescException(err); 182 } 183 altDDUrl = (URL ) urlAltddBindings.get(url); 185 } 186 187 ClientContainerDeploymentDesc clientDD = null; 191 try { 192 if (altDDUrl != null) { 193 clientDD = getInstance(url.getFile(), loaderForCls, altDDUrl.getFile()); 194 } else { 195 clientDD = getInstance(url.getFile(), loaderForCls); 196 } 197 } catch (org.objectweb.jonas_lib.deployment.api.DeploymentDescException dde) { 198 throw new ClientContainerDeploymentDescException(dde); 199 } 200 201 EjbRefDesc[] ejbRef = clientDD.getEjbRefDesc(); 203 for (int i = 0; i < ejbRef.length; i++) { 204 if (ejbRef[i].getJndiName() == null) { 205 String ejbLink = ejbRef[i].getEjbLink(); 206 String ejbRefType = ejbRef[i].getEjbRefType(); 207 if (ejbLink != null) { 208 if (earLoader == null) { 209 throw new ClientContainerDeploymentDescException( 210 "Ejb-link is not authorized from a single client jar. The client jar must be in an ear."); 211 } else { 212 String jndiName = getJndiName(url, ejbLink, earLoader, ejbRefType); 213 ejbRef[i].setJndiName(jndiName); 214 } 215 } 216 } 217 } 218 ServiceRefDesc[] serviceRef = clientDD.getServiceRefDesc(); 220 221 for (int i = 0; i < serviceRef.length; i++) { 222 223 List pcRefs = serviceRef[i].getPortComponentRefs(); 224 for (int j = 0; j < pcRefs.size(); j++) { 225 PortComponentRefDesc pcr = (PortComponentRefDesc) pcRefs.get(j); 227 String pclink = pcr.getPortComponentLink(); 228 if (pclink != null) { 229 PortComponentDesc pcDesc = getPCDesc(url, pclink, earLoader); 231 pcr.setPortComponentDesc(pcDesc); 232 } 233 } 234 } 235 236 237 MessageDestinationRefDesc[] mdRef = clientDD.getMessageDestinationRefDesc(); 239 for (int i = 0; i < mdRef.length; i++) { 240 if (mdRef[i].getJndiName() == null) { 241 String jndiName = mdRef[i].getJndiName(); 242 String mdLink = mdRef[i].getMessageDestinationLink(); 243 String mdType = mdRef[i].getMessageDestinationType(); 244 String mdUsage = mdRef[i].getMessageDestinationUsage(); 245 if (mdLink != null) { 246 if (earLoader == null) { 247 throw new ClientContainerDeploymentDescException( 248 "Message-destination-link is not authorized from a single client jar. The client jar must be in an ear."); 249 } else { 250 String mdName = getMDJndiName(url, mdLink, mdType, mdUsage, earLoader); 251 mdRef[i].setJndiName(jndiName); 254 } 255 } 256 } 257 } 258 return clientDD; 259 } 260 271 private PortComponentDesc getPCDesc(URL warURL, String pcLink, ClassLoader earLoader) 272 throws DeploymentDescException { 273 274 String moduleLink = null; 278 String pcNameLink = null; 279 280 if ((pcLink.toLowerCase().indexOf(".war" + LINK_SEPARATOR) == -1) 282 && (pcLink.toLowerCase().indexOf(".jar" + LINK_SEPARATOR) == -1)) { 283 String err = "PC-link " + pcLink 285 + " has a bad format. Correct format : filename.(jar|war)#portComponentName"; 286 throw new DeploymentDescException(err); 287 } 288 StringTokenizer st = new StringTokenizer (pcLink, LINK_SEPARATOR); 289 290 if (st.countTokens() != 2 || pcLink.startsWith(LINK_SEPARATOR) || pcLink.endsWith(LINK_SEPARATOR)) { 293 String err = "PC-link " + pcLink 294 + " has a bad format. Correct format : filename.[jar or war]#portComponentName"; 295 throw new DeploymentDescException(err); 296 } 297 298 moduleLink = st.nextToken(); 300 pcNameLink = st.nextToken(); 301 302 URL moduleLinkUrl = null; 305 try { 306 moduleLinkUrl = new File (new File (warURL.getFile()).getParent() + File.separator + moduleLink) 307 .getCanonicalFile().toURL(); 308 } catch (MalformedURLException mue) { 309 String err = "Error when creating an url for the module filename. Error :" + mue.getMessage(); 310 throw new DeploymentDescException(err); 311 } catch (IOException ioe) { 312 String err = "Error when creating/accessing a file. Error :" + ioe.getMessage(); 313 throw new DeploymentDescException(err); 314 } 315 316 if (!new File (moduleLinkUrl.getFile()).exists()) { 318 String err = "Cannot get the deployment descriptor for '" + moduleLinkUrl.getFile() 319 + "'. The file doesn't exist."; 320 throw new DeploymentDescException(err); 321 } 322 323 ClassLoader loaderForCls = null; 326 ClassLoader current = Thread.currentThread().getContextClassLoader(); 327 if (moduleLink.toLowerCase().endsWith(".war")) { 328 try { 329 loaderForCls = new WebappClassLoader(moduleLinkUrl, current); 331 } catch (IOException ioe) { 332 throw new DeploymentDescException("Unable to create Web ClassLoader", ioe); 333 } 334 } else { 335 loaderForCls = current; 337 } 338 339 WSDeploymentDesc wsDD = null; 340 341 try { 342 wsDD = WSDeploymentDescManager.getInstance().getDeploymentDesc(moduleLinkUrl, loaderForCls, earLoader); 343 } catch (DeploymentDescException e) { 344 String err = "Cannot get the deployment descriptor for '" + moduleLinkUrl.getFile() + "'."; 345 throw new DeploymentDescException(err, e); 346 } 347 if (wsDD == null) { 348 String err = "Port component link " + pcNameLink + " not found in " + moduleLinkUrl.getFile(); 350 throw new DeploymentDescException(err); 351 } 352 353 List sdl = wsDD.getServiceDescs(); 355 boolean isFound = false; 356 PortComponentDesc pcd = null; 357 for (int i = 0; (i < sdl.size()) && !isFound; i++) { 358 if (sdl.get(i) != null) { 359 pcd = ((ServiceDesc) sdl.get(i)).getPortComponent(pcNameLink); 360 isFound = (pcd != null); 361 } 363 } 364 if (!isFound) { 365 String err = "the port component link " + pcNameLink + " doesn't exist in " + moduleLinkUrl.getFile(); 367 throw new DeploymentDescException(err); 368 } 369 370 return pcd; 371 } 372 373 374 386 public static ClientContainerDeploymentDesc getInstance(String clientFileName, ClassLoader classLoaderForCls) 387 throws DeploymentDescException { 388 389 return getInstance(clientFileName, classLoaderForCls, null); 390 } 391 392 407 public static ClientContainerDeploymentDesc getInstance(String clientFileName, ClassLoader classLoaderForCls, 408 String altClientXmlFilename) 409 410 throws DeploymentDescException { 411 412 JarFile clientFile = null; 414 415 InputStream applicationClientInputStream = null; 417 InputStream jonasClientInputStream = null; 418 419 ZipEntry applicationClientZipEntry = null; 421 ZipEntry jonasClientZipEntry = null; 422 423 ApplicationClient applicationClient; 425 JonasClient jonasClient; 426 427 String xmlContent = ""; 429 String jonasXmlContent = ""; 430 431 File fClient = new File (clientFileName); 433 434 if (!(fClient.exists())) { 436 String err = "' " + clientFileName + "' was not found."; 437 throw new ClientContainerDeploymentDescException(err); 438 } 439 440 if ((altClientXmlFilename != null) && (!new File (altClientXmlFilename).exists())) { 443 String err = "The file for the altdd tag for the EAR case '" + altClientXmlFilename + "' was not found."; 444 throw new ClientContainerDeploymentDescException(err); 445 } 446 447 if (altClientXmlFilename == null) { 452 try { 453 clientFile = new JarFile (clientFileName); 454 455 applicationClientZipEntry = clientFile.getEntry(CLIENT_FILE_NAME); 458 if (applicationClientZipEntry == null) { 459 throw new ClientContainerDeploymentDescException("The entry '" + CLIENT_FILE_NAME 460 + "' was not found in the file '" + clientFileName + "'."); 461 } 462 applicationClientInputStream = clientFile.getInputStream(applicationClientZipEntry); 464 xmlContent = xmlContent(applicationClientInputStream); 465 applicationClientInputStream = clientFile.getInputStream(applicationClientZipEntry); 467 } catch (Exception e) { 468 if (clientFile != null) { 469 try { 470 clientFile.close(); 471 } catch (IOException ioe) { 472 } 474 } 475 throw new ClientContainerDeploymentDescException( 476 "Can not read the XML deployment descriptors of the client jar file '" + clientFileName + "'.", 477 e); 478 } 479 } else { 480 try { 481 applicationClientInputStream = new FileInputStream (altClientXmlFilename); 482 xmlContent = xmlContent(applicationClientInputStream); 483 applicationClientInputStream = new FileInputStream (altClientXmlFilename); 485 } catch (FileNotFoundException ioe) { 486 throw new ClientContainerDeploymentDescException("The altDD file '" + altClientXmlFilename 487 + "' was not found."); 488 } catch (Exception e) { 489 if (applicationClientInputStream != null) { 490 try { 491 applicationClientInputStream.close(); 492 } catch (IOException ioe) { 493 } 495 } 496 throw new ClientContainerDeploymentDescException("Cannot read the XML deployment descriptors of the client jar file '" 497 + clientFileName + "'.", e); 498 } 499 } 500 501 applicationClient = loadApplicationClient(new InputStreamReader (applicationClientInputStream), CLIENT_FILE_NAME); 502 503 try { 504 clientFile = new JarFile (clientFileName); 505 506 jonasClientZipEntry = clientFile.getEntry(JONAS_CLIENT_FILE_NAME); 509 if (jonasClientZipEntry != null) { 510 jonasClientInputStream = clientFile.getInputStream(jonasClientZipEntry); 512 jonasXmlContent = xmlContent(jonasClientInputStream); 513 jonasClientInputStream = clientFile.getInputStream(jonasClientZipEntry); 515 } 516 } catch (Exception e) { 517 if (clientFile != null) { 518 try { 519 clientFile.close(); 520 } catch (IOException ioe) { 521 } 523 } 524 throw new ClientContainerDeploymentDescException( 525 "Can not read the XML deployment descriptors of the client jar file '" + clientFileName + "'.", e); 526 } 527 528 if (jonasClientInputStream != null) { 531 jonasClient = loadJonasClient(new InputStreamReader (jonasClientInputStream), JONAS_CLIENT_FILE_NAME); 532 try { 533 jonasClientInputStream.close(); 534 } catch (IOException e) { 535 } 537 } else { 538 jonasClient = new JonasClient(); 539 } 540 541 ClientContainerDeploymentDesc clientDD = new ClientContainerDeploymentDesc(classLoaderForCls, applicationClient, jonasClient); 543 clientDD.setXmlContent(xmlContent); 544 clientDD.setJOnASXmlContent(jonasXmlContent); 545 return clientDD; 546 } 547 548 557 public static ApplicationClient loadApplicationClient(Reader reader, String fileName) 558 throws DeploymentDescException { 559 560 ApplicationClient appc = new ApplicationClient(); 561 562 if (appClientDigester == null) { 564 appClientDigester = new JDigester(appClientRuleSet, getParsingWithValidation(), true, new AppClientDTDs(), 566 new AppClientSchemas()); 567 } 568 569 try { 570 appClientDigester.parse(reader, fileName, appc); 571 } catch (DeploymentDescException e) { 572 throw e; 573 } finally { 574 appClientDigester.push(null); 575 } 576 return appc; 577 } 578 579 588 public static JonasClient loadJonasClient(Reader reader, String fileName) throws DeploymentDescException { 589 590 JonasClient jc = new JonasClient(); 591 592 if (jonasAppClientDigester == null) { 594 jonasAppClientDigester = new JDigester(jonasAppClientRuleSet, getParsingWithValidation(), true, 595 new JonasAppClientDTDs(), new JonasAppClientSchemas()); 596 } 597 598 try { 599 jonasAppClientDigester.parse(reader, fileName, jc); 600 601 } catch (DeploymentDescException e) { 602 throw e; 603 } finally { 604 jonasAppClientDigester.push(null); 605 } 606 return jc; 607 } 608 609 621 private String getJndiName(URL clientURL, String ejbLink, ClassLoader earLoader, String ejbType) 622 throws DeploymentDescException { 623 return ejbDDManager.getJndiName(clientURL, ejbLink, earLoader, ejbType, null, true); 626 } 627 628 644 private String getMDJndiName(URL clientURL, String mdLink, String mdType, String mdUsage, ClassLoader earLoader) 645 throws ClientContainerDeploymentDescException { 646 647 String ejbJarLink = null; 651 String destNameLink = null; 652 DeploymentDesc dd = null; 653 654 if (mdLink.toLowerCase().indexOf(".jar#") == -1) { 656 String err = "Message-destination-link " + mdLink 657 + " has a bad format. Correct format : filename.jar#messageDestinationName"; 658 throw new ClientContainerDeploymentDescException(err); 659 } 660 661 StringTokenizer st = new StringTokenizer (mdLink, LINK_SEPARATOR); 662 663 if (st.countTokens() != 2 || mdLink.startsWith(LINK_SEPARATOR) || mdLink.endsWith(LINK_SEPARATOR)) { 666 667 String err = "Message-destination-link " + mdLink 668 + " has a bad format. Correct format : filename.jar#messageDestinationName."; 669 throw new ClientContainerDeploymentDescException(err); 670 } 671 672 ejbJarLink = st.nextToken(); 674 destNameLink = st.nextToken(); 675 676 if (!ejbJarLink.endsWith(".jar")) { 678 String err = "Ejbjar filename " + ejbJarLink + " from the message-destination-link " + mdLink 679 + " has a bad format. Correct format : filename.jar"; 680 throw new ClientContainerDeploymentDescException(err); 681 } 682 683 URL ejbJarLinkUrl = null; 687 try { 688 ejbJarLinkUrl = new File (new File (clientURL.getFile()).getParent() + File.separator + ejbJarLink) 689 .getCanonicalFile().toURL(); 690 } catch (MalformedURLException mue) { 691 String err = "Error when creating an url for the ejb jar filename. Error :" + mue.getMessage(); 692 throw new ClientContainerDeploymentDescException(err); 693 } catch (IOException ioe) { 694 String err = "Error when creating/accessing a file. Error :" + ioe.getMessage(); 695 throw new ClientContainerDeploymentDescException(err); 696 } 697 698 if (!new File (ejbJarLinkUrl.getFile()).exists()) { 700 String err = "Cannot get the deployment descriptor for '" + ejbJarLinkUrl.getFile() 701 + "'. The file doesn't exist."; 702 throw new ClientContainerDeploymentDescException(err); 703 } 704 705 URL [] ddURL = new URL [1]; 708 ddURL[0] = ejbJarLinkUrl; 709 URLClassLoader loaderForClsEjb = new URLClassLoader (ddURL, earLoader); 710 try { 711 dd = ejbDDManager.getDeploymentDesc(ejbJarLinkUrl, loaderForClsEjb, earLoader); 712 } catch (DeploymentDescException e) { 713 String err = "Cannot get the deployment descriptor for '" + ejbJarLinkUrl.getFile() + "'."; 714 throw new ClientContainerDeploymentDescException(err, e); 715 } 716 717 JonasMessageDestination md = dd.getJonasMessageDestination(mdLink); 718 719 if (md == null) { 720 String err = "No message-destination-link was found for '" + mdLink + "' in the file " 721 + clientURL.getFile() + " specified."; 722 throw new ClientContainerDeploymentDescException(err); 723 } 724 725 728 return md.getJndiName(); 729 } 730 731 737 public void removeCache(ClassLoader earClassLoader) { 738 earCLAltDDBindings.remove(earClassLoader); 740 741 ejbDDManager.removeCache(earClassLoader); 743 } 744 745 755 public void setAltDD(ClassLoader earClassLoader, URL [] urls, URL [] altDDs) { 756 757 Hashtable urlAltddBindings = new Hashtable (); 759 760 for (int i = 0; i < urls.length; i++) { 762 if (altDDs[i] != null) { 763 urlAltddBindings.put(urls[i], altDDs[i]); 764 } 765 } 766 767 earCLAltDDBindings.put(earClassLoader, urlAltddBindings); 769 770 } 771 772 777 public int getCacheSize() { 778 int bufferSize = 0; 779 780 Enumeration keys = earCLAltDDBindings.keys(); 781 while (keys.hasMoreElements()) { 782 ClassLoader loader = (ClassLoader ) keys.nextElement(); 783 Hashtable hashtab = (Hashtable ) earCLAltDDBindings.get(loader); 784 bufferSize = bufferSize + hashtab.size(); 785 } 786 787 return bufferSize; 788 } 789 790 794 public static boolean getParsingWithValidation() { 795 return parsingWithValidation; 796 } 797 798 802 public static void setParsingWithValidation(boolean validation) { 803 ClientDeploymentDescManager.parsingWithValidation = validation; 804 } 805 } | Popular Tags |