KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > enterprise > deployment > BundleDescriptor


1 /*
2  * The contents of this file are subject to the terms
3  * of the Common Development and Distribution License
4  * (the License). You may not use this file except in
5  * compliance with the License.
6  *
7  * You can obtain a copy of the license at
8  * https://glassfish.dev.java.net/public/CDDLv1.0.html or
9  * glassfish/bootstrap/legal/CDDLv1.0.txt.
10  * See the License for the specific language governing
11  * permissions and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL
14  * Header Notice in each file and include the License file
15  * at glassfish/bootstrap/legal/CDDLv1.0.txt.
16  * If applicable, add the following below the CDDL Header,
17  * with the fields enclosed by brackets [] replaced by
18  * you own identifying information:
19  * "Portions Copyrighted [year] [name of copyright owner]"
20  *
21  * Copyright 2006 Sun Microsystems, Inc. All rights reserved.
22  */

23 package com.sun.enterprise.deployment;
24
25 import java.util.*;
26 import java.net.URI JavaDoc;
27 import java.net.URISyntaxException JavaDoc;
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 JavaDoc;
33 import javax.persistence.EntityManagerFactory;
34
35     /**
36     * I am an abstract class representing all the deployment information common
37     * to all component container structures held by an application.
38     *
39     * @author Danny Coward
40     */

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 JavaDoc DEPLOYMENT_DESCRIPTOR_DIR="META-INF";
48     private final static String JavaDoc WSDL_DIR="wsdl";
49
50     // the spec versions we should start to look at annotations
51
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 JavaDoc 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     // Physical entity manager factory corresponding to the unit name of
65
// each module-level persistence unit. Only available at runtime.
66
private Map<String JavaDoc, EntityManagerFactory> entityManagerFactories =
67         new HashMap<String JavaDoc, EntityManagerFactory>();
68     
69     /**
70      * contains the information for this module (like it's module name)
71      */

72     private ModuleDescriptor moduleDescriptor;
73     
74     /**
75      * Construct a new BundleDescriptor
76      */

77     public BundleDescriptor() {
78         super();
79         webServices.setBundleDescriptor(this);
80     }
81     
82     /**
83      * Construct a new BundleDescriptor with a name and description
84      */

85     public BundleDescriptor(String JavaDoc name, String JavaDoc description) {
86         super(name, description);
87         webServices.setBundleDescriptor(this);
88     }
89     
90     /**
91     * Sets the application to which I belong.
92     */

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     /**
109      * @return true if this module is an application object
110      */

111     public boolean isApplication() {
112     return false;
113     }
114     
115     /**
116     * The application to which I belong, or none if I am standalone.
117     */

118     public Application getApplication() {
119     return this.application;
120     }
121
122     /**
123      * Set the physical entity manager factory for a persistence unit
124      * within this module.
125      */

126     public void addEntityManagerFactory(String JavaDoc unitName,
127                                         EntityManagerFactory emf) {
128
129         entityManagerFactories.put(unitName, emf);
130     }
131
132     /**
133      * Retrieve the physical entity manager factory associated with the
134      * unitName of a persistence unit within this module. Returns null if
135      * no matching entry is found.
136      */

137     public EntityManagerFactory getEntityManagerFactory(String JavaDoc unitName) {
138         
139         return entityManagerFactories.get(unitName);
140     }
141
142     /**
143      * Returns the set of physical entity manager factories associated
144      * with persistence units in this module.
145      */

146     public Set<EntityManagerFactory> getEntityManagerFactories() {
147
148         return new HashSet<EntityManagerFactory>
149             (entityManagerFactories.values());
150
151     }
152
153     
154     /**
155      * @return a set of service-ref from this bundle or an empty set
156      * if none
157      */

158     public abstract Set getServiceReferenceDescriptors();
159
160     /**
161      * Return web services defined for this module. Not applicable for
162      * application clients.
163      */

164     public WebServicesDescriptor getWebServices() {
165         return webServices;
166     }
167     
168     public WebServiceEndpoint getWebServiceEndpointByName(String JavaDoc name) {
169         return webServices.getEndpointByName(name);
170     }
171     
172     /**
173      * @return true if this bundle descriptor defines web service clients
174      */

175     public boolean hasWebServiceClients() {
176         return false;
177     }
178
179     /**
180      * @return true if this bundle descriptor defines web services
181      */

182     public boolean hasWebServices() {
183         return getWebServices().hasWebServices();
184     }
185     
186     
187
188     /**
189      * Return the Set of message destinations I have
190      */

