1 23 package com.sun.enterprise.deployment; 24 25 import java.util.*; 26 import java.net.URI ; 27 import java.net.URISyntaxException ; 28 29 import com.sun.enterprise.util.LocalStringManagerImpl; 30 import com.sun.enterprise.deployment.util.ModuleDescriptor; 31 import com.sun.enterprise.util.io.FileUtils; 32 import javax.enterprise.deploy.shared.ModuleType ; 33 import javax.persistence.EntityManagerFactory; 34 35 41 42 public abstract class BundleDescriptor extends RootDeploymentDescriptor implements Roles { 43 44 private static LocalStringManagerImpl localStrings = 45 new LocalStringManagerImpl(BundleDescriptor.class); 46 47 private final static String DEPLOYMENT_DESCRIPTOR_DIR="META-INF"; 48 private final static String WSDL_DIR="wsdl"; 49 50 private final static double ANNOTATION_EJB_VER = 3.0; 52 private final static double ANNOTATION_WAR_VER = 2.5; 53 private final static double ANNOTATION_CAR_VER = 5.0; 54 55 private final String PERSISTENCE_UNIT_NAME_SEPARATOR = "#"; 56 57 private Application application; 58 private Set roles; 59 private Set messageDestinations = new HashSet(); 60 private WebServicesDescriptor webServices = new WebServicesDescriptor(); 61 private boolean fullFlag = false; 62 private boolean fullAttribute = false; 63 64 private Map<String , EntityManagerFactory> entityManagerFactories = 67 new HashMap<String , EntityManagerFactory>(); 68 69 72 private ModuleDescriptor moduleDescriptor; 73 74 77 public BundleDescriptor() { 78 super(); 79 webServices.setBundleDescriptor(this); 80 } 81 82 85 public BundleDescriptor(String name, String description) { 86 super(name, description); 87 webServices.setBundleDescriptor(this); 88 } 89 90 93 public void setApplication (Application a) { 94 if (this.application != null) { 95 this.removeNotificationListener(this.application); 96 } 97 this.application = a; 98 if (this.application != null) { 99 this.addNotificationListener(this.application); 100 } 101 } 102 103 void addBundleDescriptor(BundleDescriptor bundleDescriptor) { 104 this.getRoles().addAll(bundleDescriptor.getRoles()); 105 this.changed(); 106 } 107 108 111 public boolean isApplication() { 112 return false; 113 } 114 115 118 public Application getApplication() { 119 return this.application; 120 } 121 122 126 public void addEntityManagerFactory(String unitName, 127 EntityManagerFactory emf) { 128 129 entityManagerFactories.put(unitName, emf); 130 } 131 132 137 public EntityManagerFactory getEntityManagerFactory(String unitName) { 138 139 return entityManagerFactories.get(unitName); 140 } 141 142 146 public Set<EntityManagerFactory> getEntityManagerFactories() { 147 148 return new HashSet<EntityManagerFactory> 149 (entityManagerFactories.values()); 150 151 } 152 153 154 158 public abstract Set getServiceReferenceDescriptors(); 159 160 164 public WebServicesDescriptor getWebServices() { 165 return webServices; 166 } 167 168 public WebServiceEndpoint getWebServiceEndpointByName(String name) { 169 return webServices.getEndpointByName(name); 170 } 171 172 175 public boolean hasWebServiceClients() { 176 return false; 177 } 178 179 182 public boolean hasWebServices() { 183 return getWebServices().hasWebServices(); 184 } 185 186 187 188 191 public Set getMessageDestinations() { 192 if (this.messageDestinations == null) { 193 this.messageDestinations = new HashSet(); 194 } 195 return this.messageDestinations; 196 } 197 198 201 public boolean hasMessageDestinationByName(String name) { 202 for (Iterator itr = this.getMessageDestinations().iterator(); 203 itr.hasNext();) { 204 Descriptor next = (Descriptor) itr.next(); 205 if (next.getName().equals(name)) { 206 return true; 207 } 208 } 209 return false; 210 } 211 212 216 public MessageDestinationDescriptor getMessageDestinationByName 217 (String name) { 218 for (Iterator itr = this.getMessageDestinations().iterator(); 219 itr.hasNext();) { 220 Descriptor next = (Descriptor) itr.next(); 221 if (next.getName().equals(name)) { 222 return (MessageDestinationDescriptor) next; 223 } 224 } 225 throw new IllegalArgumentException (localStrings.getLocalString( 226 "enterprise.deployment.exceptionmessagedestbundle", 227 "Referencing error: this bundle has no message destination of name: {0}", new Object [] {name})); 228 } 229 230 233 public void addMessageDestination(MessageDestinationDescriptor 234 messageDestination) { 235 messageDestination.setBundleDescriptor(this); 236 this.getMessageDestinations().add(messageDestination); 237 super.changed(); 238 } 239 240 243 public void removeMessageDestination(MessageDestinationDescriptor msgDest) { 244 msgDest.setBundleDescriptor(null); 245 this.getMessageDestinations().remove(msgDest); 246 super.changed(); 247 } 248 249 253 public Set getRoles() { 254 if (this.roles == null) { 255 this.roles = new OrderedSet(); 256 } 257 if (application != null) { 258 this.roles.addAll(application.getAppRoles()); 259 } 260 261 return this.roles; 262 } 263 264 267 public void addRole(Role role) { 268 this.getRoles().add(role); 269 this.changed(); 270 } 271 272 284 public void addRole(SecurityRoleDescriptor descriptor) { 285 Role role = new Role(descriptor.getName()); 286 role.setDescription(descriptor.getDescription()); 287 this.addRole(role); 288 } 289 292 public void removeRole(Role role) { 293 this.getRoles().remove(role); 294 this.changed(); 295 } 296 297 300 protected Collection getNamedDescriptorsFrom(JndiNameEnvironment nameEnvironment) { 301 Collection namedDescriptors = new Vector(); 302 for (Iterator itr = nameEnvironment.getResourceReferenceDescriptors().iterator(); itr.hasNext();) { 303 ResourceReferenceDescriptor resourceReference = (ResourceReferenceDescriptor) itr.next(); 304 namedDescriptors.add(resourceReference); 305 } 306 for (Iterator itr = nameEnvironment.getEjbReferenceDescriptors().iterator(); itr.hasNext();) { 307 EjbReferenceDescriptor ejbReference = (EjbReferenceDescriptor) itr.next(); 308 namedDescriptors.add(ejbReference); 309 } 310 for (Iterator itr = nameEnvironment.getJmsDestinationReferenceDescriptors().iterator(); itr.hasNext();) { 311 JmsDestinationReferenceDescriptor resourceEnvRef = 312 (JmsDestinationReferenceDescriptor) itr.next(); 313 namedDescriptors.add(resourceEnvRef); 314 } 315 316 return namedDescriptors; 317 } 318 319 322 protected Vector getNamedReferencePairsFrom(JndiNameEnvironment nameEnvironment) { 323 Vector pairs = new Vector(); 324 for (Iterator itr = nameEnvironment.getResourceReferenceDescriptors().iterator(); itr.hasNext();) { 325 ResourceReferenceDescriptor resourceReference = (ResourceReferenceDescriptor) itr.next(); 326 pairs.add(NamedReferencePair.createResourceRefPair((Descriptor)nameEnvironment, resourceReference)); 327 } 328 for (Iterator itr = nameEnvironment.getEjbReferenceDescriptors().iterator(); itr.hasNext();) { 329 EjbReferenceDescriptor ejbReference = (EjbReferenceDescriptor) itr.next(); 330 pairs.add(NamedReferencePair.createEjbRefPair((Descriptor) nameEnvironment, ejbReference)); 331 } 332 for (Iterator itr = nameEnvironment.getJmsDestinationReferenceDescriptors().iterator(); itr.hasNext();) { 333 JmsDestinationReferenceDescriptor resourceEnvRef = 334 (JmsDestinationReferenceDescriptor) itr.next(); 335 pairs.add(NamedReferencePair.createResourceEnvRefPair((Descriptor) nameEnvironment, resourceEnvRef)); 336 } 337 338 return pairs; 339 } 340 341 public InjectionInfo getInjectionInfoByClass(String className, 342 JndiNameEnvironment jndiNameEnv) { 343 LifecycleCallbackDescriptor postConstructDesc = 344 getPostConstructDescriptorByClass(className, jndiNameEnv); 345 String postConstructMethodName = (postConstructDesc != null) ? 346 postConstructDesc.getLifecycleCallbackMethod() : null; 347 LifecycleCallbackDescriptor preDestroyDesc = 348 getPreDestroyDescriptorByClass(className, jndiNameEnv); 349 String preDestroyMethodName = (preDestroyDesc != null) ? 350 preDestroyDesc.getLifecycleCallbackMethod() : null; 351 return new InjectionInfo(className, 352 postConstructMethodName, preDestroyMethodName, 353 getInjectableResourcesByClass(className, 354 jndiNameEnv)); 355 } 356 357 public LifecycleCallbackDescriptor 358 getPostConstructDescriptorByClass(String className, 359 JndiNameEnvironment jndiNameEnv) 360 { 361 for (LifecycleCallbackDescriptor next : 362 jndiNameEnv.getPostConstructDescriptors()) { 363 if (next.getLifecycleCallbackClass().equals(className)) { 364 return next; 365 } 366 } 367 return null; 368 } 369 370 public LifecycleCallbackDescriptor 371 getPreDestroyDescriptorByClass(String className, 372 JndiNameEnvironment jndiNameEnv) 373 { 374 for (LifecycleCallbackDescriptor next : 375 jndiNameEnv.getPreDestroyDescriptors()) { 376 if (next.getLifecycleCallbackClass().equals(className)) { 377 return next; 378 } 379 } 380 return null; 381 } 382 383 protected List<InjectionCapable> getInjectableResources 384 (JndiNameEnvironment jndiNameEnv) { 385 386 List<InjectionCapable> injectables = 387 new LinkedList<InjectionCapable>(); 388 389 Collection allEnvProps = new HashSet(); 390 391 for(Iterator envEntryItr = 392 jndiNameEnv.getEnvironmentProperties().iterator(); 393 envEntryItr.hasNext();) { 394 EnvironmentProperty envEntry = (EnvironmentProperty) 395 envEntryItr.next(); 396 if( envEntry.hasAValue() ) { 399 allEnvProps.add(envEntry); 400 } 401 } 402 403 allEnvProps.addAll(jndiNameEnv.getEjbReferenceDescriptors()); 404 allEnvProps.addAll(jndiNameEnv.getServiceReferenceDescriptors()); 405 allEnvProps.addAll(jndiNameEnv.getResourceReferenceDescriptors()); 406 allEnvProps.addAll(jndiNameEnv.getJmsDestinationReferenceDescriptors()); 407 allEnvProps.addAll(jndiNameEnv.getMessageDestinationReferenceDescriptors()); 408 409 allEnvProps.addAll(jndiNameEnv.getEntityManagerFactoryReferenceDescriptors()); 410 allEnvProps.addAll(jndiNameEnv.getEntityManagerReferenceDescriptors()); 411 412 for(Iterator envItr = allEnvProps.iterator(); envItr.hasNext();) { 413 InjectionCapable next = (InjectionCapable) envItr.next(); 414 if( next.isInjectable() ) { 415 injectables.add(next); 416 } 417 } 418 419 return injectables; 420 } 421 422 423 427 protected List<InjectionCapable> 428 getInjectableResourcesByClass(String className, 429 JndiNameEnvironment jndiNameEnv) { 430 List<InjectionCapable> injectables = 431 new LinkedList<InjectionCapable>(); 432 433 for(InjectionCapable next : getInjectableResources(jndiNameEnv) ) { 434 if( next.isInjectable()) { 435 for (InjectionTarget target : next.getInjectionTargets()) { 436 if (target.getClassName().equals(className) ) { 437 injectables.add(next); 438 } 439 } 440 } 441 } 442 443 return injectables; 444 } 445 446 449 public ModuleDescriptor getModuleDescriptor() { 450 if (moduleDescriptor==null) { 451 moduleDescriptor = new ModuleDescriptor(); 452 moduleDescriptor.setModuleType(getModuleType()); 453 moduleDescriptor.setDescriptor(this); 454 } 455 return moduleDescriptor; 456 } 457 458 462 public void setModuleDescriptor(ModuleDescriptor descriptor) { 463 moduleDescriptor = descriptor; 464 } 465 466 469 public ClassLoader getClassLoader() { 470 if (classLoader!=null) { 471 return classLoader; 472 } 473 if (application!=null) { 474 return application.getClassLoader(); 475 } 476 throw new RuntimeException ("No class loader associated with this module " + getName()); 477 } 478 479 482 public void print(StringBuffer toStringBuffer) { 483 toStringBuffer.append("\n"); 484 super.print(toStringBuffer); 485 toStringBuffer.append("\n Roles[] = ").append(roles); 486 if (getWebServices().hasWebServices()) { 487 toStringBuffer.append("\n WebServices "); 488 ((Descriptor)(getWebServices())).print(toStringBuffer); 489 } 490 } 491 492 495 public abstract ModuleType getModuleType(); 496 497 500 public String getModuleID() { 501 if (moduleID==null) { 502 moduleID = getModuleDescriptor().getArchiveUri(); 503 } 504 if (getModuleDescriptor().isStandalone()) { 505 return moduleID; 506 } 507 if (application!=null) { 508 if (application.getModuleID()==null) { 509 return getDisplayName(); 510 } 511 return application.getModuleID()+"#"+moduleID; 512 } else { 513 return moduleID; 514 } 515 } 516 517 521 public String getDeploymentDescriptorDir() { 522 return DEPLOYMENT_DESCRIPTOR_DIR; 523 } 524 525 526 529 public String getWsdlDir() { 530 return getDeploymentDescriptorDir() + "/" + WSDL_DIR; 531 } 532 533 539 public void setFullFlag(boolean flag) { 540 fullFlag=flag; 541 } 542 543 547 public void setFullAttribute(String value) { 548 fullAttribute = Boolean.valueOf(value); 549 } 550 551 555 public boolean isFullAttribute() { 556 return fullAttribute; 557 } 558 559 569 public boolean isFullFlag() { 570 if (fullAttribute == true || fullFlag == true) { 573 return true; 574 } 575 return isDDWithNoAnnotationAllowed(); 576 } 577 578 585 public boolean isDDWithNoAnnotationAllowed() { 586 ModuleType mType = getModuleType(); 587 588 double specVersion = Double.parseDouble(getSpecVersion()); 589 590 if (mType.equals(ModuleType.RAR)) { 593 return true; 594 } else { 595 if ( (mType.equals(ModuleType.EJB) && 597 specVersion < ANNOTATION_EJB_VER) || 598 (mType.equals(ModuleType.WAR) && 599 specVersion < ANNOTATION_WAR_VER) || 600 (mType.equals(ModuleType.CAR) && 601 specVersion < ANNOTATION_CAR_VER) ) { 602 return true; 603 } else { 604 return false; 605 } 606 } 607 } 608 609 622 public PersistenceUnitDescriptor findReferencedPU(String unitName) { 623 if(unitName == null || unitName.length()==0) { return findDefaultPU(); 625 } else { 626 return findReferencedPU0(unitName); 627 } 628 } 629 630 637 public PersistenceUnitDescriptor findDefaultPU() { 638 PersistenceUnitDescriptor pu = null; 640 int totalNumberOfPUInClient = 0; 641 for (PersistenceUnitsDescriptor nextPUs: 642 getPersistenceUnitsDescriptors()) { 643 for(PersistenceUnitDescriptor nextPU : 644 nextPUs.getPersistenceUnitDescriptors()) { 645 pu = nextPU; 646 totalNumberOfPUInClient++; 647 } 648 } 649 if(totalNumberOfPUInClient == 1) { return pu; 651 } else if(totalNumberOfPUInClient == 0) { int totalNumberOfPUInEar = 0; 654 for (PersistenceUnitsDescriptor nextPUs: 655 getApplication().getPersistenceUnitsDescriptors()) { 656 for(PersistenceUnitDescriptor nextPU : 657 nextPUs.getPersistenceUnitDescriptors()) { 658 pu = nextPU; 659 totalNumberOfPUInEar++; 660 } 661 } 662 if(totalNumberOfPUInEar == 1) { 663 return pu; 664 } 665 } 666 return null; 667 } 668 669 676 private PersistenceUnitDescriptor findReferencedPU0(String unitName) { 677 int separatorIndex = 678 unitName.lastIndexOf(PERSISTENCE_UNIT_NAME_SEPARATOR); 679 680 if( separatorIndex != -1 ) { String unqualifiedUnitName = 683 unitName.substring(separatorIndex + 1); 684 String path = unitName.substring(0, separatorIndex); 685 String puRoot = getTargetUri(this, path); 688 final PersistenceUnitsDescriptor pus = 689 getApplication().getPersistenceUnitsDescriptor(puRoot); 690 if(pus!=null) { 691 for(PersistenceUnitDescriptor pu : 692 pus.getPersistenceUnitDescriptors()) { 693 if(pu.getName().equals(unqualifiedUnitName)) { 694 return pu; 695 } 696 } 697 } 698 } else { Map<String , PersistenceUnitDescriptor> visiblePUs = 702 getVisiblePUs(); 703 PersistenceUnitDescriptor result = visiblePUs.get(unitName); 704 if(result != null) return result; 705 706 int sameNamedEarScopedPUCount = 0; 708 for(String s : visiblePUs.keySet()) { 709 int idx = s.lastIndexOf(PERSISTENCE_UNIT_NAME_SEPARATOR); 710 if(idx != -1 && s.substring(idx+1).matches(unitName)) { 712 result = visiblePUs.get(s); 713 sameNamedEarScopedPUCount++; 714 } 715 } 716 if(sameNamedEarScopedPUCount == 1) { 720 return result; 721 } 722 } 723 return null; 724 } 725 726 739 public Map<String , PersistenceUnitDescriptor> getVisiblePUs() { 740 Map<String , PersistenceUnitDescriptor> result = 741 new HashMap<String , PersistenceUnitDescriptor>(); 742 743 for (PersistenceUnitsDescriptor pus : 745 getPersistenceUnitsDescriptors()) { 746 for(PersistenceUnitDescriptor pu : 747 pus.getPersistenceUnitDescriptors()) { 748 result.put(pu.getName(), pu); 750 } 751 } 752 753 final Application application = getApplication(); 755 if(application!=null) { 756 for(PersistenceUnitsDescriptor pus: 757 application.getPersistenceUnitsDescriptors()) { 758 for(PersistenceUnitDescriptor pu : 759 pus.getPersistenceUnitDescriptors()) { 760 result.put(pu.getPuRoot()+ PERSISTENCE_UNIT_NAME_SEPARATOR + pu.getName(), pu); 762 } 763 } 764 } 765 return result; 766 } 767 768 777 private String getTargetUri(BundleDescriptor origin, 778 String relativeTargetUri) { 779 try { 780 String archiveUri = origin.getModuleDescriptor().getArchiveUri(); 781 return new URI (archiveUri).resolve(relativeTargetUri).getPath(); 782 } catch (URISyntaxException use) { 783 throw new RuntimeException (use); 784 } 785 } 786 787 public String getUniqueFriendlyId() { 789 String uniqueId; 790 791 794 if (getApplication().isVirtual()) { 795 uniqueId = getApplication().getRegistrationName(); 796 } else { 797 uniqueId = getModuleDescriptor().getArchiveUri(); 798 } 799 return FileUtils.makeFriendlyFileName(uniqueId); 800 } 801 802 } 803 | Popular Tags |