1 23 24 29 30 package com.sun.jdo.api.persistence.model; 31 32 import java.io.*; 33 import java.net.URL ; 34 import java.util.*; 35 import java.lang.reflect.*; 36 import java.security.AccessController ; 37 import java.security.PrivilegedAction ; 38 39 import org.netbeans.modules.dbschema.SchemaElement; 40 import com.sun.jdo.api.persistence.model.mapping.MappingClassElement; 41 import com.sun.jdo.spi.persistence.utility.*; 42 43 48 public class RuntimeModel extends Model 49 { 50 53 private static final String CLASS_EXTENSION = "class"; 55 57 private static final String JAVA_PACKAGE = "java."; 59 61 private static final String SERIALIZABLE = "java.io.Serializable"; 63 66 private HashMap classLoaders = new HashMap(); 67 68 73 protected RuntimeModel () 74 { 75 super(); 76 } 77 78 83 public boolean isInterface (String className) 84 { 85 Class classElement = (Class )getClass(className); 86 87 return ((classElement != null) ? classElement.isInterface() : false); 88 } 89 90 102 protected BufferedInputStream getInputStreamForResource (String className, 103 ClassLoader classLoader, String resourceName) 104 { 105 InputStream is = ((className != null) ? 106 classLoader.getResourceAsStream(resourceName) : null); 107 108 BufferedInputStream rc = null; 109 if (is != null && !(is instanceof BufferedInputStream)) { 110 rc = new BufferedInputStream(is); 111 } else { 112 rc = (BufferedInputStream)is; 113 } 114 return rc; 115 } 116 117 123 private String getShortClassName (String className) 124 { 125 return JavaTypeHelper.getShortClassName(className); 126 } 127 128 134 protected String findPenultimateSuperclass (String className) 135 { 136 Class classElement = (Class )getClass(className); 137 Class objectClass = java.lang.Object .class; 138 Class testClass = null; 139 140 if (classElement == null) 141 return className; 142 143 while ((testClass = classElement.getSuperclass()) != null) 144 { 145 if (testClass.equals(objectClass)) 146 break; 147 148 classElement = testClass; 149 } 150 151 return classElement.getName(); 152 } 153 154 159 protected String getSuperclass (String className) 160 { 161 Class classElement = (Class )getClass(className); 162 163 if (classElement != null) 164 classElement = classElement.getSuperclass(); 165 166 return ((classElement != null) ? classElement.getName() : null); 167 } 168 169 178 public MappingClassElement getMappingClass (String className, 179 ClassLoader classLoader) 180 { 181 MappingClassElement mappingClass = null; 182 183 classLoader = findClassLoader(className, classLoader); 188 mappingClass = super.getMappingClass(className, classLoader); 189 if ((mappingClass != null) && (classLoader != null)) 190 { 191 String databaseRoot = mappingClass.getDatabaseRoot(); 197 198 if (!StringHelper.isEmpty(databaseRoot) && 204 (SchemaElement.forName(databaseRoot, classLoader) == null)) 205 { 206 throw new RuntimeException (I18NHelper.getMessage( 207 getMessages(), "dbschema.not_found", databaseRoot, className)); 209 } 210 } 211 212 return mappingClass; 213 } 214 215 218 public Map getClassLoaderCache () 219 { 220 return Collections.unmodifiableMap(classLoaders); 221 } 222 223 231 public void removeResourcesFromCaches (ClassLoader classLoader) 232 { 233 Collection classNames = new HashSet(); 234 235 synchronized(classLoaders) 236 { 237 for (Iterator i = classLoaders.entrySet().iterator(); i.hasNext();) 238 { 239 Map.Entry next = (Map.Entry)i.next(); 240 241 if (next.getValue() == classLoader) 243 { 244 classNames.add(next.getKey()); 247 i.remove(); 249 } 250 } 251 } 252 253 removeResourcesFromCaches(classNames); 254 } 255 256 265 protected BufferedOutputStream createFile (String className, String baseFileName, 266 String extension) throws IOException 267 { 268 char extensionCharacter = '.'; 269 File file = getFile(className, 270 baseFileName + extensionCharacter + extension); 271 272 if (file == null) 273 { 274 Class classElement = (Class )getClass(className); 275 276 if (classElement != null) 277 { 278 String path = classElement.getResource( 280 getShortClassName(className) + extensionCharacter + 281 CLASS_EXTENSION).getFile(); 282 int index = path.lastIndexOf(extensionCharacter) + 1; 283 284 file = new File(path.substring(0, index) + extension); 285 file.createNewFile(); 286 } 287 } 288 return ((file != null) 289 ? (new BufferedOutputStream(new FileOutputStream(file))) 290 : null); 291 } 292 293 299 protected void deleteFile (String className, String fileName) 300 throws IOException 301 { 302 File file = getFile(className, fileName); 303 304 if ((file != null) && file.exists()) 305 file.delete(); 306 } 307 308 316 protected File getFile (String className, String fileName) 317 throws IOException 318 { 319 Class classElement = (Class )getClass(className); 320 321 if (classElement != null) 322 { 323 URL path = classElement.getResource(fileName.substring( 325 fileName.lastIndexOf(getShortClassName(className)))); 326 327 return ((path != null) ? (new File(path.getFile())) : null); 328 } 329 330 return null; 331 } 332 333 340 public Object getClass (String className, ClassLoader classLoader) 341 { 342 if (className == null) 343 return null; 344 345 try 346 { 347 classLoader = findClassLoader(className, classLoader); 348 return Class.forName(className, true, classLoader); 349 } 350 catch (ClassNotFoundException e) 351 { 352 return null; 353 } 354 } 355 356 369 protected ClassLoader findClassLoader (String className, 370 ClassLoader classLoader) throws IllegalArgumentException 371 { 372 ClassLoader cached = null; 373 374 if (className == null) 375 return null; 376 else if (className.startsWith(JAVA_PACKAGE) || isPrimitive(className)) 377 return getClass().getClassLoader(); 381 382 synchronized (classLoaders) 383 { 384 cached = (ClassLoader )classLoaders.get(className); 385 386 if (classLoader == null) 387 { 388 classLoader = 392 (cached != null) ? cached : getClass().getClassLoader(); 393 } 394 else if (cached == null) 395 { 396 classLoaders.put(className, classLoader); 400 } 401 else if (classLoader != cached) 402 { 403 Class clazz = null; 408 Class cachedClazz = null; 409 410 try 411 { 412 String prop = ClassLoaderStrategy.getStrategy(); 413 414 clazz = Class.forName(className, true, classLoader); 418 cachedClazz = Class.forName(className, true, cached); 419 420 if (clazz.getClassLoader() == cachedClazz.getClassLoader()) 421 { 422 return cached; 425 } 426 else if (ClassLoaderStrategy.MULTIPLE_CLASS_LOADERS_IGNORE.equals(prop)) 427 { 428 return cached; 433 } 434 else if (ClassLoaderStrategy.MULTIPLE_CLASS_LOADERS_RELOAD.equals(prop)) 435 { 436 removeResourcesFromCaches(cachedClazz.getClassLoader()); 441 classLoaders.put(className, classLoader); 442 return classLoader; 443 } 444 else 445 { 446 throw new IllegalArgumentException (I18NHelper.getMessage( 451 getMessages(), "classloader.multiple", className)); 453 } 454 } 455 catch (ClassNotFoundException ex) 456 { 457 if ((clazz != null) && (cachedClazz == null)) 461 classLoaders.put(className, classLoader); 462 } 463 } 464 } 465 466 return classLoader; 467 } 468 469 480 public boolean implementsInterface (Object classElement, 481 String interfaceName) 482 { 483 Class interfaceClass = (Class )getClass(interfaceName); 484 485 if ((classElement == null) || !(classElement instanceof Class ) || 486 (interfaceClass == null)) 487 return false; 488 489 return interfaceClass.isAssignableFrom((Class )classElement); 490 } 491 492 498 public boolean hasConstructor (final String className) 499 { 500 final Class classElement = (Class )getClass(className); 501 502 if (classElement != null) 503 { 504 Boolean b = (Boolean )AccessController.doPrivileged( 505 new PrivilegedAction () 506 { 507 public Object run () 508 { 509 return JavaTypeHelper.valueOf(((Class )classElement). 510 getDeclaredConstructors().length != 0); 511 } 512 }); 513 514 return b.booleanValue(); 515 } 516 517 return false; 518 } 519 520 530 public Object getConstructor (final String className, String [] argTypeNames) 531 { 532 final Class classElement = (Class )getClass(className); 533 534 if (classElement != null) 535 { 536 final Class [] argTypes = getTypesForNames(argTypeNames); 537 538 return AccessController.doPrivileged(new PrivilegedAction () 539 { 540 public Object run () 541 { 542 try 543 { 544 return ((Class )classElement).getDeclaredConstructor( 545 argTypes); 546 } 547 catch (NoSuchMethodException ex) 548 { 549 return null; 551 } 552 } 553 }); 554 } 555 556 return null; 557 } 558 559 570 public Object getMethod (final String className, final String methodName, 571 String [] argTypeNames) 572 { 573 final Class classElement = (Class )getClass(className); 574 575 if (classElement != null) 576 { 577 final Class [] argTypes = getTypesForNames(argTypeNames); 578 579 return AccessController.doPrivileged(new PrivilegedAction () 580 { 581 public Object run () 582 { 583 try 584 { 585 return classElement.getDeclaredMethod( 586 methodName, argTypes); 587 } 588 catch (NoSuchMethodException ex) 589 { 590 return null; 592 } 593 } 594 }); 595 } 596 597 return null; 598 } 599 600 612 public String getType (Object element) 613 { 614 return getNameForType(getTypeObject(element)); 615 } 616 617 622 public List getFields (String className) 623 { 624 List returnList = new ArrayList(); 625 final Class classElement = (Class )getClass(className); 626 627 if (classElement != null) 628 { 629 Field[] fields = (Field[]) AccessController.doPrivileged( 630 new PrivilegedAction () 631 { 632 public Object run () 633 { 634 return classElement.getDeclaredFields(); 635 } 636 }); 637 int i, count = fields.length; 638 639 for (i = 0; i < count; i++) 640 returnList.add(fields[i].getName()); 641 } 642 643 return returnList; 644 } 645 646 653 public Object getField (String className, final String fieldName) 654 { 655 final Class classElement = (Class )getClass(className); 656 657 if (classElement != null) 658 { 659 return AccessController.doPrivileged(new PrivilegedAction () 660 { 661 public Object run () 662 { 663 try 664 { 665 return classElement.getDeclaredField(fieldName); 666 } 667 catch (NoSuchFieldException e) 668 { 669 return null; 671 } 672 } 673 }); 674 } 675 676 return null; 677 } 678 679 691 public boolean isSerializable (Object fieldElement) 692 { 693 Class type = getTypeObject(fieldElement); 694 695 while ((type != null) && type.isArray()) 697 type = type.getComponentType(); 698 699 return ((type != null) ? 700 (type.isPrimitive() || implementsInterface(type, SERIALIZABLE)) : 701 false); 702 } 703 704 713 public boolean isArray (String className, String fieldName) 714 { 715 Object fieldElement = getField(className, fieldName); 716 717 return ((fieldElement != null) ? 718 getTypeObject(fieldElement).isArray() : false); 719 } 720 721 736 public String getDeclaringClass (Object memberElement) 737 { 738 Class classElement = null; 739 740 if ((memberElement != null) && (memberElement instanceof Member)) 741 classElement = ((Member)memberElement).getDeclaringClass(); 742 743 return ((classElement != null) ? classElement.getName() : null); 744 } 745 746 760 public int getModifiers (Object memberElement) 761 { 762 int modifiers = 0; 763 764 if (memberElement != null) 765 { 766 if (memberElement instanceof Class ) 767 { 768 modifiers = ((Class )memberElement).getModifiers(); 769 } 770 else if (memberElement instanceof Member) 771 { 772 modifiers = ((Member)memberElement).getModifiers(); 773 } 774 } 775 776 return modifiers; 777 } 778 779 790 protected Class getTypeObject (Object element) 791 { 792 Class type = null; 793 794 if (element != null) 795 { 796 if (element instanceof Field) 797 type = ((Field)element).getType(); 798 else if (element instanceof Method) 799 type = ((Method)element).getReturnType(); 800 } 801 802 return type; 803 } 804 805 private String getNameForType (Class type) 806 { 807 String typeName = null; 808 809 if (type != null) 810 { 811 if (type.isArray()) 812 { 813 typeName = getNameForType( 814 type.getComponentType()) + "[]"; } 816 else 817 typeName = type.getName(); 818 } 819 820 return typeName; 821 } 822 823 825 private Class [] getTypesForNames (String [] typeNames) 826 { 827 Class [] classes = new Class [typeNames.length]; 828 829 for (int i = 0; i < classes.length; i++) 830 classes[i] = getTypeForName(typeNames[i]); 831 832 return classes; 833 } 834 835 838 private Class getTypeForName (String typeName) 839 { 840 Class clazz = JavaTypeHelper.getPrimitiveClass(typeName); 841 842 if (clazz == null) 843 clazz = (Class )getClass(typeName); 844 845 return clazz; 846 } 847 } 848 | Popular Tags |