1 22 package org.jboss.ejb; 23 24 import java.net.URL ; 25 import java.net.MalformedURLException ; 26 27 import java.util.Iterator ; 28 import java.util.StringTokenizer ; 29 30 import javax.management.MBeanServer ; 31 32 import org.jboss.deployers.spi.deployer.DeploymentUnit; 33 import org.jboss.deployers.spi.structure.DeploymentContext; 34 import org.jboss.deployment.DeploymentInfo; 35 import org.jboss.deployment.MainDeployerMBean; 36 import org.jboss.logging.Logger; 37 import org.jboss.metadata.ApplicationMetaData; 38 import org.jboss.metadata.BeanMetaData; 39 import org.jboss.metadata.MessageDestinationMetaData; 40 import org.jboss.metadata.WebMetaData; 41 import org.jboss.util.Strings; 42 43 51 public final class EjbUtil 52 { 53 private static final Logger log = Logger.getLogger(EjbUtil.class); 54 55 65 public static String findEjbLink(MBeanServer server, DeploymentInfo di, String link) 66 { 67 return resolveLink(server, di, link, false); 68 } 69 public static String findEjbLink(MBeanServer server, DeploymentUnit unit, String link) 70 { 71 return resolveLink(server, unit, link, false); 72 } 73 74 84 public static String findLocalEjbLink(MBeanServer server, DeploymentInfo di, String link) 85 { 86 return resolveLink(server, di, link, true); 87 } 88 public static String findLocalEjbLink(MBeanServer server, DeploymentUnit unit, String link) 89 { 90 return resolveLink(server, unit, link, true); 91 } 92 93 103 public static MessageDestinationMetaData findMessageDestination(MBeanServer server, DeploymentInfo di, String link) 104 { 105 return resolveMessageDestination(server, di, link); 106 } 107 public static MessageDestinationMetaData findMessageDestination(MBeanServer server, DeploymentUnit unit, String link) 108 { 109 return resolveMessageDestination(server, unit, link); 110 } 111 112 private static String resolveLink(MBeanServer server, DeploymentInfo di, String link, boolean isLocal) 113 { 114 if (link == null) 115 { 116 return null; 117 } 118 119 if (log.isTraceEnabled()) 120 { 121 log.trace("resolveLink( {" + di + "}, {" + link + "}, {" + isLocal + "}"); 122 } 123 124 if (di == null) 125 { 126 return null; 128 } 129 130 if (link.indexOf('#') != -1) 131 { 132 return resolveRelativeLink(server, di, link, isLocal); 134 } 135 else 136 { 137 DeploymentInfo top = di; 139 while (top.parent != null) 140 { 141 top = top.parent; 142 } 143 144 return resolveAbsoluteLink(top, link, isLocal); 145 } 146 } 147 private static String resolveLink(MBeanServer server, DeploymentUnit unit, String link, boolean isLocal) 148 { 149 if (link == null) 150 { 151 return null; 152 } 153 154 if (log.isTraceEnabled()) 155 { 156 log.trace("resolveLink( {" + unit + "}, {" + link + "}, {" + isLocal + "}"); 157 } 158 159 if (unit == null) 160 { 161 return null; 163 } 164 165 if (link.indexOf('#') != -1) 166 { 167 return resolveRelativeLink(server, unit, link, isLocal); 169 } 170 else 171 { 172 DeploymentContext top = unit.getDeploymentContext(); 174 while (top.getParent() != null) 175 { 176 top = top.getParent(); 177 } 178 179 return resolveAbsoluteLink(top, link, isLocal); 180 } 181 } 182 183 private static String resolveRelativeLink(MBeanServer server, DeploymentInfo di, String link, boolean isLocal) 184 { 185 186 String path = link.substring(0, link.indexOf('#')); 187 String ejbName = link.substring(link.indexOf('#') + 1); 188 String us = di.url.toString(); 189 190 if (us.charAt(us.length() - 1) == '/') 192 us = us.substring(0, us.length() - 1); 193 194 String ourPath = us.substring(0, us.lastIndexOf('/')); 195 196 if (log.isTraceEnabled()) 197 { 198 log.trace("Resolving relative link: " + link); 199 log.trace("Looking for: '" + link + "', we're located at: '" + ourPath + "'"); 200 } 201 202 for (StringTokenizer st = new StringTokenizer (path, "/"); st.hasMoreTokens();) 203 { 204 String s = st.nextToken(); 205 if (s.equals("..")) 206 { 207 ourPath = ourPath.substring(0, ourPath.lastIndexOf('/')); 208 } 209 else 210 { 211 ourPath += "/" + s; 212 } 213 } 214 215 URL target = null; 216 217 try 218 { 219 target = Strings.toURL(ourPath); 220 } 221 catch (MalformedURLException mue) 222 { 223 log.warn("Can't construct URL for: " + ourPath); 224 return null; 225 } 226 227 DeploymentInfo targetInfo = null; 228 try 229 { 230 targetInfo = (DeploymentInfo) server.invoke(MainDeployerMBean.OBJECT_NAME, "getDeployment", new Object [] 231 {target}, new String [] 232 {URL .class.getName()}); 233 } 234 catch (Exception e) 235 { 236 log.warn("Got Exception when looking for DeploymentInfo: " + e); 237 return null; 238 } 239 240 if (targetInfo == null) 241 { 242 log.warn("Can't locate deploymentInfo for target: " + target); 243 return null; 244 } 245 246 if (log.isTraceEnabled()) 247 { 248 log.trace("Found appropriate DeploymentInfo: " + targetInfo); 249 } 250 251 String linkTarget = null; 252 if (targetInfo.metaData instanceof ApplicationMetaData) 253 { 254 ApplicationMetaData appMD = (ApplicationMetaData) targetInfo.metaData; 255 BeanMetaData beanMD = appMD.getBeanByEjbName(ejbName); 256 257 if (beanMD != null) 258 { 259 linkTarget = getJndiName(beanMD, isLocal); 260 } 261 else 262 { 263 log.warn("No Bean named '" + ejbName + "' found in '" + path + "'!"); 264 } 265 } 266 else 267 { 268 log.warn("DeploymentInfo " + targetInfo + " is not an EJB .jar " + "file!"); 269 } 270 271 return linkTarget; 272 } 273 274 private static String resolveAbsoluteLink(DeploymentInfo di, String link, boolean isLocal) 275 { 276 if (log.isTraceEnabled()) 277 { 278 log.trace("Resolving absolute link, di: " + di); 279 } 280 281 String ejbName = null; 282 283 if (di.metaData instanceof ApplicationMetaData) 285 { 286 ApplicationMetaData appMD = (ApplicationMetaData) di.metaData; 288 BeanMetaData beanMD = appMD.getBeanByEjbName(link); 289 if (beanMD != null) 290 { 291 ejbName = getJndiName(beanMD, isLocal); 292 if (log.isTraceEnabled()) 293 { 294 log.trace("Found Bean: " + beanMD + ", resolves to: " + ejbName); 295 } 296 297 return ejbName; 298 } 299 else if (log.isTraceEnabled()) 300 { 301 log.trace("No match for ejb-link: " + link+", module names:"); 303 Iterator iter = appMD.getEnterpriseBeans(); 304 while (iter.hasNext()) 305 { 306 beanMD = (BeanMetaData) iter.next(); 307 String beanEjbName = getJndiName(beanMD, isLocal); 308 log.trace("... ejbName: " + beanEjbName); 309 } 310 } 311 } 312 313 Iterator it = di.subDeployments.iterator(); 315 while (it.hasNext() && ejbName == null) 316 { 317 DeploymentInfo child = (DeploymentInfo) it.next(); 318 ejbName = resolveAbsoluteLink(child, link, isLocal); 319 } 320 321 return ejbName; 322 } 323 324 private static String getJndiName(BeanMetaData beanMD, boolean isLocal) 325 { 326 String jndiName = null; 327 if (isLocal) 328 { 329 String localHome = beanMD.getLocalHome(); 331 if (localHome != null) 332 jndiName = beanMD.getLocalJndiName(); 333 else 334 { 335 log 336 .warn("LocalHome jndi name requested for: '" + beanMD.getEjbName() 337 + "' but there is no LocalHome class"); 338 } 339 } 340 else 341 { 342 jndiName = beanMD.getJndiName(); 343 } 344 return jndiName; 345 } 346 347 private static MessageDestinationMetaData resolveMessageDestination(MBeanServer server, DeploymentInfo di, String link) 348 { 349 if (link == null) 350 return null; 351 352 if (log.isTraceEnabled()) 353 log.trace("resolveLink( {" + di + "}, {" + link + "})"); 354 355 if (di == null) 356 return null; 358 359 if (link.indexOf('#') != -1) 360 return resolveRelativeMessageDestination(server, di, link); 362 else 363 { 364 DeploymentInfo top = di; 366 while (top.parent != null) 367 top = top.parent; 368 369 return resolveAbsoluteMessageDestination(top, link); 370 } 371 } 372 private static MessageDestinationMetaData resolveMessageDestination(MBeanServer server, DeploymentUnit unit, String link) 373 { 374 if (link == null) 375 return null; 376 377 if (log.isTraceEnabled()) 378 log.trace("resolveLink( {" + unit + "}, {" + link + "})"); 379 380 if (unit == null) 381 return null; 383 384 if (link.indexOf('#') != -1) 385 return resolveRelativeMessageDestination(server, unit, link); 387 else 388 { 389 DeploymentContext top = unit.getDeploymentContext(); 391 while (top != null) 392 { 393 top = top.getParent(); 394 } 395 396 return resolveAbsoluteMessageDestination(top.getDeploymentUnit(), link); 397 } 398 } 399 400 private static MessageDestinationMetaData resolveRelativeMessageDestination(MBeanServer server, DeploymentInfo di, String link) 401 { 402 String path = link.substring(0, link.indexOf('#')); 403 String destinationName = link.substring(link.indexOf('#') + 1); 404 String us = di.url.toString(); 405 406 if (us.charAt(us.length() - 1) == '/') 408 us = us.substring(0, us.length() - 1); 409 410 String ourPath = us.substring(0, us.lastIndexOf('/')); 411 412 if (log.isTraceEnabled()) 413 { 414 log.trace("Resolving relative message-destination-link: " + link); 415 log.trace("Looking for: '" + link + "', we're located at: '" + ourPath + "'"); 416 } 417 418 for (StringTokenizer st = new StringTokenizer (path, "/"); st.hasMoreTokens();) 419 { 420 String s = st.nextToken(); 421 if (s.equals("..")) 422 ourPath = ourPath.substring(0, ourPath.lastIndexOf('/')); 423 else 424 ourPath += "/" + s; 425 } 426 427 URL target = null; 428 try 429 { 430 target = Strings.toURL(ourPath); 431 } 432 catch (MalformedURLException mue) 433 { 434 log.warn("Can't construct URL for: " + ourPath); 435 return null; 436 } 437 438 DeploymentInfo targetInfo = null; 439 try 440 { 441 targetInfo = (DeploymentInfo) server.invoke 442 ( 443 MainDeployerMBean.OBJECT_NAME, 444 "getDeployment", 445 new Object [] {target}, 446 new String [] {URL .class.getName()} 447 ); 448 } 449 catch (Exception e) 450 { 451 log.warn("Got Exception when looking for DeploymentInfo: " + e); 452 return null; 453 } 454 455 if (targetInfo == null) 456 { 457 log.warn("Can't locate deploymentInfo for target: " + target); 458 return null; 459 } 460 461 if (log.isTraceEnabled()) 462 log.trace("Found appropriate DeploymentInfo: " + targetInfo); 463 464 if (targetInfo.metaData instanceof ApplicationMetaData) 465 { 466 ApplicationMetaData appMD = (ApplicationMetaData) targetInfo.metaData; 467 return appMD.getMessageDestination(destinationName); 468 } 469 else if (targetInfo.metaData instanceof WebMetaData) 470 { 471 WebMetaData webMD = (WebMetaData) targetInfo.metaData; 472 return webMD.getMessageDestination(destinationName); 473 } 474 else 475 { 476 log.warn("DeploymentInfo " + targetInfo + " is not an EJB .jar " + "file!"); 477 return null; 478 } 479 } 480 private static MessageDestinationMetaData resolveRelativeMessageDestination(MBeanServer server, DeploymentUnit unit, String link) 481 { 482 String path = link.substring(0, link.indexOf('#')); 483 String destinationName = link.substring(link.indexOf('#') + 1); 484 String us = unit.getName(); 485 486 if (us.charAt(us.length() - 1) == '/') 488 us = us.substring(0, us.length() - 1); 489 490 String ourPath = us.substring(0, us.lastIndexOf('/')); 491 492 if (log.isTraceEnabled()) 493 { 494 log.trace("Resolving relative message-destination-link: " + link); 495 log.trace("Looking for: '" + link + "', we're located at: '" + ourPath + "'"); 496 } 497 498 for (StringTokenizer st = new StringTokenizer (path, "/"); st.hasMoreTokens();) 499 { 500 String s = st.nextToken(); 501 if (s.equals("..")) 502 ourPath = ourPath.substring(0, ourPath.lastIndexOf('/')); 503 else 504 ourPath += "/" + s; 505 } 506 507 URL target = null; 508 try 509 { 510 target = Strings.toURL(ourPath); 511 } 512 catch (MalformedURLException mue) 513 { 514 log.warn("Can't construct URL for: " + ourPath); 515 return null; 516 } 517 518 DeploymentInfo targetInfo = null; 519 try 520 { 521 targetInfo = (DeploymentInfo) server.invoke 522 ( 523 MainDeployerMBean.OBJECT_NAME, 524 "getDeployment", 525 new Object [] {target}, 526 new String [] {URL .class.getName()} 527 ); 528 } 529 catch (Exception e) 530 { 531 log.warn("Got Exception when looking for DeploymentInfo: " + e); 532 return null; 533 } 534 535 if (targetInfo == null) 536 { 537 log.warn("Can't locate deploymentInfo for target: " + target); 538 return null; 539 } 540 541 if (log.isTraceEnabled()) 542 log.trace("Found appropriate DeploymentInfo: " + targetInfo); 543 544 if (targetInfo.metaData instanceof ApplicationMetaData) 545 { 546 ApplicationMetaData appMD = (ApplicationMetaData) targetInfo.metaData; 547 return appMD.getMessageDestination(destinationName); 548 } 549 else if (targetInfo.metaData instanceof WebMetaData) 550 { 551 WebMetaData webMD = (WebMetaData) targetInfo.metaData; 552 return webMD.getMessageDestination(destinationName); 553 } 554 else 555 { 556 log.warn("DeploymentInfo " + targetInfo + " is not an EJB .jar " + "file!"); 557 return null; 558 } 559 } 560 561 private static MessageDestinationMetaData resolveAbsoluteMessageDestination(DeploymentInfo di, String link) 562 { 563 if (log.isTraceEnabled()) 564 log.trace("Resolving absolute link, di: " + di); 565 566 if (di.metaData instanceof ApplicationMetaData) 568 { 569 ApplicationMetaData appMD = (ApplicationMetaData) di.metaData; 570 MessageDestinationMetaData mdMD = appMD.getMessageDestination(link); 571 if (mdMD != null) 572 return mdMD; 573 } 574 else if (di.metaData instanceof WebMetaData) 575 { 576 WebMetaData webMD = (WebMetaData) di.metaData; 577 return webMD.getMessageDestination(link); 578 } 579 580 Iterator it = di.subDeployments.iterator(); 582 while (it.hasNext()) 583 { 584 DeploymentInfo child = (DeploymentInfo) it.next(); 585 MessageDestinationMetaData mdMD = resolveAbsoluteMessageDestination(child, link); 586 if (mdMD != null) 587 return mdMD; 588 } 589 590 return null; 592 } 593 private static MessageDestinationMetaData resolveAbsoluteMessageDestination(DeploymentUnit unit, String link) 594 { 595 if (log.isTraceEnabled()) 596 log.trace("Resolving absolute link, unit: " + unit); 597 598 ApplicationMetaData appMD = unit.getAttachment(ApplicationMetaData.class); 600 if (appMD != null) 601 { 602 MessageDestinationMetaData mdMD = appMD.getMessageDestination(link); 603 if (mdMD != null) 604 return mdMD; 605 } 606 WebMetaData webMD = unit.getAttachment(WebMetaData.class); 607 if( webMD != null ) 608 { 609 return webMD.getMessageDestination(link); 610 } 611 612 Iterator <DeploymentContext> it = unit.getDeploymentContext().getChildren().iterator(); 614 while (it.hasNext()) 615 { 616 DeploymentContext child = it.next(); 617 MessageDestinationMetaData mdMD = resolveAbsoluteMessageDestination(child.getDeploymentUnit(), link); 618 if (mdMD != null) 619 return mdMD; 620 } 621 622 return null; 624 } 625 626 private static String resolveRelativeLink(MBeanServer server, DeploymentUnit unit, String link, boolean isLocal) 627 { 628 629 String path = link.substring(0, link.indexOf('#')); 630 String ejbName = link.substring(link.indexOf('#') + 1); 631 String us = unit.getName(); 632 633 if (us.charAt(us.length() - 1) == '/') 635 us = us.substring(0, us.length() - 1); 636 637 String ourPath = us.substring(0, us.lastIndexOf('/')); 638 639 if (log.isTraceEnabled()) 640 { 641 log.trace("Resolving relative link: " + link); 642 log.trace("Looking for: '" + link + "', we're located at: '" + ourPath + "'"); 643 } 644 645 for (StringTokenizer st = new StringTokenizer (path, "/"); st.hasMoreTokens();) 646 { 647 String s = st.nextToken(); 648 if (s.equals("..")) 649 { 650 ourPath = ourPath.substring(0, ourPath.lastIndexOf('/')); 651 } 652 else 653 { 654 ourPath += "/" + s; 655 } 656 } 657 658 URL target = null; 659 660 try 661 { 662 target = Strings.toURL(ourPath); 663 } 664 catch (MalformedURLException mue) 665 { 666 log.warn("Can't construct URL for: " + ourPath); 667 return null; 668 } 669 670 DeploymentInfo targetInfo = null; 671 try 672 { 673 targetInfo = (DeploymentInfo) server.invoke(MainDeployerMBean.OBJECT_NAME, "getDeployment", new Object [] 674 {target}, new String [] 675 {URL .class.getName()}); 676 } 677 catch (Exception e) 678 { 679 log.warn("Got Exception when looking for DeploymentInfo: " + e); 680 return null; 681 } 682 683 if (targetInfo == null) 684 { 685 log.warn("Can't locate deploymentInfo for target: " + target); 686 return null; 687 } 688 689 if (log.isTraceEnabled()) 690 { 691 log.trace("Found appropriate DeploymentInfo: " + targetInfo); 692 } 693 694 String linkTarget = null; 695 if (targetInfo.metaData instanceof ApplicationMetaData) 696 { 697 ApplicationMetaData appMD = (ApplicationMetaData) targetInfo.metaData; 698 BeanMetaData beanMD = appMD.getBeanByEjbName(ejbName); 699 700 if (beanMD != null) 701 { 702 linkTarget = getJndiName(beanMD, isLocal); 703 } 704 else 705 { 706 log.warn("No Bean named '" + ejbName + "' found in '" + path + "'!"); 707 } 708 } 709 else 710 { 711 log.warn("DeploymentInfo " + targetInfo + " is not an EJB .jar " + "file!"); 712 } 713 714 return linkTarget; 715 } 716 717 private static String resolveAbsoluteLink(DeploymentContext ctx, String link, boolean isLocal) 718 { 719 if(ctx == null) 720 throw new IllegalArgumentException ("deployment context passed is null"); 721 722 if (log.isTraceEnabled()) 723 { 724 log.trace("Resolving absolute link, ctx: " + ctx); 725 } 726 727 String ejbName = null; 728 729 ApplicationMetaData appMD = ctx.getDeploymentUnit().getAttachment(ApplicationMetaData.class); 730 if (appMD != null) 731 { 732 BeanMetaData beanMD = appMD.getBeanByEjbName(link); 734 if (beanMD != null) 735 { 736 ejbName = getJndiName(beanMD, isLocal); 737 if (log.isTraceEnabled()) 738 { 739 log.trace("Found Bean: " + beanMD + ", resolves to: " + ejbName); 740 } 741 742 return ejbName; 743 } 744 else if (log.isTraceEnabled()) 745 { 746 log.trace("No match for ejb-link: " + link+", module names:"); 748 Iterator iter = appMD.getEnterpriseBeans(); 749 while (iter.hasNext()) 750 { 751 beanMD = (BeanMetaData) iter.next(); 752 String beanEjbName = getJndiName(beanMD, isLocal); 753 log.trace("... ejbName: " + beanEjbName); 754 } 755 } 756 } 757 758 Iterator it = ctx.getChildren().iterator(); 760 while (it.hasNext() && ejbName == null) 761 { 762 DeploymentContext child = (DeploymentContext)it.next(); 763 ejbName = resolveAbsoluteLink(child, link, isLocal); 764 } 765 766 return ejbName; 767 } 768 769 } | Popular Tags |