1 23 24 package com.sun.enterprise.deployment; 25 26 import java.util.*; 27 import java.lang.reflect.*; 28 import javax.ejb.EJBException ; 29 import com.sun.enterprise.util.LocalStringManagerImpl; 30 import com.sun.enterprise.util.TypeUtil; 31 import java.util.logging.Level ; 32 import com.sun.enterprise.deployment.util.DOLUtils; 33 34 41 42 public final class PersistenceDescriptor extends Descriptor { 43 44 private Set cmpFields = new HashSet(); 45 46 private Set pkeyFields = new HashSet(); 49 50 private boolean pkeyIsOneField = false; 52 53 private boolean pkeyFieldSpecified = true; 55 56 private String primaryKeyClassName; 57 private boolean pkeyStuffInitialized = false; 58 59 private boolean pkeyFieldsAllPrimitive = false; 61 62 private static LocalStringManagerImpl localStrings = 63 new LocalStringManagerImpl(PersistenceDescriptor.class); 64 65 private EjbCMPEntityDescriptor parentDesc; 67 private Class persistentClass; private Class stateClass; private Class primaryKeyClass; 70 71 private PersistentFieldInfo[] persFieldInfo; 72 private PersistentFieldInfo[] persNoPkeyFieldInfo; 73 private PersistentFieldInfo[] pkeyFieldInfo; 74 private boolean fieldInfoInitialized=false; 75 private PersistentFieldInfo[] fkeyFields; 76 77 private CMRFieldInfo[] cmrFieldInfo; 78 79 private Field[] pkeyClassPkeyFields; 81 private Hashtable queries = new Hashtable(); 82 private HashSet allQueriedMethods; 83 84 public PersistenceDescriptor() { 85 } 86 87 90 public PersistenceDescriptor(PersistenceDescriptor pers) { 91 super(pers); 92 93 this.getCMPFields().addAll(pers.getCMPFields()); 94 } 96 97 public String getCMRFieldReturnType(String field) { 98 String returnType = "java.util.Collection"; 99 try { 100 if( !field.trim().equals("") ) { 101 Class persClass = getPersistentClass(); 102 String methodName = "get" + field.substring(0,1).toUpperCase() 103 + field.substring(1); 104 Method method = TypeUtil.getMethod 105 (persClass, persClass.getClassLoader(), methodName, 106 new String [] {}); 107 returnType = method.getReturnType().getName(); 108 } 109 } catch(Throwable t) { 110 if(DOLUtils.getDefaultLogger().isLoggable(Level.FINE)) { 111 DOLUtils.getDefaultLogger().log(Level.FINE, t.toString(), t); 112 } 113 } 114 115 return returnType; 116 } 117 118 122 public boolean classesChanged() { 123 124 persistentClass = null; 127 stateClass = null; 128 Class persClass = getPersistentClass(); 129 130 Vector fieldDescriptors = parentDesc.getFieldDescriptors(); 131 132 if( this.cmpFields != null ) { 134 for(Iterator iter = cmpFields.iterator(); iter.hasNext();) { 135 FieldDescriptor next = (FieldDescriptor) iter.next(); 136 if( !fieldDescriptors.contains(next) ) { 137 iter.remove(); 138 } 139 } 140 } 141 142 if( this.pkeyFields != null ) { 144 for(Iterator iter = pkeyFields.iterator(); iter.hasNext();) { 145 FieldDescriptor next = (FieldDescriptor) iter.next(); 146 if( !fieldDescriptors.contains(next) ) { 147 iter.remove(); 148 } 149 } 150 } 151 152 FieldDescriptor primKeyFieldDesc = parentDesc.getPrimaryKeyFieldDesc(); 153 if( (primKeyFieldDesc != null) && 154 !fieldDescriptors.contains(primKeyFieldDesc) ) { 155 parentDesc.setPrimaryKeyFieldDesc(null); 156 } 157 158 159 162 Hashtable queriesClone = (Hashtable) queries.clone(); 164 165 queries = new Hashtable(); 166 initializeAllQueriedMethods(); 167 168 Iterator it = queriesClone.keySet().iterator(); 169 while ( it.hasNext() ) { 170 Method oldMethod = (Method)it.next(); 171 Method newMethod = findEquivalentMethod(allQueriedMethods, 172 oldMethod); 173 if( newMethod != null ) { 174 QueryDescriptor oldQuery = (QueryDescriptor) 175 queriesClone.get(oldMethod); 176 QueryDescriptor newQuery = 177 new QueryDescriptor(oldQuery, newMethod); 178 queries.put(newMethod, newQuery); 181 } 182 } 183 184 invalidate(); 187 188 return false; 189 } 190 191 198 private Method findEquivalentMethod(Collection methods, 199 Method methodToMatch) { 200 Method matchedMethod = null; 201 for(Iterator iter = methods.iterator(); iter.hasNext();) { 202 Object o = iter.next(); 203 Method next; 204 if (o instanceof Method) { 205 next = (Method) o; 206 } else { 207 next = ((MethodDescriptor) o).getMethod(parentDesc); 208 if (next==null) { 209 return null; 210 } 211 } 212 if( methodsEqual(next, methodToMatch, false) ) { 214 matchedMethod = next; 215 break; 216 } 217 } 218 return matchedMethod; 219 } 220 221 227 private boolean methodsEqual(Method m1, Method m2, 228 boolean compareDeclaringClass) { 229 boolean equal = false; 230 231 do { 232 233 String m1Name = m1.getName(); 234 String m2Name = m2.getName(); 235 236 if( !m1Name.equals(m2Name) ) { break; } 237 238 String m1DeclaringClass = m1.getDeclaringClass().getName(); 239 String m2DeclaringClass = m2.getDeclaringClass().getName(); 240 241 if( compareDeclaringClass ) { 242 if( !m1DeclaringClass.equals(m2DeclaringClass) ) { break; } 243 } 244 245 Class [] m1ParamTypes = m1.getParameterTypes(); 246 Class [] m2ParamTypes = m2.getParameterTypes(); 247 248 if( m1ParamTypes.length != m2ParamTypes.length ) { break; } 249 250 equal = true; 251 for(int pIndex = 0; pIndex < m1ParamTypes.length; pIndex++) { 252 String m1ParamClass = m1ParamTypes[pIndex].getName(); 253 String m2ParamClass = m2ParamTypes[pIndex].getName(); 254 if( !m1ParamClass.equals(m2ParamClass) ) { 255 equal = false; 256 break; 257 } 258 } 259 260 } while(false); 261 262 return equal; 263 } 264 265 271 private boolean methodsEqual(MethodDescriptor m1, Method m2, 272 boolean compareDeclaringClass) { 273 274 Method m = m1.getMethod(parentDesc); 275 return methodsEqual(m, m2, compareDeclaringClass); 276 277 } 278 279 public void setParentDescriptor(Descriptor parentDesc) 280 { 281 this.parentDesc = (EjbCMPEntityDescriptor)parentDesc; 282 } 283 284 public Descriptor getParentDescriptor() 285 { 286 return parentDesc; 287 } 288 289 public EjbBundleDescriptor getEjbBundleDescriptor() 290 { 291 return parentDesc.getEjbBundleDescriptor(); 292 } 293 294 300 public Set getRelationships() 301 { 302 Set allRelationships = getEjbBundleDescriptor().getRelationships(); 303 Set myRelationships = new HashSet(); 304 for(Iterator iter = allRelationships.iterator(); iter.hasNext();) { 305 RelationshipDescriptor next = (RelationshipDescriptor) iter.next(); 306 if( next.hasParticipant(parentDesc) ) { 307 myRelationships.add(next); 308 } 309 } 310 return myRelationships; 311 } 312 313 316 public CMRFieldInfo[] getCMRFieldInfo() 317 { 318 if ( cmrFieldInfo == null ) { 319 try { 320 initCMRFieldStuff(); 321 } catch ( Exception ex ) { 322 DOLUtils.getDefaultLogger().log(Level.SEVERE, "enterprise.deployment.backend.invalidDescriptorMappingFailure", 323 new Object [] {ex.toString()}); 324 throw new EJBException (ex); 325 } 326 } 327 return cmrFieldInfo; 328 } 329 330 333 public CMRFieldInfo getCMRFieldInfoByName(String fieldName) 334 { 335 CMRFieldInfo[] cmrf = this.getCMRFieldInfo(); 336 for ( int i=0; i<cmrf.length; i++ ) { 337 if ( cmrf[i].name.equals(fieldName) ) 338 return cmrf[i]; 339 } 340 throw new EJBException ("CMRFieldInfo not found for field "+fieldName); 341 } 342 343 348 public void invalidate() { 349 cmrFieldInfo = null; 350 persFieldInfo = null; 351 fieldInfoInitialized = false; 352 pkeyStuffInitialized = false; 353 } 354 355 private void initCMRFieldStuff() 356 throws Exception 357 { 358 if ( !fieldInfoInitialized ) 359 initializeFieldInfo(); 360 361 Set relationships = getRelationships(); 362 Iterator it = relationships.iterator(); 363 CMRFieldInfo[] cmrFieldInfo2 = 366 new CMRFieldInfo[relationships.size() * 2]; 367 int count = 0; 368 while ( it.hasNext() ) { 369 RelationshipDescriptor rd = (RelationshipDescriptor)it.next(); 370 RelationRoleDescriptor source = rd.getSource(); 371 RelationRoleDescriptor sink = rd.getSink(); 372 RelationRoleDescriptor myroles[]; 373 if ( source.getPersistenceDescriptor() == 376 sink.getPersistenceDescriptor() ) { 377 myroles = new RelationRoleDescriptor[2]; 378 myroles[0] = source; 379 myroles[1] = sink; 380 } else { 381 myroles = new RelationRoleDescriptor[1]; 382 if ( source.getPersistenceDescriptor() == this ) { 383 myroles[0] = source; 384 } else { 385 myroles[0] = sink; 386 } 387 } 388 389 for (int ii = 0; ii < myroles.length; ii++) { 392 CMRFieldInfo finfo = new CMRFieldInfo(); 393 cmrFieldInfo2[count++] = finfo; 394 395 PersistenceDescriptor partnerPers = 396 myroles[ii].getPartner().getPersistenceDescriptor(); 397 398 EjbCMPEntityDescriptor partner = 400 myroles[ii].getPartner().getOwner(); 401 if ( !partner.isLocalInterfacesSupported() && 402 myroles[ii].getCMRField() != null ) { 403 throw new RuntimeException ( 404 "No local interface for target bean of CMR field"); 405 } 406 407 String type; 408 if ( myroles[ii].getPartner().getIsMany() == false ) { 409 if ( partner.isLocalInterfacesSupported() ) 411 type = partner.getLocalClassName(); 412 else 413 type = partner.getPrimaryKeyClassName(); 416 } 417 else { 418 type = myroles[ii].getCMRFieldType(); 420 if ( type == null ) { 421 type = "java.util.Collection"; 424 } 425 } 426 finfo.type = getClass(type); 427 428 finfo.name = myroles[ii].getCMRField(); 429 if ( finfo.name == null ) { 430 finfo.name = myroles[ii].composeReverseCmrFieldName(); 433 } 434 finfo.role = myroles[ii]; 435 myroles[ii].setCMRFieldInfo(finfo); 436 437 if ( rd.isOneOne() && fkeyFields != null ) { 438 PersistentFieldInfo[] cmrFkeyFields; 440 PersistentFieldInfo[] partnerPkeyFields = 441 partnerPers.getPkeyFieldInfo(); 442 cmrFkeyFields = 443 new PersistentFieldInfo[partnerPkeyFields.length]; 444 for ( int i=0; i<partnerPkeyFields.length; i++ ) { 445 String fkeyName = "_" + finfo.name + "_" 446 + partnerPkeyFields[i].name; 447 for ( int j=0; j<fkeyFields.length; j++ ) { 448 if ( fkeyFields[j].name.equals(fkeyName) ) 449 cmrFkeyFields[i] = fkeyFields[j]; 450 } 451 } 452 finfo.fkeyFields = cmrFkeyFields; 453 } 454 } 455 } 456 457 cmrFieldInfo = new CMRFieldInfo[count]; 460 System.arraycopy(cmrFieldInfo2, 0, cmrFieldInfo, 0, count); 461 462 for ( int i=cmrFieldInfo.length-1; i>0; i-- ) { 464 for ( int j=0; j<i; j++ ) { 465 if ( cmrFieldInfo[j].name.compareTo(cmrFieldInfo[j+1].name) 466 > 0 ) { 467 CMRFieldInfo tmp = cmrFieldInfo[j]; 468 cmrFieldInfo[j] = cmrFieldInfo[j+1]; 469 cmrFieldInfo[j+1] = tmp; 470 } 471 } 472 } 473 } 474 475 public void clearCMPFields() { 476 this.cmpFields.clear(); 477 setCMPFields(this.cmpFields); 478 } 479 480 public void addCMPField(String field) { 481 addCMPField(new FieldDescriptor(field)); 482 } 483 484 public void addCMPField(FieldDescriptor fieldDesc) { 485 this.cmpFields.add(fieldDesc); 486 setCMPFields(this.cmpFields); 487 } 488 489 public void removeCMPField(String field) { 490 removeCMPField(new FieldDescriptor(field)); 491 } 492 493 public void removeCMPField(FieldDescriptor fieldDesc) { 494 this.cmpFields.remove(fieldDesc); 495 setCMPFields(this.cmpFields); 496 } 497 498 502 public void setCMPFields(Set cmpFields) { 503 this.cmpFields = cmpFields; 504 persFieldInfo = null; 505 fieldInfoInitialized = false; 506 super.changed(); 507 } 508 509 512 public boolean isCMPField(String field) { 513 return this.getCMPFields().contains(new FieldDescriptor(field)); 514 } 515 516 517 522 public Set getCMPFields() { 523 return this.cmpFields; 524 } 525 526 550 551 555 public void setPkeyFields(Set pkeyFields) { 556 this.pkeyFields = pkeyFields; 557 fieldInfoInitialized = false; 558 persFieldInfo = null; 559 pkeyStuffInitialized = false; 560 super.changed(); 561 } 562 563 568 public Set getPkeyFields() { 569 if ( !pkeyStuffInitialized ) 570 initPkeyInfo(); 571 return pkeyFields; 572 } 573 574 577 public boolean isPkeyField(String field) { 578 return isPkeyField(new FieldDescriptor(field)); 579 } 580 581 public boolean isPkeyField(FieldDescriptor fieldDesc) { 582 return this.getPkeyFields().contains(fieldDesc); 583 } 584 585 591 private void initPkeyInfo() 592 { 593 try { 594 pkeyIsOneField = false; 595 pkeyFieldSpecified = true; 596 primaryKeyClassName = parentDesc.getPrimaryKeyClassName(); 597 598 FieldDescriptor fd = parentDesc.getPrimaryKeyFieldDesc(); 599 if ( pkeyFields == null || pkeyFields.size() == 0 ) { 600 pkeyFields = new HashSet(); 601 602 if ( fd != null ) pkeyFields.add(fd); 604 605 else if (!primaryKeyClassName.equals("java.lang.Object")) { 606 primaryKeyClass = getClass(primaryKeyClassName); 608 Field[] fields = primaryKeyClass.getFields(); 609 pkeyFieldsAllPrimitive = true; 610 for ( int i=0; i<fields.length; i++ ) { 611 int m = fields[i].getModifiers(); 613 if ( Modifier.isStatic(m) || Modifier.isFinal(m) ) 614 continue; 615 if ( !fields[i].getType().isPrimitive() ) 616 pkeyFieldsAllPrimitive = false; 617 pkeyFields.add(new FieldDescriptor( 618 fields[i].getName())); 619 } 620 } 621 else { 622 primaryKeyClass = getClass(primaryKeyClassName); 624 pkeyIsOneField = true; 625 pkeyFieldSpecified = false; 626 } 627 } 628 if ( fd != null ) 629 pkeyIsOneField = true; 630 631 pkeyStuffInitialized = true; 632 } 633 catch ( Exception ex ) { 634 DOLUtils.getDefaultLogger().log(Level.SEVERE, "enterprise.deployment.backend.invalidDescriptorMappingFailure", 635 new Object [] {ex.toString()}); 636 throw new EJBException (ex); 637 } 638 } 639 640 646 public boolean primaryKeyIsOneField() 647 { 648 if ( !pkeyStuffInitialized ) 649 initPkeyInfo(); 650 return pkeyIsOneField; 651 } 652 653 654 657 public boolean primaryKeyIsSpecified() 658 { 659 if( !pkeyStuffInitialized ) 660 initPkeyInfo(); 661 return pkeyFieldSpecified; 662 } 663 664 665 669 public boolean primaryKeyFieldsAllPrimitive() 670 { 671 if( !pkeyStuffInitialized ) 672 initPkeyInfo(); 673 return pkeyFieldsAllPrimitive; 674 } 675 676 677 678 682 public Class getPrimaryKeyClass() 683 { 684 if ( !pkeyStuffInitialized ) 685 initPkeyInfo(); 686 return primaryKeyClass; 687 } 688 689 690 public Class getPersistentClass() 692 { 693 if ( persistentClass == null ) { 694 persistentClass = getClass(parentDesc.getEjbClassName()); 695 } 696 return persistentClass; 697 } 698 699 public Class getStateClass() 700 { 701 if ( stateClass == null ) { 702 stateClass = getPersistentClass(); 703 if ( parentDesc.isEJB20() ) { 704 if( !Modifier.isAbstract(stateClass.getModifiers()) ) { 705 throw new EJBException ("2.x CMP bean class " 706 + stateClass.getName() + " must be decleared abstract " 707 + "or cmp-version for the corresponding bean must be set to 1.x."); 708 } 709 String stateClassName = parentDesc.getStateImplClassName(); 710 stateClass = getClass(stateClassName); 711 } 712 } 713 return stateClass; 714 } 715 716 717 718 private Class getClass(String className) 719 { 720 try { 721 return getEjbBundleDescriptor().getClassLoader().loadClass(className); 722 } catch ( Exception ex ) { 723 throw new EJBException (ex); 725 } 726 } 727 728 729 733 public void setFkeyFields(PersistentFieldInfo[] fkeyFields) { 734 this.fkeyFields = fkeyFields; 735 fieldInfoInitialized = false; 736 persFieldInfo = null; 737 super.changed(); 738 } 739 740 741 745 public PersistentFieldInfo[] getFkeyFields() { 746 if ( !fieldInfoInitialized ) 747 initializeFieldInfo(); 748 return this.fkeyFields; 749 } 750 751 752 756 public PersistentFieldInfo[] getPersistentFieldInfo() 757 { 758 if ( !fieldInfoInitialized ) 759 initializeFieldInfo(); 760 return persFieldInfo; 761 } 762 763 764 767 public PersistentFieldInfo getPersistentFieldInfoByName(String fieldName) 768 { 769 if ( !fieldInfoInitialized ) 770 initializeFieldInfo(); 771 for ( int i=0; i<persFieldInfo.length; i++ ) { 772 if ( persFieldInfo[i].name.equals(fieldName) ) 773 return persFieldInfo[i]; 774 } 775 throw new EJBException ("PersistentFieldInfo not found for field "+fieldName); 776 } 777 778 779 783 public PersistentFieldInfo[] getNonPkeyPersFieldInfo() 784 { 785 if ( !fieldInfoInitialized ) 786 initializeFieldInfo(); 787 return persNoPkeyFieldInfo; 788 } 789 790 791 794 public PersistentFieldInfo[] getPkeyFieldInfo() 795 { 796 if ( !fieldInfoInitialized ) 797 initializeFieldInfo(); 798 return pkeyFieldInfo; 799 } 800 801 804 public PersistentFieldInfo getPkeyFieldInfoByName(String fieldName) 805 { 806 if ( !fieldInfoInitialized ) 807 initializeFieldInfo(); 808 for ( int i=0; i<pkeyFieldInfo.length; i++ ) { 809 if ( pkeyFieldInfo[i].name.equals(fieldName) ) 810 return pkeyFieldInfo[i]; 811 } 812 throw new EJBException ("PersistentFieldInfo not found for pkey field "+fieldName); 813 } 814 815 816 817 821 public Field[] getPkeyClassFields() 822 { 823 if ( !fieldInfoInitialized ) 824 initializeFieldInfo(); 825 return pkeyClassPkeyFields; 826 } 827 828 829 private void initializeFieldInfo() 832 { 833 if ( !pkeyStuffInitialized ) 834 initPkeyInfo(); 835 836 int cmpFieldCount = cmpFields.size(); 837 if (cmpFieldCount==0) { 838 throw new EJBException ("No cmp field defined for CMP EJB " + parentDesc.getName()); 839 } 840 841 int fkeyCount = 0; 842 if ( fkeyFields != null ) 843 fkeyCount = fkeyFields.length; 844 845 persFieldInfo = new PersistentFieldInfo[cmpFieldCount + fkeyCount]; 846 int fcount = 0; 848 Iterator itr = cmpFields.iterator(); 849 while ( itr.hasNext() ) { 850 persFieldInfo[fcount] = new PersistentFieldInfo(); 851 persFieldInfo[fcount].name =((FieldDescriptor)itr.next()).getName(); 852 fcount++; 853 } 854 if ( fkeyFields != null ) { 856 for ( int i=0; i<fkeyFields.length; i++ ) { 857 persFieldInfo[fcount] = fkeyFields[i]; 858 fcount++; 859 } 860 } 861 862 for ( int i=persFieldInfo.length-1; i>0; i-- ) { 864 for ( int j=0; j<i; j++ ) { 865 if ( persFieldInfo[j].name.compareTo(persFieldInfo[j+1].name) 866 > 0 ) { 867 PersistentFieldInfo tmp = persFieldInfo[j]; 868 persFieldInfo[j] = persFieldInfo[j+1]; 869 persFieldInfo[j+1] = tmp; 870 } 871 } 872 } 873 874 pkeyFieldInfo = new PersistentFieldInfo[pkeyFields.size()]; 877 if ( pkeyFieldSpecified ) { 878 879 StringBuffer nonPersFieldsInPK = new StringBuffer (); 881 for ( Iterator it=pkeyFields.iterator(); it.hasNext(); ) { 882 FieldDescriptor fd = (FieldDescriptor)it.next(); 883 boolean isPersistent = false; 884 for ( int i=0; i<persFieldInfo.length; i++ ) { 885 if ( fd.getName().equals(persFieldInfo[i].name) ) { 886 isPersistent = true; 887 break; 888 } 889 } 890 if ( !isPersistent ) { 891 if ( nonPersFieldsInPK.length() != 0 ) { 893 nonPersFieldsInPK.append(", "); 894 } 895 nonPersFieldsInPK.append(fd.getName()); 896 } 897 } 898 if ( nonPersFieldsInPK.length() != 0 ) { 899 throw new EJBException (localStrings.getLocalString( 900 "enterprise.deployment.pkhasnopersistentfields", 901 "CMP bean [{0}], primary key class [{1}] has " + 902 "public non-persistent field(s) [{2}].", 903 new Object [] {getParentDescriptor().getName(), 904 getPrimaryKeyClass().getName(), 905 nonPersFieldsInPK.toString()})); 906 } 907 908 persNoPkeyFieldInfo = new PersistentFieldInfo[persFieldInfo.length - 909 pkeyFieldInfo.length]; 910 int pkeyCount = 0; 911 int noPkeyCount = 0; 912 for ( int i=0; i<persFieldInfo.length; i++ ) { 913 boolean isPkey = false; 914 for ( Iterator it=pkeyFields.iterator(); it.hasNext(); ) { 915 FieldDescriptor fd = (FieldDescriptor)it.next(); 916 if ( fd.getName().equals(persFieldInfo[i].name) ) { 917 isPkey = true; 918 break; 919 } 920 } 921 if ( isPkey ) 922 pkeyFieldInfo[pkeyCount++] = persFieldInfo[i]; 923 else 924 persNoPkeyFieldInfo[noPkeyCount++] = persFieldInfo[i]; 925 } 926 } 927 928 if ( pkeyIsOneField && pkeyFieldSpecified) { 929 pkeyFieldInfo[0].type = getPrimaryKeyClass(); 936 } 937 938 for ( int i=0; i<persFieldInfo.length; i++ ) { 940 if ( persFieldInfo[i].type == null ) 943 persFieldInfo[i].type = getCMPFieldType(persFieldInfo[i].name); 944 } 945 946 try { 951 if ( persistentClass != null 952 && !Modifier.isAbstract(persistentClass.getModifiers()) ) { 953 for ( int i=0; i<persFieldInfo.length; i++ ) { 954 persFieldInfo[i].field = getField(getStateClass(), 955 persFieldInfo[i].name); 956 } 957 } 958 959 if ( !pkeyIsOneField && primaryKeyClass != null 961 && !Modifier.isAbstract(primaryKeyClass.getModifiers()) ) { 962 pkeyClassPkeyFields = new Field[pkeyFieldInfo.length]; 963 for ( int i=0; i<pkeyFieldInfo.length; i++ ) { 964 pkeyClassPkeyFields[i] = primaryKeyClass.getField( 965 pkeyFieldInfo[i].name); 966 } 967 } 968 } catch ( NoSuchFieldException ex ) { 969 if(DOLUtils.getDefaultLogger().isLoggable(Level.FINE)) { 970 DOLUtils.getDefaultLogger().log(Level.FINE, ex.toString(), ex); 971 } 972 throw new EJBException (ex); 973 } 974 975 fieldInfoInitialized = true; 976 } 977 978 private Field getField(final Class c, final String name) 979 throws NoSuchFieldException 980 { 981 Field field = (Field)java.security.AccessController.doPrivileged( 985 new java.security.PrivilegedAction () { 986 public Object run() { 987 try { 988 return c.getDeclaredField(name); 991 } 992 catch ( NoSuchFieldException ex ) { 993 return null; 994 } 995 } 996 } 997 ); 998 999 if ( field == null ) 1000 field = c.getField(name); 1001 return field; 1002 } 1003 1004 1008 public Class getTypeFor(String field) 1009 { 1010 return getCMPFieldType(field); 1011 } 1012 1013 private Class getCMPFieldType(String field) 1014 { 1015 Class pclass = getPersistentClass(); 1016 if ( Modifier.isAbstract(pclass.getModifiers()) ) { 1017 String javaBeanName = capitalize(field); 1019 String getter = "get"+javaBeanName; 1020 try { 1021 Method method = pclass.getMethod(getter, (Class []) null); 1022 return method.getReturnType(); 1023 } catch ( Exception ex ) { 1024 throw new RuntimeException ("Cannot find accessor " + getter + " for CMP field "+field); 1025 } 1026 } 1027 else { 1028 try { 1030 Field f = getField(getStateClass(), field); 1031 return f.getType(); 1032 } catch ( NoSuchFieldException ex ) { 1033 throw new RuntimeException ("Cant find CMP field "+field+" in class "+getStateClass().getName()); 1034 } 1035 } 1036 } 1037 1038 1039 public static String capitalize(String name) 1041 { 1042 if ( Character.isUpperCase(name.charAt(0)) ) { 1045 throw new javax.ejb.EJBException ("CMP/CMR field "+name+" must start with a lower case character."); 1046 } 1047 else { 1048 char chars[] = name.toCharArray(); 1049 chars[0] = Character.toUpperCase(chars[0]); 1050 return new String (chars); 1051 } 1052 } 1053 1054 public void setQueryFor(MethodDescriptor method, QueryDescriptor query) 1055 { 1056 queries.put(method, query); 1057 } 1058 1059 1060 public QueryDescriptor getQueryFor(MethodDescriptor method) 1061 { 1062 return (QueryDescriptor)queries.get(method); 1063 } 1064 1065 public void removeQueryFor(MethodDescriptor method) 1066 { 1067 queries.remove(method); 1068 } 1069 1070 public void setQueryFor(Method method, QueryDescriptor query) 1071 { 1072 MethodDescriptor md = new MethodDescriptor(method,""); 1081 setQueryFor(md, query); 1082 } 1083 1084 public QueryDescriptor getQueryFor(Method method) 1085 { 1086 MethodDescriptor md = new MethodDescriptor(method,""); 1089 return (QueryDescriptor) queries.get(md); 1090 } 1091 1092 1096 public Set getQueriedMethods() 1097 { 1098 return queries.keySet(); 1099 } 1100 1101 1106 public Set getAllPossibleQueriedMethods() 1107 { 1108 if ( allQueriedMethods == null ) 1109 initializeAllQueriedMethods(); 1110 1111 return allQueriedMethods; 1112 } 1113 1114 private void initializeAllQueriedMethods() 1115 { 1116 allQueriedMethods = new HashSet(); 1117 1118 Method[] beanMethods = getPersistentClass().getMethods(); 1120 for ( int i=0; i<beanMethods.length; i++ ) { 1121 if ( beanMethods[i].getName().startsWith("ejbSelect") ) { 1122 allQueriedMethods.add(new MethodDescriptor(beanMethods[i], MethodDescriptor.EJB_BEAN)); 1123 } 1124 } 1125 1126 if ( parentDesc.isRemoteInterfacesSupported() ) { 1128 Class homeIntf = getClass(parentDesc.getHomeClassName()); 1129 1130 Method[] homeMethods = homeIntf.getMethods(); 1131 for ( int i=0; i<homeMethods.length; i++ ) { 1132 String name = homeMethods[i].getName(); 1133 if ( name.startsWith("find") 1134 && !name.equals("findByPrimaryKey") ) { 1135 allQueriedMethods.add(new MethodDescriptor(homeMethods[i], MethodDescriptor.EJB_HOME)); 1136 } 1137 } 1138 } 1139 if ( parentDesc.isLocalInterfacesSupported() ) { 1140 Class homeIntf = getClass(parentDesc.getLocalHomeClassName()); 1141 1142 Method[] homeMethods = homeIntf.getMethods(); 1143 for ( int i=0; i<homeMethods.length; i++ ) { 1144 String name = homeMethods[i].getName(); 1145 if ( name.startsWith("find") 1146 && !name.equals("findByPrimaryKey") ) { 1147 allQueriedMethods.add(new MethodDescriptor(homeMethods[i], MethodDescriptor.EJB_LOCALHOME)); 1148 } 1149 } 1150 } 1151 } 1152 1153 1156 public void print(StringBuffer toStringBuffer) { 1157 super.print(toStringBuffer); 1158 toStringBuffer.append("\n Entity descriptor"); 1159 toStringBuffer.append("\n cmpFields ").append(cmpFields); 1160 } 1161} 1162 | Popular Tags |