1 21 package oracle.toplink.essentials.internal.helper; 23 24 import java.math.*; 25 import java.util.*; 26 import java.io.*; 27 import java.security.AccessController ; 28 import java.security.PrivilegedActionException ; 29 import java.sql.*; 30 31 import oracle.toplink.essentials.exceptions.*; 32 import oracle.toplink.essentials.internal.security.PrivilegedAccessHelper; 33 import oracle.toplink.essentials.internal.security.PrivilegedGetClassLoaderForClass; 34 import oracle.toplink.essentials.internal.security.PrivilegedGetContextClassLoader; 35 36 47 public class ConversionManager implements Serializable, Cloneable { 48 protected Map defaultNullValues; 49 50 54 protected boolean shouldUseClassLoaderFromCurrentThread = false; 55 protected static ConversionManager defaultManager; 56 57 58 private static ClassLoader defaultLoader; 59 protected ClassLoader loader; 60 61 62 protected Hashtable dataTypesConvertedFromAClass; 63 64 65 protected Hashtable dataTypesConvertedToAClass; 66 67 public ConversionManager() { 68 this.defaultNullValues = new HashMap(5); 69 this.dataTypesConvertedFromAClass = new Hashtable(); 70 this.dataTypesConvertedToAClass = new Hashtable(); 71 } 72 73 76 public Object clone() { 77 try { 78 return super.clone(); 79 } catch (CloneNotSupportedException exception) { 80 return null; 81 } 82 } 83 84 92 public Object convertObject(Object sourceObject, Class javaClass) throws ConversionException { 93 if (sourceObject == null) { 94 if (javaClass != null) { 97 return getDefaultNullValue(javaClass); 98 } else { 99 return null; 100 } 101 } 102 103 if ((sourceObject.getClass() == javaClass) || (javaClass == null) || (javaClass == ClassConstants.OBJECT) || (javaClass == ClassConstants.BLOB) || (javaClass == ClassConstants.CLOB) || ClassConstants.NOCONVERSION.isAssignableFrom(javaClass)) { 104 return sourceObject; 105 } 106 107 if (javaClass.isPrimitive() && (((sourceObject instanceof Boolean ) && (javaClass == ClassConstants.PBOOLEAN)) || ((sourceObject instanceof Long ) && (javaClass == ClassConstants.PLONG)) || ((sourceObject instanceof Integer ) && (javaClass == ClassConstants.PINT)) || ((sourceObject instanceof Float ) && (javaClass == ClassConstants.PFLOAT)) || ((sourceObject instanceof Double ) && (javaClass == ClassConstants.PDOUBLE)) || ((sourceObject instanceof Byte ) && (javaClass == ClassConstants.PBYTE)) || ((sourceObject instanceof Character ) && (javaClass == ClassConstants.PCHAR)) || ((sourceObject instanceof Short ) && (javaClass == ClassConstants.PSHORT)))) { 109 return sourceObject; 110 } 111 112 try { 113 if (javaClass == ClassConstants.STRING) { 114 return convertObjectToString(sourceObject); 115 } else if (javaClass == ClassConstants.UTILDATE) { 116 return convertObjectToUtilDate(sourceObject); 117 } else if (javaClass == ClassConstants.SQLDATE) { 118 return convertObjectToDate(sourceObject); 119 } else if (javaClass == ClassConstants.TIME) { 120 return convertObjectToTime(sourceObject); 121 } else if (javaClass == ClassConstants.TIMESTAMP) { 122 return convertObjectToTimestamp(sourceObject); 123 } else if ((javaClass == ClassConstants.CALENDAR) || (javaClass == ClassConstants.GREGORIAN_CALENDAR)) { 124 return convertObjectToCalendar(sourceObject); 125 } else if ((javaClass == ClassConstants.CHAR) || (javaClass == ClassConstants.PCHAR)) { 126 return convertObjectToChar(sourceObject); 127 } else if ((javaClass == ClassConstants.INTEGER) || (javaClass == ClassConstants.PINT)) { 128 return convertObjectToInteger(sourceObject); 129 } else if ((javaClass == ClassConstants.DOUBLE) || (javaClass == ClassConstants.PDOUBLE)) { 130 return convertObjectToDouble(sourceObject); 131 } else if ((javaClass == ClassConstants.FLOAT) || (javaClass == ClassConstants.PFLOAT)) { 132 return convertObjectToFloat(sourceObject); 133 } else if ((javaClass == ClassConstants.LONG) || (javaClass == ClassConstants.PLONG)) { 134 return convertObjectToLong(sourceObject); 135 } else if ((javaClass == ClassConstants.SHORT) || (javaClass == ClassConstants.PSHORT)) { 136 return convertObjectToShort(sourceObject); 137 } else if ((javaClass == ClassConstants.BYTE) || (javaClass == ClassConstants.PBYTE)) { 138 return convertObjectToByte(sourceObject); 139 } else if (javaClass == ClassConstants.BIGINTEGER) { 140 return convertObjectToBigInteger(sourceObject); 141 } else if (javaClass == ClassConstants.BIGDECIMAL) { 142 return convertObjectToBigDecimal(sourceObject); 143 } else if (javaClass == ClassConstants.NUMBER) { 144 return convertObjectToNumber(sourceObject); 145 } else if ((javaClass == ClassConstants.BOOLEAN) || (javaClass == ClassConstants.PBOOLEAN)) { 146 return convertObjectToBoolean(sourceObject); 147 } else if (javaClass == ClassConstants.APBYTE) { 148 return convertObjectToByteArray(sourceObject); 149 } else if (javaClass == ClassConstants.ABYTE) { 150 return convertObjectToByteObjectArray(sourceObject); 151 } else if (javaClass == ClassConstants.APCHAR) { 152 return convertObjectToCharArray(sourceObject); 153 } else if (javaClass == ClassConstants.ACHAR) { 154 return convertObjectToCharacterArray(sourceObject); 155 } else if ((sourceObject.getClass() == ClassConstants.STRING) && (javaClass == ClassConstants.CLASS)) { 156 return convertObjectToClass(sourceObject); 157 } 158 } catch (ConversionException ce) { 159 throw ce; 160 } catch (Exception e) { 161 throw ConversionException.couldNotBeConverted(sourceObject, javaClass, e); 162 } 163 164 if (javaClass.isInstance(sourceObject)) { 166 return sourceObject; 167 } 168 169 throw ConversionException.couldNotBeConverted(sourceObject, javaClass); 170 } 171 172 176 protected BigDecimal convertObjectToBigDecimal(Object sourceObject) throws ConversionException { 177 BigDecimal bigDecimal = null; 178 179 try { 180 if (sourceObject instanceof String ) { 181 bigDecimal = new BigDecimal((String )sourceObject); 182 } else if (sourceObject instanceof BigInteger) { 183 bigDecimal = new BigDecimal((BigInteger)sourceObject); 184 } else if (sourceObject instanceof Number ) { 185 bigDecimal = new BigDecimal(((Number )sourceObject).doubleValue()); 186 } else { 187 throw ConversionException.couldNotBeConverted(sourceObject, ClassConstants.BIGDECIMAL); 188 } 189 } catch (NumberFormatException exception) { 190 throw ConversionException.couldNotBeConverted(sourceObject, ClassConstants.BIGDECIMAL, exception); 191 } 192 return bigDecimal; 193 } 194 195 199 protected BigInteger convertObjectToBigInteger(Object sourceObject) throws ConversionException { 200 BigInteger bigInteger = null; 201 202 try { 203 if (sourceObject instanceof BigInteger) { 204 bigInteger = (BigInteger)sourceObject; 205 } else if (sourceObject instanceof String ) { 206 bigInteger = new BigInteger((String )sourceObject); 207 } else if (sourceObject instanceof BigDecimal) { 208 bigInteger = ((BigDecimal)sourceObject).toBigInteger(); 209 } else if (sourceObject instanceof Number ) { 210 bigInteger = new BigInteger(new Long (((Number )sourceObject).longValue()).toString()); 211 } else { 212 throw ConversionException.couldNotBeConverted(sourceObject, ClassConstants.BIGINTEGER); 213 } 214 } catch (NumberFormatException exception) { 215 throw ConversionException.couldNotBeConverted(sourceObject, ClassConstants.BIGINTEGER, exception); 216 } 217 218 return bigInteger; 219 } 220 221 226 protected Boolean convertObjectToBoolean(Object sourceObject) { 227 if (sourceObject instanceof Character ) { 228 switch (Character.toLowerCase(((Character )sourceObject).charValue())) { 229 case '1': 230 case 't': 231 return new Boolean (true); 232 case '0': 233 case 'f': 234 return new Boolean (false); 235 } 236 } 237 if (sourceObject instanceof String ) { 238 String stringValue = ((String )sourceObject).toLowerCase(); 239 if (stringValue.equals("t") || stringValue.equals("true") || stringValue.equals("1")) { 240 return new Boolean (true); 241 } else if (stringValue.equals("f") || stringValue.equals("false") || stringValue.equals("0")) { 242 return new Boolean (false); 243 } 244 } 245 if (sourceObject instanceof Number ) { 246 int intValue = ((Number )sourceObject).intValue(); 247 if (intValue != 0) { 248 return new Boolean (true); 249 } else if (intValue == 0) { 250 return new Boolean (false); 251 } 252 } 253 throw ConversionException.couldNotBeConverted(sourceObject, ClassConstants.BOOLEAN); 254 } 255 256 264 protected Byte convertObjectToByte(Object sourceObject) throws ConversionException { 265 try { 266 if (sourceObject instanceof String ) { 267 return new Byte ((String )sourceObject); 268 } 269 if (sourceObject instanceof Number ) { 270 return new Byte (((Number )sourceObject).byteValue()); 271 } 272 } catch (NumberFormatException exception) { 273 throw ConversionException.couldNotBeConverted(sourceObject, ClassConstants.BYTE, exception); 274 } 275 276 throw ConversionException.couldNotBeConverted(sourceObject, ClassConstants.BYTE); 277 } 278 279 285 protected byte[] convertObjectToByteArray(Object sourceObject) throws ConversionException { 286 if (sourceObject instanceof byte[]) { 288 return (byte[])sourceObject; 289 } else if (sourceObject instanceof Byte []) { 291 Byte [] objectBytes = (Byte [])sourceObject; 292 byte[] bytes = new byte[objectBytes.length]; 293 for (int index = 0; index < objectBytes.length; index++) { 294 bytes[index] = objectBytes[index].byteValue(); 295 } 296 return bytes; 297 } else if (sourceObject instanceof String ) { 298 return Helper.buildBytesFromHexString((String )sourceObject); 299 } else if (sourceObject instanceof Blob) { 300 Blob blob = (Blob)sourceObject; 301 try { 302 return blob.getBytes(1L, (int)blob.length()); 303 } catch (SQLException exception) { 304 throw DatabaseException.sqlException(exception); 305 } 306 } 307 308 throw ConversionException.couldNotBeConverted(sourceObject, ClassConstants.APBYTE); 309 } 310 311 317 protected Byte [] convertObjectToByteObjectArray(Object sourceObject) throws ConversionException { 318 byte[] bytes = convertObjectToByteArray(sourceObject); 319 Byte [] objectBytes = new Byte [bytes.length]; 320 for (int index = 0; index < bytes.length; index++) { 321 objectBytes[index] = new Byte (bytes[index]); 322 } 323 return objectBytes; 324 } 325 326 330 protected Calendar convertObjectToCalendar(Object sourceObject) throws ConversionException { 331 if (sourceObject instanceof Calendar) { 332 return (Calendar)sourceObject; 333 } else if (sourceObject instanceof java.util.Date ) { 334 return Helper.calendarFromUtilDate((java.util.Date )sourceObject); 336 } 337 return Helper.calendarFromUtilDate(convertObjectToUtilDate(sourceObject)); 338 } 339 340 344 protected Character convertObjectToChar(Object sourceObject) throws ConversionException { 345 if (sourceObject instanceof String ) { 346 if (((String )sourceObject).length() < 1) { 347 return null; 348 } 349 return new Character (((String )sourceObject).charAt(0)); 350 } 351 352 if (sourceObject instanceof Number ) { 353 return new Character ((char)((Number )sourceObject).byteValue()); 354 } 355 356 throw ConversionException.couldNotBeConverted(sourceObject, ClassConstants.CHAR); 357 } 358 359 362 protected Character [] convertObjectToCharacterArray(Object sourceObject) throws ConversionException { 363 String stringValue = convertObjectToString(sourceObject); 364 Character [] chars = new Character [stringValue.length()]; 365 for (int index = 0; index < stringValue.length(); index++) { 366 chars[index] = new Character (stringValue.charAt(index)); 367 } 368 return chars; 369 } 370 371 374 protected char[] convertObjectToCharArray(Object sourceObject) throws ConversionException { 375 if (sourceObject instanceof Character []) { 376 Character [] objectChars = (Character [])sourceObject; 377 char[] chars = new char[objectChars.length]; 378 for (int index = 0; index < objectChars.length; index++) { 379 chars[index] = objectChars[index].charValue(); 380 } 381 return chars; 382 } 383 String stringValue = convertObjectToString(sourceObject); 384 char[] chars = new char[stringValue.length()]; 385 for (int index = 0; index < stringValue.length(); index++) { 386 chars[index] = stringValue.charAt(index); 387 } 388 return chars; 389 } 390 391 395 protected Class convertObjectToClass(Object sourceObject) throws ConversionException { 396 Class theClass = null; 397 if (!(sourceObject instanceof String )) { 398 throw ConversionException.couldNotBeConverted(sourceObject, ClassConstants.CLASS); 399 } 400 try { 401 theClass = getPrimitiveClass((String )sourceObject); 403 if (theClass == null) { 404 theClass = getLoader().loadClass((String )sourceObject); 405 } 406 } catch (Exception exception) { 407 throw ConversionException.couldNotBeConvertedToClass(sourceObject, ClassConstants.CLASS, exception); 408 } 409 return theClass; 410 } 411 412 416 protected java.sql.Date convertObjectToDate(Object sourceObject) throws ConversionException { 417 java.sql.Date date = null; 418 Class sourceClass = sourceObject.getClass(); 419 420 if (sourceObject instanceof java.sql.Date ) { 421 date = (java.sql.Date )sourceObject; } else if (sourceObject instanceof java.sql.Timestamp ) { 423 date = Helper.dateFromTimestamp((java.sql.Timestamp )sourceObject); 424 } else if (sourceObject.getClass() == ClassConstants.UTILDATE) { 425 date = Helper.sqlDateFromUtilDate((java.util.Date )sourceObject); 426 } else if (sourceObject instanceof Calendar) { 427 return Helper.dateFromCalendar((Calendar)sourceObject); 428 } else if (sourceObject instanceof String ) { 429 date = Helper.dateFromString((String )sourceObject); 430 } else if (sourceObject instanceof Long ) { 431 date = Helper.dateFromLong((Long )sourceObject); 432 } else { 433 throw ConversionException.couldNotBeConverted(sourceObject, ClassConstants.SQLDATE); 434 } 435 return date; 436 } 437 438 445 protected Double convertObjectToDouble(Object sourceObject) throws ConversionException { 446 try { 447 if (sourceObject instanceof String ) { 448 return new Double ((String )sourceObject); 449 } 450 if (sourceObject instanceof Number ) { 451 return new Double (((Number )sourceObject).doubleValue()); 452 } 453 } catch (NumberFormatException exception) { 454 throw ConversionException.couldNotBeConverted(sourceObject, ClassConstants.DOUBLE, exception); 455 } 456 throw ConversionException.couldNotBeConverted(sourceObject, ClassConstants.DOUBLE); 457 } 458 459 465 protected Float convertObjectToFloat(Object sourceObject) throws ConversionException { 466 try { 467 if (sourceObject instanceof String ) { 468 return new Float ((String )sourceObject); 469 } 470 if (sourceObject instanceof Number ) { 471 return new Float (((Number )sourceObject).floatValue()); 472 } 473 } catch (NumberFormatException exception) { 474 throw ConversionException.couldNotBeConverted(sourceObject, ClassConstants.FLOAT, exception); 475 } 476 477 throw ConversionException.couldNotBeConverted(sourceObject, ClassConstants.FLOAT); 478 } 479 480 486 protected Integer convertObjectToInteger(Object sourceObject) throws ConversionException { 487 try { 488 if (sourceObject instanceof String ) { 489 return new Integer ((String )sourceObject); 490 } 491 492 if (sourceObject instanceof Number ) { 493 return new Integer (((Number )sourceObject).intValue()); 494 } 495 496 if (sourceObject instanceof Boolean ) { 497 if (((Boolean )sourceObject).booleanValue()) { 498 return new Integer (1); 499 } else { 500 return new Integer (0); 501 } 502 } 503 } catch (NumberFormatException exception) { 504 throw ConversionException.couldNotBeConverted(sourceObject, ClassConstants.INTEGER, exception); 505 } 506 507 throw ConversionException.couldNotBeConverted(sourceObject, ClassConstants.INTEGER); 508 } 509 510 517 protected Long convertObjectToLong(Object sourceObject) throws ConversionException { 518 try { 519 if (sourceObject instanceof String ) { 520 return new Long ((String )sourceObject); 521 } 522 if (sourceObject instanceof Number ) { 523 return new Long (((Number )sourceObject).longValue()); 524 } 525 if (sourceObject instanceof java.util.Date ) { 526 return new Long (((java.util.Date )sourceObject).getTime()); 527 } 528 if (sourceObject instanceof java.util.Calendar ) { 529 return new Long (JavaPlatform.getTimeInMillis(((java.util.Calendar )sourceObject))); 530 } 531 532 if (sourceObject instanceof Boolean ) { 533 if (((Boolean )sourceObject).booleanValue()) { 534 return new Long (1); 535 } else { 536 return new Long (0); 537 } 538 } 539 } catch (NumberFormatException exception) { 540 throw ConversionException.couldNotBeConverted(sourceObject, ClassConstants.LONG, exception); 541 } 542 543 throw ConversionException.couldNotBeConverted(sourceObject, ClassConstants.LONG); 544 } 545 546 555 protected BigDecimal convertObjectToNumber(Object sourceObject) throws ConversionException { 556 try { 557 if (sourceObject instanceof String ) { 558 return new BigDecimal((String )sourceObject); 559 } 560 561 if (sourceObject instanceof Number ) { 562 return new BigDecimal(((Number )sourceObject).doubleValue()); 563 } 564 565 if (sourceObject instanceof Boolean ) { 566 if (((Boolean )sourceObject).booleanValue()) { 567 return new BigDecimal(1); 568 } else { 569 return new BigDecimal(0); 570 } 571 } 572 } catch (NumberFormatException exception) { 573 throw ConversionException.couldNotBeConverted(sourceObject, ClassConstants.NUMBER, exception); 574 } 575 576 throw ConversionException.couldNotBeConverted(sourceObject, ClassConstants.NUMBER); 577 } 578 579 586 protected Short convertObjectToShort(Object sourceObject) throws ConversionException { 587 try { 588 if (sourceObject instanceof String ) { 589 return new Short ((String )sourceObject); 590 } 591 592 if (sourceObject instanceof Number ) { 593 return new Short (((Number )sourceObject).shortValue()); 594 } 595 596 if (sourceObject instanceof Boolean ) { 597 if (((Boolean )sourceObject).booleanValue()) { 598 return new Short ((short)1); 599 } else { 600 return new Short ((short)0); 601 } 602 } 603 } catch (Exception exception) { 604 throw ConversionException.couldNotBeConverted(sourceObject, ClassConstants.SHORT, exception); 605 } 606 607 throw ConversionException.couldNotBeConverted(sourceObject, ClassConstants.SHORT); 608 } 609 610 616 protected String convertObjectToString(Object sourceObject) throws ConversionException { 617 if (sourceObject.getClass() == ClassConstants.UTILDATE) { 618 return Helper.printTimestamp(Helper.timestampFromDate((java.util.Date )sourceObject)); 619 } else if (sourceObject instanceof Calendar) { 620 return Helper.printCalendar((Calendar)sourceObject); 621 } else if (sourceObject instanceof java.sql.Timestamp ) { 622 return Helper.printTimestamp((java.sql.Timestamp )sourceObject); 623 } else if (sourceObject instanceof java.sql.Date ) { 624 return Helper.printDate((java.sql.Date )sourceObject); 625 } else if (sourceObject instanceof java.sql.Time ) { 626 return Helper.printTime((java.sql.Time )sourceObject); 627 } else if (sourceObject instanceof byte[]) { 628 return Helper.buildHexStringFromBytes((byte[])sourceObject); 629 } else if (sourceObject instanceof Byte []) { 631 return Helper.buildHexStringFromBytes(convertObjectToByteArray(sourceObject)); 632 } else if (sourceObject instanceof char[]) { 633 return new String ((char[])sourceObject); 634 } else if (sourceObject instanceof Character []) { 635 return new String (convertObjectToCharArray(sourceObject)); 636 } else if (sourceObject instanceof Class ) { 637 return ((Class )sourceObject).getName(); 638 } else if (sourceObject instanceof Character ) { 639 return sourceObject.toString(); 640 } else if (sourceObject instanceof Clob) { 641 Clob clob = (Clob)sourceObject; 642 try { 643 return clob.getSubString(1L, (int)clob.length()); 644 } catch (SQLException exception) { 645 throw DatabaseException.sqlException(exception); 646 } 647 } 648 649 return sourceObject.toString(); 650 } 651 652 657 protected java.sql.Time convertObjectToTime(Object sourceObject) throws ConversionException { 658 java.sql.Time time = null; 659 660 if (sourceObject instanceof java.sql.Time ) { 661 return (java.sql.Time )sourceObject; } 663 664 if (sourceObject instanceof String ) { 665 time = Helper.timeFromString((String )sourceObject); 666 } else if (sourceObject.getClass() == ClassConstants.UTILDATE) { 667 time = Helper.timeFromDate((java.util.Date )sourceObject); 668 } else if (sourceObject instanceof java.sql.Timestamp ) { 669 time = Helper.timeFromTimestamp((java.sql.Timestamp )sourceObject); 670 } else if (sourceObject instanceof Calendar) { 671 return Helper.timeFromCalendar((Calendar)sourceObject); 672 } else if (sourceObject instanceof Long ) { 673 time = Helper.timeFromLong((Long )sourceObject); 674 } else { 675 throw ConversionException.couldNotBeConverted(sourceObject, ClassConstants.TIME); 676 } 677 return time; 678 } 679 680 685 protected java.sql.Timestamp convertObjectToTimestamp(Object sourceObject) throws ConversionException { 686 java.sql.Timestamp timestamp = null; 687 688 if (sourceObject instanceof java.sql.Timestamp ) { 689 return (java.sql.Timestamp )sourceObject; } 691 692 if (sourceObject instanceof String ) { 693 timestamp = Helper.timestampFromString((String )sourceObject); 694 } else if (sourceObject instanceof java.util.Date ) { timestamp = Helper.timestampFromDate((java.util.Date )sourceObject); 696 } else if (sourceObject instanceof Calendar) { 697 return Helper.timestampFromCalendar((Calendar)sourceObject); 698 } else if (sourceObject instanceof Long ) { 699 timestamp = Helper.timestampFromLong((Long )sourceObject); 700 } else { 701 throw ConversionException.couldNotBeConverted(sourceObject, ClassConstants.TIMESTAMP); 702 } 703 return timestamp; 704 } 705 706 711 protected java.util.Date convertObjectToUtilDate(Object sourceObject) throws ConversionException { 712 java.util.Date date = null; 713 714 if (sourceObject.getClass() == java.util.Date .class) { 715 date = (java.util.Date )sourceObject; } else if (sourceObject instanceof java.sql.Date ) { 717 date = Helper.utilDateFromSQLDate((java.sql.Date )sourceObject); 718 } else if (sourceObject instanceof java.sql.Time ) { 719 date = Helper.utilDateFromTime((java.sql.Time )sourceObject); 720 } else if (sourceObject instanceof String ) { 721 date = Helper.utilDateFromTimestamp(Helper.timestampFromString((String )sourceObject)); 722 } else if (sourceObject instanceof java.sql.Timestamp ) { 723 date = Helper.utilDateFromTimestamp((java.sql.Timestamp )sourceObject); 724 } else if (sourceObject instanceof Calendar) { 725 return ((Calendar)sourceObject).getTime(); 726 } else if (sourceObject instanceof Long ) { 727 date = Helper.utilDateFromLong((Long )sourceObject); 728 } else { 729 throw ConversionException.couldNotBeConverted(sourceObject, ClassConstants.UTILDATE); 730 } 731 return date; 732 } 733 734 739 public Class convertClassNameToClass(String className) throws ConversionException { 740 return convertObjectToClass(className); 741 } 742 743 748 public static ConversionManager getDefaultManager() { 749 if (defaultManager == null) { 750 setDefaultManager(new ConversionManager()); 751 defaultManager.setShouldUseClassLoaderFromCurrentThread(true); 752 } 753 return defaultManager; 754 } 755 756 761 public Object getDefaultNullValue(Class theClass) { 762 return getDefaultNullValues().get(theClass); 763 } 764 765 770 public Map getDefaultNullValues() { 771 return defaultNullValues; 772 } 773 774 777 public ClassLoader getLoader() { 778 if (shouldUseClassLoaderFromCurrentThread()) { 779 if (PrivilegedAccessHelper.shouldUsePrivilegedAccess()){ 780 try { 781 return (ClassLoader )AccessController.doPrivileged(new PrivilegedGetContextClassLoader(Thread.currentThread())); 782 } catch (PrivilegedActionException exception) { 783 } 785 } else { 786 return PrivilegedAccessHelper.getContextClassLoader(Thread.currentThread()); 787 } 788 } 789 if (loader == null) { 790 if (defaultLoader == null) { 791 ClassLoader loader = null; 793 if (PrivilegedAccessHelper.shouldUsePrivilegedAccess()){ 794 try{ 795 loader = (ClassLoader )AccessController.doPrivileged(new PrivilegedGetClassLoaderForClass(ClassConstants.ConversionManager_Class)); 796 } catch (PrivilegedActionException exc){ 797 } 799 } else { 800 loader = PrivilegedAccessHelper.getClassLoaderForClass(ClassConstants.ConversionManager_Class); 801 } 802 setLoader(loader); 803 } else { 804 setLoader(getDefaultLoader()); 805 } 806 } 807 return loader; 808 } 809 810 817 public static Class loadClass(String className) { 818 return (Class )getDefaultManager().convertObject(className, ClassConstants.CLASS); 819 } 820 821 825 public static Class getObjectClass(Class javaClass) { 826 if (javaClass == null) { 828 return null; 829 } 830 831 if (javaClass.isPrimitive()) { 832 if (javaClass == ClassConstants.PCHAR) { 833 return ClassConstants.CHAR; 834 } 835 if (javaClass == ClassConstants.PINT) { 836 return ClassConstants.INTEGER; 837 } 838 if (javaClass == ClassConstants.PDOUBLE) { 839 return ClassConstants.DOUBLE; 840 } 841 if (javaClass == ClassConstants.PFLOAT) { 842 return ClassConstants.FLOAT; 843 } 844 if (javaClass == ClassConstants.PLONG) { 845 return ClassConstants.LONG; 846 } 847 if (javaClass == ClassConstants.PSHORT) { 848 return ClassConstants.SHORT; 849 } 850 if (javaClass == ClassConstants.PBYTE) { 851 return ClassConstants.BYTE; 852 } 853 if (javaClass == ClassConstants.PBOOLEAN) { 854 return ClassConstants.BOOLEAN; 855 } 856 } else if (javaClass == ClassConstants.APBYTE) { 857 return ClassConstants.APBYTE; 858 } else if (javaClass == ClassConstants.APCHAR) { 859 return ClassConstants.APCHAR; 860 } else { 861 return javaClass; 862 } 863 864 return javaClass; 865 } 866 867 871 public static Class getPrimitiveClass(String classType) { 872 if (classType.equals("int")) { 873 return Integer.TYPE; 874 } else if (classType.equals("boolean")) { 875 return Boolean.TYPE; 876 } else if (classType.equals("char")) { 877 return Character.TYPE; 878 } else if (classType.equals("short")) { 879 return Short.TYPE; 880 } else if (classType.equals("byte")) { 881 return Byte.TYPE; 882 } else if (classType.equals("float")) { 883 return Float.TYPE; 884 } else if (classType.equals("double")) { 885 return Double.TYPE; 886 } else if (classType.equals("long")) { 887 return Long.TYPE; 888 } 889 890 return null; 891 } 892 893 898 public static void setDefaultManager(ConversionManager theManager) { 899 defaultManager = theManager; 900 } 901 902 908 public void setDefaultNullValue(Class theClass, Object theValue) { 909 getDefaultNullValues().put(theClass, theValue); 910 } 911 912 917 public void setDefaultNullValues(Map defaultNullValues) { 918 this.defaultNullValues = defaultNullValues; 919 } 920 921 925 public void setLoader(ClassLoader classLoader) { 926 shouldUseClassLoaderFromCurrentThread = false; 927 loader = classLoader; 928 } 929 930 935 public static void setDefaultLoader(ClassLoader classLoader) { 936 defaultLoader = classLoader; 937 } 938 939 944 public static ClassLoader getDefaultLoader() { 945 return defaultLoader; 946 } 947 948 955 public void setShouldUseClassLoaderFromCurrentThread(boolean useCurrentThread) { 956 this.shouldUseClassLoaderFromCurrentThread = useCurrentThread; 957 } 958 959 966 public boolean shouldUseClassLoaderFromCurrentThread() { 967 return this.shouldUseClassLoaderFromCurrentThread; 968 } 969 970 976 public Vector getDataTypesConvertedFrom(Class javaClass) { 977 if (dataTypesConvertedFromAClass.isEmpty()) { 978 buildDataTypesConvertedFromAClass(); 979 } 980 return (Vector)dataTypesConvertedFromAClass.get(javaClass); 981 } 982 983 989 public Vector getDataTypesConvertedTo(Class javaClass) { 990 if (dataTypesConvertedToAClass.isEmpty()) { 991 buildDataTypesConvertedToAClass(); 992 } 993 return (Vector)dataTypesConvertedToAClass.get(javaClass); 994 } 995 996 protected Vector buildNumberVec() { 997 Vector vec = new Vector(); 998 vec.addElement(BigInteger.class); 999 vec.addElement(BigDecimal.class); 1000 vec.addElement(Byte .class); 1001 vec.addElement(Double .class); 1002 vec.addElement(Float .class); 1003 vec.addElement(Integer .class); 1004 vec.addElement(Long .class); 1005 vec.addElement(Short .class); 1006 vec.addElement(Number .class); 1007 return vec; 1008 } 1009 1010 protected Vector buildDateTimeVec() { 1011 Vector vec = new Vector(); 1012 vec.addElement(java.util.Date .class); 1013 vec.addElement(Timestamp.class); 1014 vec.addElement(Calendar.class); 1015 return vec; 1016 } 1017 1018 protected void buildDataTypesConvertedFromAClass() { 1019 dataTypesConvertedFromAClass.put(BigDecimal.class, buildFromBigDecimalVec()); 1020 dataTypesConvertedFromAClass.put(BigInteger.class, buildFromBigIntegerVec()); 1021 dataTypesConvertedFromAClass.put(Blob.class, buildFromBlobVec()); 1022 dataTypesConvertedFromAClass.put(Boolean .class, buildFromBooleanVec()); 1023 dataTypesConvertedFromAClass.put(byte[].class, buildFromByteArrayVec()); 1024 dataTypesConvertedFromAClass.put(Byte .class, buildFromByteVec()); 1025 dataTypesConvertedFromAClass.put(Calendar.class, buildFromCalendarVec()); 1026 dataTypesConvertedFromAClass.put(Character .class, buildFromCharacterVec()); 1027 dataTypesConvertedFromAClass.put(Clob.class, buildFromClobVec()); 1028 dataTypesConvertedFromAClass.put(java.sql.Date .class, buildFromDateVec()); 1029 dataTypesConvertedFromAClass.put(Double .class, buildFromDoubleVec()); 1030 dataTypesConvertedFromAClass.put(Float .class, buildFromFloatVec()); 1031 dataTypesConvertedFromAClass.put(Integer .class, buildFromIntegerVec()); 1032 dataTypesConvertedFromAClass.put(Long .class, buildFromLongVec()); 1033 dataTypesConvertedFromAClass.put(Number .class, buildFromNumberVec()); 1034 dataTypesConvertedFromAClass.put(Short .class, buildFromShortVec()); 1035 dataTypesConvertedFromAClass.put(String .class, buildFromStringVec()); 1036 dataTypesConvertedFromAClass.put(Timestamp.class, buildFromTimestampVec()); 1037 dataTypesConvertedFromAClass.put(Time.class, buildFromTimeVec()); 1038 dataTypesConvertedFromAClass.put(java.util.Date .class, buildFromUtilDateVec()); 1039 dataTypesConvertedFromAClass.put(Byte [].class, buildFromByteObjectArraryVec()); 1040 dataTypesConvertedFromAClass.put(char[].class, buildFromCharArrayVec()); 1041 dataTypesConvertedFromAClass.put(Character [].class, buildFromCharacterArrayVec()); 1042 } 1043 1044 protected Vector buildFromBooleanVec() { 1045 Vector vec = new Vector(); 1046 vec.addElement(String .class); 1047 vec.addElement(Boolean .class); 1048 vec.addElement(Integer .class); 1049 vec.addElement(Long .class); 1050 vec.addElement(Short .class); 1051 vec.addElement(Number .class); 1052 vec.addElement(Character [].class); 1053 vec.addElement(char[].class); 1054 vec.addElement(boolean.class); 1055 vec.addElement(int.class); 1056 vec.addElement(long.class); 1057 vec.addElement(short.class); 1058 return vec; 1059 } 1060 1061 protected Vector buildFromNumberVec() { 1062 Vector vec = buildNumberVec(); 1063 vec.addElement(String .class); 1064 vec.addElement(Character .class); 1065 vec.addElement(Boolean .class); 1066 vec.addElement(Character [].class); 1067 vec.addElement(char[].class); 1068 vec.addElement(char.class); 1069 vec.addElement(int.class); 1070 vec.addElement(double.class); 1071 vec.addElement(float.class); 1072 vec.addElement(long.class); 1073 vec.addElement(short.class); 1074 vec.addElement(byte.class); 1075 vec.addElement(boolean.class); 1076 return vec; 1077 } 1078 1079 protected Vector buildFromBigDecimalVec() { 1080 return buildFromNumberVec(); 1081 } 1082 1083 protected Vector buildFromBigIntegerVec() { 1084 return buildFromNumberVec(); 1085 } 1086 1087 protected Vector buildFromIntegerVec() { 1088 return buildFromNumberVec(); 1089 } 1090 1091 protected Vector buildFromFloatVec() { 1092 return buildFromNumberVec(); 1093 } 1094 1095 protected Vector buildFromDoubleVec() { 1096 return buildFromNumberVec(); 1097 } 1098 1099 protected Vector buildFromShortVec() { 1100 return buildFromNumberVec(); 1101 } 1102 1103 protected Vector buildFromByteVec() { 1104 return buildFromNumberVec(); 1105 } 1106 1107 protected Vector buildFromLongVec() { 1108 Vector vec = buildFromNumberVec(); 1109 vec.addAll(buildDateTimeVec()); 1110 vec.addElement(java.sql.Date .class); 1111 vec.addElement(Time.class); 1112 return vec; 1113 } 1114 1115 protected Vector buildFromStringVec() { 1116 Vector vec = buildFromLongVec(); 1117 vec.addElement(Byte [].class); 1118 vec.addElement(byte[].class); 1119 vec.addElement(Clob.class); 1120 return vec; 1121 } 1122 1123 protected Vector buildFromCharacterVec() { 1124 Vector vec = new Vector(); 1125 vec.addElement(String .class); 1126 vec.addElement(Boolean .class); 1127 vec.addElement(Character [].class); 1128 vec.addElement(Character .class); 1129 vec.addElement(char[].class); 1130 vec.addElement(char.class); 1131 vec.addElement(boolean.class); 1132 return vec; 1133 } 1134 1135 protected Vector buildFromByteArrayVec() { 1136 Vector vec = new Vector(); 1137 vec.addElement(String .class); 1138 vec.addElement(byte[].class); 1139 vec.addElement(Byte [].class); 1140 vec.addElement(Character [].class); 1141 vec.addElement(char[].class); 1142 return vec; 1143 } 1144 1145 protected Vector buildFromClobVec() { 1146 Vector vec = new Vector(); 1147 vec.addElement(String .class); 1148 vec.addElement(Character [].class); 1149 vec.addElement(char[].class); 1150 return vec; 1151 } 1152 1153 protected Vector buildFromBlobVec() { 1154 Vector vec = new Vector(); 1155 vec.addElement(String .class); 1156 vec.addElement(Byte [].class); 1157 vec.addElement(byte[].class); 1158 vec.addElement(Character [].class); 1159 vec.addElement(char[].class); 1160 return vec; 1161 } 1162 1163 protected Vector buildFromUtilDateVec() { 1164 Vector vec = buildDateTimeVec(); 1165 vec.addElement(String .class); 1166 vec.addElement(Long .class); 1167 vec.addElement(java.sql.Date .class); 1168 vec.addElement(Time.class); 1169 vec.addElement(long.class); 1170 vec.addElement(Character [].class); 1171 vec.addElement(char[].class); 1172 return vec; 1173 } 1174 1175 protected Vector buildFromTimestampVec() { 1176 return buildFromUtilDateVec(); 1177 } 1178 1179 protected Vector buildFromCalendarVec() { 1180 return buildFromUtilDateVec(); 1181 } 1182 1183 protected Vector buildFromDateVec() { 1184 Vector vec = buildDateTimeVec(); 1185 vec.addElement(String .class); 1186 vec.addElement(Long .class); 1187 vec.addElement(java.sql.Date .class); 1188 vec.addElement(long.class); 1189 vec.addElement(Character [].class); 1190 vec.addElement(char[].class); 1191 return vec; 1192 } 1193 1194 protected Vector buildFromTimeVec() { 1195 Vector vec = buildDateTimeVec(); 1196 vec.addElement(String .class); 1197 vec.addElement(Long .class); 1198 vec.addElement(Time.class); 1199 vec.addElement(long.class); 1200 vec.addElement(Character [].class); 1201 vec.addElement(char[].class); 1202 return vec; 1203 } 1204 1205 protected Vector buildFromByteObjectArraryVec() { 1206 Vector vec = new Vector(); 1207 vec.addElement(Blob.class); 1208 vec.addElement(byte[].class); 1209 return vec; 1210 } 1211 1212 protected Vector buildFromCharArrayVec() { 1213 Vector vec = new Vector(); 1214 vec.addElement(Clob.class); 1215 return vec; 1216 } 1217 1218 protected Vector buildFromCharacterArrayVec() { 1219 Vector vec = new Vector(); 1220 vec.addElement(Clob.class); 1221 return vec; 1222 } 1223 1224 protected void buildDataTypesConvertedToAClass() { 1225 dataTypesConvertedToAClass.put(BigDecimal.class, buildToBigDecimalVec()); 1226 dataTypesConvertedToAClass.put(BigInteger.class, buildToBigIntegerVec()); 1227 dataTypesConvertedToAClass.put(Boolean .class, buildToBooleanVec()); 1228 dataTypesConvertedToAClass.put(Byte .class, buildToByteVec()); 1229 dataTypesConvertedToAClass.put(byte[].class, buildToByteArrayVec()); 1230 dataTypesConvertedToAClass.put(Byte [].class, buildToByteObjectArrayVec()); 1231 dataTypesConvertedToAClass.put(Calendar.class, buildToCalendarVec()); 1232 dataTypesConvertedToAClass.put(Character .class, buildToCharacterVec()); 1233 dataTypesConvertedToAClass.put(Character [].class, buildToCharacterArrayVec()); 1234 dataTypesConvertedToAClass.put(char[].class, buildToCharArrayVec()); 1235 dataTypesConvertedToAClass.put(java.sql.Date .class, buildToDateVec()); 1236 dataTypesConvertedToAClass.put(Double .class, buildToDoubleVec()); 1237 dataTypesConvertedToAClass.put(Float .class, buildToFloatVec()); 1238 dataTypesConvertedToAClass.put(Integer .class, buildToIntegerVec()); 1239 dataTypesConvertedToAClass.put(Long .class, buildToLongVec()); 1240 dataTypesConvertedToAClass.put(Number .class, buildToNumberVec()); 1241 dataTypesConvertedToAClass.put(Short .class, buildToShortVec()); 1242 dataTypesConvertedToAClass.put(String .class, buildToStringVec()); 1243 dataTypesConvertedToAClass.put(Timestamp.class, buildToTimestampVec()); 1244 dataTypesConvertedToAClass.put(Time.class, buildToTimeVec()); 1245 dataTypesConvertedToAClass.put(java.util.Date .class, buildToUtilDateVec()); 1246 dataTypesConvertedToAClass.put(Clob.class, buildToClobVec()); 1247 dataTypesConvertedToAClass.put(Blob.class, buildToBlobVec()); 1248 } 1249 1250 protected Vector buildAllTypesToAClassVec() { 1251 Vector vec = new Vector(); 1252 vec.addElement(String .class); 1253 vec.addElement(Integer .class); 1254 vec.addElement(java.util.Date .class); 1255 vec.addElement(java.sql.Date .class); 1256 vec.addElement(Time.class); 1257 vec.addElement(Timestamp.class); 1258 vec.addElement(Calendar.class); 1259 vec.addElement(Character .class); 1260 vec.addElement(Double .class); 1261 vec.addElement(Float .class); 1262 vec.addElement(Long .class); 1263 vec.addElement(Short .class); 1264 vec.addElement(Byte .class); 1265 vec.addElement(BigInteger.class); 1266 vec.addElement(BigDecimal.class); 1267 vec.addElement(Number .class); 1268 vec.addElement(Boolean .class); 1269 vec.addElement(Character [].class); 1270 vec.addElement(Blob.class); 1271 vec.addElement(Clob.class); 1272 return vec; 1273 } 1274 1275 protected Vector buildToBigDecimalVec() { 1276 Vector vec = buildNumberVec(); 1277 vec.addElement(String .class); 1278 return vec; 1279 } 1280 1281 protected Vector buildToBigIntegerVec() { 1282 return buildToBigDecimalVec(); 1283 } 1284 1285 protected Vector buildToBooleanVec() { 1286 Vector vec = buildToBigDecimalVec(); 1287 vec.addElement(Character .class); 1288 vec.addElement(Boolean .class); 1289 return vec; 1290 } 1291 1292 protected Vector buildToByteVec() { 1293 return buildToBigDecimalVec(); 1294 } 1295 1296 protected Vector buildToDoubleVec() { 1297 return buildToBigDecimalVec(); 1298 } 1299 1300 protected Vector buildToFloatVec() { 1301 return buildToBigDecimalVec(); 1302 } 1303 1304 protected Vector buildToIntegerVec() { 1305 Vector vec = buildToBigDecimalVec(); 1306 vec.addElement(Boolean .class); 1307 return vec; 1308 } 1309 1310 protected Vector buildToLongVec() { 1311 Vector vec = buildToIntegerVec(); 1312 vec.addElement(Calendar.class); 1313 vec.addElement(java.util.Date .class); 1314 return vec; 1315 } 1316 1317 protected Vector buildToNumberVec() { 1318 return buildToIntegerVec(); 1319 } 1320 1321 protected Vector buildToShortVec() { 1322 return buildToIntegerVec(); 1323 } 1324 1325 protected Vector buildToByteArrayVec() { 1326 Vector vec = new Vector(); 1327 vec.addElement(String .class); 1328 vec.addElement(Blob.class); 1329 vec.addElement(byte[].class); 1330 vec.addElement(Byte [].class); 1331 return vec; 1332 } 1333 1334 protected Vector buildToByteObjectArrayVec() { 1335 Vector vec = buildToByteArrayVec(); 1336 vec.addElement(Byte [].class); 1337 return vec; 1338 } 1339 1340 protected Vector buildToCharacterVec() { 1341 Vector vec = buildToBigDecimalVec(); 1342 vec.addElement(Character .class); 1343 return vec; 1344 } 1345 1346 protected Vector buildToCharacterArrayVec() { 1347 return buildAllTypesToAClassVec(); 1348 } 1349 1350 protected Vector buildToCharArrayVec() { 1351 return buildAllTypesToAClassVec(); 1352 } 1353 1354 protected Vector buildToStringVec() { 1355 return buildAllTypesToAClassVec(); 1356 } 1357 1358 protected Vector buildToCalendarVec() { 1359 Vector vec = buildDateTimeVec(); 1360 vec.addElement(String .class); 1361 vec.addElement(Long .class); 1362 vec.addElement(java.sql.Date .class); 1363 vec.addElement(Time.class); 1364 return vec; 1365 } 1366 1367 protected Vector buildToTimestampVec() { 1368 return buildToCalendarVec(); 1369 } 1370 1371 protected Vector buildToUtilDateVec() { 1372 return buildToCalendarVec(); 1373 } 1374 1375 protected Vector buildToDateVec() { 1376 Vector vec = buildDateTimeVec(); 1377 vec.addElement(String .class); 1378 vec.addElement(Long .class); 1379 vec.addElement(java.sql.Date .class); 1380 return vec; 1381 } 1382 1383 protected Vector buildToTimeVec() { 1384 Vector vec = buildDateTimeVec(); 1385 vec.addElement(String .class); 1386 vec.addElement(Long .class); 1387 vec.addElement(Time.class); 1388 return vec; 1389 } 1390 1391 protected Vector buildToBlobVec() { 1392 Vector vec = new Vector(); 1393 vec.addElement(Byte [].class); 1394 vec.addElement(byte[].class); 1395 return vec; 1396 } 1397 1398 protected Vector buildToClobVec() { 1399 Vector vec = new Vector(); 1400 vec.addElement(String .class); 1401 vec.addElement(char[].class); 1402 vec.addElement(Character [].class); 1403 return vec; 1404 } 1405} 1406 | Popular Tags |