KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > j2ee > persistence > dd > PersistenceUtils


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 package org.netbeans.modules.j2ee.persistence.dd;
21
22 import java.beans.PropertyChangeEvent JavaDoc;
23 import java.beans.PropertyChangeListener JavaDoc;
24 import java.io.IOException JavaDoc;
25 import java.lang.ref.WeakReference JavaDoc;
26 import java.util.ArrayList JavaDoc;
27 import java.util.Collections JavaDoc;
28 import java.util.Comparator JavaDoc;
29 import java.util.HashMap JavaDoc;
30 import java.util.HashSet JavaDoc;
31 import java.util.Iterator JavaDoc;
32 import java.util.List JavaDoc;
33 import java.util.Map JavaDoc;
34 import java.util.Set JavaDoc;
35 import java.util.WeakHashMap JavaDoc;
36 import org.netbeans.api.java.classpath.ClassPath;
37 import org.netbeans.api.project.FileOwnerQuery;
38 import org.netbeans.api.project.Project;
39 import org.netbeans.modules.j2ee.metadata.MetadataUnit;
40 import org.netbeans.modules.j2ee.persistence.api.PersistenceScope;
41 import org.netbeans.modules.j2ee.persistence.api.PersistenceScopes;
42 import org.netbeans.modules.j2ee.persistence.api.metadata.orm.Entity;
43 import org.netbeans.modules.j2ee.persistence.api.metadata.orm.EntityMappings;
44 import org.netbeans.modules.j2ee.persistence.dd.persistence.model_1_0.Persistence;
45 import org.netbeans.modules.j2ee.persistence.dd.persistence.model_1_0.PersistenceUnit;
46 import org.netbeans.modules.j2ee.persistence.spi.PersistenceClassPathProvider;
47 import org.netbeans.modules.j2ee.persistence.unit.PUDataObject;
48 import org.openide.filesystems.FileObject;
49 import org.openide.loaders.DataObject;
50 import org.openide.loaders.DataObjectNotFoundException;
51 import org.openide.util.WeakListeners;
52
53 /**
54  *
55  * @author Marek Fukala, Andrei Badea
56  */

