1 21 package oracle.toplink.essentials.internal.ejb.cmp3.metadata; 22 23 import java.io.InputStream ; 24 import java.io.IOException ; 25 26 import java.util.Collection ; 27 import java.util.HashSet ; 28 import java.util.Iterator ; 29 import java.util.Set ; 30 31 import javax.persistence.spi.PersistenceUnitInfo; 32 33 import oracle.toplink.essentials.ejb.cmp3.persistence.PersistenceUnitProcessor; 34 import oracle.toplink.essentials.internal.ejb.cmp3.metadata.accessors.ClassAccessor; 35 import oracle.toplink.essentials.internal.ejb.cmp3.metadata.accessors.objects.MetadataClass; 36 37 import oracle.toplink.essentials.internal.ejb.cmp3.xml.accessors.XMLClassAccessor; 38 39 import oracle.toplink.essentials.internal.ejb.cmp3.xml.XMLConstants; 40 import oracle.toplink.essentials.internal.ejb.cmp3.xml.XMLHelper; 41 import oracle.toplink.essentials.internal.ejb.cmp3.xml.XMLLogger; 42 import oracle.toplink.essentials.internal.ejb.cmp3.xml.XMLValidator; 43 44 import oracle.toplink.essentials.internal.sessions.AbstractSession; 45 46 import org.w3c.dom.Node ; 47 import org.w3c.dom.NodeList ; 48 49 55 public class MetadataProcessor { 56 protected ClassLoader m_loader; 57 protected MetadataLogger m_logger; 58 protected MetadataProject m_project; 59 protected MetadataValidator m_validator; 60 61 66 public MetadataProcessor(AbstractSession session, ClassLoader loader, boolean enableLazyForOneToOne) { 67 m_loader = loader; 68 m_project = new MetadataProject(session, enableLazyForOneToOne); 69 } 70 71 76 public MetadataProcessor(AbstractSession session, ClassLoader loader, Collection <Class > entities, boolean enableLazyForOneToOne) { 77 m_loader = loader; 78 m_project = new MetadataProject(session, enableLazyForOneToOne); 79 80 for (Class entity : entities) { 81 m_project.addDescriptor(new MetadataDescriptor(entity)); 82 } 83 } 84 85 90 public void addEntityListeners() { 91 for (MetadataDescriptor descriptor: m_project.getDescriptors()) { 92 ClassAccessor accessor = descriptor.getClassAccessor(); 94 95 accessor.setAnnotatedElement(descriptor.getJavaClass()); 98 accessor.clearMappedSuperclasses(); 99 100 accessor.processListeners(m_loader); 101 } 102 } 103 104 109 public void addNamedQueries() { 110 m_project.processNamedQueries(m_validator); 111 m_project.processNamedNativeQueries(m_loader); 112 } 113 114 119 public static Set <String > buildClassSet(InputStream xmlDocumentInputStream, String fileName, ClassLoader loader) { 120 XMLHelper helper = new XMLHelper(xmlDocumentInputStream, fileName, loader); 121 122 String defaultPkg = helper.getNodeValue(new String [] {XMLConstants.ENTITY_MAPPINGS, XMLConstants.PACKAGE, XMLConstants.TEXT}); 124 125 HashSet <String > classSet = new HashSet <String >(); 128 classSet.addAll(buildClassSetForNodeList(helper, XMLConstants.ENTITY, defaultPkg)); 129 130 return classSet; 131 } 132 133 138 protected static Set <String > buildClassSetForNodeList(XMLHelper helper, String xPath, String defaultPkg) { 139 HashSet <String > classNames = new HashSet <String >(); 140 NodeList nodes = helper.getNodes(XMLConstants.ENTITY_MAPPINGS, xPath); 141 int nodeCount = nodes.getLength(); 142 143 for (int i = 0; i < nodeCount; i++) { 144 classNames.add(XMLHelper.getFullyQualifiedClassName(helper.getNode(nodes.item(i), XMLConstants.ATT_CLASS).getNodeValue(), defaultPkg)); 146 } 147 148 return classNames; 149 } 150 151 155 public MetadataLogger getLogger() { 156 return m_logger; 157 } 158 159 162 public MetadataProject getProject() { 163 return m_project; 164 } 165 166 170 public MetadataValidator getValidator() { 171 return m_validator; 172 } 173 174 179 public AbstractSession processAnnotations() { 180 m_validator = new MetadataValidator(); 182 m_logger = new MetadataLogger(m_project.getSession()); 183 184 for (MetadataDescriptor descriptor: 187 m_project.getDescriptors().toArray(new MetadataDescriptor[]{})) { 188 ClassAccessor accessor = descriptor.getClassAccessor(); 190 191 if (accessor == null) { 194 accessor = new ClassAccessor(new MetadataClass(descriptor.getJavaClass()), this, descriptor); 195 descriptor.setClassAccessor(accessor); 196 accessor.process(); 197 } 198 } 199 200 return m_project.process(); 204 } 205 206 217 public void processPersistenceUnitMetadata(AbstractSession session, PersistenceUnitInfo persistenceUnitInfo, Collection <String > mappingFileNames, Collection <Class > entities) { 218 if (! mappingFileNames.isEmpty()) { 219 Iterator <String > fileNames = mappingFileNames.iterator(); 222 223 Iterator <String > mappingFileNamesIt = mappingFileNames.iterator(); 224 while (mappingFileNamesIt.hasNext()){ 225 String fileName = mappingFileNamesIt.next(); 226 InputStream inputStream = null; 227 m_logger = new XMLLogger(session); 228 try { 229 inputStream = PersistenceUnitProcessor.createInputStreamForFileInPersistenceUnit(fileName, persistenceUnitInfo, m_loader); 230 231 if (inputStream == null){ 232 getLogger().logWarningMessage(XMLLogger.COULD_NOT_FIND_ORM_XML_FILE, fileName); 233 continue; 234 } 235 XMLHelper helper = new XMLHelper(inputStream, fileNames.next(), m_loader); 237 238 NodeList nodes = helper.getNodes(XMLConstants.ENTITY_MAPPINGS, XMLConstants.MAPPED_SUPERCLASS); 240 241 for (int i = 0; i < nodes.getLength(); i++) { 242 Node node = nodes.item(i); 243 Class cls = helper.getNodeValue(nodes.item(i), XMLConstants.ATT_CLASS, void.class); 244 m_project.addMappedSuperclass(cls, node, helper); 245 } 246 247 nodes = helper.getNodes(XMLConstants.ENTITY_MAPPINGS, XMLConstants.EMBEDDABLE); 249 250 for (int i = 0; i < nodes.getLength(); i++) { 251 Node node = nodes.item(i); 252 Class cls = helper.getNodeValue(nodes.item(i), XMLConstants.ATT_CLASS, void.class); 253 m_project.addEmbeddable(cls, node, helper); 254 } 255 256 Node persistenceUnitMetadataNode = helper.getNode(new String [] {XMLConstants.ENTITY_MAPPINGS, XMLConstants.PU_METADATA}); 258 259 if (persistenceUnitMetadataNode != null) { 260 MetadataPersistenceUnit persistenceUnit = new MetadataPersistenceUnit(); 261 262 persistenceUnit.setIsMetadataComplete(helper.getNode(persistenceUnitMetadataNode, XMLConstants.METADATA_COMPLETE) != null); 264 265 Node persistenceUnitDefaultsNode = helper.getNode(persistenceUnitMetadataNode, XMLConstants.PU_DEFAULTS); 267 268 if (persistenceUnitDefaultsNode != null) { 269 persistenceUnit.setAccess(helper.getNodeTextValue(persistenceUnitDefaultsNode, XMLConstants.ACCESS)); 271 272 persistenceUnit.setSchema(helper.getNodeTextValue(persistenceUnitDefaultsNode, XMLConstants.SCHEMA)); 274 275 persistenceUnit.setCatalog(helper.getNodeTextValue(persistenceUnitDefaultsNode, XMLConstants.CATALOG)); 277 278 persistenceUnit.setIsCascadePersist(helper.getNode(persistenceUnitDefaultsNode, XMLConstants.CASCADE_PERSIST) != null); 280 281 NodeList listenerNodes = helper.getNodes(persistenceUnitDefaultsNode, XMLConstants.ENTITY_LISTENERS, XMLConstants.ENTITY_LISTENER); 285 if (listenerNodes != null) { 286 m_project.addDefaultListeners(listenerNodes, helper); 287 } 288 } 289 290 MetadataPersistenceUnit existingPersistenceUnit = m_project.getPersistenceUnit(); 294 if (existingPersistenceUnit != null) { 295 if (! existingPersistenceUnit.equals(persistenceUnit)) { 296 (new XMLValidator()).throwPersistenceUnitMetadataConflict(existingPersistenceUnit.getConflict()); 297 } 298 } else { 299 m_project.setPersistenceUnit(persistenceUnit); 300 } 301 } 302 } catch (IOException exception){ 303 getLogger().logWarningMessage(XMLLogger.ERROR_LOADING_ORM_XML_FILE, fileName, exception); 304 } catch (RuntimeException re) { 305 throw re; 306 } finally { 307 if (inputStream != null){ 308 try{ 309 inputStream.close(); 310 } catch (IOException exc){}; 311 } 312 } 313 314 } 315 } 316 317 if (! entities.isEmpty()) { 320 for (Class entity : entities) { 321 m_project.addDescriptor(new MetadataDescriptor(entity)); 322 } 323 } 324 } 325 326 331 public void processXML(InputStream xmlDocumentStream, String fileName) { 332 if (m_project.hasDescriptors()) { 333 m_validator = new XMLValidator(); 335 m_logger = new XMLLogger(m_project.getSession()); 336 XMLHelper helper = new XMLHelper(xmlDocumentStream, fileName, m_loader); 337 338 MetadataDescriptor desc = m_project.getDescriptors().iterator().next(); 341 XMLClassAccessor dummyAccessor = new XMLClassAccessor(new MetadataClass(desc.getJavaClass()), null, helper, this, desc); 342 dummyAccessor.processEntityMappings(); 343 344 NodeList entityNodes = helper.getNodes(XMLConstants.ENTITY_MAPPINGS, XMLConstants.ENTITY); 346 347 if (entityNodes != null) { 348 for (int i = 0; i < entityNodes.getLength(); i++) { 349 Node entityNode = entityNodes.item(i); 350 Class entityClass = helper.getClassForNode(entityNode); 351 MetadataDescriptor descriptor = m_project.getDescriptor(entityClass); 352 353 ClassAccessor accessor = descriptor.getClassAccessor(); 355 356 if (accessor == null) { 359 accessor = new XMLClassAccessor(new MetadataClass(descriptor.getJavaClass()), entityNode, helper, this, descriptor); 360 descriptor.setClassAccessor(accessor); 361 accessor.process(); 362 } 363 } 364 } 365 } else { 366 } 368 } 369 370 375 public void setClassLoader(ClassLoader loader) { 376 m_loader = loader; 377 } 378 } 379 | Popular Tags |