1 19 package org.netbeans.modules.j2ee.sun.share.configbean; 20 21 import java.io.IOException ; 22 import java.util.ArrayList ; 23 import java.util.Collection ; 24 import java.util.HashMap ; 25 import java.util.Iterator ; 26 import java.util.List ; 27 28 import java.beans.PropertyChangeEvent ; 29 import java.beans.PropertyChangeListener ; 30 import java.util.Map ; 31 32 import javax.enterprise.deploy.spi.DConfigBean ; 33 import javax.enterprise.deploy.model.DDBean ; 34 import javax.enterprise.deploy.model.DDBeanRoot ; 35 import javax.enterprise.deploy.model.XpathEvent ; 36 import javax.enterprise.deploy.spi.exceptions.BeanNotFoundException ; 37 import javax.enterprise.deploy.spi.exceptions.ConfigurationException ; 38 39 import org.xml.sax.SAXException ; 40 41 import org.openide.DialogDisplayer; 42 import org.openide.ErrorManager; 43 import org.openide.NotifyDescriptor; 44 45 import org.netbeans.modules.j2ee.sun.dd.api.CommonDDBean; 46 import org.netbeans.modules.j2ee.sun.dd.api.DDException; 47 import org.netbeans.modules.j2ee.sun.dd.api.DDProvider; 48 import org.netbeans.modules.j2ee.sun.dd.api.common.DefaultResourcePrincipal; 49 import org.netbeans.modules.j2ee.sun.dd.api.common.MessageDestination; 50 import org.netbeans.modules.j2ee.sun.dd.api.ejb.CmpResource; 51 import org.netbeans.modules.j2ee.sun.dd.api.ejb.EnterpriseBeans; 52 import org.netbeans.modules.j2ee.sun.dd.api.ejb.PmDescriptors; 53 import org.netbeans.modules.j2ee.sun.dd.api.ejb.SchemaGeneratorProperties; 54 import org.netbeans.modules.j2ee.sun.dd.api.ejb.SunEjbJar; 55 import org.netbeans.modules.j2ee.sun.dd.api.web.SunWebApp; 56 57 import org.netbeans.modules.j2ee.deployment.common.api.SourceFileMap; 58 import org.netbeans.modules.j2ee.deployment.common.api.OriginalCMPMapping; 59 60 import org.netbeans.modules.j2ee.sun.share.configbean.Base.DefaultSnippet; 61 62 import org.netbeans.modules.j2ee.sun.share.configbean.customizers.common.ValidationSupport; 64 65 import com.sun.jdo.api.persistence.mapping.ejb.beans.SunCmpMappings; 66 import com.sun.jdo.api.persistence.mapping.ejb.EJBInfoHelper; 67 import com.sun.jdo.api.persistence.mapping.ejb.ConversionHelper; 68 import com.sun.jdo.modules.persistence.mapping.core.util.MappingContext; 69 import com.sun.jdo.modules.persistence.mapping.ejb.EJBDevelopmentInfoHelper; 70 import com.sun.jdo.modules.persistence.mapping.ejb.util.MappingConverter; 71 import com.sun.jdo.modules.persistence.mapping.ejb.util.SunOneUtilsCMP; 72 73 import com.sun.jdo.api.persistence.model.Model; 75 import com.sun.jdo.api.persistence.model.ModelException; 76 import com.sun.jdo.api.persistence.model.jdo.PersistenceClassElement; 77 import com.sun.jdo.api.persistence.model.jdo.PersistenceFieldElement; 78 import com.sun.jdo.api.persistence.model.jdo.PersistenceElementProperties; 79 import com.sun.jdo.api.persistence.model.jdo.RelationshipElement; 80 import com.sun.jdo.api.persistence.model.mapping.MappingClassElement; 81 import com.sun.jdo.api.persistence.model.mapping.MappingElementProperties; 82 import com.sun.jdo.api.persistence.model.jdo.impl.PersistenceFieldElementImpl; 83 import com.sun.jdo.api.persistence.model.jdo.impl.RelationshipElementImpl; 84 import com.sun.jdo.api.persistence.model.mapping.MappingFieldElement; 85 import com.sun.jdo.api.persistence.model.mapping.impl.MappingFieldElementImpl; 86 import com.sun.jdo.api.persistence.model.mapping.impl.MappingRelationshipElementImpl; 87 89 90 94 public class EjbJarRoot extends BaseRoot implements javax.enterprise.deploy.spi.DConfigBean { 95 96 public static final String CMP_MAPPINGS_CHANGED = "CmpMappingsChanged"; 98 private static final String ABSOLUTE_XPATH_ROOT = "/ejb-jar/"; 100 private static final String SECURITY_ROLE_R_XPATH = "assembly-descriptor/security-role"; static final String SECURITY_ROLE_XPATH = ABSOLUTE_XPATH_ROOT + SECURITY_ROLE_R_XPATH; 102 103 private static final String ENTITY_R_XPATH = "enterprise-beans/entity"; static final String ENTITY_XPATH = ABSOLUTE_XPATH_ROOT + ENTITY_R_XPATH; 105 106 private static final String SESSION_R_XPATH = "enterprise-beans/session"; static final String SESSION_XPATH = ABSOLUTE_XPATH_ROOT + SESSION_R_XPATH; 108 109 private static final String MD_R_XPATH = "enterprise-beans/message-driven"; static final String MD_XPATH = ABSOLUTE_XPATH_ROOT + MD_R_XPATH; 111 112 private static final String CMP_FIELD_XPATH = ENTITY_XPATH + "/cmp-field"; private static final String CMP_FIELD_NAME_XPATH = CMP_FIELD_XPATH + "/field-name"; private static final String EJB_RELATION_XPATH = "/ejb-jar/relationships/ejb-relation"; private static final String CMR_FIELD_XPATH = EJB_RELATION_XPATH + "/ejb-relationship-role/cmr-field"; private static final String CMR_FIELD_NAME_XPATH = CMR_FIELD_XPATH + "/cmr-field-name"; private static final String CMP_MAPPING_FILE = "sun-cmp-mappings.xml"; 119 120 121 private String name; 122 123 124 private PmDescriptors pmDescriptors; 125 126 127 private CmpResource cmpResource; 128 129 130 private List messageDestinations; 131 132 133 private MappingContext mappingContext; 134 135 136 private EJBInfoHelper ejbInfoHelper; 137 138 141 private SourceFileMap sourceFileMap; 142 143 144 private ConversionHelper conversionHelper; 145 146 private PropertyChangeListener cmpMappingListener = 147 new PropertyChangeListener () { 148 public void propertyChange(PropertyChangeEvent evt) { 149 Object source = evt.getSource(); 150 String propName = evt.getPropertyName(); 151 152 if (((source instanceof MappingClassElement) && 153 MappingElementProperties.PROP_MODIFIED.equals(propName)) || 154 ((source instanceof PersistenceClassElement) && 155 PersistenceElementProperties.PROP_MODIFIED.equals(propName))) 156 { 157 boolean modified = 158 ((Boolean )evt.getNewValue()).booleanValue(); 159 160 if (modified) 162 getPCS().firePropertyChange("mapping", null, null); 163 } 164 } 165 }; 166 167 168 public EjbJarRoot() { 169 setDescriptorElement(bundle.getString("BDN_EjbJarRoot")); } 171 172 protected void init(DDBeanRoot dDBean, SunONEDeploymentConfiguration dc, DDBean ddbExtra) throws ConfigurationException { 173 super.init(dDBean, dc, ddbExtra); 174 175 loadFromPlanFile(dc); 176 } 177 178 181 182 private static ValidationSupport validationSupport = new ValidationSupport(); 183 184 public static final String FIELD_EJBJAR_NAME = "enterprise-beans/name"; 186 public static final String FIELD_CMP_RESOURCE = "enterprise-beans/cmp-resource"; 187 public static final String FIELD_CMPRES_JNDI_NAME = "enterprise-beans/cmp-resource/jndi-name"; 188 public static final String FIELD_CMPRES_DB_CREATE = "enterprise-beans/cmp-resource/create-tables-at-deploy"; 189 public static final String FIELD_CMPRES_DB_DROP = "enterprise-beans/cmp-resource/drop-tables-at-undeploy"; 190 public static final String FIELD_CMPRES_DB_VENDOR = "enterprise-beans/cmp-resource/database-vendor-name"; 191 public static final String FIELD_PM_DESCRIPTORS = "enterprise-beans/pm-descriptors"; 192 193 protected void updateValidationFieldList() { 194 super.updateValidationFieldList(); 195 196 validationFieldList.add(FIELD_EJBJAR_NAME); 197 validationFieldList.add(FIELD_CMP_RESOURCE); 198 validationFieldList.add(FIELD_PM_DESCRIPTORS); 199 } 200 201 public boolean validateField(String fieldId) { 202 Collection errors = new ArrayList (); 203 204 String absoluteFieldXpath = getAbsoluteXpath(fieldId); 214 if(fieldId.equals(FIELD_EJBJAR_NAME)) { 215 errors.add(executeValidator(ValidationError.PARTITION_EJBJAR_CMP_RESOURCE, 216 name, absoluteFieldXpath, bundle.getString("LBL_Name"))); } else if(fieldId.equals(FIELD_CMP_RESOURCE)) { 218 if(hasCmpResource()) { 220 errors.add(executeValidator(ValidationError.PARTITION_EJBJAR_CMP_RESOURCE, 221 cmpResource.getJndiName(), getAbsoluteXpath(FIELD_CMPRES_JNDI_NAME), 222 bundle.getString("LBL_Jndi_Name"))); errors.add(executeValidator(ValidationError.PARTITION_EJBJAR_CMP_RESOURCE, 224 cmpResource.getCreateTablesAtDeploy(), getAbsoluteXpath(FIELD_CMPRES_DB_CREATE), 225 bundle.getString("LBL_Create_Table_At_Deploy"))); errors.add(executeValidator(ValidationError.PARTITION_EJBJAR_CMP_RESOURCE, 227 cmpResource.getDropTablesAtUndeploy(), getAbsoluteXpath(FIELD_CMPRES_DB_DROP), 228 bundle.getString("LBL_Drop_Table_At_Undeploy"))); errors.add(executeValidator(ValidationError.PARTITION_EJBJAR_CMP_RESOURCE, 230 cmpResource.getDatabaseVendorName(), getAbsoluteXpath(FIELD_CMPRES_DB_VENDOR), 231 bundle.getString("LBL_Database_Vendor_Name"))); } else { 233 errors.add(ValidationError.getValidationErrorMask( 234 ValidationError.PARTITION_EJBJAR_CMP_RESOURCE, getAbsoluteXpath(FIELD_CMPRES_JNDI_NAME))); 235 errors.add(ValidationError.getValidationErrorMask( 236 ValidationError.PARTITION_EJBJAR_CMP_RESOURCE, getAbsoluteXpath(FIELD_CMPRES_DB_CREATE))); 237 errors.add(ValidationError.getValidationErrorMask( 238 ValidationError.PARTITION_EJBJAR_CMP_RESOURCE, getAbsoluteXpath(FIELD_CMPRES_DB_DROP))); 239 errors.add(ValidationError.getValidationErrorMask( 240 ValidationError.PARTITION_EJBJAR_CMP_RESOURCE, getAbsoluteXpath(FIELD_CMPRES_DB_VENDOR))); 241 } 242 } else if(fieldId.equals(FIELD_PM_DESCRIPTORS)) { 243 if(pmDescriptors != null && pmDescriptors.sizePmDescriptor() > 0 && pmDescriptors.getPmInuse() == null) { 244 errors.add(ValidationError.getValidationError(ValidationError.PARTITION_EJBJAR_PM_DESCRIPTORS, 245 getAbsoluteXpath(FIELD_PM_DESCRIPTORS), bundle.getString("ERR_PmInuseRequired"))); } else { 247 errors.add(ValidationError.getValidationErrorMask(ValidationError.PARTITION_EJBJAR_PM_DESCRIPTORS, 248 getAbsoluteXpath(FIELD_PM_DESCRIPTORS))); 249 } 250 } 251 252 boolean noErrors = true; 253 Iterator errorIter = errors.iterator(); 254 255 while(errorIter.hasNext()) { 256 ValidationError error = (ValidationError) errorIter.next(); 257 getMessageDB().updateError(error); 258 259 if(Utils.notEmpty(error.getMessage())) { 260 noErrors = false; 261 } 262 } 263 264 return noErrors; 266 } 267 268 private ValidationError executeValidator(ValidationError.Partition partition, String propertyValue, String fieldXpath, String fieldLabel) { 269 ValidationError error = null; 270 Collection messages = validationSupport.validate(propertyValue, fieldXpath, fieldLabel); 271 272 if(messages != null && messages.size() > 0) { 273 error = ValidationError.getValidationError(partition, fieldXpath, (String ) (messages.iterator().next())); 274 } else { 275 error = ValidationError.getValidationErrorMask(partition, fieldXpath); 276 } 277 278 return error; 279 } 280 281 private boolean hasCmpDrp() { 282 if(cmpResource != null) { 283 DefaultResourcePrincipal drp = cmpResource.getDefaultResourcePrincipal(); 284 if(drp != null && (Utils.notEmpty(drp.getName()) || Utils.notEmpty(drp.getPassword()))) { 285 return true; 286 } 287 } 288 289 return false; 290 } 291 292 private boolean hasCmpSgp() { 293 if(cmpResource != null) { 294 SchemaGeneratorProperties sgp = cmpResource.getSchemaGeneratorProperties(); 295 if(sgp != null && sgp.sizePropertyElement() > 0) { 296 return true; 297 } 298 } 299 300 return false; 301 } 302 303 private boolean hasCmpResource() { 304 if(cmpResource != null) { 305 if(Utils.notEmpty(cmpResource.getJndiName()) || 306 Utils.notEmpty(cmpResource.getCreateTablesAtDeploy()) || 307 Utils.notEmpty(cmpResource.getDropTablesAtUndeploy()) || 308 Utils.notEmpty(cmpResource.getDatabaseVendorName()) || 309 cmpResource.sizePropertyElement() > 0 || 310 hasCmpDrp() || 311 hasCmpSgp() 312 ) { 313 return true; 314 } 315 } 316 317 return false; 318 } 319 320 protected String getAbsoluteXpath(String field) { 321 String baseXpath = "/sun-ejb-jar"; 322 StringBuffer buf = new StringBuffer (baseXpath.length() + field.length() + 1); 323 buf.append(baseXpath); 324 buf.append("/"); buf.append(field); 326 return buf.toString(); 327 } 328 329 332 333 public DConfigBean getDConfigBean(DDBeanRoot dDBeanRoot) { 334 BaseRoot rootDCBean = null; 335 336 J2EEBaseVersion moduleVersion = getJ2EEModuleVersion(); 337 if(moduleVersion.compareTo(EjbJarVersion.EJBJAR_2_1) >= 0) { 338 rootDCBean = createWebServicesRoot(dDBeanRoot); 339 } 340 341 return rootDCBean; 342 } 343 344 345 349 public J2EEBaseVersion getJ2EEModuleVersion() { 350 DDBeanRoot ddbRoot = (DDBeanRoot ) getDDBean(); 351 352 String versionString = ddbRoot.getDDBeanRootVersion(); 354 if(versionString == null) { 355 versionString = ddbRoot.getModuleDTDVersion(); 357 } 358 359 J2EEBaseVersion ejbJarVersion = EjbJarVersion.getEjbJarVersion(versionString); 360 if(ejbJarVersion == null) { 361 ejbJarVersion = EjbJarVersion.EJBJAR_2_1; 363 } 364 365 return ejbJarVersion; 366 } 367 368 371 public String generateDocType(ASDDVersion version) { 372 String ejbDocType = generateDocType("sun-ejb-jar", version.getSunEjbJarPublicId(), version.getSunEjbJarSystemId()); String result = ejbDocType; 374 375 if(hasSunCmpMappings()) { 376 StringBuffer buffer = new StringBuffer (512); 377 buffer.append("sun-ejb-jar.xml: "); buffer.append(ejbDocType); buffer.append("\nsun-cmp-mappings.xml: "); buffer.append(generateDocType("sun-cmp-mappings", version.getSunCmpMappingsPublicId(), version.getSunCmpMappingsSystemId())); result = buffer.toString(); 382 } 383 384 return result; 385 } 386 387 389 public List getEjbs() { 390 List ejbs = new ArrayList (); 391 for(Iterator iter = getChildren().iterator(); iter.hasNext(); ) { 392 Object child = iter.next(); 393 if(child instanceof BaseEjb) { 394 ejbs.add(child); 395 } 396 } 397 return ejbs; 398 } 399 400 public String getHelpId() { 401 return "AS_CFG_EjbJarRoot"; } 403 404 408 private boolean hasSunCmpMappings() { 409 return mappingContext != null && ejbInfoHelper != null; 410 } 411 412 416 Collection getSnippets() { 417 Collection snippets = new ArrayList (); 418 Snippet snipOne = new DefaultSnippet() { 420 public CommonDDBean getDDSnippet() { 421 SunEjbJar sej = getConfig().getStorageFactory().createSunEjbJar(); 422 String version = sej.getVersion().toString(); 423 424 EnterpriseBeans eb = sej.newEnterpriseBeans(); 425 426 if(null != name) { 427 eb.setName(name); 428 } 429 430 PmDescriptors pmDesc = getPmDescriptors(); 431 if(pmDesc != null && (pmDesc.sizePmDescriptor() > 0 || pmDesc.getPmInuse() != null)) { 432 eb.setPmDescriptors((PmDescriptors) pmDesc.cloneVersion(version)); 433 } 434 435 if(hasCmpResource()) { 436 CmpResource cmpResource = getCmpResource(); 437 eb.setCmpResource((CmpResource) cmpResource.cloneVersion(version)); 438 } 439 440 MessageDestination [] msgDests = (MessageDestination []) 441 Utils.listToArray(getMessageDestinations(), MessageDestination.class, version); 442 if(msgDests != null) { 443 eb.setMessageDestination(msgDests); 444 } 445 446 sej.setEnterpriseBeans(eb); 447 448 450 restoreAllNamedBeans(sej, version); 451 452 restoreNamedBeans(getNamedBeanMap(EnterpriseBeans.EJB), 454 EnterpriseBeans.EJB, eb, version); 455 456 return sej; 457 } 458 }; 459 460 snippets.add(snipOne); 461 464 return snippets; 465 } 466 467 Snippet getCmpMappingSnippet() { 468 return new Snippet() { 469 public CommonDDBean getDDSnippet() { 470 return null; 471 } 472 public org.netbeans.modules.schema2beans.BaseBean getCmpDDSnippet() { 473 SunCmpMappings sunCmpMappings = 474 SunOneUtilsCMP.getSunCmpMappings(mappingContext, ejbInfoHelper); 475 476 Iterator iterator = 478 mappingContext.getModel().getMappingCache().values().iterator(); 479 while (iterator.hasNext()) { 480 ((MappingClassElement)iterator.next()).setModified(false); 481 } 483 484 ASDDVersion asVersion = ASDDVersion.SUN_APPSERVER_8_1; 490 sunCmpMappings.graphManager().setDoctype( 491 asVersion.getSunCmpMappingsPublicId(), asVersion.getSunCmpMappingsSystemId()); 492 493 return sunCmpMappings; 494 } 495 public boolean hasDDSnippet() { 496 return mappingContext != null && ejbInfoHelper != null; 498 } 499 public String getFileName() { 500 return CMP_MAPPING_FILE; 501 } 502 public String getPropertyName() { 504 assert(false); 506 return null; 507 } 508 public CommonDDBean mergeIntoRootDD(CommonDDBean ddRoot) { 509 assert(false); 511 return null; 512 } 513 public CommonDDBean mergeIntoRovingDD(CommonDDBean ddParent) { 514 assert(false); 516 return null; 517 } 518 }; 520 } 521 522 525 protected CommonDDBean processParentBean(CommonDDBean bean, DConfigBean child) { 526 if(child instanceof BaseEjb) { 528 return ((SunEjbJar) bean).getEnterpriseBeans(); 529 } 530 531 return bean; 533 } 534 535 539 public void removeDConfigBean(DConfigBean dConfigBean) throws BeanNotFoundException { 540 if ((dConfigBean != null) && (dConfigBean instanceof CmpEntityEjb)) 542 removeMappingForCmp(((CmpEntityEjb)dConfigBean).getEjbName()); 543 super.removeDConfigBean(dConfigBean); 544 } 545 546 public class SunEjbJarParser implements ConfigParser { 548 public Object parse(java.io.InputStream stream) throws IOException , SAXException , DDException { 549 DDProvider provider = DDProvider.getDefault(); 550 SunEjbJar result = null; 551 552 if(stream != null) { 553 result = provider.getEjbDDRoot(new org.xml.sax.InputSource (stream)); 555 } else { 556 result = (SunEjbJar) provider.newGraph(SunEjbJar.class, 558 getConfig().getAppServerVersion().getEjbJarVersionAsString()); 559 } 560 561 getConfig().internalSetAppServerVersion(ASDDVersion.getASDDVersionFromEjbVersion(result.getVersion())); 563 564 return result; 565 } 566 } 567 568 public class EjbJarRootFinder implements ConfigFinder { 569 public Object find(Object obj) { 570 SunEjbJar retVal = (SunEjbJar) obj; 571 return retVal; 572 } 573 } 574 575 public class SunCmpMappingsParser implements ConfigParser { 577 public Object parse(java.io.InputStream stream) throws IOException , SAXException , DDException { 578 SunCmpMappings result = null; 579 580 if(stream != null) { 581 try { 583 result = SunCmpMappings.createGraph(stream); 584 } catch(Exception ex) { 585 throw new RuntimeException ("Examine wrapped exception...", ex); } 591 } else { 592 result = SunCmpMappings.createGraph(); 594 } 595 596 return result; 601 } 602 } 603 604 public class SunCmpMappingsRootFinder implements ConfigFinder { 605 public Object find(Object obj) { 606 SunCmpMappings retVal = (SunCmpMappings) obj; 607 return retVal; 608 } 609 } 610 611 protected ConfigParser getParser() { 612 return new SunEjbJarParser(); 613 } 614 615 boolean loadFromPlanFile(SunONEDeploymentConfiguration config) { 616 String uriText = getUriText(); 617 618 SunEjbJar beanGraph = (SunEjbJar) config.getBeans(uriText, constructFileName(), 619 getParser(), new EjbJarRootFinder()); 620 boolean cmpMappingLoaded = false; 621 622 clearProperties(); 623 624 if(null != beanGraph) { 625 EnterpriseBeans eb = beanGraph.getEnterpriseBeans(); 626 if (null != eb) { 627 this.name = eb.getName(); 628 629 PmDescriptors pmDesc = eb.getPmDescriptors(); 630 if(pmDesc != null && (pmDesc.sizePmDescriptor() > 0 || pmDesc.getPmInuse() != null)) { 631 pmDescriptors = (PmDescriptors) pmDesc.clone(); 632 } 633 634 CmpResource cr = eb.getCmpResource(); 635 if(cr != null) { 636 cmpResource = (CmpResource) cr.clone(); 637 } 638 639 messageDestinations = Utils.arrayToList(eb.getMessageDestination()); 640 } 641 642 saveAllNamedBeans(beanGraph); 644 645 if(eb != null) { 647 saveNamedBeans(EnterpriseBeans.EJB, org.netbeans.modules.j2ee.sun.dd.api.ejb.Ejb.EJB_NAME, eb.getEjb()); 648 } 649 } else { 650 setDefaultProperties(); 651 } 652 653 cmpMappingLoaded = loadCmpMappingsFromPlanFile(config); 654 655 return ((beanGraph != null) || cmpMappingLoaded); 656 } 657 658 private static Collection ejbJarBeanSpecs = new ArrayList (); 659 660 static { 661 ejbJarBeanSpecs.add(new NamedBean(SunWebApp.SECURITY_ROLE_MAPPING, 666 org.netbeans.modules.j2ee.sun.dd.api.common.SecurityRoleMapping.ROLE_NAME)); 667 } 668 669 protected Collection getNamedBeanSpecs() { 670 return ejbJarBeanSpecs; 671 } 672 673 674 public MappingContext getMappingContext () { 675 return getMappingContext(null, getEJBInfoHelper()); 676 } 677 678 private MappingContext getMappingContext (SunCmpMappings beanGraph, 679 EJBInfoHelper infoHelper) { 680 if (mappingContext == null) { 681 try { 682 mappingContext = SunOneUtilsCMP.getMappingContext(beanGraph, 683 ejbInfoHelper); 684 SunOneUtilsCMP.setExistingMappingContext(ejbInfoHelper, 685 mappingContext); 686 Iterator iterator = 689 mappingContext.getModel().getMappingCache().values().iterator(); 690 while (iterator.hasNext()) { 691 addMappingListener((MappingClassElement)iterator.next()); 692 } 693 } catch(IllegalStateException ex) { 694 696 ErrorManager.getDefault().notify(ErrorManager.WARNING, ex); 701 } catch(Exception ex) { 702 ErrorManager.getDefault().notify(ErrorManager.INFORMATIONAL, ex); 706 } 707 } 708 709 return mappingContext; 710 } 711 712 private void addMappingListener (MappingClassElement mce) { 713 mce.addPropertyChangeListener(cmpMappingListener); 714 } 717 718 public EJBInfoHelper getEJBInfoHelper () { 719 return getEJBInfoHelper(null); 720 } 721 722 public EJBInfoHelper getEJBInfoHelper(SunCmpMappings beanGraph) { 723 if (ejbInfoHelper == null) { 724 sourceFileMap = SourceFileMap.findSourceMap(getConfig().getDeployableObject()); 725 ejbInfoHelper = new EJBDevelopmentInfoHelper(beanGraph, sourceFileMap); 726 } 727 728 return ejbInfoHelper; 729 } 730 731 private ConversionHelper getConversionHelper () { 732 if (conversionHelper == null) { 733 conversionHelper = getEJBInfoHelper().createConversionHelper(); 734 } 735 736 return conversionHelper; 737 } 738 739 public void mapCmpBeans(OriginalCMPMapping[] mapping) { 740 SunCmpMappings beanGraph = (SunCmpMappings) getConfig().getBeans( 741 getUriText(), CMP_MAPPING_FILE, new SunCmpMappingsParser(), 742 new SunCmpMappingsRootFinder()); 743 EJBInfoHelper infoHelper = getEJBInfoHelper(); 744 745 getMappingContext(beanGraph, infoHelper); 751 752 MappingConverter mappingConverter = new MappingConverter( 753 infoHelper, SourceFileMap.findSourceMap(getConfig().getDeployableObject())); 754 Collection newMCEs = null; 755 756 try { 757 newMCEs = mappingConverter.toMappingClasses(mapping); 758 } catch (Exception e) { 759 ErrorManager.getDefault().notify(ErrorManager.INFORMATIONAL, e); 763 } 764 765 if (newMCEs != null) { 766 Iterator iterator = newMCEs.iterator(); 767 while (iterator.hasNext()) { 768 addMappingListener((MappingClassElement)iterator.next()); 769 } 770 getPCS().firePropertyChange(CMP_MAPPINGS_CHANGED, null, newMCEs); 771 } 772 } 773 774 public void ensureCmpMappingExists (String beanName) { 775 try { 776 MappingContext mappingContext = getMappingContext(); 777 EJBInfoHelper infoHelper = getEJBInfoHelper(); 778 ConversionHelper myConversionHelper = getConversionHelper(); 779 780 if (mappingContext.getModel().getMappingClass( 783 myConversionHelper.getMappedClassName(beanName)) == null) { 784 MappingConverter mappingConverter = new MappingConverter( 785 infoHelper, SourceFileMap.findSourceMap(getConfig().getDeployableObject())); 786 MappingClassElement newMCE = null; 787 try { 788 newMCE = mappingConverter.toMappingClass(beanName); 789 } catch (Exception e) { 790 ErrorManager.getDefault().notify(ErrorManager.INFORMATIONAL, e); 794 } 795 796 if (newMCE != null) { 797 addMappingListener(newMCE); 798 getPCS().firePropertyChange(CMP_MAPPINGS_CHANGED, null, newMCE); 799 } 800 } 801 } catch(NullPointerException ex) { 802 ErrorManager.getDefault().notify(ErrorManager.EXCEPTION, ex); 805 } 806 } 807 808 private void removeMappingForCmp(String beanName) { 809 try { 810 if ((beanName != null) && (mappingContext != null)) { 811 EJBInfoHelper infoHelper = getEJBInfoHelper(); 812 ConversionHelper myConversionHelper = getConversionHelper(); 813 Model model = mappingContext.getModel(); 814 String pcClassName = conversionHelper.getMappedClassName(beanName); 815 MappingClassElement mce = model.getMappingClass(pcClassName); 816 817 if (mce != null) { 818 mce.removePropertyChangeListener(cmpMappingListener); 820 model.updateKeyForClass(null, pcClassName); 821 } 822 } 823 } catch(NullPointerException ex) { 824 ErrorManager.getDefault().notify(ErrorManager.EXCEPTION, ex); 827 } 828 } 829 830 private void addMappingForCmrField(String beanName, String fieldName) { 831 addMappingForCmpField(beanName, fieldName, true); 832 } 833 834 private void addMappingForCmpField(String beanName, String fieldName) { 835 addMappingForCmpField(beanName, fieldName, false); 836 } 837 838 private void addMappingForCmpField(String beanName, String fieldName, 839 boolean isRelationship) { 840 try { 841 if ((beanName != null) && (mappingContext != null)) { 842 EJBInfoHelper infoHelper = getEJBInfoHelper(); 843 ConversionHelper myConversionHelper = getConversionHelper(); 844 Model model = mappingContext.getModel(); 845 String pcClassName = conversionHelper.getMappedClassName(beanName); 846 MappingClassElement mce = model.getMappingClass(pcClassName); 847 848 if ((mce != null) && (mce.getField(fieldName) == null)) { 849 PersistenceClassElement pce = 850 model.getPersistenceClass(pcClassName); 851 PersistenceFieldElement newPFE = ((isRelationship) ? 859 new RelationshipElement( 860 new RelationshipElementImpl(fieldName), pce) : 861 new PersistenceFieldElement(new 862 PersistenceFieldElementImpl(fieldName), pce)); 863 864 try { 865 pce.addField(newPFE); 866 MappingFieldElement mfe = ((isRelationship) ? 868 new MappingRelationshipElementImpl(fieldName, mce) : 869 new MappingFieldElementImpl(fieldName, mce)); 870 mce.addField(mfe); 871 } catch (ModelException e) { 872 ErrorManager.getDefault().notify(ErrorManager.INFORMATIONAL, e); 875 } 876 } 877 } 878 } catch(NullPointerException ex) { 879 ErrorManager.getDefault().notify(ErrorManager.EXCEPTION, ex); 882 } 883 } 884 885 private void removeMappingForCmpField(String beanName, String fieldName, 886 boolean removeInverse) { 887 try { 888 if (mappingContext != null) { 889 EJBInfoHelper infoHelper = getEJBInfoHelper(); 890 ConversionHelper myConversionHelper = getConversionHelper(); 891 Model model = mappingContext.getModel(); 892 String pcClassName = conversionHelper.getMappedClassName(beanName); 893 MappingClassElement mce = model.getMappingClass(pcClassName); 894 895 if (mce != null) { 896 MappingFieldElement mfe = mce.getField(fieldName); 897 PersistenceFieldElement pfe = 898 model.getPersistenceClass(pcClassName).getField(fieldName); 899 RelationshipElement inverse = ( 900 (removeInverse && (pfe instanceof RelationshipElement)) ? 901 ((RelationshipElement)pfe).getInverseRelationship(model) : 902 null); 903 904 try { 905 model.removeFieldElement(pfe); 906 if (mfe != null) 907 mce.removeField(mfe); 908 909 if (inverse != null) { 910 String inverseName = inverse.getName(); 911 MappingClassElement inverseMCE = model.getMappingClass( 912 inverse.getDeclaringClass().getName()); 913 MappingFieldElement inverseMFE = 914 inverseMCE.getField(inverseName); 915 916 model.removeFieldElement(inverse); 917 if (inverseMFE != null) 918 inverseMCE.removeField(inverseMFE); 919 } 920 } catch (ModelException e) { 921 ErrorManager.getDefault().notify(ErrorManager.INFORMATIONAL, e); 924 } 925 } 926 } 927 } catch(NullPointerException ex) { 928 ErrorManager.getDefault().notify(ErrorManager.EXCEPTION, ex); 931 } 932 } 933 934 private void removeMappingForCmpField(String beanName, String fieldName) { 935 removeMappingForCmpField(beanName, fieldName, false); 936 } 937 938 private void renameMappingForCmpField(String beanName, String oldFieldName, 939 String newFieldName) { 940 try { 941 if ((beanName != null) && (mappingContext != null)) { 942 EJBInfoHelper infoHelper = getEJBInfoHelper(); 943 ConversionHelper myConversionHelper = getConversionHelper(); 944 Model model = mappingContext.getModel(); 945 String pcClassName = conversionHelper.getMappedClassName(beanName); 946 MappingClassElement mce = model.getMappingClass(pcClassName); 947 948 if (mce != null) { 949 MappingFieldElement mfe = mce.getField(oldFieldName); 950 PersistenceFieldElement pfe = model.getPersistenceClass( 951 pcClassName).getField(oldFieldName); 952 953 try { 954 if (mfe != null) { 955 mfe.setName(newFieldName); 956 } 957 958 if (pfe != null) { 959 pfe.setName(newFieldName); 960 if (pfe instanceof RelationshipElement) { 961 RelationshipElement relationship = (RelationshipElement)pfe; 962 RelationshipElement inverse = relationship.getInverseRelationship(model); 963 if (inverse != null) { 964 inverse.setInverseRelationship(relationship, model); 965 } 966 } 967 } 968 } catch (ModelException e) { 969 ErrorManager.getDefault().notify(ErrorManager.INFORMATIONAL, e); 972 } 973 } 974 } 975 } catch(NullPointerException ex) { 976 ErrorManager.getDefault().notify(ErrorManager.EXCEPTION, ex); 979 } 980 } 981 982 boolean loadCmpMappingsFromPlanFile(SunONEDeploymentConfiguration config) { 983 SunCmpMappings beanGraph = (SunCmpMappings) config.getBeans( 984 getUriText(), CMP_MAPPING_FILE, new SunCmpMappingsParser(), 985 new SunCmpMappingsRootFinder()); 986 if (beanGraph != null) { 990 994 getMappingContext(beanGraph, getEJBInfoHelper(beanGraph)); 997 } 998 999 return (beanGraph != null); 1000 } 1001 1002 protected void clearProperties() { 1003 StorageBeanFactory beanFactory = getConfig().getStorageFactory(); 1004 1005 name = null; 1006 pmDescriptors = beanFactory.createPmDescriptors(); 1007 cmpResource = beanFactory.createCmpResource(); 1008 messageDestinations = null; 1009 } 1010 1011 protected void setDefaultProperties() { 1012 } 1013 1014 1015 1018 private HashMap ejbRootFactoryMap; 1019 1020 1025 protected java.util.Map getXPathToFactoryMap() { 1026 if(ejbRootFactoryMap == null) { 1027 ejbRootFactoryMap = new HashMap (17); 1028 1029 ejbRootFactoryMap.put(SECURITY_ROLE_R_XPATH, new DCBGenericFactory(SecurityRoleMapping.class)); 1030 ejbRootFactoryMap.put(ENTITY_R_XPATH, new EntityEjbDCBFactory(this)); 1031 ejbRootFactoryMap.put(SESSION_R_XPATH, new SessionEjbDCBFactory()); 1032 ejbRootFactoryMap.put(MD_R_XPATH, new DCBGenericFactory(MDEjb.class)); 1033 1034 1039 } 1047 1048 return ejbRootFactoryMap; 1049 } 1050 1051 1052 public void addCmpResourceIfNotPresent() { 1053 if(null == cmpResource){ 1054 cmpResource = getConfig().getStorageFactory().createCmpResource(); 1055 } 1056 1057 if(!Utils.notEmpty(cmpResource.getJndiName())) { 1058 cmpResource.setJndiName(Utils.getDefaultCmpResourceJndiName(this)); 1059 } 1060 } 1061 1062 1063 1067 public String getName() { 1068 return this.name; 1069 } 1070 1071 1072 1078 public void setName(String name) throws java.beans.PropertyVetoException { 1079 String oldName = this.name; 1080 getVCS().fireVetoableChange("name", oldName, name); 1081 this.name = name; 1082 getPCS().firePropertyChange("name", oldName, name); 1083 } 1084 1085 1086 1090 public PmDescriptors getPmDescriptors() { 1091 return this.pmDescriptors; 1092 } 1093 1094 1095 1101 public void setPmDescriptors(PmDescriptors pmDescriptors) throws java.beans.PropertyVetoException { 1102 PmDescriptors oldPmDescriptors = this.pmDescriptors; 1103 getVCS().fireVetoableChange("pmDescriptors", oldPmDescriptors, pmDescriptors); 1104 this.pmDescriptors = pmDescriptors; 1105 getPCS().firePropertyChange("pmDescriptors", oldPmDescriptors, pmDescriptors); 1106 } 1107 1108 1109 1113 public CmpResource getCmpResource() { 1114 return this.cmpResource; 1115 } 1116 1117 1118 1124 public void setCmpResource(CmpResource cmpResource) throws java.beans.PropertyVetoException { 1125 CmpResource oldCmpResource = this.cmpResource; 1126 getVCS().fireVetoableChange("cmpResource", oldCmpResource, cmpResource); 1127 this.cmpResource = cmpResource; 1128 getPCS().firePropertyChange("cmpResource", oldCmpResource, cmpResource); 1129 } 1130 1131 1135 public List getMessageDestinations() { 1136 return messageDestinations; 1137 } 1138 1139 public MessageDestination getMessageDestination(int index) { 1140 return (MessageDestination) messageDestinations.get(index); 1141 } 1142 1143 1149 public void setMessageDestinations(List newMessageDestinations) throws java.beans.PropertyVetoException { 1150 List oldMessageDestinations = messageDestinations; 1151 getVCS().fireVetoableChange("messageDestinations", oldMessageDestinations, newMessageDestinations); messageDestinations = newMessageDestinations; 1153 getPCS().firePropertyChange("messageDestinations", oldMessageDestinations, messageDestinations); } 1155 1156 public void addMessageDestination(MessageDestination newMessageDestination) throws java.beans.PropertyVetoException { 1157 getVCS().fireVetoableChange("messageDestination", null, newMessageDestination); if(messageDestinations == null) { 1159 messageDestinations = new ArrayList (); 1160 } 1161 messageDestinations.add(newMessageDestination); 1162 getPCS().firePropertyChange("messageDestination", null, newMessageDestination ); } 1164 1165 public void removeMessageDestination(MessageDestination oldMessageDestination) throws java.beans.PropertyVetoException { 1166 getVCS().fireVetoableChange("messageDestination", oldMessageDestination, null); messageDestinations.remove(oldMessageDestination); 1168 getPCS().firePropertyChange("messageDestination", oldMessageDestination, null ); } 1170 1171 1173 public void fireXpathEvent(XpathEvent xpathEvent) { 1174 DDBean bean = xpathEvent.getBean(); 1176 String xpath = bean.getXpath(); 1177 1178 if (CMP_FIELD_XPATH.equals(xpath)) { 1180 String beanName = extractBeanName(xpath, bean); 1181 String fieldName = extractFieldName(xpath, bean); 1182 1183 if ((beanName != null) && (fieldName != null)) { 1184 if (xpathEvent.isAddEvent()) { 1185 addMappingForCmpField(beanName, fieldName); 1186 } else if (xpathEvent.isRemoveEvent()) { 1187 removeMappingForCmpField(beanName, fieldName); 1188 } 1189 } 1190 } else if (CMP_FIELD_NAME_XPATH.equals(xpath)) { 1191 if (xpathEvent.isChangeEvent()) { 1193 String beanName = extractBeanName(xpath, bean); 1194 1195 if (beanName != null) { 1196 PropertyChangeEvent changeEvent = 1197 xpathEvent.getChangeEvent(); 1198 renameMappingForCmpField(beanName, 1199 changeEvent.getOldValue().toString(), 1200 changeEvent.getNewValue().toString()); 1201 } 1202 } 1203 } else if (EJB_RELATION_XPATH.equals(xpath)) { 1204 if (xpathEvent.isRemoveEvent()) { 1207 String beanName = extractBeanName(xpath, bean); 1208 String fieldName = extractFieldName(xpath, bean); 1209 1210 if ((beanName != null) && (fieldName != null)) { 1216 removeMappingForCmpField(beanName, fieldName, true); 1217 } 1218 } 1219 } else if (CMR_FIELD_XPATH.equals(xpath)) { 1220 if (xpathEvent.isRemoveEvent()) { 1223 String beanName = extractBeanName(xpath, bean); 1224 String fieldName = extractFieldName(xpath, bean); 1225 1226 if ((beanName != null) && (fieldName != null)) { 1227 removeMappingForCmpField(beanName, fieldName); 1228 } 1229 } 1230 } else if (CMR_FIELD_NAME_XPATH.equals(xpath)) { 1231 String beanName = extractBeanName(xpath, bean); 1232 String fieldName = extractFieldName(xpath, bean); 1233 1234 if ((beanName != null) && (fieldName != null)) { 1239 if (xpathEvent.isAddEvent()) { 1240 addMappingForCmrField(beanName, fieldName); 1241 } else if (xpathEvent.isRemoveEvent()) { 1242 removeMappingForCmpField(beanName, fieldName); 1243 } else if(xpathEvent.isChangeEvent()) { 1244 if (beanName != null) { 1245 PropertyChangeEvent changeEvent = 1246 xpathEvent.getChangeEvent(); 1247 renameMappingForCmpField(beanName, 1248 changeEvent.getOldValue().toString(), 1249 changeEvent.getNewValue().toString()); 1250 } 1251 } 1252 } 1253 } 1254 } 1255 1256 private String extractFieldName(String xpath, DDBean bean) { 1257 DDBean field = null; 1258 1259 if (CMP_FIELD_XPATH.equals(xpath)) { 1260 field = bean.getChildBean("field-name")[0]; } else if (EJB_RELATION_XPATH.equals(xpath)) { 1262 DDBean [] fields = bean.getChildBean( 1263 "ejb-relationship-role/cmr-field/cmr-field-name"); field = ((fields.length > 0) ? fields[0] : null); 1265 } else if (CMR_FIELD_XPATH.equals(xpath)) { 1266 DDBean [] fields = bean.getChildBean("cmr-field-name"); field = (((fields != null) && (fields.length > 0)) ? 1268 fields[0] : null); 1269 } else if (CMR_FIELD_NAME_XPATH.equals(xpath)) { 1270 field = bean; 1271 } 1272 1273 return ((field != null) ? field.getText() : null); 1274 } 1275 1276 private String extractBeanName(String xpath, DDBean bean) { 1277 DDBean entity = null; 1278 1279 if (CMP_FIELD_XPATH.equals(xpath)) { 1280 entity = bean.getChildBean("../ejb-name")[0]; 1281 } else if (CMP_FIELD_NAME_XPATH.equals(xpath)) { 1282 entity = bean.getChildBean("../../ejb-name")[0]; } else if (EJB_RELATION_XPATH.equals(xpath)) { 1284 DDBean [] entities = bean.getChildBean( 1285 "ejb-relationship-role/relationship-role-source/ejb-name"); entity = ((entities.length > 0) ? entities[0] : null); 1287 } else if (CMR_FIELD_XPATH.equals(xpath)) { 1288 entity = bean.getChildBean("../relationship-role-source/ejb-name")[0]; } else if (CMR_FIELD_NAME_XPATH.equals(xpath)) { 1290 entity = bean.getChildBean( 1291 "../../relationship-role-source/ejb-name")[0]; } 1293 1294 return ((entity != null) ? entity.getText() : null); 1295 } 1296} 1297 | Popular Tags |