1 22 package org.jboss.util.propertyeditor; 23 24 import java.beans.BeanInfo ; 25 import java.beans.IntrospectionException ; 26 import java.beans.Introspector ; 27 import java.beans.PropertyDescriptor ; 28 import java.beans.PropertyEditor ; 29 import java.beans.PropertyEditorManager ; 30 import java.lang.reflect.Method ; 31 import java.security.AccessController ; 32 import java.security.PrivilegedAction ; 33 import java.util.HashMap ; 34 import java.util.Iterator ; 35 import java.util.Properties ; 36 37 import org.jboss.logging.Logger; 38 import org.jboss.util.Classes; 39 40 50 public class PropertyEditors 51 { 52 53 private static Logger log = Logger.getLogger(PropertyEditors.class); 54 55 56 private static final String NULL = "null"; 57 58 59 private static boolean disableIsNull = false; 60 61 62 private static boolean initialized = false; 63 64 static 65 { 66 init(); 67 } 68 69 74 public synchronized static void init() 75 { 76 if( initialized == false ) 77 { 78 AccessController.doPrivileged(Initialize.instance); 79 initialized = true; 80 } 81 } 82 83 90 public static final boolean isNull(final String value) 91 { 92 return isNull(value, true, true); 93 } 94 95 103 public static final boolean isNull(final String value, final boolean trim, final boolean empty) 104 { 105 if (disableIsNull) 107 return false; 108 if (value == null) 110 return true; 111 String trimmed = trim ? value.trim() : value; 113 if (empty && trimmed.length() == 0) 115 return true; 116 return NULL.equalsIgnoreCase(trimmed); 118 } 119 120 126 public static boolean isNullHandlingEnabled() 127 { 128 return !disableIsNull; 129 } 130 131 137 public static PropertyEditor findEditor(final Class type) 138 { 139 return PropertyEditorManager.findEditor(type); 140 } 141 142 148 public static PropertyEditor findEditor(final String typeName) 149 throws ClassNotFoundException 150 { 151 Class type = Classes.getPrimitiveTypeForName(typeName); 153 if (type == null) 154 { 155 ClassLoader loader = Thread.currentThread().getContextClassLoader(); 157 type = loader.loadClass(typeName); 158 } 159 160 return PropertyEditorManager.findEditor(type); 161 } 162 163 171 public static PropertyEditor getEditor(final Class type) 172 { 173 PropertyEditor editor = findEditor(type); 174 if (editor == null) 175 { 176 throw new RuntimeException ("No property editor for type: " + type); 177 } 178 179 return editor; 180 } 181 182 190 public static PropertyEditor getEditor(final String typeName) 191 throws ClassNotFoundException 192 { 193 PropertyEditor editor = findEditor(typeName); 194 if (editor == null) 195 { 196 throw new RuntimeException ("No property editor for type: " + typeName); 197 } 198 199 return editor; 200 } 201 202 208 public static void registerEditor(final Class type, final Class editorType) 209 { 210 PropertyEditorManager.registerEditor(type, editorType); 211 } 212 213 219 public static void registerEditor(final String typeName, 220 final String editorTypeName) 221 throws ClassNotFoundException 222 { 223 ClassLoader loader = Thread.currentThread().getContextClassLoader(); 224 Class type = loader.loadClass(typeName); 225 Class editorType = loader.loadClass(editorTypeName); 226 227 PropertyEditorManager.registerEditor(type, editorType); 228 } 229 230 242 public static Object convertValue(String text, String typeName) 243 throws ClassNotFoundException , IntrospectionException 244 { 245 Class typeClass = Classes.getPrimitiveTypeForName(typeName); 247 if (typeClass == null) 248 { 249 ClassLoader loader = Thread.currentThread().getContextClassLoader(); 250 typeClass = loader.loadClass(typeName); 251 } 252 253 PropertyEditor editor = PropertyEditorManager.findEditor(typeClass); 254 if (editor == null) 255 { 256 throw new IntrospectionException 257 ("No property editor for type=" + typeClass); 258 } 259 260 editor.setAsText(text); 261 return editor.getValue(); 262 } 263 264 278 public static void mapJavaBeanProperties(Object bean, Properties beanProps) 279 throws IntrospectionException 280 { 281 mapJavaBeanProperties(bean, beanProps, true); 282 } 283 284 300 public static void mapJavaBeanProperties(Object bean, Properties beanProps, boolean isStrict) 301 throws IntrospectionException 302 { 303 304 HashMap propertyMap = new HashMap (); 305 BeanInfo beanInfo = Introspector.getBeanInfo(bean.getClass()); 306 PropertyDescriptor [] props = beanInfo.getPropertyDescriptors(); 307 for (int p = 0; p < props.length; p++) 308 { 309 String fieldName = props[p].getName(); 310 propertyMap.put(fieldName, props[p]); 311 } 312 313 boolean trace = log.isTraceEnabled(); 314 Iterator keys = beanProps.keySet().iterator(); 315 if( trace ) 316 log.trace("Mapping properties for bean: "+bean); 317 while( keys.hasNext() ) 318 { 319 String name = (String ) keys.next(); 320 String text = beanProps.getProperty(name); 321 PropertyDescriptor pd = (PropertyDescriptor ) propertyMap.get(name); 322 if (pd == null) 323 { 324 331 if (name.length() > 1) 332 { 333 char first = name.charAt(0); 334 String exName = Character.toUpperCase(first) + name.substring(1); 335 pd = (PropertyDescriptor ) propertyMap.get(exName); 336 337 if (pd == null) 339 { 340 exName = Character.toLowerCase(first) + name.substring(1); 341 pd = (PropertyDescriptor ) propertyMap.get(exName); 342 } 343 } 344 345 if (pd == null) 346 { 347 if (isStrict) 348 { 349 String msg = "No property found for: "+name+" on JavaBean: "+bean; 350 throw new IntrospectionException (msg); 351 } 352 else 353 { 354 continue; 356 } 357 } 358 } 359 Method setter = pd.getWriteMethod(); 360 if( trace ) 361 log.trace("Property editor found for: "+name+", editor: "+pd+", setter: "+setter); 362 if (setter != null) 363 { 364 Class ptype = pd.getPropertyType(); 365 PropertyEditor editor = PropertyEditorManager.findEditor(ptype); 366 if (editor == null) 367 { 368 if( trace ) 369 log.trace("Failed to find property editor for: "+name); 370 } 371 try 372 { 373 editor.setAsText(text); 374 Object args[] = {editor.getValue()}; 375 setter.invoke(bean, args); 376 } 377 catch (Exception e) 378 { 379 if( trace ) 380 log.trace("Failed to write property", e); 381 } 382 } 383 } 384 } 385 386 391 public String [] getEditorSearchPath() 392 { 393 return PropertyEditorManager.getEditorSearchPath(); 394 } 395 396 401 public void setEditorSearchPath(final String [] path) 402 { 403 PropertyEditorManager.setEditorSearchPath(path); 404 } 405 406 private static class Initialize implements PrivilegedAction 407 { 408 static Initialize instance = new Initialize(); 409 410 public Object run() 411 { 412 String [] currentPath = PropertyEditorManager.getEditorSearchPath(); 413 int length = currentPath != null ? currentPath.length : 0; 414 String [] newPath = new String [length+2]; 415 System.arraycopy(currentPath, 0, newPath, 2, length); 416 newPath[0] = "org.jboss.util.propertyeditor"; 419 newPath[1] = "org.jboss.mx.util.propertyeditor"; 420 PropertyEditorManager.setEditorSearchPath(newPath); 421 422 426 Class strArrayType = String [].class; 427 PropertyEditorManager.registerEditor(strArrayType, StringArrayEditor.class); 428 Class clsArrayType = Class [].class; 429 PropertyEditorManager.registerEditor(clsArrayType, ClassArrayEditor.class); 430 Class intArrayType = int[].class; 431 PropertyEditorManager.registerEditor(intArrayType, IntArrayEditor.class); 432 Class byteArrayType = byte[].class; 433 PropertyEditorManager.registerEditor(byteArrayType, ByteArrayEditor.class); 434 435 PropertyEditorManager.registerEditor(Character.TYPE, CharacterEditor.class); 437 438 try 439 { 440 if (System.getProperty("org.jboss.util.property.disablenull") != null) 441 disableIsNull = true; 442 } 443 catch (Throwable ignored) 444 { 445 log.trace("Error retrieving system property org.jboss.util.property.diablenull", ignored); 446 } 447 return null; 448 } 449 } 450 } 451 | Popular Tags |