1 19 20 package org.apache.cayenne.jpa.conf; 21 22 import java.io.IOException ; 23 import java.net.URL ; 24 import java.util.Enumeration ; 25 import java.util.HashSet ; 26 import java.util.Map ; 27 import java.util.Set ; 28 29 import javax.persistence.spi.PersistenceUnitInfo; 30 31 import org.apache.cayenne.jpa.JpaProviderException; 32 import org.apache.cayenne.jpa.map.JpaClassDescriptor; 33 import org.apache.cayenne.jpa.map.JpaEntityMap; 34 35 65 public class EntityMapLoader { 66 67 static final String DESCRIPTOR_LOCATION = "META-INF/orm.xml"; 68 69 protected EntityMapLoaderContext context; 70 protected Map <String , JpaClassDescriptor> descriptors; 71 72 76 public EntityMapLoader(PersistenceUnitInfo persistenceUnit) { 77 loadEntityMap(persistenceUnit); 78 } 79 80 83 public JpaEntityMap getEntityMap() { 84 return context.getEntityMap(); 85 } 86 87 91 protected void loadEntityMap(PersistenceUnitInfo persistenceUnit) 92 throws JpaProviderException { 93 94 this.context = new EntityMapLoaderContext(persistenceUnit); 95 96 try { 97 loadFromAnnotations(persistenceUnit); 98 updateFromXML(persistenceUnit); 99 updateFromDefaults(); 100 } 101 catch (JpaProviderException e) { 102 throw e; 103 } 104 catch (Exception e) { 105 throw new JpaProviderException("Error loading ORM descriptors", e); 106 } 107 } 108 109 112 protected void updateFromDefaults() { 113 new EntityMapDefaultsProcessor().applyDefaults(context); 114 } 115 116 129 protected void updateFromXML(PersistenceUnitInfo unit) throws IOException { 130 131 EntityMapMergeProcessor merger = new EntityMapMergeProcessor(context); 132 133 Set loadedLocations = new HashSet (); 134 EntityMapXMLLoader loader = new EntityMapXMLLoader( 135 context.getTempClassLoader(), 136 false); 137 138 loadedLocations.add(DESCRIPTOR_LOCATION); 140 Enumeration <URL > standardDescriptors = context.getTempClassLoader().getResources( 141 DESCRIPTOR_LOCATION); 142 143 while (standardDescriptors.hasMoreElements()) { 144 JpaEntityMap map = loader.getEntityMap(standardDescriptors.nextElement()); 145 merger.mergeOverride(map); 146 } 147 148 151 for (String descriptor : unit.getMappingFileNames()) { 153 154 if (loadedLocations.add(descriptor)) { 157 158 Enumeration <URL > mappedDescriptors = context 159 .getTempClassLoader() 160 .getResources(descriptor); 161 while (mappedDescriptors.hasMoreElements()) { 162 JpaEntityMap map = loader.getEntityMap(mappedDescriptors 163 .nextElement()); 164 merger.mergeOverride(map); 165 } 166 } 167 } 168 } 169 170 173 protected void loadFromAnnotations(PersistenceUnitInfo persistenceUnit) { 174 175 if (!persistenceUnit.getManagedClassNames().isEmpty()) { 176 177 ClassLoader loader = context.getTempClassLoader(); 180 EntityMapAnnotationLoader annotationLoader = new EntityMapAnnotationLoader( 181 context); 182 183 for (String className : persistenceUnit.getManagedClassNames()) { 184 185 Class managedClass; 186 try { 187 managedClass = Class.forName(className, true, loader); 188 } 189 catch (ClassNotFoundException e) { 190 throw new JpaProviderException("Class not found: " + className, e); 191 } 192 193 annotationLoader.loadClassMapping(managedClass); 194 } 195 } 196 } 197 198 public EntityMapLoaderContext getContext() { 199 return context; 200 } 201 } 202 | Popular Tags |