57 public class PersistenceUtils {
58     
59     // TODO multiple mapping files
60

61     /**
62      * Maps persistence.xml files to instances of EntityMappingCache, which
63      * cache MetadataUnits for persistence units.
64      */

65     private static final Map JavaDoc<FileObject, EntityMappingsCache> persistence2EMCache = new WeakHashMap JavaDoc<FileObject, EntityMappingsCache>();
66     
67     /**
68      * Maps projects to MetadataUnit containing the classpath with entity
69      * classes for those projects.
70      */

71     private static final WeakHashMap JavaDoc<Project, MetadataUnit> project2MUCache = new WeakHashMap JavaDoc<Project, MetadataUnit>();
72     
73     private PersistenceUtils() {
74     }
75     
76     /**
77      * Returns an EntityMappings instance containing the entity classes in
78      * the given persistence unit of the given persistence scope.
79      *
80      * @return an EntityMapping instance or null if e.g. the given persistence
81      * scope does not contain a persistence.xml file or the
82      * given persistence unit.
83      * @throws NullPointerException if <code>persistenceScope</code> or
84      * <code>persistenceUnitName</code> are null.
85      * IOException if an I/O exception occured, e.g. if the persistence.xml
86      * file could not be read.
87      */

88     public static EntityMappings getEntityMappings(PersistenceScope persistenceScope, String JavaDoc persistenceUnitName) throws IOException JavaDoc {
89         if (persistenceScope == null) {
90             throw new NullPointerException JavaDoc("The persistenceScope parameter cannot be null"); // NOI18N
91
}
92         if (persistenceUnitName == null) {
93             throw new NullPointerException JavaDoc("The persistenceUnitName parameter cannot be null"); // NOI18N
94
}
95         
96         EntityMappingsCache emCache = getEntityMappingsCache(persistenceScope);
97         if (emCache != null) {
98             return emCache.getEntityMappings(persistenceUnitName);
99         }
100         return null;
101     }
102     
103     /**
104      * Returns the EntityMappings instances containing the entity classes in
105      * all persistence units of all persistence scopes in the given project.
106      *
107      * @return a list of EntityMappings instances; never null.
108      * @throws NullPointerException if <code>project</code> is null.
109      * IOException if an I/O exception occured, e.g. if the persistence.xml
110      * file could not be read.
111      */

112     public static List JavaDoc<EntityMappings> getEntityMappings(Project project) throws IOException JavaDoc {
113         if (project == null) {
114             throw new NullPointerException JavaDoc("The project parameter cannot be null"); // NOI18N
115
}
116         
117         List JavaDoc<EntityMappings> result = new ArrayList JavaDoc<EntityMappings>();
118         for (PersistenceScope persistenceScope : getPersistenceScopes(project)) {
119             EntityMappingsCache emCache = getEntityMappingsCache(persistenceScope);
120             if (emCache != null) {
121                 result.addAll(emCache.getEntityMappings());
122             }
123         }
124         
125         return Collections.unmodifiableList(result);
126     }
127     
128     /**
129      * Returns an EntityMappingsCache for the given persistence.xml file.
130      * Not private because used in tests.
131      *
132      * @return an EntityMappingCache instance; can be null e.g. when
133      * the given persistence.xml file is not
134      * {@link FileObject#isValid valid}.
135      */

136     static EntityMappingsCache getEntityMappingsCache(PersistenceScope persistenceScope) throws IOException JavaDoc {
137         FileObject persistenceXml = persistenceScope.getPersistenceXml();
138         if (persistenceXml == null) {
139             return null;
140         }
141         
142         EntityMappingsCache emCache = null;
143         synchronized (persistence2EMCache) {
144             emCache = persistence2EMCache.get(persistenceXml);
145             if (emCache == null && persistenceXml.isValid()) {
146                 emCache = EntityMappingsCache.create(persistenceScope);
147                 persistence2EMCache.put(persistenceXml, emCache);
148             }
149         }
150         return emCache;
151     }
152     
153     /**
154      * Removes the given persistence.xml file from the cache of
155      * EntityMappingCache instances.
156      */

157     private static void removeFromCache(FileObject persistenceXml) {
158         synchronized (persistence2EMCache) {
159             persistence2EMCache.remove(persistenceXml);
160         }
161     }
162     
163     /**
164      * Returns the set of entity classes in the given persistence scope as
165      * defined by the persistence.xml file of that persistence scope.
166      *
167      * @return a set of entity classes; never null.
168      * @throws NullPointerException if <code>persistenceScope</code> is null.
169      * IOException if an I/O exception occured, e.g. if the persistence.xml
170      * file could not be read.
171      */

172     public static Set JavaDoc<Entity> getEntityClasses(PersistenceScope persistenceScope) throws IOException JavaDoc {
173         if (persistenceScope == null) {
174             throw new NullPointerException JavaDoc("The persistenceScope parameter cannot be null"); // NOI18N
175
}
176         
177         FileObject persistenceXml = persistenceScope.getPersistenceXml();
178         if (persistenceXml == null) {
179             return Collections.emptySet();
180         }
181         
182         Set JavaDoc<Entity> result = new HashSet JavaDoc<Entity>();
183         Persistence persistence = PersistenceMetadata.getDefault().getRoot(persistenceXml);
184         for (PersistenceUnit persistenceUnit : persistence.getPersistenceUnit()) {
185             EntityMappings entityMappings = getEntityMappings(persistenceScope, persistenceUnit.getName());
186             if (entityMappings == null) {
187                 continue;
188             }
189             
190             for (Entity entity : entityMappings.getEntity()) {
191                 result.add(entity);
192             }
193         }
194         
195         return Collections.unmodifiableSet(result);
196     }
197     
198     /**
199      * Returns the set of entity classes in the given project as defined by
200      * the persistence units in the project's persistence.xml file(s). If
201      * there are duplicate entity classes (according to their name), only
202      * the first entity class will be contained in the returned set.
203      *
204      * @return a set of entity classes; never null.
205      * @throws NullPointerException if <code>project</code> is null.
206      * IOException if an I/O exception occured, e.g. if the persistence.xml
207      * file could not be read.
208      */

209     public static Set JavaDoc<Entity> getEntityClasses(Project project) throws IOException JavaDoc {
210         if (project == null) {
211             throw new NullPointerException JavaDoc("The project parameter cannot be null"); // NOI18N
212
}
213         
214         Set JavaDoc<Entity> result = new HashSet JavaDoc<Entity>();
215         Set JavaDoc<String JavaDoc> entityNames = new HashSet JavaDoc<String JavaDoc>();
216         for (PersistenceScope persistenceScope : getPersistenceScopes(project)) {
217             // issue 79891: leaving out Entity instances with duplicate names
218
for (Entity entity : getEntityClasses(persistenceScope)) {
219                 String JavaDoc entityName = entity.getName();
220                 if (!entityNames.contains(entityName)) {
221                     result.add(entity);
222                     entityNames.add(entityName);
223                 }
224             }
225         }
226         
227         return Collections.unmodifiableSet(result);
228     }
229     
230     /**
231      * Returns the set of entity classes in the given list of EntityMappings.
232      * If there are duplicate entity classes (according to their name), only
233      * the first entity class will be contained in the returned set.
234      *
235      * @return a set of entity classes; never null.
236      * @throws NullPointerException if <code>project</code> is null.
237      */

238     public static Set JavaDoc<Entity> getEntityClasses(List JavaDoc<EntityMappings> entityMappingsList) {
239         if (entityMappingsList == null) {
240             throw new NullPointerException JavaDoc("The entityMappingsList parameter cannot be null"); // NOI18N
241
}
242         
243         Set JavaDoc<Entity> result = new HashSet JavaDoc<Entity>();
244         Set JavaDoc<String JavaDoc> entityNames = new HashSet JavaDoc<String JavaDoc>();
245         for (EntityMappings em : entityMappingsList) {
246             // issue 79891: leaving out Entity instances with duplicate names
247
for (Entity entity : em.getEntity()) {
248                 String JavaDoc entityName = entity.getName();
249                 if (!entityNames.contains(entityName)) {
250                     result.add(entity);
251                     entityNames.add(entityName);
252                 }
253             }
254         }
255         
256         return Collections.unmodifiableSet(result);
257     }
258     
259     /**
260      * Returns an EntityMappings instance containing the entity classes in the given project
261      * defined by annotations. The return value
262      * will not contain entity mappings specified in ORM files. It may
263      * overlap with entity mappings contained in the return value of
264      * the {@link #getEntityMappings(PersistenceScope, String)}.
265      *
266      * @return an EntityMappings instance; never null.
267      * @throws NullPointerException if <code>project</code> is null.
268      */

269     public static EntityMappings getAnnotationEntityMappings(Project project) {
270         if (project == null) {
271             throw new NullPointerException JavaDoc("The project parameter cannot be null"); // NOI18N
272
}
273         
274         try {
275             return ORMMetadata.getDefault().getRoot(getPersistenceMetadataUnit(project));
276         } catch (IOException JavaDoc e) {
277             // should not occur, that is why we are catching here
278
// but anyway...
279
return new org.netbeans.modules.j2ee.persistence.dd.orm.model_1_0.EntityMappings();
280         }
281     }
282     
283     /**
284      * Returns the set of entity classes in the given project
285      * defined by annotations. The return value
286      * will not contain entity classes specified in ORM files. It may
287      * overlap with entity classes contained in the return value of
288      * the {@link #getEntityClasses(Project)}.
289      *
290      * @return a set of entity classes; never null.
291      * @throws NullPointerException if <code>project</code> is null.
292      */

293     public static Set JavaDoc<Entity> getAnnotationEntityClasses(Project project) {
294         Set JavaDoc<Entity> result = new HashSet JavaDoc<Entity>();
295         for (Entity entity : getAnnotationEntityMappings(project).getEntity()) {
296             result.add(entity);
297         }
298         return result;
299     }
300     
301     /**
302      * Returns a MetadataUnit that represents entity classes in a project as
303      * specified by the {@org.netbeans.modules.j2ee.persistence.spi.PersistenceClassPathProvider}
304      * interface.
305      *
306      * @param project project where metadata units should be found
307      * @return a single metadata unit
308      */

309     private static MetadataUnit getPersistenceMetadataUnit(Project project) {
310         MetadataUnit mu;
311         synchronized (project2MUCache) {
312             mu = project2MUCache.get(project);
313             if (mu == null) {
314                 ClassPath classpath = null;
315                 PersistenceClassPathProvider provider = (PersistenceClassPathProvider)project.getLookup().lookup(PersistenceClassPathProvider.class);
316                 if (provider != null) {
317                     classpath = provider.getClassPath();
318                 }
319                 mu = new WeakMetadataUnit(null, classpath);
320                 project2MUCache.put(project, mu);
321             }
322         }
323         return mu;
324     }
325     
326     /**
327      * Returns the persistence unit(s) the given entity class belongs to. Since
328      * an entity class can belong to any persistence unit, this returns all
329      * persistence units in all persistence.xml files in the project which owns
330      * the given entity class.
331      *
332      * @return an array of PersistenceUnit's; never null.
333      * @throws NullPointerException if <code>sourceFile</code> is null.
334      */

335     public static PersistenceUnit[] getPersistenceUnits(FileObject sourceFile) throws IOException JavaDoc {
336         if (sourceFile == null) {
337             throw new NullPointerException JavaDoc("The sourceFile parameter cannot be null"); // NOI18N
338
}
339         
340         Project project = FileOwnerQuery.getOwner(sourceFile);
341         if (project == null) {
342             return new PersistenceUnit[0];
343         }
344         
345         List JavaDoc<PersistenceUnit> result = new ArrayList JavaDoc<PersistenceUnit>();
346         for (PersistenceScope persistenceScope : getPersistenceScopes(project)) {
347             Persistence persistence = PersistenceMetadata.getDefault().getRoot(persistenceScope.getPersistenceXml());
348             for (PersistenceUnit persistenceUnit : persistence.getPersistenceUnit()) {
349                 result.add(persistenceUnit);
350             }
351         }
352         
353         return (PersistenceUnit[])result.toArray(new PersistenceUnit[result.size()]);
354     }
355     
356     /**
357      * Returns the entity mappings corresponding to sibling entity classes
358      * of the passed entity class.
359      *
360      * <p>This is not doable correctly through
361      * PersistenceScope since the list of sibling entity classes is given
362      * by the persistence unit(s) that the entity class is referenced in,
363      * and we don't know which persistence unit to choose. Thus we resort
364      * to looking just in the entity classes defined in annotations.</p>
365      *
366      * @return an instance of EntityMappings; can be null if e.g. the
367      * passed entity class is not owned by a project.
368      * @throws NullPointerException if <code>sourceFile</code> is null.
369      */

370     public static EntityMappings getEntityMappings(FileObject sourceFile) {
371         if (sourceFile == null) {
372             throw new NullPointerException JavaDoc("The sourceFile parameter cannot be null"); // NOI18N
373
}
374         
375         Project project = FileOwnerQuery.getOwner(sourceFile);
376         if (project == null) {
377             return null;
378         }
379         
380         return getAnnotationEntityMappings(project);
381     }
382     
383     /**
384      * Sorts a set of entity classes using the entity names as the key.
385      *
386      * @return the list of sorted entities.
387      * @throws NullPointerException if <code>entityClasses</code> is null.
388      * IOException if an I/O exception occured, e.g. if the persistence.xml
389      * file could not be read.
390      */

391     public static List JavaDoc<Entity> sortEntityClasses(Set JavaDoc<Entity> entityClasses) {
392         if (entityClasses == null) {
393             throw new NullPointerException JavaDoc("The entityClasses parameter cannot be null"); // NOI18N
394
}
395         List JavaDoc<Entity> entityList = new ArrayList JavaDoc(entityClasses);
396         Collections.sort(entityList, new EntityComparator());
397         return entityList;
398     }
399     
400     
401     /**
402      * Searches the given entity mappings for the specified entity class.
403      *
404      * @param className the Java class to search for.
405      * @param entityMappings the entity mappings to be searched.
406      * @return the entity class or null if it could not be found.
407      * @throws NullPointerException if <code>className</code> or
408      * <code>entityMappings</code> were null.
409      */

410     public static Entity getEntity(String JavaDoc className, EntityMappings entityMappings) {
411         if (className == null) {
412             throw new NullPointerException JavaDoc("The javaClass parameter cannot be null"); // NOI18N
413
}
414         if (entityMappings == null) {
415             throw new NullPointerException JavaDoc("The entityMappings parameter cannot be null"); // NOI18N
416
}
417         
418         for (Entity entity : entityMappings.getEntity()) {
419             if (className.equals(entity.getClass2())) {
420                 return entity;
421             }
422         }
423         return null;
424     }
425     
426     /**
427      * Returns an array containing all persistence scopes provided by the
428      * given project. This is just an utility method which does:
429      *
430      * <pre>
431      * PersistenceScopes.getPersistenceScopes(project).getPersistenceScopes();
432      * </pre>
433      *
434      * <p>with all the necessary checks for null (returning an empty
435      * array in this case).</p>
436      *
437      * @param project the project to retrieve the persistence scopes from.
438      * @return the list of persistence scopes provided by <code>project</code>;
439      * or an empty array if the project provides no persistence
440      * scopes; never null.
441      * @throws NullPointerException if <code>project</code> was null.
442      */

443     public static PersistenceScope[] getPersistenceScopes(Project project) {
444         if (project == null) {
445             throw new NullPointerException JavaDoc("The project parameter cannot be null"); // NOI18N
446
}
447         
448         PersistenceScopes persistenceScopes = PersistenceScopes.getPersistenceScopes(project);
449         if (persistenceScopes != null) {
450             return persistenceScopes.getPersistenceScopes();
451         }
452         return new PersistenceScope[0];
453     }
454     
455     /**
456      * Maintains a cache of metadata units for persistence units in a persistence scope.
457      * Not private because used in tests.
458      */

459     static final class EntityMappingsCache implements PropertyChangeListener JavaDoc {
460         
461         private final WeakReference JavaDoc<PersistenceScope> persistenceScopeRef;
462         
463         private final Map JavaDoc<String JavaDoc, WeakMetadataUnit> metadataUnitCache = new HashMap JavaDoc<String JavaDoc, WeakMetadataUnit>();
464         
465         /**
466          * Creates a new EntityMappingsCache. A factory method is needed in order
467          * to avoid starting listening in the constructor, making the newly
468          * created object available to other classes before the constructor
469          * finished.
470          */

471         public static EntityMappingsCache create(PersistenceScope persistenceScope) {
472             EntityMappingsCache result = new EntityMappingsCache(persistenceScope);
473             result.startListening();
474             return result;
475         }
476         
477         private EntityMappingsCache(PersistenceScope persistenceScope) {
478             persistenceScopeRef = new WeakReference JavaDoc<PersistenceScope>(persistenceScope);
479         }
480         
481         private void startListening() {
482             PUDataObject pudo = getPUDataObject();
483             if (pudo != null) {
484                 pudo.addPropertyChangeListener(WeakListeners.propertyChange(this, pudo));
485             }
486         }
487         
488         public EntityMappings getEntityMappings(String JavaDoc persistenceUnitName) throws IOException JavaDoc {
489             MetadataUnit mu = getMetadataUnit(persistenceUnitName);
490             if (mu != null) {
491                 return ORMMetadata.getDefault().getRoot(mu);
492             } else {
493                 return null;
494             }
495         }
496         
497         /**
498          * Returns the list of EntityMappings (one for each persistence unit).
499          */

500         public List JavaDoc<EntityMappings> getEntityMappings() throws IOException JavaDoc {
501             PUDataObject pudo = getPUDataObject();
502             if (pudo == null) {
503                 return Collections.emptyList();
504             }
505             
506             List JavaDoc<EntityMappings> entityMappingsList = new ArrayList JavaDoc<EntityMappings>();
507             
508             Persistence persistence = pudo.getPersistence();
509             synchronized (this) {
510                 for (PersistenceUnit persistenceUnit : persistence.getPersistenceUnit()) {
511                     EntityMappings em = getEntityMappings(persistenceUnit.getName());
512                     if (em != null) {
513                         entityMappingsList.add(em);
514                     }
515                 }
516             }
517             
518             return Collections.unmodifiableList(entityMappingsList);
519         }
520         
521         /**
522          * Finds a metadata unit for a persistence unit, creating a new one if
523          * not found in cache. Not private because of tests.
524          */

525         MetadataUnit getMetadataUnit(String JavaDoc persistenceUnitName) {
526             PersistenceScope persistenceScope = persistenceScopeRef.get();
527             if (persistenceScope == null) {
528                 return null;
529             }
530             
531             WeakMetadataUnit metadataUnit = null;
532             synchronized (this) {
533                 metadataUnit = metadataUnitCache.get(persistenceUnitName);
534                 if (metadataUnit == null) {
535                     PUDataObject pudo = getPUDataObject();
536                     if (pudo != null) {
537                         metadataUnit = createMetadataUnit(pudo, persistenceScope.getClassPath(), persistenceUnitName);
538                         if (metadataUnit != null) {
539                             metadataUnitCache.put(persistenceUnitName, metadataUnit);
540                         }
541                     }
542                 }
543             }
544             
545             return metadataUnit;
546         }
547         
548         private WeakMetadataUnit createMetadataUnit(PUDataObject pudo, ClassPath classPath, String JavaDoc persistenceUnitName) {
549             assert Thread.holdsLock(this);
550             
551             Persistence persistence = pudo.getPersistence();
552             for (PersistenceUnit unit : persistence.getPersistenceUnit()) {
553                 if (persistenceUnitName.equals(unit.getName())) {
554                     return new WeakMetadataUnit(getMappingFile(pudo), classPath);
555                 }
556             }
557             return null;
558         }
559         
560         /**
561          * Called when the persistence.xml file changed. Removes the metadata
562          * units corresponding to removed persistence units and refreshes the
563          * contents of remaining metadata units based on the contents of
564          * persistence.xml.
565          */

566         private void refreshCache() {
567             PUDataObject pudo = getPUDataObject();
568             if (pudo == null) {
569                 return;
570             }
571             
572             Persistence persistence = pudo.getPersistence();
573             synchronized (this) {
574                 Set JavaDoc<String JavaDoc> persistenceUnitNames = new HashSet JavaDoc<String JavaDoc>();
575                 for (PersistenceUnit persistenceUnit : persistence.getPersistenceUnit()) {
576                     persistenceUnitNames.add(persistenceUnit.getName());
577                 }
578                 
579                 // remove persistence units no longer in persistence.xml
580
for (Iterator JavaDoc<Map.Entry JavaDoc<String JavaDoc, WeakMetadataUnit>> i = metadataUnitCache.entrySet().iterator(); i.hasNext();) {
581                     if (!persistenceUnitNames.contains(i.next().getKey())) {
582                         i.remove();
583                     }
584                 }
585                 
586                 // refresh the remaining persistence units
587
for (PersistenceUnit persistenceUnit : persistence.getPersistenceUnit()) {
588                     // if (!metadataUnitCache.containsKey(persistenceUnit.get))
589
WeakMetadataUnit metadataUnit = metadataUnitCache.get(persistenceUnit.getName());
590                     if (metadataUnit == null) {
591                         // we don't cache a MU for this PU yet
592
continue;
593                     }
594                     
595                     refreshMetadataUnit(metadataUnit, pudo);
596                 }
597             }
598         }
599         
600         private static void refreshMetadataUnit(WeakMetadataUnit metadataUnit, PUDataObject pudo) {
601             metadataUnit.setDeploymentDescriptor(getMappingFile(pudo));
602         }
603         
604         private static FileObject getMappingFile(PUDataObject pudo) {
605             // XXX for now there can only be one deployment descriptor in MetadataUnit
606
// using META-INF/orm.xml for that and ignoring mapping-file elements in the persistence unit
607

608             FileObject persistenceXml = pudo.getPrimaryFile();
609             FileObject ormXml = null;
610             if (persistenceXml != null) {
611                 FileObject metaInf = persistenceXml.getParent();
612                 if (metaInf != null) {
613                     ormXml = metaInf.getFileObject("orm.xml"); // NOI18N
614
}
615             }
616             return ormXml;
617         }
618         
619         PUDataObject getPUDataObject() {
620             PersistenceScope persistenceScope = persistenceScopeRef.get();
621             FileObject persistenceXml = null;
622             if (persistenceScope != null) {
623                 persistenceXml = persistenceScope.getPersistenceXml();
624             }
625             
626             if (persistenceXml == null) {
627                 return null;
628             }
629             
630             try {
631                 return (PUDataObject)DataObject.find(persistenceXml);
632             } catch (DataObjectNotFoundException e) {
633                 return null;
634             }
635         }
636         
637         public void propertyChange(PropertyChangeEvent JavaDoc evt) {
638             if (DataObject.PROP_VALID.equals(evt.getPropertyName())) {
639                 removeFromCache(((DataObject)evt.getSource()).getPrimaryFile());
640             } else {
641                 refreshCache();
642             }
643         }
644     }
645     
646     /**
647      * Compares entity classes lexicographically by fully qualified names.
648      */

649     private static final class EntityComparator implements Comparator JavaDoc<Entity> {
650         
651         public int compare(Entity o1, Entity o2) {
652             String JavaDoc name1 = o1.getClass2();
653             String JavaDoc name2 = o2.getClass2();
654             if (name1 == null) {
655                 return name2 == null ? 0 : -1;
656             } else {
657                 return name2 == null ? 1 : name1.compareTo(name2);
658             }
659         }
660     }
661 }
662
Popular Tags