1 22 package org.gjt.sp.util; 23 24 import java.beans.BeanInfo ; 25 import java.beans.IntrospectionException ; 26 import java.beans.Introspector ; 27 import java.beans.PropertyDescriptor ; 28 29 import java.lang.reflect.Array ; 30 import java.lang.reflect.Method ; 31 32 import java.util.HashMap ; 33 import java.util.Map ; 34 import java.util.Properties ; 35 import java.util.StringTokenizer ; 36 37 55 public abstract class PropertiesBean 56 { 57 58 63 public static void clearPropertyCache() 64 { 65 PROPERTY_CACHE.clear(); 66 } 67 68 70 77 protected PropertiesBean(String root) 78 { 79 this(root, ':'); 80 } 81 82 91 protected PropertiesBean(String root, char arraysep) 92 { 93 if (root == null) 94 throw new IllegalArgumentException ("root cannot be null"); 95 this.root = root; 96 this.arraysep = arraysep; 97 } 98 99 101 104 public void load(Properties p) 105 { 106 try 107 { 108 PropertyDescriptor [] _props = getPropertyDescriptors(); 109 for (int i = 0; i < _props.length; i++) 110 { 111 if ("class".equals(_props[i].getName())) 112 continue; 113 114 Method _set = _props[i].getWriteMethod(); 115 if (_set != null) 116 { 117 String _pname = root + "." + _props[i].getName(); 118 Object _val = p.getProperty(_pname); 119 if (_val != null) 120 _val = parse((String )_val, _props[i].getPropertyType()); 121 _set.invoke(this, _val); 122 } 123 } 124 } 125 catch (Exception e) 126 { 127 Log.log(Log.ERROR, this, e); 131 } 132 } 133 134 137 public void save(Properties p) 138 { 139 try 140 { 141 PropertyDescriptor [] _props = getPropertyDescriptors(); 142 for (int i = 0; i < _props.length; i++) 143 { 144 if ("class".equals(_props[i].getName())) 145 continue; 146 147 Method _get = _props[i].getReadMethod(); 148 if (_get != null) 149 { 150 Object _val = _get.invoke(this); 151 String _pname = root + "." + _props[i].getName(); 152 if (_val != null) 153 p.setProperty(_pname, encode(_val)); 154 else 155 p.remove(_pname); 156 } 157 } 158 } 159 catch (Exception e) 160 { 161 Log.log(Log.ERROR, this, e); 165 } 166 } 167 168 171 public void clean(Properties p) 172 { 173 174 try 175 { 176 PropertyDescriptor [] _props = getPropertyDescriptors(); 177 for (int i = 0; i < _props.length; i++) 178 { 179 if ("class".equals(_props[i].getName())) 180 continue; 181 182 String _pname = root + "." + _props[i].getName(); 183 p.remove(_pname); 184 } 185 } 186 catch (Exception e) 187 { 188 Log.log(Log.ERROR, this, e); 192 } 193 } 194 195 197 private PropertyDescriptor [] getPropertyDescriptors() 198 throws IntrospectionException 199 { 200 PropertyDescriptor [] _props; 201 synchronized (PROPERTY_CACHE) 202 { 203 _props = PROPERTY_CACHE.get(getClass().getName()); 204 if (_props == null) 205 { 206 BeanInfo _info = Introspector.getBeanInfo(getClass()); 207 _props = _info.getPropertyDescriptors(); 208 PROPERTY_CACHE.put(getClass().getName(), _props); 209 } 210 } 211 return _props; 212 } 213 214 private String encode(Object value) 215 { 216 Class _class = value.getClass(); 217 if (_class.isArray()) 218 { 219 StringBuilder _val = new StringBuilder (); 220 int _len = Array.getLength(value); 221 for (int i = 0; i < _len; i++) 222 { 223 String _str = encode(Array.get(value, i)); 224 if (_str == null) 225 return null; 226 _val.append(_str); 227 if (i < _len - 1) 228 _val.append(arraysep); 229 } 230 return _val.toString(); 231 } 232 else 233 { 234 if (_class != Boolean .class && _class != Boolean.TYPE 236 && _class != Character .class && _class != Character.TYPE 237 && _class != Double .class && _class != Double.TYPE 238 && _class != Float .class && _class != Float.TYPE 239 && _class != Integer .class && _class != Integer.TYPE 240 && _class != Long .class && _class != Long.TYPE 241 && _class != Short .class && _class != Short.TYPE 242 && _class != String .class) 243 { 244 Log.log(Log.WARNING, this, "unsupported type: " + _class.getName()); 245 return null; 246 } 247 return value.toString(); 248 } 249 } 250 251 private Object parse(String value, Class <?> _class) 252 { 253 Object _ret = null; 254 if (_class.isArray()) 255 { 256 StringTokenizer st = new StringTokenizer (value, String.valueOf(arraysep)); 257 Class _type = _class.getComponentType(); 258 _ret = Array.newInstance(_type, st.countTokens()); 259 int _cnt = st.countTokens(); 260 for (int i = 0; i < _cnt; i++) 261 { 262 Object _val = parse(st.nextToken(), _type); 263 if (_val == null) 264 return null; 265 Array.set(_ret, i, _val); 266 } 267 } 268 else 269 { 270 if (_class == Boolean .class || _class == Boolean.TYPE) 271 _ret = Boolean.valueOf(value); 272 else if (_class == Character .class || _class == Character.TYPE) 273 _ret = Character.valueOf(value.charAt(0)); 274 else if (_class == Double .class || _class == Double.TYPE) 275 _ret = Double.valueOf(value); 276 else if (_class == Float .class || _class == Float.TYPE) 277 _ret = Float.valueOf(value); 278 else if (_class == Integer .class || _class == Integer.TYPE) 279 _ret = Integer.valueOf(value); 280 else if (_class == Long .class || _class == Long.TYPE) 281 _ret = Long.valueOf(value); 282 else if (_class == Short .class || _class == Short.TYPE) 283 _ret = Short.valueOf(value); 284 else if (_class == String .class) 285 _ret = value; 286 else 287 Log.log(Log.WARNING, this, "unsupported type: " + _class.getName()); 288 289 } 290 return _ret; 291 } 292 293 295 296 private static final Map <String ,PropertyDescriptor []> PROPERTY_CACHE 297 = new HashMap <String ,PropertyDescriptor []>(); 298 299 301 private final char arraysep; 302 private final String root; 303 304 } 305 306 | Popular Tags |