1 15 package org.apache.hivemind.util; 16 17 import java.beans.BeanInfo ; 18 import java.beans.Introspector ; 19 import java.util.HashMap ; 20 import java.util.List ; 21 import java.util.Map ; 22 23 import org.apache.hivemind.ApplicationRuntimeException; 24 import org.apache.hivemind.HiveMind; 25 26 31 public class PropertyUtils 32 { 33 private static final Map _classAdaptors = new HashMap (); 34 35 private PropertyUtils() 37 { 38 } 39 40 50 public static void write(Object target, String propertyName, Object value) 51 { 52 ClassAdaptor a = getAdaptor(target); 53 54 a.write(target, propertyName, value); 55 } 56 57 63 public static void smartWrite(Object target, String propertyName, String value) 64 { 65 ClassAdaptor a = getAdaptor(target); 66 67 a.smartWrite(target, propertyName, value); 68 } 69 70 85 86 public static void configureProperties(Object target, String initializer) 87 { 88 ClassAdaptor a = getAdaptor(target); 89 90 a.configureProperties(target, initializer); 91 } 92 93 101 102 public static boolean isWritable(Object target, String propertyName) 103 { 104 return getAdaptor(target).isWritable(propertyName); 105 } 106 107 public static boolean isReadable(Object target, String propertyName) 108 { 109 return getAdaptor(target).isReadable(propertyName); 110 } 111 112 120 121 public static Object read(Object target, String propertyName) 122 { 123 ClassAdaptor a = getAdaptor(target); 124 125 return a.read(target, propertyName); 126 } 127 128 136 public static Class getPropertyType(Object target, String propertyName) 137 { 138 ClassAdaptor a = getAdaptor(target); 139 140 return a.getPropertyType(target, propertyName); 141 } 142 143 149 public static PropertyAdaptor getPropertyAdaptor(Object target, String propertyName) 150 { 151 ClassAdaptor a = getAdaptor(target); 152 153 return a.getPropertyAdaptor(target, propertyName); 154 } 155 156 159 public static List getReadableProperties(Object target) 160 { 161 return getAdaptor(target).getReadableProperties(); 162 } 163 164 167 public static List getWriteableProperties(Object target) 168 { 169 return getAdaptor(target).getWriteableProperties(); 170 } 171 172 private static ClassAdaptor getAdaptor(Object target) 173 { 174 if (target == null) 175 throw new ApplicationRuntimeException(UtilMessages.nullObject()); 176 177 Class targetClass = target.getClass(); 178 179 synchronized (HiveMind.INTROSPECTOR_MUTEX) 180 { 181 ClassAdaptor result = (ClassAdaptor) _classAdaptors.get(targetClass); 182 183 if (result == null) 184 { 185 result = buildClassAdaptor(target, targetClass); 186 _classAdaptors.put(targetClass, result); 187 } 188 189 return result; 190 } 191 } 192 193 private static ClassAdaptor buildClassAdaptor(Object target, Class targetClass) 194 { 195 try 196 { 197 BeanInfo info = Introspector.getBeanInfo(targetClass); 198 199 return new ClassAdaptor(info.getPropertyDescriptors()); 200 } 201 catch (Exception ex) 202 { 203 throw new ApplicationRuntimeException(UtilMessages.unableToIntrospect(targetClass, ex), 204 target, null, ex); 205 } 206 } 207 208 211 public static void clearCache() 212 { 213 synchronized (HiveMind.INTROSPECTOR_MUTEX) 214 { 215 _classAdaptors.clear(); 216 Introspector.flushCaches(); 217 } 218 } 219 220 } | Popular Tags |