1 18 package org.apache.beehive.netui.util.type; 19 20 import org.apache.beehive.netui.util.internal.InternalStringBuilder; 21 22 import java.io.InputStream ; 23 import java.math.BigDecimal ; 24 import java.math.BigInteger ; 25 import java.text.DateFormat ; 26 import java.util.Date ; 27 import java.util.Enumeration ; 28 import java.util.HashMap ; 29 import java.util.Iterator ; 30 import java.util.LinkedHashMap ; 31 import java.util.Locale ; 32 import java.util.Map ; 33 import java.util.Properties ; 34 35 import org.apache.beehive.netui.util.Bundle; 36 import org.apache.beehive.netui.util.config.ConfigUtil; 37 import org.apache.beehive.netui.util.config.bean.NetuiConfigDocument.NetuiConfig; 38 import org.apache.beehive.netui.util.config.bean.TypeConverters; 39 import org.apache.beehive.netui.util.logging.Logger; 40 41 45 public final class TypeUtils { 46 47 private static final Logger LOGGER = Logger.getInstance(TypeUtils.class); 48 private static final String TYPE_CONVERTER_PROPERTIES = "/properties/netui-typeconverter.properties"; 49 private static final String EMPTY_STRING = ""; 50 private static final HashMap TYPE_CONVERTERS = new HashMap (); 51 private static String TO_STRING = null; 52 53 static { 54 initialize(); 56 57 Map map = readFromConfig(); 58 if(map != null) { 59 load(map); 60 map.clear(); 61 } 62 63 map = readFromProperties(); 64 if(map != null) 65 load(map); 66 67 LOGGER.info(registeredConvertersToString()); 68 } 69 70 71 private TypeUtils() {} 72 73 82 public static final Object convertToObject(String value, Class type) { 83 return convertToObject(value, type, null); 84 } 85 86 97 public static final Object convertToObject(String value, Class type, Locale locale) { 98 BaseTypeConverter converter = lookupTypeConverter(type); 99 assert converter != null; 100 return converter.convertToObject(type, value, locale); 101 } 102 103 public static final byte convertToByte(String value) { 104 return ((Byte )convertToObject(value, byte.class, null)).byteValue(); 105 } 106 107 public static final boolean convertToBoolean(String value) { 108 return ((Boolean )convertToObject(value, boolean.class, null)).booleanValue(); 109 } 110 111 public static final char convertToChar(String value) { 112 return ((Character )convertToObject(value, char.class, null)).charValue(); 113 } 114 115 public static final double convertToDouble(String value) { 116 return ((Double )convertToObject(value, double.class, null)).doubleValue(); 117 } 118 119 public static final float convertToFloat(String value) { 120 return ((Float )convertToObject(value, float.class, null)).floatValue(); 121 } 122 123 public static final int convertToInt(String value) { 124 return ((Integer )convertToObject(value, int.class, null)).intValue(); 125 } 126 127 public static final long convertToLong(String value) { 128 return ((Long )convertToObject(value, long.class, null)).longValue(); 129 } 130 131 public static final short convertToShort(String value) { 132 return ((Short )convertToObject(value, short.class, null)).shortValue(); 133 } 134 135 public static final Byte convertToByteObject(String value) { 136 return (Byte )convertToObject(value, Byte .class, null); 137 } 138 139 public static final Boolean convertToBooleanObject(String value) { 140 return (Boolean )convertToObject(value, Boolean .class, null); 141 } 142 143 public static final Character convertToCharacterObject(String value) { 144 return (Character )convertToObject(value, Character .class, null); 145 } 146 147 public static final Double convertToDoubleObject(String value) { 148 return (Double )convertToObject(value, Double .class, null); 149 } 150 151 public static final Float convertToFloatObject(String value) { 152 return (Float )convertToObject(value, Float .class, null); 153 } 154 155 public static final Integer convertToIntegerObject(String value) { 156 return (Integer )convertToObject(value, Integer .class, null); 157 } 158 159 public static final Long convertToLongObject(String value) { 160 return (Long )convertToObject(value, Long .class, null); 161 } 162 163 public static final Short convertToShortObject(String value) { 164 return (Short )convertToObject(value, Short .class, null); 165 } 166 167 174 private static final BaseTypeConverter lookupTypeConverter(Class type) { 175 BaseTypeConverter converter = (BaseTypeConverter)TYPE_CONVERTERS.get(type); 176 if(converter == null) { 177 String msg = "Could not find a TypeConverter for converting a String to an object of type \"" + 178 (type != null ? type.getName() : null) + "\""; 179 TypeConverterNotFoundException tcn = new TypeConverterNotFoundException(msg); 180 181 if(type == null) 182 msg = Bundle.getErrorString("TypeUtils_nullType"); 183 else 184 msg = Bundle.getErrorString("TypeUtils_noConverterForType", new Object []{type.getName()}); 185 tcn.setLocalizedMessage(msg); 186 LOGGER.error(msg); 187 188 throw tcn; 189 } 190 191 return converter; 192 } 193 194 private static String registeredConvertersToString() { 195 if(TO_STRING != null) 196 return TO_STRING; 197 198 InternalStringBuilder sb = new InternalStringBuilder(256); 199 sb.append(TypeUtils.class.getName() + " regestered converters (class name, TypeConverter implementation):\n"); 200 sb.append(":::::\n"); 201 Iterator iterator = TYPE_CONVERTERS.keySet().iterator(); 202 while(iterator.hasNext()) { 203 Class key = (Class )iterator.next(); 204 String keyName = key.getName(); 205 String value = (TYPE_CONVERTERS.get(key) != null ? TYPE_CONVERTERS.get(key).getClass().getName() : "null"); 206 sb.append(keyName); 207 sb.append(", "); 208 sb.append(value); 209 sb.append("\n"); 210 } 211 sb.append(":::::\n"); 212 TO_STRING = sb.toString(); 213 214 return TO_STRING; 215 } 216 217 private static Map readFromProperties() { 218 Properties props = null; 219 InputStream is = null; 220 try { 221 is = (TypeUtils.class).getClassLoader().getResourceAsStream(TYPE_CONVERTER_PROPERTIES); 222 223 LOGGER.debug("found type converter InputStream at " + TYPE_CONVERTER_PROPERTIES + " " + (is != null ? "true" : "false")); 224 225 if(is == null) 226 return null; 227 228 props = new Properties (); 229 props.load(is); 230 } catch(Exception e) { 231 LOGGER.warn("Error occurred reading type converter properties file", e); 232 } finally { 233 try { 234 if(is != null) is.close(); 235 } catch(Exception ignore) { 236 } 237 } 238 239 LinkedHashMap map = new LinkedHashMap (); 240 Enumeration e = props.propertyNames(); 241 while(e.hasMoreElements()) { 242 String key = (String )e.nextElement(); 243 map.put(key, props.getProperty(key)); 244 } 245 246 return map; 247 } 248 249 private static final Map readFromConfig() { 250 NetuiConfig config = ConfigUtil.getConfig(); 251 if(config == null) 252 return null; 253 254 TypeConverters converters = config.getTypeConverters(); 255 if(converters == null) 256 return null; 257 258 org.apache.beehive.netui.util.config.bean.TypeConverters.TypeConverter[] converterArray = 259 converters.getTypeConverterArray(); 260 if(converterArray == null) 261 return null; 262 263 LinkedHashMap map = new LinkedHashMap (); 264 for(int i = 0; i < converterArray.length; i++) { 265 map.put(converterArray[i].getType(), converterArray[i].getConverterClass()); 266 } 267 268 return map; 269 } 270 271 private static void load(Map map) { 272 for ( Iterator i = map.keySet().iterator(); i.hasNext(); ) 274 { 275 String key = (String ) i.next(); 276 String className = (String ) map.get(key); 277 278 if((key == null || key.equals(EMPTY_STRING)) || (className == null || className.equals(EMPTY_STRING))) { 279 LOGGER.warn("Could not create a TypeConverter for type \"" + key + "\" and TypeConverter \"" + className + "\""); 280 continue; 281 } 282 283 Class targetClazz = null; 284 285 286 try { 287 targetClazz = Class.forName(key); 288 } catch(ClassNotFoundException cnf) { 289 LOGGER.warn("Could not create a TypeConverter for type \"" + key + "\" because the \"convert-to\" type could not be found."); 290 continue; 291 } 292 293 Class tcClazz = null; 294 BaseTypeConverter tc = null; 295 try { 297 tcClazz = Class.forName(className); 298 Object obj = tcClazz.newInstance(); 299 300 if(obj instanceof TypeConverter) 304 tc = new DelegatingTypeConverter((TypeConverter)obj); 305 else if(obj instanceof BaseTypeConverter) 306 tc = (BaseTypeConverter)obj; 307 else throw new IllegalStateException ("Attempt to load illegal type converter type: " + tcClazz); 308 } catch(ClassNotFoundException cnf) { 309 LOGGER.warn("Could not create a TypeConverter for type \"" + key + "\" because the TypeConverter implementation class \"" + 310 (tcClazz != null ? tcClazz.getName() : null) + "\" could not be found."); 311 312 continue; 313 } catch(Exception e) { 314 if(LOGGER.isWarnEnabled()) 315 LOGGER.warn("Could not create a TypeConverter for type \"" + key + "\" because the implementation class \"" + 316 (tcClazz != null ? tcClazz.getName() : null) + "\" could not be instantiated."); 317 continue; 318 } 319 320 321 if(TYPE_CONVERTERS.containsKey(targetClazz)) 322 if(LOGGER.isWarnEnabled()) 323 LOGGER.warn("Overwriting a previously defined TypeConverter named \"" + targetClazz + 324 "\" with a new TypeConverter implementation of type \"" + className + "\""); 325 326 if(LOGGER.isInfoEnabled()) 327 LOGGER.info("Adding a type converter; target type=\"" + targetClazz.getName() + 328 "\" TypeConverter implementation=\"" + tc.getClass().getName() + "\""); 329 330 TYPE_CONVERTERS.put(targetClazz, tc); 331 } 332 } 333 334 340 private static void initialize() { 341 TYPE_CONVERTERS.put(byte.class, new BaseTypeConverter() { 342 public Object convertToObject(Class type, String value, Locale locale) { 343 return (value == null || value.equals(EMPTY_STRING) ? new Byte ((byte)0) : new Byte (value.trim())); 344 } 345 346 }); 347 TYPE_CONVERTERS.put(Byte .class, new BaseTypeConverter() { 348 public Object convertToObject(Class type, String value, Locale locale) { 349 if(value == null || value.equals(EMPTY_STRING)) 350 return null; 351 else 352 return TypeUtils.convertToObject(value, byte.class, null); 353 } 354 }); 355 356 TYPE_CONVERTERS.put(boolean.class, new BaseTypeConverter() { 357 public Object convertToObject(Class type, String value, Locale locale) { 358 if(value == null || value.equals(EMPTY_STRING)) 359 return Boolean.FALSE; 360 361 value = value.toLowerCase().trim(); 362 if(value.equals("on") || value.equals("true")) 363 return Boolean.TRUE; 364 else 365 return Boolean.FALSE; 366 } 367 }); 368 TYPE_CONVERTERS.put(Boolean .class, new BaseTypeConverter() { 369 public Object convertToObject(Class type, String value, Locale locale) { 370 if(value == null || value.equals(EMPTY_STRING)) 371 return null; 372 else 373 return TypeUtils.convertToObject(value, boolean.class, null); 374 } 375 }); 376 377 TYPE_CONVERTERS.put(char.class, new BaseTypeConverter() { 378 public Object convertToObject(Class type, String value, Locale locale) { 379 if(value == null || value.equals(EMPTY_STRING)) 380 return new Character ('\u0000'); 381 else 382 return new Character (value.charAt(0)); 383 } 384 }); 385 TYPE_CONVERTERS.put(Character .class, new BaseTypeConverter() { 386 public Object convertToObject(Class type, String value, Locale locale) { 387 if(value == null || value.equals(EMPTY_STRING)) 388 return null; 389 else 390 return TypeUtils.convertToObject(value, char.class, null); 391 } 392 }); 393 394 TYPE_CONVERTERS.put(double.class, new BaseTypeConverter() { 395 public Object convertToObject(Class type, String value, Locale locale) { 396 if(value == null || value.equals(EMPTY_STRING)) 397 return new Double (0.0); 398 else 399 return new Double (value.trim()); 400 } 401 }); 402 TYPE_CONVERTERS.put(Double .class, new BaseTypeConverter() { 403 public Object convertToObject(Class type, String value, Locale locale) { 404 if(value == null || value.equals(EMPTY_STRING)) 405 return null; 406 else 407 return TypeUtils.convertToObject(value, double.class, null); 408 } 409 }); 410 411 TYPE_CONVERTERS.put(float.class, new BaseTypeConverter() { 412 public Object convertToObject(Class type, String value, Locale locale) { 413 if(value == null || value.equals(EMPTY_STRING)) 414 return new Float (0.0); 415 else 416 return new Float (value.trim()); 417 } 418 }); 419 TYPE_CONVERTERS.put(Float .class, new BaseTypeConverter() { 420 public Object convertToObject(Class type, String value, Locale locale) { 421 if(value == null || value.equals(EMPTY_STRING)) 422 return null; 423 else 424 return TypeUtils.convertToObject(value, float.class, null); 425 } 426 }); 427 428 TYPE_CONVERTERS.put(int.class, new BaseTypeConverter() { 429 public Object convertToObject(Class type, String value, Locale locale) { 430 if(value == null || value.equals(EMPTY_STRING)) 431 return new Integer (0); 432 else 433 return new Integer (value.trim()); 434 } 435 }); 436 TYPE_CONVERTERS.put(Integer .class, new BaseTypeConverter() { 437 public Object convertToObject(Class type, String value, Locale locale) { 438 if(value == null || value.equals(EMPTY_STRING)) 439 return null; 440 else 441 return TypeUtils.convertToObject(value, int.class, null); 442 } 443 }); 444 445 TYPE_CONVERTERS.put(long.class, new BaseTypeConverter() { 446 public Object convertToObject(Class type, String value, Locale locale) { 447 if(value == null || value.equals(EMPTY_STRING)) 448 return new Long (0); 449 else 450 return new Long (value.trim()); 451 } 452 }); 453 TYPE_CONVERTERS.put(Long .class, new BaseTypeConverter() { 454 public Object convertToObject(Class type, String value, Locale locale) { 455 if(value == null || value.equals(EMPTY_STRING)) 456 return null; 457 else 458 return TypeUtils.convertToObject(value, long.class, null); 459 } 460 }); 461 462 TYPE_CONVERTERS.put(short.class, new BaseTypeConverter() { 463 public Object convertToObject(Class type, String value, Locale locale) { 464 if(value == null || value.equals(EMPTY_STRING)) 465 return new Short ((short)0); 466 else 467 return new Short (value.trim()); 468 } 469 }); 470 TYPE_CONVERTERS.put(Short .class, new BaseTypeConverter() { 471 public Object convertToObject(Class type, String value, Locale locale) { 472 if(value == null || value.equals(EMPTY_STRING)) 473 return null; 474 else 475 return TypeUtils.convertToObject(value, short.class, null); 476 } 477 }); 478 479 TYPE_CONVERTERS.put(String .class, new BaseTypeConverter() { 480 public Object convertToObject(Class type, String value, Locale locale) { 481 if(value == null) 482 return null; 483 else 484 return value; 485 } 486 }); 487 488 TYPE_CONVERTERS.put(java.math.BigDecimal .class, new BaseTypeConverter() { 489 public Object convertToObject(Class type, String value, Locale locale) { 490 if(value == null || value.equals(EMPTY_STRING)) 491 return null; 492 else 493 return new BigDecimal (value.trim()); 494 } 495 }); 496 497 TYPE_CONVERTERS.put(java.math.BigInteger .class, new BaseTypeConverter() { 498 public Object convertToObject(Class type, String value, Locale locale) { 499 if(value == null || value.equals(EMPTY_STRING)) 500 return null; 501 else 502 return new BigInteger (value.trim()); 503 } 504 }); 505 506 TYPE_CONVERTERS.put(byte[].class, new BaseTypeConverter() { 507 public Object convertToObject(Class type, String value, Locale locale) { 508 if(value == null || value.equals(EMPTY_STRING)) 509 return null; 510 else 511 return value.getBytes(); 512 } 513 }); 514 515 TYPE_CONVERTERS.put(Byte [].class, new BaseTypeConverter() { 516 public Object convertToObject(Class type, String value, Locale locale) { 517 if(value == null || value.equals(EMPTY_STRING)) 518 return null; 519 else { 520 byte[] bytes = value.getBytes(); 521 Byte [] wBytes = new Byte [bytes.length]; 522 523 for(int i = 0; i < bytes.length; i++) 524 wBytes[i] = new Byte (bytes[i]); 525 526 return wBytes; 527 } 528 } 529 }); 530 531 TYPE_CONVERTERS.put(Date .class, new BaseTypeConverter() { 532 public Object convertToObject(Class type, String value, Locale locale) { 533 if(value == null || value.equals(EMPTY_STRING)) 534 return null; 535 536 try { 537 if(locale == null) 538 locale = Locale.getDefault(); 539 540 DateFormat df = DateFormat.getDateInstance(DateFormat.SHORT, locale); 541 return df.parse(value); 542 } catch(java.text.ParseException pe) { 543 String msg = "Caugnt an error converting a String to a DateFormat.SHORT formatted Date"; 544 LOGGER.warn(msg, pe); 545 546 TypeConversionException tce = new TypeConversionException(msg, pe); 547 tce.setLocalizedMessage(Bundle.getString("TypeUtils_javaUtilDateConvertError", new Object []{pe.getMessage()})); 548 throw tce; 549 } 550 } 551 }); 552 553 554 TYPE_CONVERTERS.put(java.sql.Date .class, new BaseTypeConverter() { 555 public Object convertToObject(Class type, String value, Locale locale) { 556 if(value == null || value.equals(EMPTY_STRING)) return null; 557 558 try { 559 return java.sql.Date.valueOf(value); 560 } catch(Exception e) { 561 String msg = "Caught an error converting a String to a java.sql.Date"; 562 LOGGER.error(msg, e); 563 564 TypeConversionException tce = new TypeConversionException(msg, e); 565 tce.setLocalizedMessage(Bundle.getString("TypeUtils_javaSqlDateConvertError", new Object []{e.getMessage()})); 566 throw tce; 567 } 568 } 569 }); 570 571 572 TYPE_CONVERTERS.put(java.sql.Timestamp .class, new BaseTypeConverter() { 573 public Object convertToObject(Class type, String value, Locale locale) { 574 if(value == null || value.equals(EMPTY_STRING)) 575 return null; 576 577 try { 578 return java.sql.Timestamp.valueOf(value); 579 } catch(Exception e) { 580 String msg = "Caught an error converting a String to a java.sql.Timestamp"; 581 LOGGER.error(msg, e); 582 583 TypeConversionException tce = new TypeConversionException(msg, e); 584 tce.setLocalizedMessage(Bundle.getString("TypeUtils_javaSqlTimestampConvertError", new Object []{e.getMessage()})); 585 throw tce; 586 } 587 } 588 }); 589 590 591 TYPE_CONVERTERS.put(java.sql.Time .class, new BaseTypeConverter() { 592 public Object convertToObject(Class type, String value, Locale locale) { 593 if(value == null || value.equals(EMPTY_STRING)) 594 return null; 595 596 try { 597 return java.sql.Time.valueOf(value); 598 } catch(Exception e) { 599 String msg = "Caught an error converting a String to a java.sql.Time"; 600 LOGGER.error(msg, e); 601 602 TypeConversionException tce = new TypeConversionException(msg, e); 603 tce.setLocalizedMessage(Bundle.getString("TypeUtils_javaSqlTimeConvertError", new Object []{e.getMessage()})); 604 throw tce; 605 } 606 } 607 }); 608 } 609 } 610 | Popular Tags |