191     public Set getMessageDestinations() {
192     if (this.messageDestinations == null) {
193         this.messageDestinations = new HashSet();
194     }
195     return this.messageDestinations;
196     }
197     
198     /**
199      * Returns true if I have an message destiation by that name.
200      */

201     public boolean hasMessageDestinationByName(String JavaDoc 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     /**
213      * Returns a message destination descriptor that I have by the
214      * same name, or throws an IllegalArgumentException
215      */

216     public MessageDestinationDescriptor getMessageDestinationByName
217         (String JavaDoc 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 JavaDoc(localStrings.getLocalString(
226                                        "enterprise.deployment.exceptionmessagedestbundle",
227                                        "Referencing error: this bundle has no message destination of name: {0}", new Object JavaDoc[] {name}));
228     }
229     
230     /**
231      * Add a message destination to me.
232      */

233     public void addMessageDestination(MessageDestinationDescriptor
234                                       messageDestination) {
235     messageDestination.setBundleDescriptor(this);
236     this.getMessageDestinations().add(messageDestination);
237     super.changed();
238     }
239     
240     /**
241      * Remove the given message destination descriptor from my (by equality).
242      */

243     public void removeMessageDestination(MessageDestinationDescriptor msgDest) {
244     msgDest.setBundleDescriptor(null);
245     this.getMessageDestinations().remove(msgDest);
246     super.changed();
247     }
248
249      /**
250      * Return the set of com.sun.enterprise.deployment.Role objects
251      * I have plus the ones from application
252      */

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     /**
265     *Adds a role object to me.
266     */

267     public void addRole(Role role) {
268     this.getRoles().add(role);
269     this.changed();
270     }
271     
272     /**
273      *Adds a Role object based on the supplied SecurityRoleDescriptor.
274      *<p>
275      *A change in SecurityRoleNode to fix bug 4933385 causes the DOL to use SecurityRoleDescriptor, rather
276      *than Role, to contain information about security roles. To minimize the impact on BundleDescriptor,
277      *this method has been added for use by the DOL as it processes security-role elements.
278      *<p>
279      *This method creates a new Role object based on the characteristics of the SecurityRoleDescriptor
280      *and then delegates to addRole(Role) to preserve the rest of the behavior of this class.
281      *
282      *@param descriptor SecurityRoleDescriptor that describes the username and description of the role
283      */

284     public void addRole(SecurityRoleDescriptor descriptor) {
285         Role role = new Role(descriptor.getName());
286         role.setDescription(descriptor.getDescription());
287         this.addRole(role);
288     }
289     /**
290     * Removes a role object from me.
291     */

292     public void removeRole(Role role) {
293     this.getRoles().remove(role);
294     this.changed();
295     }
296     
297     /**
298     * Utility method for iterating the set of named descriptors in the supplied nameEnvironment
299     */

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      /**
320     * Utility method for iterating the set of NameReference pairs in the supplied nameEnvironment
321     */

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 JavaDoc className,
342                               JndiNameEnvironment jndiNameEnv) {
343         LifecycleCallbackDescriptor postConstructDesc =
344             getPostConstructDescriptorByClass(className, jndiNameEnv);
345         String JavaDoc postConstructMethodName = (postConstructDesc != null) ?
346             postConstructDesc.getLifecycleCallbackMethod() : null;
347         LifecycleCallbackDescriptor preDestroyDesc =
348             getPreDestroyDescriptorByClass(className, jndiNameEnv);
349         String JavaDoc 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 JavaDoc 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 JavaDoc 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             // Only env-entries that have been assigned a value are
397
// eligible for injection.
398
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     /**
424      * Define implementation of getInjectableResourceByClass here so it
425      * isn't replicated across appclient, web, ejb descriptors.
426      */

427     protected List<InjectionCapable>
428         getInjectableResourcesByClass(String JavaDoc 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     /**
447      * @return the module descriptor for this bundle
448      */

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     /**
459      * Sets the module descriptor for this bundle
460      * @param descriptor for the module
461      */

462     public void setModuleDescriptor(ModuleDescriptor descriptor) {
463         moduleDescriptor = descriptor;
464     }
465     
466     /**
467      * @return the class loader associated with this module
468      */

469     public ClassLoader JavaDoc getClassLoader() {
470         if (classLoader!=null) {
471             return classLoader;
472         }
473         if (application!=null) {
474             return application.getClassLoader();
475         }
476         throw new RuntimeException JavaDoc("No class loader associated with this module " + getName());
477     }
478     
479     /**
480     * Prints a formatted string representing my state.
481     */

482     public void print(StringBuffer JavaDoc 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     /**
493      * @return the type of this bundle descriptor
494      */

495     public abstract ModuleType JavaDoc getModuleType();
496
497     /**
498      * @return the module ID for this module descriptor
499      */

500     public String JavaDoc 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     /**
518      * @return the deployment descriptor directory location inside
519      * the archive file
520      */

521     public String JavaDoc getDeploymentDescriptorDir() {
522         return DEPLOYMENT_DESCRIPTOR_DIR;
523     }
524     
525     
526     /**
527      * @return the wsdl directory location inside the archive file
528      */

529     public String JavaDoc getWsdlDir() {
530         return getDeploymentDescriptorDir() + "/" + WSDL_DIR;
531     }
532  
533     /**
534      * Sets the full flag of the bundle descriptor. Once set, the annotations
535      * of the classes contained in the archive described by this bundle
536      * descriptor will be ignored.
537      * @param flag a boolean to set or unset the flag
538      */

539      public void setFullFlag(boolean flag) {
540          fullFlag=flag;
541      }
542
543     /**
544      * Sets the full attribute of the deployment descriptor
545      * @param value the full attribute
546      */

547     public void setFullAttribute(String JavaDoc value) {
548         fullAttribute = Boolean.valueOf(value);
549     }
550
551     /**
552      * Get the full attribute of the deployment descriptor
553      * @return the full attribute
554      */

555     public boolean isFullAttribute() {
556         return fullAttribute;
557     }
558     
559     /**
560      * @ return true for following cases:
561      * 1. When the full attribute is true. This attribute only applies to
562      * ejb module with schema version equal or later than 3.0;
563             web module and schema version equal or later than than 2.5;
564             appclient module and schema version equal or later than 5.0.
565      * 2. When it's been tagged as "full" when processing annotations.
566      * 3. When DD has a version which doesn't allowed annotations.
567      * return false otherwise.
568      */

569     public boolean isFullFlag() {
570         // if the full attribute is true or it's been tagged as full,
571
// return true
572
if (fullAttribute == true || fullFlag == true) {
573             return true;
574         }
575         return isDDWithNoAnnotationAllowed();
576     }
577
578     /**
579      * @ return true for following cases:
580      * a. connector module;
581      * b. ejb module and schema version earlier than 3.0;
582      * c. web module and schema version earlier than 2.5;
583      * d. appclient module and schema version earlier than 5.0.
584      */

585     public boolean isDDWithNoAnnotationAllowed() {
586         ModuleType JavaDoc mType = getModuleType();
587
588         double specVersion = Double.parseDouble(getSpecVersion());
589
590         // connector DD doesn't have annotation, so always treated
591
// as full DD
592
if (mType.equals(ModuleType.RAR)) {
593             return true;
594         } else {
595             // we do not process annotations for earlier versions of DD
596
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     /**
610      * It accepts both a quailified (e.g.) "lib/a.jar#FooPU" as well as
611      * unqualified name (e.g.) "FooPU". It then searched all the
612      * PersistenceUnits that are defined in the scope of this bundle
613      * descriptor to see which one matches the give name.
614      * @param unitName as used in @PersistenceUnit, @PersistenceContext
615      * <persistence-context-ref> or <persistence-unit-name>.
616      * If null, this method returns the default PU, if available.
617      * The reason it accepts null for default PU is because "" gets converted to
618      * null in EntityManagerReferenceHandler.processNewEmRefAnnotation.
619      * @return PersistenceUnitDescriptor that this unitName resolves to.
620      * Returns null, if unitName could not be resolved.
621      */

622     public PersistenceUnitDescriptor findReferencedPU(String JavaDoc unitName) {
623         if(unitName == null || unitName.length()==0) { // uses default PU.
624
return findDefaultPU();
625         } else {
626             return findReferencedPU0(unitName);
627         }
628     }
629
630     /**
631      * This method is responsible for finding default persistence unit for
632      * a bundle descriptor.
633      * @return the default persistence unit for this bundle. returns null,
634      * if there isno PU defined or default can not be calculated because there
635      * are more than 1 PUs defined.
636      */

637     public PersistenceUnitDescriptor findDefaultPU() {
638         // step #1: see if we have only one PU in the local scope.
639
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) { // there is only one PU in this client.
650
return pu;
651         } else if(totalNumberOfPUInClient == 0) { // there are no PUs in this client-jar.
652
// step #2: see if we have only one PU in the ear.
653
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     /**
670      * Internal method.
671      * This method is used to find referenced PU with a given name.
672      * It does not accept null or empty unit name.
673      * @param unitName
674      * @return
675      */

676     private PersistenceUnitDescriptor findReferencedPU0(String JavaDoc unitName) {
677         int separatorIndex =
678             unitName.lastIndexOf(PERSISTENCE_UNIT_NAME_SEPARATOR);
679
680         if( separatorIndex != -1 ) { // qualified name
681
// uses # => must be defined in a utility jar at ear scope.
682
String JavaDoc unqualifiedUnitName =
683                 unitName.substring(separatorIndex + 1);
684             String JavaDoc path = unitName.substring(0, separatorIndex);
685             // it' necessary to call getTargetUri as that takes care of
686
// converting ././b to canonical forms.
687
String JavaDoc 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 { // uses unqualified name.
699
// first look to see if there is a match with unqualified name,
700
// b'cos local scope takes precedence.
701
Map<String JavaDoc, PersistenceUnitDescriptor> visiblePUs =
702                     getVisiblePUs();
703             PersistenceUnitDescriptor result = visiblePUs.get(unitName);
704             if(result != null) return result;
705
706             // next look to see if there is unique match in ear scope.
707
int sameNamedEarScopedPUCount = 0;
708             for(String JavaDoc s : visiblePUs.keySet()) {
709                 int idx = s.lastIndexOf(PERSISTENCE_UNIT_NAME_SEPARATOR);
710                 if(idx != -1 // ear scoped
711
&& s.substring(idx+1).matches(unitName)) {
712                     result = visiblePUs.get(s);
713                     sameNamedEarScopedPUCount++;
714                 }
715             }
716             // if there are more than one ear scoped PU with same name (this
717
// is possible when PU is inside two different library jar),
718
// then user can not use unqualified name.
719
if(sameNamedEarScopedPUCount == 1) {
720                 return result;
721             }
722         }
723         return null;
724     }
725
726     /**
727      * This method returns all the PUs that are defined in this bundle as well
728      * as the PUs defined in the ear level. e.g. for the following ear:
729      * ear/lib/a.jar#defines FooPU
730      * /lib/b.jar#defines FooPU
731      * ejb.jar#defines FooPU
732      * for the EjbBundleDescriptor (ejb.jar), the map will contain
733      * {(lib/a.jar#FooPU, PU1), (lib/b.jar#FooPU, PU2), (FooPU, PU3)}.
734      *
735      * @return a map of names to PUDescriptors that are visbible to this
736      * bundle descriptor. The name is a qualified name for ear scoped PUs
737      * where as it is in unqualified form for local PUs.
738      */

739     public Map<String JavaDoc, PersistenceUnitDescriptor> getVisiblePUs() {
740         Map<String JavaDoc, PersistenceUnitDescriptor> result =
741                 new HashMap<String JavaDoc, PersistenceUnitDescriptor>();
742
743         // local scoped PUs
744
for (PersistenceUnitsDescriptor pus :
745                 getPersistenceUnitsDescriptors()) {
746             for(PersistenceUnitDescriptor pu :
747                     pus.getPersistenceUnitDescriptors()) {
748                 // for local PUs, use unqualified name.
749
result.put(pu.getName(), pu);
750             }
751         }
752
753         // ear scoped PUs
754
final Application application = getApplication();
755         if(application!=null) {
756             for(PersistenceUnitsDescriptor pus:
757                     application.getPersistenceUnitsDescriptors()) {
758                 for(PersistenceUnitDescriptor pu :
759                         pus.getPersistenceUnitDescriptors()) {
760                     // use fully qualified name for ear scoped PU
761
result.put(pu.getPuRoot()+ PERSISTENCE_UNIT_NAME_SEPARATOR + pu.getName(), pu);
762                 }
763             }
764         }
765         return result;
766     }
767
768     /**
769      * Get the uri of a target based on a source module and a a relative uri
770      * from the perspective of that source module.
771      *
772      * @param origin bundle descriptor within an application
773      * @param relativeTargetUri relative uri from the given bundle
774      * descriptor
775      * @return target uri
776      */

777     private String JavaDoc getTargetUri(BundleDescriptor origin,
778                                String JavaDoc relativeTargetUri) {
779         try {
780             String JavaDoc archiveUri = origin.getModuleDescriptor().getArchiveUri();
781             return new URI JavaDoc(archiveUri).resolve(relativeTargetUri).getPath();
782         } catch (URISyntaxException JavaDoc use) {
783             throw new RuntimeException JavaDoc(use);
784         }
785     }
786
787     // return a short unique representation of this BundleDescriptor
788
public String JavaDoc getUniqueFriendlyId() {
789         String JavaDoc uniqueId;
790
791         // for standalone jars, return its registration name
792
// for applications, return the module uri
793

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