1 15 package org.apache.hivemind.util; 16 17 import java.beans.PropertyDescriptor ; 18 import java.util.ArrayList ; 19 import java.util.HashMap ; 20 import java.util.Iterator ; 21 import java.util.List ; 22 import java.util.Map ; 23 import java.util.StringTokenizer ; 24 25 import org.apache.hivemind.ApplicationRuntimeException; 26 27 33 class ClassAdaptor 34 { 35 private final Map _propertyAdaptorMap = new HashMap (); 36 37 ClassAdaptor(PropertyDescriptor [] properties) 38 { 39 for (int i = 0; i < properties.length; i++) 40 { 41 PropertyDescriptor d = properties[i]; 42 43 String name = d.getName(); 44 45 _propertyAdaptorMap.put(name, new PropertyAdaptor(name, d.getPropertyType(), d 46 .getReadMethod(), d.getWriteMethod())); 47 } 48 } 49 50 58 public void write(Object target, String propertyName, Object value) 59 { 60 PropertyAdaptor a = getPropertyAdaptor(target, propertyName); 61 62 a.write(target, value); 63 } 64 65 71 72 public void smartWrite(Object target, String propertyName, String value) 73 { 74 PropertyAdaptor a = getPropertyAdaptor(target, propertyName); 75 76 a.smartWrite(target, value); 77 } 78 79 87 public Object read(Object target, String propertyName) 88 { 89 PropertyAdaptor a = getPropertyAdaptor(target, propertyName); 90 91 return a.read(target); 92 } 93 94 102 public Class getPropertyType(Object target, String propertyName) 103 { 104 PropertyAdaptor a = getPropertyAdaptor(target, propertyName); 105 106 return a.getPropertyType(); 107 } 108 109 112 113 public boolean isReadable(String propertyName) 114 { 115 PropertyAdaptor result = (PropertyAdaptor) _propertyAdaptorMap.get(propertyName); 116 117 return result != null && result.isReadable(); 118 } 119 120 123 124 public boolean isWritable(String propertyName) 125 { 126 PropertyAdaptor result = (PropertyAdaptor) _propertyAdaptorMap.get(propertyName); 127 128 return result != null && result.isWritable(); 129 } 130 131 PropertyAdaptor getPropertyAdaptor(Object target, String propertyName) 132 { 133 PropertyAdaptor result = (PropertyAdaptor) _propertyAdaptorMap.get(propertyName); 134 135 if (result == null) 136 throw new ApplicationRuntimeException( 137 UtilMessages.noSuchProperty(target, propertyName), target, null, null); 138 139 return result; 140 } 141 142 145 public List getReadableProperties() 146 { 147 List result = new ArrayList (_propertyAdaptorMap.size()); 148 149 Iterator i = _propertyAdaptorMap.values().iterator(); 150 151 while (i.hasNext()) 152 { 153 PropertyAdaptor a = (PropertyAdaptor) i.next(); 154 155 if (a.isReadable()) 156 result.add(a.getPropertyName()); 157 } 158 159 return result; 160 } 161 162 165 public List getWriteableProperties() 166 { 167 List result = new ArrayList (_propertyAdaptorMap.size()); 168 169 Iterator i = _propertyAdaptorMap.values().iterator(); 170 171 while (i.hasNext()) 172 { 173 PropertyAdaptor a = (PropertyAdaptor) i.next(); 174 175 if (a.isWritable()) 176 result.add(a.getPropertyName()); 177 } 178 179 return result; 180 } 181 182 188 189 public void configureProperties(Object target, String initializer) 190 { 191 StringTokenizer tokenizer = new StringTokenizer (initializer, ","); 192 193 while (tokenizer.hasMoreTokens()) 194 { 195 configurePropertyFromToken(target, tokenizer.nextToken()); 196 } 197 } 198 199 210 private void configurePropertyFromToken(Object target, String token) 211 { 212 int equalsx = token.indexOf('='); 213 214 if (equalsx > 0) 215 { 216 String propertyName = token.substring(0, equalsx).trim(); 217 String value = token.substring(equalsx + 1); 218 219 smartWrite(target, propertyName, value); 220 return; 221 } 222 223 boolean negate = token.startsWith("!"); 224 225 String propertyName = negate ? token.substring(1) : token; 226 227 Boolean value = negate ? Boolean.FALSE : Boolean.TRUE; 228 229 write(target, propertyName, value); 230 } 231 } | Popular Tags |