1 23 24 25 package com.sun.jdo.api.persistence.enhancer.meta; 26 27 import java.util.Map ; 28 import java.util.List ; 29 import java.util.Collection ; 30 import java.util.HashSet ; 31 import java.util.HashMap ; 32 import java.util.ArrayList ; 33 import java.util.Iterator ; 34 import java.util.Properties ; 35 import java.util.StringTokenizer ; 36 37 import java.io.PrintWriter ; 38 39 import com.sun.jdo.api.persistence.enhancer.meta.JDOMetaDataProperties.JDOClass; 40 import com.sun.jdo.api.persistence.enhancer.meta.JDOMetaDataProperties.JDOField; 41 42 import com.sun.jdo.api.persistence.enhancer.util.Support; 43 44 47 public class JDOMetaDataPropertyImpl extends Support 49 implements ExtendedJDOMetaData 50 { 51 55 private static final HashSet transientTypePrefixes = new HashSet (); 57 private static final HashSet secondClassObjectTypes = new HashSet (); 58 private static final HashSet mutableSecondClassObjectTypes = new HashSet (); 59 60 61 64 private final JDOMetaDataProperties properties; 65 66 67 70 71 static 72 { 73 transientTypePrefixes.add("java/"); transientTypePrefixes.add("javax/"); transientTypePrefixes.add("com/sun/jdo/"); 77 mutableSecondClassObjectTypes.add("java/util/Date"); mutableSecondClassObjectTypes.add("com/sun/jdo/spi/persistence/support/sqlstore/sco/Date"); mutableSecondClassObjectTypes.add("java/sql/Date"); mutableSecondClassObjectTypes.add("com/sun/jdo/spi/persistence/support/sqlstore/sco/SqlTime"); mutableSecondClassObjectTypes.add("java/sql/Time"); mutableSecondClassObjectTypes.add("com/sun/jdo/spi/persistence/support/sqlstore/sco/SqlDate"); mutableSecondClassObjectTypes.add("java/sql/Timestamp"); mutableSecondClassObjectTypes.add("com/sun/jdo/spi/persistence/support/sqlstore/sco/SqlTimestamp"); mutableSecondClassObjectTypes.add("java/util/Collection"); mutableSecondClassObjectTypes.add("java/util/Set"); mutableSecondClassObjectTypes.add("java/util/List"); mutableSecondClassObjectTypes.add("java/util/HashSet"); mutableSecondClassObjectTypes.add("java/util/Vector"); mutableSecondClassObjectTypes.add("java/util/ArrayList"); 92 secondClassObjectTypes.add("java/lang/Boolean"); secondClassObjectTypes.add("java/lang/Byte"); secondClassObjectTypes.add("java/lang/Short"); secondClassObjectTypes.add("java/lang/Integer"); secondClassObjectTypes.add("java/lang/Long"); secondClassObjectTypes.add("java/lang/Float"); secondClassObjectTypes.add("java/lang/Double"); secondClassObjectTypes.add("java/lang/Number"); secondClassObjectTypes.add("java/lang/Character"); secondClassObjectTypes.add("java/lang/String"); secondClassObjectTypes.add("java/math/BigInteger"); secondClassObjectTypes.add("java/math/BigDecimal"); secondClassObjectTypes.addAll(mutableSecondClassObjectTypes); 105 106 } 108 109 113 public JDOMetaDataPropertyImpl(Properties properties, 114 PrintWriter out) 115 throws JDOMetaDataUserException, JDOMetaDataFatalError 116 { 117 if (properties == null) { 119 final String msg 120 = "Initializing meta data: properties == null"; throw new JDOMetaDataFatalError(msg); 122 } 123 130 131 this.properties = new JDOMetaDataProperties (properties); 132 readProperties (); 133 } 135 136 140 public JDOMetaDataPropertyImpl(Properties properties) 141 throws JDOMetaDataUserException, JDOMetaDataFatalError 142 { 143 this (properties, null); 144 } 145 146 149 public boolean isPersistenceCapableClass(String classPath) 150 throws JDOMetaDataUserException, JDOMetaDataFatalError 151 { 152 for (Iterator i = transientTypePrefixes.iterator(); i.hasNext();) { 154 final String typePrefix = (String )i.next(); 155 if (classPath.startsWith(typePrefix)) 156 return false; 157 } 158 JDOClass clazz = getJDOClass (classPath); 159 return (clazz != null ? clazz.isPersistent () : false); 160 } 161 162 165 166 public boolean isTransientClass(String classPath) 167 throws JDOMetaDataUserException, JDOMetaDataFatalError 168 { 169 return ! isPersistenceCapableClass (classPath); 170 } 171 172 175 public boolean isPersistenceCapableRootClass(String classPath) 176 throws JDOMetaDataUserException, JDOMetaDataFatalError 177 { 178 if ( ! isPersistenceCapableClass (classPath)) 180 { 181 return false; 182 } 183 String superclass = getSuperClass (classPath); 184 return (superclass != null ? ! isPersistenceCapableClass (superclass) : true); 185 186 } 189 190 193 public String getPersistenceCapableRootClass(String classPath) 194 throws JDOMetaDataUserException, JDOMetaDataFatalError 195 { 196 return (isPersistenceCapableClass(classPath) ? classPath : null); 198 } 199 200 203 204 public final String getSuperClass (String classname) 205 { 206 207 JDOClass clazz = getJDOClass (classname); 208 return (clazz != null ? clazz.getSuperClassName () : null); 209 210 } 212 213 216 public boolean isSecondClassObjectType(String classPath) 217 throws JDOMetaDataUserException, JDOMetaDataFatalError 218 { 219 return secondClassObjectTypes.contains(classPath); 220 } 221 222 225 public boolean isMutableSecondClassObjectType(String classPath) 226 throws JDOMetaDataUserException, JDOMetaDataFatalError 227 { 228 return mutableSecondClassObjectTypes.contains(classPath); 229 } 230 231 234 public boolean isPersistentField(String classPath, String fieldName) 235 throws JDOMetaDataUserException, JDOMetaDataFatalError 236 { 237 JDOField field = getJDOField (classPath, fieldName); 238 return (field != null ? field.isPersistent () : false); 239 } 240 241 244 public boolean isTransactionalField(String classPath, String fieldName) 245 throws JDOMetaDataUserException, JDOMetaDataFatalError 246 { 247 JDOField field = getJDOField (classPath, fieldName); 248 return (field != null ? field.isTransactional () : false); 249 } 250 251 254 public boolean isPrimaryKeyField(String classPath, String fieldName) 255 throws JDOMetaDataUserException, JDOMetaDataFatalError 256 { 257 JDOField field = getJDOField (classPath, fieldName); 258 return (field != null ? field.isPk () : false); 259 } 260 261 265 public boolean isDefaultFetchGroupField(String classPath, String fieldName) 266 throws JDOMetaDataUserException, JDOMetaDataFatalError 267 { 268 JDOField field = getJDOField (classPath, fieldName); 269 return (field != null ? field.isInDefaultFetchGroup () : false); 270 } 271 272 276 public int getFieldNo(String classPath, String fieldName) 277 throws JDOMetaDataUserException, JDOMetaDataFatalError 278 { 279 JDOClass clazz = getJDOClass (classPath); 280 return (clazz != null ? clazz.getIndexOfField (fieldName) : -1); 281 } 282 283 287 public String [] getManagedFields (String classname) 288 { 289 290 JDOClass clazz = getJDOClass (classname); 291 return (clazz != null ? clazz.getManagedFieldNames () : new String [] {}); 292 293 } 295 296 299 300 public final String [] getKnownClasses () 301 { 302 303 return this.properties.getKnownClassNames (); 304 305 } 307 308 311 312 public final String [] getKnownFields (String classname) 313 { 314 315 JDOClass clazz = getJDOClass (classname); 316 return (clazz != null ? clazz.getFields () : new String [] {}); 317 318 } 320 321 324 325 public final int getClassModifiers (String classname) 326 { 327 328 JDOClass clazz = getJDOClass (classname); 329 return (clazz != null ? clazz.getModifiers () : 0); 330 331 } 333 334 337 338 public final int getFieldModifiers (String classname, 339 String fieldname) 340 { 341 342 JDOField field = getJDOField (classname, fieldname); 343 return (field != null ? field.getModifiers () : 0); 344 345 } 347 348 351 352 public final String getFieldType (String classname, 353 String fieldname) 354 { 355 356 JDOField field = getJDOField (classname, fieldname); 357 return (field != null ? field.getType () : null); 358 359 } 361 362 365 366 private final JDOClass getJDOClass (String classname) 367 throws JDOMetaDataUserException 368 { 369 370 return this.properties.getJDOClass (classname); 371 372 } 374 375 378 379 private final void readProperties () 380 { 381 382 String [] classnames = this.properties.getKnownClassNames (); 384 for (int i = classnames.length - 1; i >= 0; i--) 385 { 386 JDOClass clazz = getJDOClass (classnames [i]); 388 if (clazz.isPersistent () && secondClassObjectTypes.contains (clazz.getName ())) 390 { 391 throw new JDOMetaDataUserException ("ERROR: Parsing meta data properties: " + 392 "The persistent-capable class '" + clazz.getName () + 393 "' is second class object type."); 394 } 395 } 396 397 } 399 400 403 404 private final JDOField getJDOField (String classname, 405 String fieldname) 406 { 407 408 JDOClass clazz = getJDOClass (classname); 409 return (clazz != null ? clazz.getField (fieldname) : null); 410 411 } 413 414 417 418 public String getKeyClass(String classPath) 419 throws JDOMetaDataUserException, JDOMetaDataFatalError 420 { 421 final JDOClass clazz = getJDOClass(classPath); 422 return (clazz != null ? clazz.getOidClassName() : null); 423 } 424 425 426 429 430 public boolean isKeyField(String classPath, String fieldName) 431 throws JDOMetaDataUserException, JDOMetaDataFatalError 432 { 433 final JDOField field = getJDOField(classPath, fieldName); 434 return (field != null ? field.isPk() : false); 435 } 436 437 438 441 442 public boolean isKnownNonManagedField(String classPath, 443 String fieldName, 444 String fieldSig) 445 { 446 final JDOClass clazz = getJDOClass(classPath); 447 if (clazz == null) { 448 return true; 449 } 450 final JDOField field = clazz.getField(fieldName); 451 return (field != null ? field.isKnownTransient() : false); 452 } 453 454 455 458 459 public boolean isManagedField(String classPath, String fieldName) 460 throws JDOMetaDataUserException, JDOMetaDataFatalError 461 { 462 return (isPersistentField(classPath, fieldName) 463 || isTransactionalField(classPath, fieldName)); 464 } 465 466 467 470 471 public int getFieldFlags(String classPath, String fieldName) 472 throws JDOMetaDataUserException, JDOMetaDataFatalError 473 { 474 if (!isManagedField(classPath, fieldName)) { 475 affirm(!isTransactionalField(classPath, fieldName)); 476 affirm(!isPersistentField(classPath, fieldName)); 477 affirm(!isKeyField(classPath, fieldName)); 478 affirm(!isDefaultFetchGroupField(classPath, fieldName)); 479 return 0; 480 } 481 483 if (isTransactionalField(classPath, fieldName)) { 484 affirm(!isPersistentField(classPath, fieldName)); 485 affirm(!isKeyField(classPath, fieldName)); 486 return CHECK_WRITE; 489 } 490 affirm(isPersistentField(classPath, fieldName)); 492 493 if (isKeyField(classPath, fieldName)) { 494 return MEDIATE_WRITE; 497 } 498 500 if (isDefaultFetchGroupField(classPath, fieldName)) { 501 return CHECK_READ | CHECK_WRITE; 502 } 503 505 return MEDIATE_READ | MEDIATE_WRITE; 506 } 507 508 509 512 513 public int[] getFieldFlags(String classPath, String [] fieldNames) 514 throws JDOMetaDataUserException, JDOMetaDataFatalError 515 { 516 final int n = (fieldNames != null ? fieldNames.length : 0); 517 final int[] flags = new int[n]; 518 for (int i = 0; i < n; i++) { 519 flags[i] = getFieldFlags(classPath, fieldNames[i]); 520 } 521 return flags; 522 } 523 524 525 528 529 public final String [] getFieldType(String classname, 530 String [] fieldnames) 531 { 532 final int n = (fieldnames != null ? fieldnames.length : 0); 533 final String [] types = new String [n]; 534 for (int i = 0; i < n; i++) { 535 types[i] = getFieldType(classname, fieldnames[i]); 536 } 537 return types; 538 } 539 540 541 544 545 public int[] getFieldNo(String classPath, String [] fieldNames) 546 throws JDOMetaDataUserException, JDOMetaDataFatalError 547 { 548 final int n = (fieldNames != null ? fieldNames.length : 0); 549 final int[] flags = new int[n]; 550 for (int i = 0; i < n; i++) { 551 flags[i] = getFieldNo(classPath, fieldNames[i]); 552 } 553 return flags; 554 } 555 556 557 560 561 public String [] getKeyFields(String classPath) 562 throws JDOMetaDataUserException, JDOMetaDataFatalError 563 { 564 final List keys = new ArrayList (); 565 final String [] fieldNames = getManagedFields(classPath); 566 final int n = fieldNames.length; 567 for (int i = 0; i < n; i++) { 568 if (isKeyField(classPath, fieldNames[i])) { 569 keys.add(fieldNames[i]); 570 } 571 } 572 return (String [])keys.toArray(new String [keys.size()]); 573 } 574 575 576 579 580 public String getPersistenceCapableSuperClass(String classPath) 581 throws JDOMetaDataUserException, JDOMetaDataFatalError 582 { 583 for (String clazz = getSuperClass(classPath); 584 clazz != null; 585 clazz = getSuperClass(clazz)) { 586 if (isPersistenceCapableClass(clazz)) { 587 return clazz; 588 } 589 } 590 return null; 591 } 592 593 594 597 598 public String getSuperKeyClass(String classPath) 599 throws JDOMetaDataUserException, JDOMetaDataFatalError 600 { 601 for (String superClass = getPersistenceCapableSuperClass(classPath); 602 superClass != null; 603 superClass = getPersistenceCapableSuperClass(superClass)) { 604 final String superKeyClass = getKeyClass(superClass); 605 if (superKeyClass != null) { 606 return superKeyClass; 607 } 608 } 609 return null; 610 } 611 612 613 616 673 674 } 676 677 | Popular Tags |