1 9 package org.jboss.portal.core.impl.user; 10 11 import java.lang.reflect.Field ; 12 import java.util.ArrayList ; 13 import java.util.Collection ; 14 import java.util.Collections ; 15 import java.util.HashMap ; 16 import java.util.HashSet ; 17 import java.util.Iterator ; 18 import java.util.Map ; 19 import java.util.Set ; 20 21 import org.jboss.portal.core.model.PropertyMap; 22 import org.jboss.portal.portlet.PortletConstants; 23 24 30 public class PropertyMapImpl implements PropertyMap 31 { 32 33 private static final Property[] props = initialize(); 34 35 private static Property[] initialize() 36 { 37 return new Property[] 38 { 39 new Property(PortletConstants.INFO_USER_NAME_NICKNAME, "userName", false) 40 }; 41 } 42 43 private UserImpl user; 44 45 public PropertyMapImpl(UserImpl user) 46 { 47 this.user = user; 48 } 49 50 private Map getDynamic() 51 { 52 return user.getDynamic(); 53 } 54 55 public boolean isReadOnly(Object key) 56 { 57 if (!(key instanceof String )) 58 { 59 return false; 60 } 61 Property prop = getProperty((String )key); 62 return prop != null && prop.writable; 63 } 64 65 public int size() 66 { 67 return props.length + getDynamic().size(); 68 } 69 70 public boolean isEmpty() 71 { 72 return false; 73 } 74 75 public boolean containsKey(Object key) 76 { 77 if (!(key instanceof String )) 78 { 79 return false; 80 } 81 Property prop = getProperty((String )key); 82 if (prop != null) 83 { 84 return true; 85 } 86 return getDynamic().containsKey(key); 87 } 88 89 public boolean containsValue(Object value1) 90 { 91 for (int i = 0;i < props.length;i++) 92 { 93 Property prop = props[i]; 94 Object value2 = prop.get(user); 95 if (value1 == value2 || (value1 != null && value1.equals(value2))) 96 { 97 return true; 98 } 99 } 100 return getDynamic().containsValue(value1); 101 } 102 103 public Object get(Object key) 104 { 105 if (!(key instanceof String )) 106 { 107 return null; 108 } 109 Property prop = getProperty((String )key); 110 if (prop != null && prop.field.getName().equals(key)) 111 { 112 return prop.get(user); 113 } 114 return getDynamic().get(key); 115 } 116 117 121 public Object put(Object key, Object newValue) throws IllegalArgumentException 122 { 123 if (!(key instanceof String )) 124 { 125 throw new IllegalArgumentException ("Key is not a String"); 126 } 127 Property prop = getProperty((String )key); 128 if (prop != null && prop.field.getName().equals(key)) 129 { 130 if (!prop.writable) 131 { 132 throw new IllegalArgumentException ("Key " + key + " is not modifiable"); 133 } 134 else 135 { 136 Object oldValue = prop.get(user); 137 prop.set(user, newValue); 138 return oldValue; 139 } 140 } 141 return getDynamic().put(key, newValue); 142 } 143 144 149 public Object remove(Object key) throws IllegalArgumentException 150 { 151 if (!(key instanceof String )) 152 { 153 return null; 154 } 155 Property prop = getProperty((String )key); 156 if (prop != null) 157 { 158 throw new IllegalArgumentException ("Key " + key + " is not modifiable"); 159 } 160 return getDynamic().remove(key); 161 } 162 163 166 public void clear() 167 { 168 getDynamic().clear(); 169 } 170 171 public Set keySet() 172 { 173 Set set = new HashSet (getDynamic().keySet()); 175 176 for (int i = 0;i < props.length;i++) 178 { 179 set.add(props[i].name); 180 } 181 return set; 182 } 183 184 public Collection values() 185 { 186 ArrayList collection = new ArrayList (size()); 187 for (int i = 0;i < props.length;i++) 188 { 189 collection.add(props[i].get(user)); 190 } 191 collection.addAll(getDynamic().values()); 192 return collection; 193 } 194 195 198 public Set entrySet() 199 { 200 Map copy = new HashMap (getDynamic()); 201 for (int i = 0;i < props.length;i++) 202 { 203 Property prop = props[i]; 204 copy.put(prop.name, prop.get(user)); 205 } 206 return Collections.unmodifiableMap(copy).entrySet(); 207 } 208 209 public void putAll(Map map) 210 { 211 for (Iterator i = map.entrySet().iterator();i.hasNext();) 212 { 213 Map.Entry entry = (Entry)i.next(); 214 Object key = entry.getKey(); 215 if (key instanceof String ) 216 { 217 Object value = entry.getValue(); 218 Property prop = getProperty((String )key); 219 if (prop != null) 220 { 221 if (prop.writable) 222 { 223 prop.set(user, value); 224 } 225 else 226 { 227 } 229 } 230 else 231 { 232 getDynamic().put(key, value); 233 } 234 } 235 } 236 } 237 238 241 private static class Property 242 { 243 244 final String name; 245 final boolean writable; 246 final Field field; 247 248 public Property(String propertyName, String fieldName, boolean writable) 249 { 250 try 251 { 252 this.name = propertyName; 253 this.writable = writable; 254 this.field = UserImpl.class.getField(fieldName); 255 } 256 catch (NoSuchFieldException e) 257 { 258 throw new Error (e); 259 } 260 } 261 public void set(Object instance, Object value) 262 { 263 try 264 { 265 field.set(instance, value); 266 } 267 catch (IllegalAccessException e) 268 { 269 throw new Error (e); 270 } 271 } 272 public Object get(Object instance) 273 { 274 try 275 { 276 return field.get(instance); 277 } 278 catch (IllegalAccessException e) 279 { 280 throw new Error (e); 281 } 282 } 283 } 284 285 private Property getProperty(String key) 286 { 287 for (int i = 0;i < props.length;i++) 288 { 289 Property prop = props[i]; 290 if (prop.name.equals(key)) 291 { 292 return prop; 293 } 294 } 295 return null; 296 } 297 } 298 | Popular Tags |