1 22 package org.jboss.ejb.plugins.cmp.jdbc.metadata; 23 24 import java.util.ArrayList ; 25 import java.util.Collection ; 26 import java.util.Collections ; 27 import java.util.HashMap ; 28 import java.util.HashSet ; 29 import java.util.Iterator ; 30 import java.util.Map ; 31 32 import org.w3c.dom.Element ; 33 34 import org.jboss.deployment.DeploymentException; 35 import org.jboss.metadata.ApplicationMetaData; 36 import org.jboss.metadata.BeanMetaData; 37 import org.jboss.metadata.EntityMetaData; 38 import org.jboss.metadata.MetaData; 39 import org.jboss.metadata.RelationMetaData; 40 41 import org.jboss.ejb.plugins.cmp.jdbc.SQLUtil; 42 43 51 public final class JDBCApplicationMetaData 52 { 53 private final static Class JDBC_PM = org.jboss.ejb.plugins.cmp.jdbc.JDBCStoreManager.class; 54 55 59 private final ClassLoader classLoader; 60 61 64 private final ApplicationMetaData applicationMetaData; 65 66 69 private final Map userTypeMappings; 70 71 74 private final Map typeMappings = new HashMap (); 75 76 79 private final Map entities = new HashMap (); 80 81 84 private final Collection relationships = new HashSet (); 85 86 89 private final Map entityRoles = new HashMap (); 90 91 94 private final Map valueClasses = new HashMap (); 95 96 99 private final Map entitiesByAbstractSchemaName = new HashMap (); 100 101 104 private final Map entitiesByInterface = new HashMap (); 105 106 109 private final Map entityCommands = new HashMap (); 110 111 123 public JDBCApplicationMetaData(ApplicationMetaData applicationMetaData, ClassLoader classLoader) 124 throws DeploymentException 125 { 126 this.classLoader = classLoader; 128 this.applicationMetaData = applicationMetaData; 129 130 Iterator beans = applicationMetaData.getEnterpriseBeans(); 133 while(beans.hasNext()) 134 { 135 BeanMetaData bean = (BeanMetaData)beans.next(); 136 137 if(bean.isEntity()) 139 { 140 EntityMetaData entity = (EntityMetaData)bean; 141 142 Class pm; 144 try 145 { 146 pm = classLoader.loadClass(entity.getContainerConfiguration().getPersistenceManager()); 147 } 148 catch (ClassNotFoundException e) 149 { 150 throw new DeploymentException("Unable to load persistence manager", e); 151 } 152 if(entity.isCMP() && 153 (JDBC_PM.isAssignableFrom(pm) || pm.getName().equals("org.jboss.ejb.plugins.cmp.jdbc2.JDBCStoreManager2"))) 154 { 155 JDBCEntityMetaData jdbcEntity = new JDBCEntityMetaData(this, entity); 156 157 entities.put(entity.getEjbName(), jdbcEntity); 158 159 String schemaName = jdbcEntity.getAbstractSchemaName(); 160 if(schemaName != null) 161 { 162 entitiesByAbstractSchemaName.put(schemaName, jdbcEntity); 163 } 164 165 Class remote = jdbcEntity.getRemoteClass(); 166 if(remote != null) 167 { 168 entitiesByInterface.put(remote, jdbcEntity); 169 } 170 171 Class local = jdbcEntity.getLocalClass(); 172 if(local != null) 173 { 174 entitiesByInterface.put(local, jdbcEntity); 175 } 176 177 entityRoles.put(entity.getEjbName(), new HashSet ()); 179 } 180 } 181 } 182 183 Iterator iterator = applicationMetaData.getRelationships(); 185 while(iterator.hasNext()) 186 { 187 RelationMetaData relation = (RelationMetaData)iterator.next(); 188 189 JDBCRelationMetaData jdbcRelation = 191 new JDBCRelationMetaData(this, relation); 192 relationships.add(jdbcRelation); 193 194 JDBCRelationshipRoleMetaData left = 196 jdbcRelation.getLeftRelationshipRole(); 197 Collection leftEntityRoles = 198 (Collection )entityRoles.get(left.getEntity().getName()); 199 leftEntityRoles.add(left); 200 201 JDBCRelationshipRoleMetaData right = 203 jdbcRelation.getRightRelationshipRole(); 204 Collection rightEntityRoles = 205 (Collection )entityRoles.get(right.getEntity().getName()); 206 rightEntityRoles.add(right); 207 } 208 209 userTypeMappings = Collections.EMPTY_MAP; 210 } 211 212 225 public JDBCApplicationMetaData(Element element, JDBCApplicationMetaData defaultValues) 226 throws DeploymentException 227 { 228 232 classLoader = defaultValues.classLoader; 233 applicationMetaData = defaultValues.applicationMetaData; 234 235 Element userTypeMaps = MetaData.getOptionalChild(element, "user-type-mappings"); 236 if(userTypeMaps != null) 237 { 238 userTypeMappings = new HashMap (); 239 Iterator iter = MetaData.getChildrenByTagName(userTypeMaps, "user-type-mapping"); 240 while(iter.hasNext()) 241 { 242 Element userTypeMappingEl = (Element )iter.next(); 243 JDBCUserTypeMappingMetaData userTypeMapping = new JDBCUserTypeMappingMetaData(userTypeMappingEl); 244 userTypeMappings.put(userTypeMapping.getJavaType(), userTypeMapping); 245 } 246 } 247 else 248 userTypeMappings = defaultValues.getUserTypeMappings(); 249 250 typeMappings.putAll(defaultValues.typeMappings); 252 Element typeMaps = MetaData.getOptionalChild(element, "type-mappings"); 253 if(typeMaps != null) 254 { 255 for(Iterator i = MetaData.getChildrenByTagName(typeMaps, "type-mapping"); i.hasNext();) 256 { 257 Element typeMappingElement = (Element )i.next(); 258 JDBCTypeMappingMetaData typeMapping = 259 new JDBCTypeMappingMetaData(typeMappingElement); 260 typeMappings.put(typeMapping.getName(), typeMapping); 261 } 262 } 263 264 valueClasses.putAll(defaultValues.valueClasses); 266 Element valueClassesElement = 267 MetaData.getOptionalChild(element, "dependent-value-classes"); 268 if(valueClassesElement != null) 269 { 270 for(Iterator i = MetaData.getChildrenByTagName( 271 valueClassesElement, "dependent-value-class"); 272 i.hasNext();) 273 { 274 275 Element valueClassElement = (Element )i.next(); 276 JDBCValueClassMetaData valueClass = 277 new JDBCValueClassMetaData(valueClassElement, classLoader); 278 valueClasses.put(valueClass.getJavaType(), valueClass); 279 } 280 } 281 282 entityCommands.putAll(defaultValues.entityCommands); 284 Element entityCommandMaps = MetaData.getOptionalChild( 285 element, "entity-commands"); 286 if(entityCommandMaps != null) 287 { 288 for(Iterator i = 289 MetaData.getChildrenByTagName(entityCommandMaps, "entity-command"); 290 i.hasNext();) 291 { 292 293 Element entityCommandElement = (Element )i.next(); 294 JDBCEntityCommandMetaData entityCommand = 295 new JDBCEntityCommandMetaData(entityCommandElement); 296 entityCommands.put(entityCommand.getCommandName(), entityCommand); 297 } 298 } 299 300 Element rWords = MetaData.getOptionalChild(element,"reserved-words"); 303 if (rWords!=null) 304 { 305 for (Iterator i = MetaData.getChildrenByTagName(rWords,"word"); i.hasNext() ; ) 306 { 307 Element rWord = (Element )i.next(); 308 SQLUtil.addToRwords(MetaData.getElementContent(rWord)); 309 } 310 } 311 312 entities.putAll(defaultValues.entities); 315 entitiesByAbstractSchemaName.putAll( 316 defaultValues.entitiesByAbstractSchemaName); 317 entitiesByInterface.putAll(defaultValues.entitiesByInterface); 318 Element defaults = MetaData.getOptionalChild(element, "defaults"); 319 if(defaults != null) 320 { 321 ArrayList values = new ArrayList (entities.values()); 322 for(Iterator i = values.iterator(); i.hasNext();) 323 { 324 JDBCEntityMetaData entityMetaData = (JDBCEntityMetaData)i.next(); 325 326 entityMetaData = 328 new JDBCEntityMetaData(this, defaults, entityMetaData); 329 330 entities.put(entityMetaData.getName(), entityMetaData); 332 333 String schemaName = entityMetaData.getAbstractSchemaName(); 334 if(schemaName != null) 335 { 336 entitiesByAbstractSchemaName.put(schemaName, entityMetaData); 337 } 338 339 Class remote = entityMetaData.getRemoteClass(); 340 if(remote != null) 341 { 342 entitiesByInterface.put(remote, entityMetaData); 343 } 344 345 Class local = entityMetaData.getLocalClass(); 346 if(local != null) 347 { 348 entitiesByInterface.put(local, entityMetaData); 349 } 350 } 351 } 352 353 Element enterpriseBeans = 356 MetaData.getOptionalChild(element, "enterprise-beans"); 357 if(enterpriseBeans != null) 358 { 359 for(Iterator i = 360 MetaData.getChildrenByTagName(enterpriseBeans, "entity"); 361 i.hasNext();) 362 { 363 364 Element beanElement = (Element )i.next(); 365 366 String ejbName = 368 MetaData.getUniqueChildContent(beanElement, "ejb-name"); 369 JDBCEntityMetaData entityMetaData = getBeanByEjbName(ejbName); 370 371 if(entityMetaData == null) 372 { 373 throw new DeploymentException("Configuration found in " + 374 "jbosscmp-jdbc.xml for entity " + ejbName + " but bean " + 375 "is not a jbosscmp-jdbc-managed cmp entity in " + 376 "ejb-jar.xml"); 377 } 378 entityMetaData = 379 new JDBCEntityMetaData(this, beanElement, entityMetaData); 380 entities.put(entityMetaData.getName(), entityMetaData); 381 382 String schemaName = entityMetaData.getAbstractSchemaName(); 383 if(schemaName != null) 384 { 385 entitiesByAbstractSchemaName.put(schemaName, entityMetaData); 386 } 387 388 Class remote = entityMetaData.getRemoteClass(); 389 if(remote != null) 390 { 391 entitiesByInterface.put(remote, entityMetaData); 392 } 393 394 Class local = entityMetaData.getLocalClass(); 395 if(local != null) 396 { 397 entitiesByInterface.put(local, entityMetaData); 398 } 399 } 400 } 401 402 if(defaults == null) 405 { 406 relationships.addAll(defaultValues.relationships); 408 entityRoles.putAll(defaultValues.entityRoles); 409 } 410 else 411 { 412 413 for(Iterator i = entities.values().iterator(); i.hasNext();) 415 { 416 JDBCEntityMetaData entity = (JDBCEntityMetaData)i.next(); 417 entityRoles.put(entity.getName(), new HashSet ()); 418 } 419 420 for(Iterator i = defaultValues.relationships.iterator(); 422 i.hasNext();) 423 { 424 425 JDBCRelationMetaData relationMetaData = 426 (JDBCRelationMetaData)i.next(); 427 428 relationMetaData = 430 new JDBCRelationMetaData(this, defaults, relationMetaData); 431 432 relationships.add(relationMetaData); 434 435 JDBCRelationshipRoleMetaData left = 437 relationMetaData.getLeftRelationshipRole(); 438 Collection leftEntityRoles = 439 (Collection )entityRoles.get(left.getEntity().getName()); 440 leftEntityRoles.add(left); 441 442 JDBCRelationshipRoleMetaData right = 444 relationMetaData.getRightRelationshipRole(); 445 Collection rightEntityRoles = 446 (Collection )entityRoles.get(right.getEntity().getName()); 447 rightEntityRoles.add(right); 448 } 449 } 450 451 Element relationshipsElement = 454 MetaData.getOptionalChild(element, "relationships"); 455 if(relationshipsElement != null) 456 { 457 458 Map relationByName = new HashMap (); 460 for(Iterator i = relationships.iterator(); i.hasNext();) 461 { 462 JDBCRelationMetaData relation = (JDBCRelationMetaData)i.next(); 463 if(relation.getRelationName() != null) 464 { 465 relationByName.put(relation.getRelationName(), relation); 466 } 467 } 468 469 470 for(Iterator i = MetaData.getChildrenByTagName(relationshipsElement, "ejb-relation"); i.hasNext();) 471 { 472 Element relationElement = (Element )i.next(); 473 474 String relationName = MetaData.getUniqueChildContent(relationElement, "ejb-relation-name"); 476 JDBCRelationMetaData oldRelation = (JDBCRelationMetaData)relationByName.get(relationName); 477 478 if(oldRelation == null) 479 { 480 throw new DeploymentException("Configuration found in " + 481 "jbosscmp-jdbc.xml for relation " + relationName + 482 " but relation is not a jbosscmp-jdbc-managed relation " + 483 "in ejb-jar.xml"); 484 } 485 JDBCRelationMetaData newRelation = 487 new JDBCRelationMetaData(this, relationElement, oldRelation); 488 489 relationships.remove(oldRelation); 491 relationships.add(newRelation); 492 493 JDBCRelationshipRoleMetaData newLeft = 495 newRelation.getLeftRelationshipRole(); 496 Collection leftEntityRoles = 497 (Collection )entityRoles.get(newLeft.getEntity().getName()); 498 leftEntityRoles.remove(oldRelation.getLeftRelationshipRole()); 499 leftEntityRoles.add(newLeft); 500 501 JDBCRelationshipRoleMetaData newRight = 503 newRelation.getRightRelationshipRole(); 504 Collection rightEntityRoles = 505 (Collection )entityRoles.get(newRight.getEntity().getName()); 506 rightEntityRoles.remove(oldRelation.getRightRelationshipRole()); 507 rightEntityRoles.add(newRight); 508 } 509 } 510 511 } 512 513 518 public JDBCTypeMappingMetaData getTypeMappingByName(String name) 519 { 520 return (JDBCTypeMappingMetaData)typeMappings.get(name); 521 } 522 523 529 public Collection getRolesForEntity(String entityName) 530 { 531 Collection roles = (Collection )entityRoles.get(entityName); 532 return Collections.unmodifiableCollection(roles); 533 } 534 535 539 public Collection getValueClasses() 540 { 541 return Collections.unmodifiableCollection(valueClasses.values()); 542 } 543 544 549 public ClassLoader getClassLoader() 550 { 551 return classLoader; 552 } 553 554 559 public JDBCEntityMetaData getBeanByEjbName(String name) 560 { 561 return (JDBCEntityMetaData)entities.get(name); 562 } 563 564 569 public JDBCEntityCommandMetaData getEntityCommandByName(String name) 570 { 571 return (JDBCEntityCommandMetaData)entityCommands.get(name); 572 } 573 574 public Map getUserTypeMappings() 575 { 576 return Collections.unmodifiableMap(userTypeMappings); 577 } 578 } 579 | Popular Tags |