1 23 24 29 30 package com.sun.jdo.spi.persistence.support.ejb.model; 31 32 import java.io.*; 33 import java.util.*; 34 import java.lang.reflect.*; 35 36 import com.sun.enterprise.deployment.*; 37 import com.sun.jdo.api.persistence.model.RuntimeModel; 38 import com.sun.jdo.spi.persistence.support.ejb.model.util.NameMapper; 39 import com.sun.jdo.api.persistence.model.jdo.RelationshipElement; 40 import com.sun.jdo.api.persistence.model.jdo.PersistenceFieldElement; 41 import com.sun.jdo.spi.persistence.utility.JavaTypeHelper; 42 43 59 public class DeploymentDescriptorModel extends RuntimeModel 60 { 61 private ClassLoader _classLoader; 62 private NameMapper _nameMapper; 63 64 67 public static final String SIGNATURE = "$RCSfile: DeploymentDescriptorModel.java,v $ $Revision: 1.2 $"; 69 75 public DeploymentDescriptorModel (NameMapper nameMapper, 76 ClassLoader classLoader) 77 { 78 super(); 79 _classLoader = classLoader; 80 _nameMapper = nameMapper; 81 } 82 83 private ClassLoader getClassLoader () { return _classLoader; } 84 private NameMapper getNameMapper () { return _nameMapper; } 85 86 99 protected BufferedInputStream getInputStreamForResource (String className, 100 ClassLoader classLoader, String resourceName) 101 { 102 return super.getInputStreamForResource( 103 className, getClassLoader(), resourceName); 104 } 105 106 116 protected String findPenultimateSuperclass (String className) 117 { 118 return (isPCClassName(className) ? className : 119 super.findPenultimateSuperclass(className)); 120 } 121 122 131 protected String getSuperclass (String className) 132 { 133 return (isPCClassName(className) ? "java.lang.Object" : super.getSuperclass(className)); 135 } 136 137 151 protected BufferedOutputStream createFile (String className, String baseFileName, 152 String extension) throws IOException 153 { 154 BufferedOutputStream outputStream = 155 super.createFile(className, baseFileName, extension); 156 157 if (outputStream != null) 158 return outputStream; 159 160 throw new UnsupportedOperationException (); 161 } 162 163 170 protected void deleteFile (String className, String fileName) 171 throws IOException 172 { 173 throw new UnsupportedOperationException (); 174 } 175 176 190 public Object getClass (final String className, 191 final ClassLoader classLoader) 192 { 193 String testClass = className; 194 195 if (className != null) 198 { 199 NameMapper nameMapper = getNameMapper(); 200 String ejbName = 201 (isPCClassName(className) ? getEjbName(className) : className); 202 203 if (nameMapper.isEjbName(ejbName)) 204 testClass = nameMapper.getAbstractBeanClassForEjbName(ejbName); 205 else 206 { 207 String keyClass = 208 nameMapper.getKeyClassForPersistenceKeyClass(className); 209 210 if (keyClass != null) 211 { 212 if (NameMapper.PRIMARY_KEY_FIELD == 216 getPersistenceKeyClassType(className)) 217 { 218 if (isPrimitive(keyClass)) 219 return JavaTypeHelper.getPrimitiveClass(keyClass); 220 if (isByteArray(keyClass) || keyClass.endsWith("[]")) 221 return byte[].class; 222 } 223 224 testClass = keyClass; 225 } 226 } 227 } 228 229 return super.getClass(testClass, getClassLoader()); 230 } 231 232 244 public Object getConstructor (final String className, String [] argTypeNames) 245 { 246 Object returnObject = null; 247 248 if ((NameMapper.PRIMARY_KEY_FIELD == 249 getPersistenceKeyClassType(className)) && 250 argTypeNames.equals(NO_ARGS)) 251 { 252 returnObject = new MemberWrapper(className, null, Modifier.PUBLIC, 253 (Class )getClass(className)); 254 } 255 256 if (returnObject == null) 257 { 258 returnObject = super.getConstructor(className, argTypeNames); 259 260 if (returnObject instanceof Constructor) returnObject = new MemberWrapper((Constructor)returnObject); 262 } 263 264 return returnObject; 265 } 266 267 286 public Object getMethod (final String className, final String methodName, 287 String [] argTypeNames) 288 { 289 int keyClassType = getPersistenceKeyClassType(className); 290 Object returnObject = null; 291 292 if (isPCClassName(className)) 293 { 294 if ((methodName.equals("readObject") && argTypeNames.equals(READ_OBJECT_ARGS)) || 296 (methodName.equals("writeObject") && argTypeNames.equals(WRITE_OBJECT_ARGS))) 298 { 299 returnObject = new MemberWrapper(methodName, 300 Void.TYPE, Modifier.PRIVATE, (Class )getClass(className)); 301 } 302 } 303 if ((NameMapper.UNKNOWN_KEY_CLASS == keyClassType) || 304 (NameMapper.PRIMARY_KEY_FIELD == keyClassType)) 305 { 306 if (methodName.equals("equals") && argTypeNames.equals(EQUALS_ARGS)) 308 { 309 returnObject = new MemberWrapper(methodName, 310 Boolean.TYPE, Modifier.PUBLIC, (Class )getClass(className)); 311 } 312 else if (methodName.equals("hashCode") && argTypeNames.equals(NO_ARGS)) 314 { 315 returnObject = new MemberWrapper(methodName, 316 Integer.TYPE, Modifier.PUBLIC, (Class )getClass(className)); 317 } 318 } 319 320 if (returnObject == null) 321 { 322 returnObject = super.getMethod(className, methodName, argTypeNames); 323 324 if (returnObject instanceof Method) returnObject = new MemberWrapper((Method)returnObject); 326 } 327 328 return returnObject; 329 } 330 331 348 public Object getInheritedMethod (String className, String methodName, 349 String [] argTypeNames) 350 { 351 return ((NameMapper.PRIMARY_KEY_FIELD == 355 getPersistenceKeyClassType(className)) ? 356 getMethod(className, methodName, argTypeNames) : 357 super.getInheritedMethod(className, methodName, argTypeNames)); 358 } 359 360 368 public List getFields (final String className) 369 { 370 final EjbCMPEntityDescriptor descriptor = getCMPDescriptor(className); 371 String testClass = className; 372 373 if (descriptor != null) { 375 Iterator iterator = descriptor.getFieldDescriptors().iterator(); 376 List returnList = new ArrayList(); 377 378 while (iterator.hasNext()) 379 returnList.add(((FieldDescriptor)iterator.next()).getName()); 380 381 return returnList; 382 } 383 else 384 { 385 NameMapper nameMapper = getNameMapper(); 386 String ejbName = 387 nameMapper.getEjbNameForPersistenceKeyClass(className); 388 389 switch (getPersistenceKeyClassType(className)) 390 { 391 case NameMapper.USER_DEFINED_KEY_CLASS: 394 testClass = nameMapper.getKeyClassForEjbName(ejbName); 395 break; 396 case NameMapper.PRIMARY_KEY_FIELD: 398 return Arrays.asList(new String []{ 399 getCMPDescriptor(ejbName). 400 getPrimaryKeyFieldDesc().getName()}); 401 case NameMapper.UNKNOWN_KEY_CLASS: 403 String pcClassName = 404 nameMapper.getPersistenceClassForEjbName(ejbName); 405 PersistenceFieldElement[] fields = 406 getPersistenceClass(pcClassName).getFields(); 407 int i, count = ((fields != null) ? fields.length : 0); 408 409 for (i = 0; i < count; i++) 410 { 411 PersistenceFieldElement pfe = fields[i]; 412 413 if (pfe.isKey()) 414 return Arrays.asList(new String []{pfe.getName()}); 415 } 416 break; 417 } 418 } 419 420 return super.getFields(testClass); 421 } 422 423 436 public List getAllFields (String className) 437 { 438 return ((NameMapper.PRIMARY_KEY_FIELD == 442 getPersistenceKeyClassType(className)) ? getFields(className) : 443 super.getAllFields(className)); 444 } 445 446 460 public Object getField (final String className, String fieldName) 461 { 462 String testClass = className; 463 Object returnObject = null; 464 465 if (className != null) 466 { 467 NameMapper nameMapper = getNameMapper(); 468 boolean isPCClass = isPCClassName(className); 469 boolean isPKClassName = false; 470 String searchClassName = className; 471 String searchFieldName = fieldName; 472 473 if (isPCClass) 476 { 477 searchFieldName = nameMapper. 478 getEjbFieldForPersistenceField(className, fieldName); 479 searchClassName = getEjbName(className); 480 } 481 else { 483 String ejbName = 484 nameMapper.getEjbNameForPersistenceKeyClass(className); 485 486 switch (getPersistenceKeyClassType(className)) 487 { 488 case NameMapper.PRIMARY_KEY_FIELD: 491 testClass = ejbName; 492 searchClassName = ejbName; 493 isPKClassName = true; 494 break; 495 case NameMapper.UNKNOWN_KEY_CLASS: 501 testClass = nameMapper. 502 getPersistenceClassForEjbName(ejbName); 503 isPCClass = true; 504 isPKClassName = true; 505 break; 506 } 507 } 508 509 if (nameMapper.isEjbName(searchClassName)) 510 { 511 searchClassName = nameMapper. 512 getAbstractBeanClassForEjbName(searchClassName); 513 } 514 515 returnObject = super.getField(searchClassName, searchFieldName); 516 517 if (returnObject == null) returnObject = getFieldWrapper(testClass, searchFieldName); 519 else if (returnObject instanceof Field) returnObject = new MemberWrapper((Field)returnObject); 521 522 if (isPCClass) 523 { 524 returnObject = updateFieldWrapper( 525 (MemberWrapper)returnObject, testClass, fieldName); 526 } 527 if (isPKClassName && (returnObject instanceof MemberWrapper)) 531 ((MemberWrapper)returnObject)._modifiers = Modifier.PUBLIC; 532 533 } 534 535 return returnObject; 536 } 537 538 555 public String getFieldType (String className, String fieldName) 556 { 557 String returnType = super.getFieldType(className, fieldName); 558 559 if (!isCollection(returnType) && isPCClassName(className)) 560 { 561 NameMapper nameMapper = getNameMapper(); 562 String ejbName = 563 nameMapper.getEjbNameForPersistenceClass(className); 564 String ejbField = 565 nameMapper.getEjbFieldForPersistenceField(className, fieldName); 566 567 if (nameMapper.isGeneratedEjbRelationship(ejbName, ejbField)) 568 { 569 String [] inverse = 570 nameMapper.getEjbFieldForGeneratedField(ejbName, ejbField); 571 572 returnType = nameMapper. 573 getPersistenceClassForEjbName(inverse[0]); 574 } 575 576 if (nameMapper.isLocalInterface(returnType)) 577 { 578 returnType = nameMapper.getPersistenceClassForLocalInterface( 579 className, fieldName, returnType); 580 } 581 } 582 583 return returnType; 584 } 585 586 602 public String getDeclaringClass (Object memberElement) 603 { 604 if ((memberElement != null) && (memberElement instanceof MemberWrapper)) 605 { 606 Class classElement = 607 ((MemberWrapper)memberElement).getDeclaringClass(); 608 609 return ((classElement != null) ? classElement.getName() : null); 610 } 611 612 return super.getDeclaringClass(memberElement); 613 } 614 615 630 public int getModifiers (Object memberElement) 631 { 632 if ((memberElement != null) && (memberElement instanceof MemberWrapper)) 633 return ((MemberWrapper)memberElement).getModifiers(); 634 635 return super.getModifiers(memberElement); 636 } 637 638 647 public int getModifiersForClass (String className) 648 { 649 int modifiers = super.getModifiersForClass(className); 650 651 if (isPCClassName(className)) 652 modifiers &= ~Modifier.ABSTRACT; 653 else if (getNameMapper(). 654 getKeyClassForPersistenceKeyClass(className) != null) 655 { 656 modifiers |= Modifier.STATIC; 657 } 658 659 return modifiers; 660 } 661 662 676 public boolean isValidKeyType (String className, String fieldName) 677 { 678 return (((NameMapper.PRIMARY_KEY_FIELD == 679 getPersistenceKeyClassType(className)) && 680 isPrimitive(className, fieldName)) ? false : 681 super.isValidKeyType(className, fieldName)); 682 } 683 684 695 protected Class getTypeObject (Object element) 696 { 697 Class type = super.getTypeObject(element); 698 699 if ((element != null) && (element instanceof MemberWrapper)) 700 type = ((MemberWrapper)element).getType(); 701 702 return type; 703 } 704 705 715 protected ClassLoader findClassLoader (String className, 716 ClassLoader classLoader) 717 { 718 return getClassLoader(); 719 } 720 721 private boolean isPCClassName (String className) 724 { 725 return (getEjbName(className) != null); 726 } 727 728 private String getEjbName (String className) 729 { 730 return getNameMapper().getEjbNameForPersistenceClass(className); 731 } 732 733 private EjbCMPEntityDescriptor getCMPDescriptor (String className) 734 { 735 String descriptorName = (isPCClassName(className) ? 736 getEjbName(className) : className); 737 738 return getNameMapper().getDescriptorForEjbName(descriptorName); 739 } 740 741 private int getPersistenceKeyClassType (String className) 742 { 743 int returnValue = -1; 744 745 if (getCMPDescriptor(className) == null) 746 { 747 NameMapper nameMapper = getNameMapper(); 748 String ejbName = 749 nameMapper.getEjbNameForPersistenceKeyClass(className); 750 751 if (ejbName != null) 752 returnValue = nameMapper.getKeyClassTypeForEjbName(ejbName); 753 } 754 755 return returnValue; 756 } 757 758 private MemberWrapper getFieldWrapper (String className, String fieldName) 759 { 760 EjbCMPEntityDescriptor descriptor = getCMPDescriptor(className); 761 MemberWrapper returnObject = null; 762 763 if (descriptor != null) 764 { 765 PersistenceDescriptor persistenceDescriptor = 766 descriptor.getPersistenceDescriptor(); 767 768 if (persistenceDescriptor != null) 769 { 770 Class fieldType = null; 771 772 try 773 { 774 fieldType = persistenceDescriptor.getTypeFor(fieldName); 775 } 776 catch (RuntimeException e) 777 { 778 } 780 781 returnObject = ((fieldType == null) ? null : 782 new MemberWrapper(fieldName, fieldType, 783 Modifier.PRIVATE, (Class )getClass(className))); 784 } 785 } 786 787 return returnObject; 788 } 789 790 private MemberWrapper updateFieldWrapper (MemberWrapper returnObject, 791 String className, String fieldName) 792 { 793 NameMapper nameMapper = getNameMapper(); 794 795 if (returnObject == null) 796 { 797 PersistenceFieldElement field = 801 getPersistenceFieldInternal(className, fieldName); 802 803 if (field != null) 804 { 805 String ejbName = getEjbName(className); 806 String ejbFieldName = nameMapper. 807 getEjbFieldForPersistenceField(className, fieldName); 808 809 if (field.isKey() && (ejbName != null) && 812 (nameMapper.getKeyClassTypeForEjbName(ejbName) == 813 NameMapper.UNKNOWN_KEY_CLASS)) 814 { 815 returnObject = new MemberWrapper(ejbFieldName, 816 Long .class, Modifier.PRIVATE, 817 (Class )getClass(className)); 818 } 819 820 else if ((field instanceof RelationshipElement) && 824 nameMapper.isGeneratedEjbRelationship(ejbName, 825 ejbFieldName)) 826 { 827 RelationshipElement rel = (RelationshipElement)field; 828 Class classType = null; 829 830 if (rel.getUpperBound() > 1) 832 classType = java.util.HashSet .class; 833 else 834 { 835 String [] inverse = nameMapper. 836 getEjbFieldForGeneratedField(ejbName, ejbFieldName); 837 838 classType = (Class )getClass(inverse[0]); 839 } 840 841 if (classType != null) 842 { 843 returnObject = new MemberWrapper(ejbFieldName, 844 classType, Modifier.PRIVATE, 845 (Class )getClass(className)); 846 } 847 } 848 else if (ejbFieldName.startsWith( 851 NameMapper.GENERATED_VERSION_FIELD_PREFIX) && 852 nameMapper.isGeneratedField(ejbName, ejbFieldName)) 853 { 854 returnObject = new MemberWrapper(ejbFieldName, 855 Long.TYPE, Modifier.PRIVATE, 856 (Class )getClass(className)); 857 } 858 } 859 } 860 861 if (!isPersistentTypeAllowed(getType(returnObject), 864 getClassLoader()) && isSerializable(returnObject)) 865 { 866 returnObject.setType(byte[].class); 867 } 868 869 return returnObject; 870 } 871 872 private class MemberWrapper 873 { 874 private String _name; 875 private Class _type; 876 private int _modifiers; 877 private Class _declaringClass; 878 879 private MemberWrapper (Member member) 880 { 881 this(member.getName(), ((member instanceof Field) ? 882 ((Field)member).getType() : ((member instanceof Method) ? 883 ((Method)member).getReturnType() : null)), 884 member.getModifiers(), member.getDeclaringClass()); 885 } 886 887 private MemberWrapper (String name, Class type, int modifiers, 888 Class declaringClass) 889 { 890 _name = name; 891 _type = type; 892 _modifiers = modifiers; 893 _declaringClass = declaringClass; 894 } 895 896 private Class getType () { return _type; } 897 private void setType (Class type) { _type = type; } 898 private String getName () { return _name; } 899 private int getModifiers () { return _modifiers; } 900 private Class getDeclaringClass () { return _declaringClass; } 901 902 905 public String toString () { return getName(); } 906 } 907 } 908 | Popular Tags |